home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 17.6 KB | 513 lines |
- /*---------------------------------------------------------------------------
-
- Written by the Personal Journal developers of Dow Jones & Company, Inc.
-
- Dow Jones makes no representations or warranties about
- the suitability of this software, either express or
- implied, including but not limited to the implied warranties
- of merchantability, fitness for a particular purpose,
- or non-infringement. Dow Jones will not be liable for
- any damages suffered by a user as a result of using,
- modifying or distributing this software or its derivatives.
-
-
-
- @(#)Paper.java 0.00 20-Dec-95
-
-
- A collection of stories, arranged into sections, transferred
- from a Personal Journal server.
-
- A paper observes its subsections, which in turn observe it.
- When a paper receives a StoryContainerNotification from one
- of its subsections, it relays the message to its other subsections.
-
-
- Authors:
-
- rphall Rick Hall
- jlee James Lee
-
-
- Version Ident:
-
- $Header$
-
-
- History:
-
- 20-Dec-95 rphall Initial Creation
- 27-Dec-95 rphall Observerable sections, observable paper
- 21-Mar-96 jlee Added DownloadProgressNotification functionality
-
- ---------------------------------------------------------------------------*/
-
- package pj.io;
-
- import pj.io.PaperStory;
- import pj.io.PaperSection;
-
- import collections.Assertable;
- import collections.ImplementationCheckable;
- import collections.ImplementationError;
- import java.applet.Applet;
- import java.util.NoSuchElementException;
- import java.util.Observable;
- import java.util.Observer;
- import java.util.Vector;
-
- /**
- * A collection of stories, arranged into sections, transferred
- * from a Personal Journal server.
- * @version 0.00, 20-Dec-95
- * @author Rick Hall
- */
- public class Paper extends Observable implements Assertable, StoryContainer, ImplementationCheckable, Observer
- {
- // --- Class variables
-
- /** Front page Business and Finance */
- public static final String idFPBusFin = "101";
- /** Front page World-Wide */
- public static final String idFPWorld = "102";
- /** Front page Personal News */
- public static final String idFPNews = "103";
- /** Front page News Flash */
- public static final String idFPFlash = "104";
- /** TJI Business and Finance */
- public static final String idTJIBusFin= "201";
- /** TJI World-Wide */
- public static final String idTJIWorld = "202";
- /** TJI Personal News */
- public static final String idTJINews = "203";
- /** Portfolio Stock Quote */
- public static final String idPortQuote= "301";
- /** Portfolio Advertisement */
- public static final String idPortAd = "302";
- /** Markets Dow Jones IA */
- public static final String idMrkDjia = "401";
- /** Markets Stock Indices */
- public static final String idMrkStkIdx= "402";
- /** Markets Exchange Rates */
- public static final String idMrkExch = "403";
- /** Markets Treasuries */
- public static final String idMrkTreas = "404";
- /** Markets Wrapup */
- public static final String idMrkWrap = "405";
- /** Sports Highlights */
- public static final String idSpHilite = "501";
- /** Sports Scores */
- public static final String idSpScores = "502";
- /** Sports Advertisement */
- public static final String idSpAd = "503";
- /** Weather US Northeast */
- public static final String idWthUSNE = "601";
- /** Weather US Southeast */
- public static final String idWthUSSE = "602";
- /** Weather US Midwest */
- public static final String idWthUSMW = "603";
- /** Weather US West */
- public static final String idWthUSWest= "604";
- /** Weather Canada */
- public static final String idWthCanada= "605";
- /** Weather South America */
- public static final String idWthSouthA= "606";
- /** Weather Europe */
- public static final String idWthEurope= "607";
- /** Weather Mid-East */
- public static final String idWthMidE = "608";
- /** Weather Africa */
- public static final String idWthAfrica= "609";
- /** Weather Asia */
- public static final String idWthAsia = "610";
- /** Weather Advertisement */
- public static final String idWthAd = "611";
- /** Protocol Log (Obsolete) */
- public static final String idPcolLog = "701";
- /** Protocol End of transmission */
- public static final String idPcolEOT = "702";
- /** Protocol Error Message */
- public static final String idPcolErr = "703";
- /** Protocol Timestamp */
- public static final String idPcolTimes= "704";
-
-
- // --- Instance variables
-
- private final int idxSubsect = 0;
- private final int idxType = 1;
-
- private final String strSectionAttrib[][] =
- { // Subsection Type
- { idFPBusFin , PaperSection.typeArticle}, // Front page Business and Finance
- { idFPWorld , PaperSection.typeArticle}, // Front page World-Wide
- { idFPNews , PaperSection.typeArticle}, // Front page Personal News
- { idFPFlash , PaperSection.typeArticle}, // Front page News Flash
- { idTJIBusFin , PaperSection.typeArticle}, // TJI Business and Finance
- { idTJIWorld , PaperSection.typeArticle}, // TJI World-Wide
- { idTJINews , PaperSection.typeArticle}, // TJI Personal News
- { idPortQuote , PaperSection.typeArticle}, // Portfolio Stock Quote
- { idPortAd , PaperSection.typeArticle}, // Portfolio Advertisement
- { idMrkDjia , PaperSection.typeArticle}, // Markets Dow Jones IA
- { idMrkStkIdx , PaperSection.typeArticle}, // Markets Stock Indices
- { idMrkExch , PaperSection.typeArticle}, // Markets Exchange Rates
- { idMrkTreas , PaperSection.typeArticle}, // Markets Treasuries
- { idMrkWrap , PaperSection.typeArticle}, // Markets Wrapup
- { idSpHilite , PaperSection.typeArticle}, // Sports Highlights
- { idSpScores , PaperSection.typeArticle}, // Sports Scores
- { idSpAd , PaperSection.typeArticle}, // Sports Advertisement
- { idWthUSNE , PaperSection.typeArticle}, // Weather US Northeast
- { idWthUSSE , PaperSection.typeArticle}, // Weather US Southeast
- { idWthUSMW , PaperSection.typeArticle}, // Weather US Midwest
- { idWthUSWest , PaperSection.typeArticle}, // Weather US West
- { idWthCanada , PaperSection.typeArticle}, // Weather Canada
- { idWthSouthA , PaperSection.typeArticle}, // Weather South America
- { idWthEurope , PaperSection.typeArticle}, // Weather Europe
- { idWthMidE , PaperSection.typeArticle}, // Weather Mid-East
- { idWthAfrica , PaperSection.typeArticle}, // Weather Africa
- { idWthAsia , PaperSection.typeArticle}, // Weather Asia
- { idWthAd , PaperSection.typeArticle}, // Weather Advertisement
- { idPcolLog , PaperSection.typeArticle}, // Protocol Log (Obsolete)
- { idPcolEOT , PaperSection.typeArticle}, // Protocol End of transmission
- { idPcolErr , PaperSection.typeArticle}, // Protocol Error Message
- { idPcolTimes , PaperSection.typeArticle} // Protocol Timestamp
- };
-
- private final int idxFirstEnumSection = 0; // Front page B&F
- private final int idxLastEnumSection = 27; // Last weather page
- private Vector vSection; // Implementation of this container
- private int idxCurrent; // A cursor into sections
-
-
-
- // --- Public constructors
-
- public Paper() throws ImplementationError
- {
- vSection = new Vector();
- for (int i=0; i<strSectionAttrib.length; i++)
- {
- PaperSection sec = new PaperSection (
- sectionString(strSectionAttrib[i][idxSubsect]),
- strSectionAttrib[i][idxSubsect],
- strSectionAttrib[i][idxType] );
- sec.addObserver(this);
- addObserver(sec);
- vSection.addElement(sec);
- }
- idxCurrent = 0;
- checkImplementation();
- System.out.println("Debug-Paper:constructed");
- }
-
- // --- Public operations
-
- public void assert(boolean predicate)
- throws ImplementationError
- {
- ImplementationError.assert(this, predicate);
- }
-
- public void checkImplementation()
- throws ImplementationError
- {
- assert( vSection.size() == strSectionAttrib.length );
- for (int i=0; i<strSectionAttrib.length; i++)
- assert( vSection.elementAt(i) instanceof PaperSection );
- assert( idxCurrent >= 0 );
- assert( idxCurrent < vSection.size() );
- }
-
- public synchronized void update(Observable o, Object arg)
- {
- if ( arg instanceof DownloadProgressNotification )
- {
- setChanged();
- notifyObservers( arg );
- }
- else
- if ( arg instanceof StoryContainerNotification )
- {
- setChanged();
- notifyObservers(arg);
-
- if ( !( ((StoryContainerNotification) arg).strSubsect.equals(idPcolEOT) ) )
- return;
-
- // HIDEOUS KLUDGE: define PaperEOTNotification instead of this PcolEOT
- // crap
-
- // The Protocol EOT story requires special handling
- if ( ((StoryContainerNotification)arg).newState == PaperSection.stateAdding )
- {
- // Send a fake notification so that all subsections go to stateAdded
- PaperSection fake = new PaperSection( sectionString(idPcolEOT), idPcolEOT, PaperSection.typeArticle);
- StoryContainerNotification scn = new StoryContainerNotification( fake,idPcolEOT,PaperSection.stateAdding );
- setChanged();
- notifyObservers(scn);
- } // EOT adding
- }
- else
- return;
-
- } // update
-
-
-
- public void addStory( PaperStory ps )
- throws MalformedPaperStoryException, IncompatiblePaperStoryException,
- IllegalArgumentException
- {
- if (ps == null)
- throw new IllegalArgumentException();
-
- for (int i=0; i<strSectionAttrib.length; i++ )
- {
- if ( strSectionAttrib[i][idxSubsect].equals(ps.getSubsect()) )
- {
- ((PaperSection) vSection.elementAt(i)).addStory(ps);
- break;
- } // if
- } // for
- } // addStory
-
- public final int numStories()
- {
- int num = 0;
- for (int i=0; i<strSectionAttrib.length; i++ )
- num += ((PaperSection) vSection.elementAt(i)).numStories();
- return num;
- }
-
- public final synchronized void removeStories()
- {
- int num = 0;
- for (int i=0; i<strSectionAttrib.length; i++ )
- ((PaperSection) vSection.elementAt(i)).removeStories();
- }
-
- public final synchronized PaperStory firstStory()
- throws NoSuchElementException
- {
- return ((PaperSection)
- vSection.elementAt(idxFirstEnumSection)).firstStory();
- }
-
- public final synchronized PaperStory nextStory()
- throws NoSuchElementException
- {
- int idxAttempt = idxCurrent;
- PaperStory temp = null;
-
- // Pathologies
- if ( numStories() == 0 )
- throw new NoSuchElementException();
-
- if ( numStories() == 1 )
- return ((PaperSection)
- vSection.elementAt(idxAttempt)).nextStory();
-
- // Typical case
- try {
- // Try the current section
- temp= ((PaperSection)
- vSection.elementAt(idxAttempt)).nextStory();
- }
- catch (NoSuchElementException e)
- {
- // Bump to the next section
- if (idxAttempt >= idxLastEnumSection)
- idxAttempt = idxFirstEnumSection;
- else
- idxAttempt++;
- }
- if ( temp != null )
- return temp;
-
- // Iterate over remaining sections to find next story
- for (int i=0; i<vSection.size()-1; i++ )
- {
- try {
- // Try the current section
- temp= ((PaperSection)
- vSection.elementAt(idxAttempt)).firstStory();
- break;
- }
- catch (NoSuchElementException e)
- {
- // Bump to the next section
- if (idxAttempt >= idxLastEnumSection)
- idxAttempt = idxFirstEnumSection;
- else
- idxAttempt++;
- }
- } // for
-
- // Finish up
- if (temp != null)
- {
- idxCurrent = idxAttempt;
- return temp;
- }
- else
- {
- checkImplementation();
- throw new NoSuchElementException();
- }
-
- } // nextStory
-
- public final synchronized PaperStory lastStory()
- throws NoSuchElementException
- {
- return ((PaperSection)
- vSection.elementAt(idxLastEnumSection)).lastStory();
- }
-
- public final synchronized PaperStory previousStory()
- throws NoSuchElementException
- {
- int idxAttempt = idxCurrent;
- PaperStory temp = null;
-
- // Pathologies
- if ( numStories() == 0 )
- throw new NoSuchElementException();
-
- if ( numStories() == 1 )
- return ((PaperSection)
- vSection.elementAt(idxAttempt)).previousStory();
-
- // Typical case
- try {
- // Try the current section
- temp= ((PaperSection)
- vSection.elementAt(idxAttempt)).previousStory();
- }
- catch (NoSuchElementException e)
- {
- // Decrement to the previous section
- if (idxAttempt == idxFirstEnumSection)
- temp= ((PaperSection)
- vSection.elementAt(idxAttempt)).firstStory();
- else
- idxAttempt--;
- }
-
- if ( temp != null )
- return temp;
-
- // Iterate over remaining sections to find previous story
- for (int i=0; i<vSection.size()-1; i++ )
- {
- try {
- // Try the current section
- temp= ((PaperSection)
- vSection.elementAt(idxAttempt)).lastStory();
- break;
- }
- catch (NoSuchElementException e)
- {
- // Decrement to the previous section
- if (idxAttempt == idxFirstEnumSection)
- {
- temp =((PaperSection)
- vSection.elementAt(idxAttempt)).lastStory();
- break;
- }
- else
- idxAttempt--;
- } // catch
-
- } // for
-
- // Finish up
- if (temp != null)
- {
- idxCurrent = idxAttempt;
- return temp;
- }
- else
- {
- checkImplementation();
- throw new NoSuchElementException();
- }
-
- } // previousStory
-
- public final synchronized PaperStory currentStory()
- throws NoSuchElementException
- {
- return ((PaperSection)
- vSection.elementAt(idxCurrent)).currentStory();
- }
-
- /**
- * Return a PaperSection matching the given section id.
- * See the public variables of Paper for a list of valid id strings.
- * @param name A section id.
- * @return The section matching id.
- * @exception NoSuchElementException thrown if no section exists
- * with the given id.
- */
- public PaperSection section(String id) throws NoSuchElementException
- {
- PaperSection temp = null;
- for (int i=0; i<vSection.size()-1; i++ )
- {
- PaperSection temp2 = (PaperSection) vSection.elementAt(i);
- if ( temp2.getId().equals(id) )
- {
- temp = temp2;
- break;
- }
- } // for
-
- if (temp == null) throw new NoSuchElementException();
- return temp;
-
- } // section
-
- public String toString()
- {
- if (vSection.size() < 1 )
- return "";
-
- String temp = ((PaperSection) vSection.elementAt(0)).toString();
- for (int i=1; i<vSection.size(); i++)
- temp += ((PaperSection) vSection.elementAt(i)).toString();
-
- return temp;
- }
-
- /**
- * A section string can be calculated from the subsection string.
- * <pre>
- * e.g.
- * Subsection "702" ==> Section "700"
- * </pre>
- * @param subsection A subsection string
- * @return The corresponding section string
- * @exception ImplementationError thrown if subsection string
- * does not parse to an integer
- */
- public static String sectionString(String subsection)
- throws ImplementationError
- {
- int i = 0;
- try {
- i = (new Integer(subsection)).intValue();
- i = 100 * (i/100);
- }
- catch(NumberFormatException e)
- {
- throw new ImplementationError();
- }
- return (new Integer(i)).toString();
-
- } // sectionString
-
-
-
- } // Paper
-