Rev 19562 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
/*==============================================================================Project:JAVA Source file for the class IndexStreamCOPYRIGHT (C), 1998, Thomas Baier* This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA$Source: /scratch/CVS-ARCHIVE/R/doc/html/search/IndexStream.java,v $$Revision: 1.4 $$Date: 2002/05/05 22:34:52 $$Author: pd $==============================================================================*//* -------------------------------- Imports --------------------------------- */import java.util.*;import java.applet.*;import java.awt.*;import java.net.*;import java.io.*;/*==============================================================================Interface of class IndexStream==============================================================================*//*------------------------------------------------------------------------------CLASS: IndexStreamSUPER: ObjectCONF. TO:PURPOSE: reading and parsing a stream of multiple index entries, also handlessome keywords, e.g. include of other streams, provides a contextfor environment spaceNOTES:HISTORY: 98-05-08: created------------------------------------------------------------------------------*/public class IndexStream extends Object{/*============================================================================Public methods============================================================================*//*----------------------------------------------------------------------------INTERFACE:PURPOSE: default constructorNOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-05-08: created98-05-15: new static member for include directive----------------------------------------------------------------------------*/public IndexStream (URL inputURL){// init the instance dataiInputURL = inputURL;iIndex = 0;iData = new Vector ();// read the index data from the streamreadStream ();}/*----------------------------------------------------------------------------INTERFACE:PURPOSE:NOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-05-08: created----------------------------------------------------------------------------*/public Value popEntry (){if (iIndex < 0) {iIndex = 0;}if (iIndex >= iData.size ()) {return null;}iIndex++;return (Value) iData.elementAt (iIndex - 1);}/*----------------------------------------------------------------------------INTERFACE:PURPOSE:NOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-05-08: created----------------------------------------------------------------------------*/public void pushEntry (Value anEntry){if (iIndex < 0) {iIndex = 0;}if (iIndex >= iData.size ()) {iIndex = iData.size ();}iData.insertElementAt (anEntry,iIndex);return;}/*============================================================================Protected methods============================================================================*//*============================================================================Private methods============================================================================*//*----------------------------------------------------------------------------INTERFACE:PURPOSE:NOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-05-08: created98-05-15: catch all exception (e.g. security violation)98-06-07: correctly trace exceptions98-11-14: replaced deprecated DataInputStream.readLine()----------------------------------------------------------------------------*/public void readStream (){try {InputStream stream = iInputURL.openStream ();// according to JDK 1.1.3 docs, now use BufferedReader. Here the docs://// As of JDK 1.1, the preferred way to read lines of text is via the// BufferedReader.readLine() method. Programs that use the// DataInputStream class to read lines can be converted to use the// BufferedReader class by replacing code of the form// DataInputStream d = new DataInputStream(in);// with// BufferedReader d = new BufferedReader(new InputStreamReader(in));BufferedReader is = new BufferedReader (new InputStreamReader (stream));// now read and parse all lines of the file.String line = is.readLine ();while (line != null) {parseLine (line);line = is.readLine ();}is.close ();stream.close ();} catch (IOException exc) {// there's some error, ignore it!} catch (Exception exc) {Tracer.write ("exction opening " + iInputURL.toString () + "\n");Tracer.write ("string: " + exc.toString () + "\n");if (exc.getMessage () != null) {Tracer.write ("info: " + exc.getMessage () + "\n");}}return;}/*----------------------------------------------------------------------------INTERFACE:PURPOSE:NOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-05-08: created----------------------------------------------------------------------------*/public void parseLine (String line){// if the line is empty, we'll ignore itif (line.length () == 0) {return;}// if the line starts with #, its a commentif (line.startsWith ("#")) {return;}// if the line starts with whitespace, its a continuationif (line.startsWith ("\t")|| line.startsWith (" ")) {parseContinuation (line);}// else, it must be a key/value pairparseKeyValue (line);return;}/*----------------------------------------------------------------------------INTERFACE:PURPOSE:NOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-05-08: created----------------------------------------------------------------------------*/public void parseContinuation (String line){// if there's no key/value pair in the list, ignore itif (iData.size () < 1) {return;}// we'll trim whitespaceString value = line.trim ();// get the last objectValue lastObject = (Value) iData.lastElement ();// modify itlastObject.addToValue (value);return;}/*----------------------------------------------------------------------------INTERFACE:PURPOSE:NOTES: for safety, we'll ignore everything not okPARAMS:THROWS:RETURNS: voidHISTORY: 98-05-08: created98-05-15: handle include directive----------------------------------------------------------------------------*/public void parseKeyValue (String line){int index = line.indexOf (":");// not found or : as first characterif (index < 1) {return;}// everything from the start to the : is a keyString key = line.substring (0,index);String value = null;// value starts after :, trim whitespacetry {value = line.substring (index + 1).trim ();} catch (IndexOutOfBoundsException exc) {line = "";}// 98-05-15: handle include directiveif (key.equalsIgnoreCase (cIncludeDirective)) {// the value is assumed to be the URL of the included filetry {URL includedURL = new URL (iInputURL,value);Tracer.write ("URL to be included expands to " +includedURL.toString () + "\n");IndexStream idxStream = new IndexStream (includedURL);if (idxStream != null) {Value copyValue = idxStream.popEntry ();while (copyValue != null) {iData.addElement (copyValue);copyValue = idxStream.popEntry ();}}idxStream = null;} catch (MalformedURLException exc) {// ignore errorsTracer.write ("error parsing include URL " + value + "\n");return;}} else {// create the value and add it to the vectorValue newValue = new Value (key,value);iData.addElement (newValue);}return;}/*============================================================================Instance Variables============================================================================*/private URL iInputURL;private int iIndex;private Vector iData;/*============================================================================Static Data============================================================================*/private static final String cIncludeDirective = "include";}/*==============================================================================HISTORY:$Log: IndexStream.java,v $Revision 1.4 2002/05/05 22:34:52 pd.subset/.subset2, perfomace tweak in [[.data.frameRevision 1.3 1999/08/10 09:56:03 ripleychange FSF address in copyrightsadd some copyrights in src/gnome and elsewhereRevision 1.2 1999/03/04 17:15:18 leischvarious bugfixesRevision 1.1.4.1 1999/03/02 15:19:55 leischsearch used only kewords, no titlesRevision 1.2 1998/05/15 22:08:57 baierhandle includeRevision 1.1 1998/05/10 02:43:37 baierInitial revision==============================================================================*/// Local Variables:// mode: Java// mode: font-lock// End: