The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1230 leisch 1
/*==============================================================================
2
 
3
  Project: 
4
 
5
  JAVA Source file for the class IndexStream
6
 
7
  COPYRIGHT (C), 1998, Thomas Baier
3765 leisch 8
 
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program; if not, write to the Free Software
5458 ripley 21
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 
1230 leisch 23
  $Source: /scratch/CVS-ARCHIVE/R/doc/html/search/IndexStream.java,v $
24
 
19562 pd 25
  $Revision: 1.4 $
1230 leisch 26
 
19562 pd 27
  $Date: 2002/05/05 22:34:52 $
1230 leisch 28
 
19562 pd 29
  $Author: pd $
1230 leisch 30
 
31
==============================================================================*/
32
 
33
 
34
/* -------------------------------- Imports --------------------------------- */
35
 
36
import java.util.*;
37
import java.applet.*;
38
import java.awt.*;
39
import java.net.*;
40
import java.io.*;
41
 
42
 
43
 
44
/*==============================================================================
45
                          Interface of class IndexStream
46
==============================================================================*/
47
 
48
/*------------------------------------------------------------------------------
49
  CLASS:    IndexStream
50
  SUPER:    Object
51
  CONF. TO: 
52
  PURPOSE:  reading and parsing a stream of multiple index entries, also handles
53
            some keywords, e.g. include of other streams, provides a context
54
            for environment space
55
  NOTES:    
56
 
57
  HISTORY:  98-05-08: created
58
------------------------------------------------------------------------------*/
59
public class IndexStream extends Object
60
{
61
  /*============================================================================
62
                                Public methods
63
  ============================================================================*/
64
 
65
  /*----------------------------------------------------------------------------
66
    INTERFACE: 
67
    PURPOSE:   default constructor
68
 
69
    NOTES:    
70
 
71
    PARAMS:   
72
    THROWS:    
73
    RETURNS:   void
74
 
75
    HISTORY:   98-05-08: created
3765 leisch 76
               98-05-15: new static member for include directive
1230 leisch 77
  ----------------------------------------------------------------------------*/
78
  public IndexStream (URL inputURL)
79
  {
80
    // init the instance data
81
    iInputURL = inputURL;
82
    iIndex = 0;
83
    iData = new Vector ();
84
 
85
    // read the index data from the stream
86
    readStream ();
87
  }
88
 
89
 
90
  /*----------------------------------------------------------------------------
91
    INTERFACE: 
92
    PURPOSE:   
93
 
94
    NOTES:     
95
 
96
    PARAMS:    
97
    THROWS:    
98
    RETURNS:   void
99
 
100
    HISTORY:   98-05-08: created
101
  ----------------------------------------------------------------------------*/
102
  public Value popEntry ()
103
  {
104
    if (iIndex < 0) {
105
      iIndex = 0;
106
    }
107
 
108
    if (iIndex >= iData.size ()) {
109
      return null;
110
    }
111
 
112
    iIndex++;
113
 
114
    return (Value) iData.elementAt (iIndex - 1);
115
  }
116
 
117
 
118
  /*----------------------------------------------------------------------------
119
    INTERFACE: 
120
    PURPOSE:   
121
 
122
    NOTES:     
123
 
124
    PARAMS:    
125
    THROWS:    
126
    RETURNS:   void
127
 
128
    HISTORY:   98-05-08: created
129
  ----------------------------------------------------------------------------*/
130
  public void pushEntry (Value anEntry)
131
  {
132
    if (iIndex < 0) {
133
      iIndex = 0;
134
    }
135
 
136
    if (iIndex >= iData.size ()) {
137
      iIndex = iData.size ();
138
    }
139
 
140
    iData.insertElementAt (anEntry,iIndex);
141
 
142
    return;
143
  }
144
 
145
 
146
  /*============================================================================
147
                              Protected methods
148
  ============================================================================*/
149
 
150
  /*============================================================================
151
                               Private methods
152
  ============================================================================*/
153
 
154
  /*----------------------------------------------------------------------------
155
    INTERFACE: 
156
    PURPOSE:   
157
 
158
    NOTES:     
159
 
160
    PARAMS:    
161
    THROWS:    
162
    RETURNS:   void
163
 
164
    HISTORY:   98-05-08: created
3765 leisch 165
               98-05-15: catch all exception (e.g. security violation)
166
               98-06-07: correctly trace exceptions
167
               98-11-14: replaced deprecated DataInputStream.readLine()
1230 leisch 168
  ----------------------------------------------------------------------------*/
169
  public void readStream ()
170
  {
171
    try {
172
      InputStream stream = iInputURL.openStream ();
3765 leisch 173
      // according to JDK 1.1.3 docs, now use BufferedReader. Here the docs:
174
      //
175
      // As of JDK 1.1, the preferred way to read lines of text is via the
176
      // BufferedReader.readLine() method. Programs that use the
177
      // DataInputStream class to read lines can be converted to use the
178
      // BufferedReader class by replacing code of the form 
179
      //   DataInputStream d = new DataInputStream(in); 
180
      // with 
181
      //   BufferedReader d = new BufferedReader(new InputStreamReader(in)); 
182
      BufferedReader is = new BufferedReader (new InputStreamReader (stream));
1230 leisch 183
 
184
      // now read and parse all lines of the file.
185
      String line = is.readLine ();
186
 
187
      while (line != null) {
188
	parseLine (line);
189
	line = is.readLine ();
190
      }
191
 
192
      is.close ();
193
      stream.close ();
194
    } catch (IOException exc) {
195
      // there's some error, ignore it!
3765 leisch 196
    } catch (Exception exc) {
197
      Tracer.write ("exction opening " + iInputURL.toString () + "\n");
198
      Tracer.write ("string: " + exc.toString () + "\n");
199
      if (exc.getMessage () != null) {
200
	Tracer.write ("info: " + exc.getMessage () + "\n");
201
      }
1230 leisch 202
    }
203
 
204
    return;
205
  }
206
 
207
 
208
  /*----------------------------------------------------------------------------
209
    INTERFACE: 
210
    PURPOSE:   
211
 
212
    NOTES:     
213
 
214
    PARAMS:    
215
    THROWS:    
216
    RETURNS:   void
217
 
218
    HISTORY:   98-05-08: created
219
  ----------------------------------------------------------------------------*/
220
  public void parseLine (String line)
221
  {
222
    // if the line is empty, we'll ignore it
223
    if (line.length () == 0) {
224
      return;
225
    }
226
 
227
    // if the line starts with #, its a comment
228
    if (line.startsWith ("#")) {
229
      return;
230
    }
231
 
232
    // if the line starts with whitespace, its a continuation
233
    if (line.startsWith ("\t")
234
	|| line.startsWith (" ")) {
235
      parseContinuation (line);
236
    }
237
 
238
    // else, it must be a key/value pair
239
    parseKeyValue (line);
240
 
241
    return;
242
  }
243
 
244
 
245
  /*----------------------------------------------------------------------------
246
    INTERFACE: 
247
    PURPOSE:   
248
 
249
    NOTES:     
250
 
251
    PARAMS:    
252
    THROWS:    
253
    RETURNS:   void
254
 
255
    HISTORY:   98-05-08: created
256
  ----------------------------------------------------------------------------*/
257
  public void parseContinuation (String line)
258
  {
259
    // if there's no key/value pair in the list, ignore it
260
    if (iData.size () < 1) {
261
      return;
262
    }
263
 
264
    // we'll trim whitespace
265
    String value = line.trim ();
266
 
267
    // get the last object
268
    Value lastObject = (Value) iData.lastElement ();
269
 
270
    // modify it
271
    lastObject.addToValue (value);
272
 
273
    return;
274
  }
275
 
276
 
277
  /*----------------------------------------------------------------------------
278
    INTERFACE: 
279
    PURPOSE:   
280
 
281
    NOTES:     for safety, we'll ignore everything not ok
282
 
283
    PARAMS:    
284
    THROWS:    
285
    RETURNS:   void
286
 
287
    HISTORY:   98-05-08: created
3765 leisch 288
               98-05-15: handle include directive
1230 leisch 289
  ----------------------------------------------------------------------------*/
290
  public void parseKeyValue (String line)
291
  {
292
    int index = line.indexOf (":");
293
 
294
    // not found or : as first character
295
    if (index < 1) {
296
      return;
297
    }
298
 
299
    // everything from the start to the : is a key
300
    String key = line.substring (0,index);
301
 
302
    String value = null;
303
 
304
    // value starts after :, trim whitespace
305
    try {
306
      value = line.substring (index + 1).trim ();
307
    } catch (IndexOutOfBoundsException exc) {
308
      line = "";
309
    }
310
 
3765 leisch 311
    // 98-05-15: handle include directive
312
    if (key.equalsIgnoreCase (cIncludeDirective)) {
313
      // the value is assumed to be the URL of the included file
314
      try {
315
	URL includedURL = new URL (iInputURL,value);
1230 leisch 316
 
3765 leisch 317
	Tracer.write ("URL to be included expands to " +
318
		      includedURL.toString () + "\n");
1230 leisch 319
 
3765 leisch 320
	IndexStream idxStream = new IndexStream (includedURL);
321
 
322
	if (idxStream != null) {
323
	  Value copyValue = idxStream.popEntry ();
324
	  while (copyValue != null) {
325
	    iData.addElement (copyValue);
326
	    copyValue = idxStream.popEntry ();
327
	  }
328
	}
329
	idxStream = null;
330
      } catch (MalformedURLException exc) {
331
	// ignore errors
332
	Tracer.write ("error parsing include URL " + value + "\n");
333
	return;
334
      }
335
    } else {
336
      // create the value and add it to the vector
337
      Value newValue = new Value (key,value);
338
 
339
      iData.addElement (newValue);
340
    }
341
 
1230 leisch 342
    return;
343
  }
344
 
345
 
346
  /*============================================================================
347
                             Instance Variables
348
  ============================================================================*/
349
 
350
  private URL    iInputURL;
351
  private int    iIndex;
352
  private Vector iData;
353
 
354
 
355
  /*============================================================================
356
                                Static Data
357
  ============================================================================*/
3765 leisch 358
  private static final String cIncludeDirective = "include";
1230 leisch 359
}
360
 
361
 
362
/*==============================================================================
363
 
364
  HISTORY:
365
 
366
  $Log: IndexStream.java,v $
19562 pd 367
  Revision 1.4  2002/05/05 22:34:52  pd
368
  .subset/.subset2, perfomace tweak in [[.data.frame
369
 
5458 ripley 370
  Revision 1.3  1999/08/10 09:56:03  ripley
371
  change FSF address in copyrights
372
  add some copyrights in src/gnome and elsewhere
373
 
3765 leisch 374
  Revision 1.2  1999/03/04 17:15:18  leisch
375
  various bugfixes
1230 leisch 376
 
3765 leisch 377
  Revision 1.1.4.1  1999/03/02 15:19:55  leisch
378
  search used only kewords, no titles
379
 
380
  Revision 1.2  1998/05/15 22:08:57  baier
381
  handle include
382
 
1230 leisch 383
  Revision 1.1  1998/05/10 02:43:37  baier
384
  Initial revision
385
 
386
 
387
==============================================================================*/
388
 
389
 
390
// Local Variables:
391
// mode: Java
392
// mode: font-lock
393
// End: