The R Project SVN R

Rev

Rev 3765 | Go to most recent revision | 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
8
  ALL RIGHTS RESERVED.
9
 
10
  $Source: /scratch/CVS-ARCHIVE/R/doc/html/search/IndexStream.java,v $
11
 
12
  $Revision: 1.1 $
13
 
14
  $Date: 1998/05/15 10:38:09 $
15
 
16
  $Author: leisch $
17
 
18
==============================================================================*/
19
 
20
 
21
/* -------------------------------- Imports --------------------------------- */
22
 
23
import java.util.*;
24
import java.applet.*;
25
import java.awt.*;
26
import java.net.*;
27
import java.io.*;
28
 
29
 
30
 
31
/*==============================================================================
32
                          Interface of class IndexStream
33
==============================================================================*/
34
 
35
/*------------------------------------------------------------------------------
36
  CLASS:    IndexStream
37
  SUPER:    Object
38
  CONF. TO: 
39
  PURPOSE:  reading and parsing a stream of multiple index entries, also handles
40
            some keywords, e.g. include of other streams, provides a context
41
            for environment space
42
  NOTES:    
43
 
44
  HISTORY:  98-05-08: created
45
------------------------------------------------------------------------------*/
46
public class IndexStream extends Object
47
{
48
  /*============================================================================
49
                                Public methods
50
  ============================================================================*/
51
 
52
  /*----------------------------------------------------------------------------
53
    INTERFACE: 
54
    PURPOSE:   default constructor
55
 
56
    NOTES:    
57
 
58
    PARAMS:   
59
    THROWS:    
60
    RETURNS:   void
61
 
62
    HISTORY:   98-05-08: created
63
  ----------------------------------------------------------------------------*/
64
  public IndexStream (URL inputURL)
65
  {
66
    // init the instance data
67
    iInputURL = inputURL;
68
    iIndex = 0;
69
    iData = new Vector ();
70
 
71
    // read the index data from the stream
72
    readStream ();
73
  }
74
 
75
 
76
  /*----------------------------------------------------------------------------
77
    INTERFACE: 
78
    PURPOSE:   
79
 
80
    NOTES:     
81
 
82
    PARAMS:    
83
    THROWS:    
84
    RETURNS:   void
85
 
86
    HISTORY:   98-05-08: created
87
  ----------------------------------------------------------------------------*/
88
  public Value popEntry ()
89
  {
90
    if (iIndex < 0) {
91
      iIndex = 0;
92
    }
93
 
94
    if (iIndex >= iData.size ()) {
95
      return null;
96
    }
97
 
98
    iIndex++;
99
 
100
    return (Value) iData.elementAt (iIndex - 1);
101
  }
102
 
103
 
104
  /*----------------------------------------------------------------------------
105
    INTERFACE: 
106
    PURPOSE:   
107
 
108
    NOTES:     
109
 
110
    PARAMS:    
111
    THROWS:    
112
    RETURNS:   void
113
 
114
    HISTORY:   98-05-08: created
115
  ----------------------------------------------------------------------------*/
116
  public void pushEntry (Value anEntry)
117
  {
118
    if (iIndex < 0) {
119
      iIndex = 0;
120
    }
121
 
122
    if (iIndex >= iData.size ()) {
123
      iIndex = iData.size ();
124
    }
125
 
126
    iData.insertElementAt (anEntry,iIndex);
127
 
128
    return;
129
  }
130
 
131
 
132
  /*============================================================================
133
                              Protected methods
134
  ============================================================================*/
135
 
136
  /*============================================================================
137
                               Private methods
138
  ============================================================================*/
139
 
140
  /*----------------------------------------------------------------------------
141
    INTERFACE: 
142
    PURPOSE:   
143
 
144
    NOTES:     
145
 
146
    PARAMS:    
147
    THROWS:    
148
    RETURNS:   void
149
 
150
    HISTORY:   98-05-08: created
151
  ----------------------------------------------------------------------------*/
152
  public void readStream ()
153
  {
154
    try {
155
      InputStream stream = iInputURL.openStream ();
156
      DataInputStream is = new DataInputStream (stream);
157
 
158
      // now read and parse all lines of the file.
159
      String line = is.readLine ();
160
 
161
      while (line != null) {
162
	parseLine (line);
163
	line = is.readLine ();
164
      }
165
 
166
      is.close ();
167
      stream.close ();
168
    } catch (IOException exc) {
169
      // there's some error, ignore it!
170
    }
171
 
172
    return;
173
  }
174
 
175
 
176
  /*----------------------------------------------------------------------------
177
    INTERFACE: 
178
    PURPOSE:   
179
 
180
    NOTES:     
181
 
182
    PARAMS:    
183
    THROWS:    
184
    RETURNS:   void
185
 
186
    HISTORY:   98-05-08: created
187
  ----------------------------------------------------------------------------*/
188
  public void parseLine (String line)
189
  {
190
    // if the line is empty, we'll ignore it
191
    if (line.length () == 0) {
192
      return;
193
    }
194
 
195
    // if the line starts with #, its a comment
196
    if (line.startsWith ("#")) {
197
      return;
198
    }
199
 
200
    // if the line starts with whitespace, its a continuation
201
    if (line.startsWith ("\t")
202
	|| line.startsWith (" ")) {
203
      parseContinuation (line);
204
    }
205
 
206
    // else, it must be a key/value pair
207
    parseKeyValue (line);
208
 
209
    return;
210
  }
211
 
212
 
213
  /*----------------------------------------------------------------------------
214
    INTERFACE: 
215
    PURPOSE:   
216
 
217
    NOTES:     
218
 
219
    PARAMS:    
220
    THROWS:    
221
    RETURNS:   void
222
 
223
    HISTORY:   98-05-08: created
224
  ----------------------------------------------------------------------------*/
225
  public void parseContinuation (String line)
226
  {
227
    // if there's no key/value pair in the list, ignore it
228
    if (iData.size () < 1) {
229
      return;
230
    }
231
 
232
    // we'll trim whitespace
233
    String value = line.trim ();
234
 
235
    // get the last object
236
    Value lastObject = (Value) iData.lastElement ();
237
 
238
    // modify it
239
    lastObject.addToValue (value);
240
 
241
    return;
242
  }
243
 
244
 
245
  /*----------------------------------------------------------------------------
246
    INTERFACE: 
247
    PURPOSE:   
248
 
249
    NOTES:     for safety, we'll ignore everything not ok
250
 
251
    PARAMS:    
252
    THROWS:    
253
    RETURNS:   void
254
 
255
    HISTORY:   98-05-08: created
256
  ----------------------------------------------------------------------------*/
257
  public void parseKeyValue (String line)
258
  {
259
    int index = line.indexOf (":");
260
 
261
    // not found or : as first character
262
    if (index < 1) {
263
      return;
264
    }
265
 
266
    // everything from the start to the : is a key
267
    String key = line.substring (0,index);
268
 
269
    String value = null;
270
 
271
    // value starts after :, trim whitespace
272
    try {
273
      value = line.substring (index + 1).trim ();
274
    } catch (IndexOutOfBoundsException exc) {
275
      line = "";
276
    }
277
 
278
    // create the value and add it to the vector
279
    Value newValue = new Value (key,value);
280
 
281
    iData.addElement (newValue);
282
 
283
    return;
284
  }
285
 
286
 
287
  /*============================================================================
288
                             Instance Variables
289
  ============================================================================*/
290
 
291
  private URL    iInputURL;
292
  private int    iIndex;
293
  private Vector iData;
294
 
295
 
296
  /*============================================================================
297
                                Static Data
298
  ============================================================================*/
299
}
300
 
301
 
302
/*==============================================================================
303
 
304
  HISTORY:
305
 
306
  $Log: IndexStream.java,v $
307
  Revision 1.1  1998/05/15 10:38:09  leisch
308
  New: Search Engine
309
 
310
  Revision 1.1  1998/05/10 02:43:37  baier
311
  Initial revision
312
 
313
 
314
==============================================================================*/
315
 
316
 
317
// Local Variables:
318
// mode: Java
319
// mode: font-lock
320
// End: