The R Project SVN R

Rev

Rev 5458 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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