KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > config > ConfigFileParser


1 /*
2  * Created on 31-Mar-2004
3  *
4  */

5 package com.jofti.config;
6
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 import org.xmlpull.v1.XmlPullParser;
16 import org.xmlpull.v1.XmlPullParserException;
17 import org.xmlpull.v1.XmlPullParserFactory;
18
19 import com.jofti.exception.JoftiException;
20
21 /**
22
23  *
24  * The parser is responsible for parsing the XMl configuration file and returns a Map of IndexConfig objects (one for each index in the file).<p>
25  * The current implementation uses KXML as its parser (as it is small and we probably will not get any version interference with
26  * other XML parsers).
27  *
28   * @author Steve Woodcock (steve@jofti.com)<p>
29   * @version 1.0
30  */

31 public class ConfigFileParser {
32     
33     
34  /**
35   * Parses the config file from the file name. The method uses getResourceAsStream() so the filename must be
36   * on the classpath.<p>
37   *
38  * @param configFile
39  * @return Map of IndexConfig objects.
40  * @throws JoftiException
41  */

42 public Map JavaDoc parseIndexConfig(String JavaDoc configFile) throws JoftiException
43  {
44     
45     Map JavaDoc temp =null;
46     
47     InputStream JavaDoc stream = null;
48     
49     try {
50         stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(configFile);
51
52         if (stream == null){
53             throw new JoftiException("Unable to find config file '" + configFile +"' on classpath - ensure the file name is correctly configured");
54         }
55         temp= parseConfig(stream);
56     
57     } catch (JoftiException e) {
58         e.printStackTrace();
59     } catch(Throwable JavaDoc t){
60         t.printStackTrace();
61     }finally{
62     
63         if (stream != null){
64             try{
65                 stream.close();
66             } catch(Exception JavaDoc e){
67                 // we do not care about this
68
}
69         }
70     }
71     return temp;
72 }
73
74 /**
75  * Parses the config file from an inputstream. The method allows calling code to load the configfile however it wants and
76  * pass the location independent stream containing the file.
77  * on the classpath.<p>
78  *
79 * @param stream
80 * @return Map of IndexConfig objects.
81 * @throws JoftiException
82 */

83  public Map JavaDoc parseIndexConfig(InputStream JavaDoc stream) throws JoftiException
84  {
85     
86     Map JavaDoc temp =null;
87     
88     
89     try {
90     
91         if (stream == null){
92             throw new JoftiException("InputStream is null ensure the file name is correctly configured");
93         }
94         temp= parseConfig(stream);
95     
96     } catch (JoftiException e) {
97         e.printStackTrace();
98     } catch(Throwable JavaDoc t){
99         t.printStackTrace();
100     }finally{
101     
102         if (stream != null){
103             try{
104                 stream.close();
105             } catch(Exception JavaDoc e){
106                 // we do not care about this
107
}
108         }
109     }
110     return temp;
111 }
112  
113  
114  private XmlPullParser getParser() throws Exception JavaDoc{
115      return XmlPullParserFactory.newInstance("org.kxml2.io.KXmlParser",Thread.currentThread().getContextClassLoader().getClass()).newPullParser();
116
117  }
118  
119  
120  private Map JavaDoc parseConfig(InputStream JavaDoc stream) throws JoftiException
121  {
122     Map JavaDoc indexMap = new HashMap JavaDoc();
123     
124     
125
126     XmlPullParser parser = null;
127     
128     try {
129     
130         if (stream == null){
131             throw new JoftiException("InputStrean is null - ensure the file name is correctly configured");
132         }
133         
134         parser = getParser();
135
136         parser.setInput(stream, null);
137
138         int eventType = parser.getEventType();
139         do {
140             if(eventType == XmlPullParser.START_DOCUMENT) {
141                 
142                     indexMap = processIndexes(parser);
143                 
144             }
145             
146             if (parser.getEventType() !=XmlPullParser.END_DOCUMENT ){
147                 eventType = parser.next();
148             }
149         }while (eventType != XmlPullParser.END_DOCUMENT);
150         parser.setInput (null);
151     } catch (JoftiException e) {
152         
153         e.printStackTrace();
154     } catch (XmlPullParserException e) {
155         
156         e.printStackTrace();
157     } catch (IOException JavaDoc e) {
158         
159         e.printStackTrace();
160     } catch(Throwable JavaDoc t){
161         t.printStackTrace();
162     }finally{
163     
164         if (stream != null){
165             try{
166                 stream.close();
167             } catch(Exception JavaDoc e){
168                 // we do not care about this
169
}
170         }
171     }
172
173
174     return indexMap;
175 }
176
177  
178  private Map JavaDoc processIndexes(XmlPullParser parser) throws XmlPullParserException, IOException JavaDoc, JoftiException{
179     
180     
181     Map JavaDoc temp = new HashMap JavaDoc();
182     //get the next tag - this should be indexes
183
while(true){
184         int tagType = parser.nextTag();
185         
186         if (tagType == XmlPullParser.START_TAG){
187             String JavaDoc name = parser.getName();
188             
189             if ("indexes".equalsIgnoreCase(name)){
190                 if (!parser.isEmptyElementTag()){
191                     // the next one should be an index tag
192
tagType = parser.nextTag();
193                     // should be index
194
while ( tagType == XmlPullParser.START_TAG && "index".equalsIgnoreCase(parser.getName())){
195                         IndexConfig config = processIndex(parser);
196                         temp.put(config.getName(),config);
197                         parser.nextTag();
198                     }
199                     
200                 }else{
201                     throw new JoftiException("Indexes definition in config file is empty");
202                 }
203             }else{
204                 throw new JoftiException("Unrecognised tag " + name + " at root of document");
205             }
206         }else if (tagType == XmlPullParser.END_TAG){
207             break;
208         }
209         
210         //parser.nextTag();
211
parser.require(XmlPullParser.END_TAG,null,"indexes");
212         break;
213      }
214     return temp;
215  }
216  
217   
218  IndexConfig processIndex(XmlPullParser parser)
219     throws IOException JavaDoc, XmlPullParserException,JoftiException {
220     
221     
222     
223     DefaultIndexConfig config = new DefaultIndexConfig();
224         
225     String JavaDoc name = parser.getName();
226     
227     
228     if ("index".equalsIgnoreCase(name)){
229             
230              //process the attributes for the index tag
231
int numAttributes = parser.getAttributeCount();
232             if (numAttributes >= 0){
233                 for (int i=0;i<numAttributes;i++){
234                     String JavaDoc attributeName = parser.getAttributeName(i);
235                     if (attributeName.equalsIgnoreCase("name")){
236                         config.setName(parser.getAttributeValue(i));
237                     } else
238                     if (attributeName.equalsIgnoreCase("init-method")){
239                         //config.setName(parser.getAttributeValue(i));
240
} else
241                     if (attributeName.equalsIgnoreCase("destroy-method")){
242                         //config.setName(parser.getAttributeValue(i));
243
}
244                     else {
245                         //log warning here
246
}
247                 }
248             }
249             
250             // now we have either cache/classes/type
251
int event = parser.nextTag();
252
253            name= parser.getName();
254             while ((event == XmlPullParser.START_TAG) && ("cache".equalsIgnoreCase(name) ||
255                     "classes".equalsIgnoreCase(name) ||"parser".equalsIgnoreCase(name) || "type".equalsIgnoreCase(name)
256                     || "disk-overflow".equalsIgnoreCase(name)
257                     || "queries".equalsIgnoreCase(name)))
258             {
259                 
260             
261                 if (name.equalsIgnoreCase("cache")){
262                     //see if empty
263
config = processCache(parser,config);
264                     parser.require(XmlPullParser.END_TAG,"","cache");
265                     
266                 }else if (name.equalsIgnoreCase("classes")){
267                     config = (DefaultIndexConfig)processClasses(parser, config);
268                     parser.require(XmlPullParser.END_TAG,"","classes");
269                     
270                 }else if (name.equalsIgnoreCase("queries")){
271                     config = (DefaultIndexConfig)processQueries(parser, config);
272                     parser.require(XmlPullParser.END_TAG,"","queries");
273                     
274                 }else if (name.equalsIgnoreCase("parser")){
275                     config.setParserType(parser.nextText().trim());
276                     parser.require(XmlPullParser.END_TAG,"","parser");
277                     
278                 }else if (name.equalsIgnoreCase("type")){
279                     config.setIndexType(parser.nextText().trim());
280                     parser.require(XmlPullParser.END_TAG,"","type");
281                 }else if (name.equalsIgnoreCase("disk-overflow")){
282                     config = (DefaultIndexConfig)processOverflow(parser, config);
283                     parser.require(XmlPullParser.END_TAG,"","disk-overflow");
284                 }
285                 
286                 event = parser.nextTag();
287                 name= parser.getName();
288             
289         }
290         
291         parser.require(XmlPullParser.END_TAG, "", "index");
292         
293     }
294     return config;
295  }
296  
297  private DefaultIndexConfig processCache(XmlPullParser parser, DefaultIndexConfig config)
298  throws IOException JavaDoc, XmlPullParserException, JoftiException{
299     
300             // we may have an adapter here
301
int event = parser.nextTag();
302             String JavaDoc name = parser.getName();
303             if(name.equalsIgnoreCase("adapter")){
304                 int adapterAttribs = parser.getAttributeCount();
305                 for (int i=0;i<adapterAttribs;i++){
306                     String JavaDoc temp = parser.getAttributeName(i);
307                     if (temp.equalsIgnoreCase("init-method")){
308                         //config.setCacheInitMethod(parser.getAttributeValue(i));
309
} else
310                     if (temp.equalsIgnoreCase("destroy-method")){
311                         //config.setCacheDestroyMethod(parser.getAttributeValue(i));
312
} else {
313                         //log warning here
314
}
315                 }
316                 //now see if we have a type or an end tag
317
event = parser.nextTag();
318                 while (event == XmlPullParser.START_TAG){
319                     //must be the type tag
320
name = parser.getName();
321                     if (name.equalsIgnoreCase("type")){
322                         String JavaDoc content = parser.nextText();
323                         config.setCacheAdapter(content.trim());
324                         parser.require(XmlPullParser.END_TAG,"","type");
325                         event = parser.nextTag();
326                         name = parser.getName();
327                     }
328                     if (name.equalsIgnoreCase("init-properties")){
329                         Properties JavaDoc props = new Properties JavaDoc();
330                         if (!parser.isEmptyElementTag()){
331                             //loop through attributes
332
while (parser.nextTag() == XmlPullParser.START_TAG){
333                                 name = parser.getName();
334                                 if (name.equalsIgnoreCase("property")){
335                                     String JavaDoc temp = parser.getAttributeValue(0);
336                                     if (temp != null){
337                                         props.put(temp, parser.nextText().trim());
338                                     }
339                                 }
340                                 parser.require(XmlPullParser.END_TAG,"",name);
341                             }
342                             config.setAdapterProperties(props);
343                             //assert the
344
parser.require(XmlPullParser.END_TAG,"","init-properties");
345                             event = parser.nextTag();
346                             
347                         }
348                         
349                     }
350                 }
351                 
352                 parser.require(XmlPullParser.END_TAG,"","adapter");
353                 parser.nextTag();
354             }
355
356             return config;
357         }
358
359  
360  private IndexConfig processOverflow(XmlPullParser parser, IndexConfig config)
361  throws IOException JavaDoc, XmlPullParserException, JoftiException{
362     
363     Properties JavaDoc props = config.getIndexProperties();
364                 //now see if we have a type or an end tag
365
int event = parser.nextTag();
366                 while (event == XmlPullParser.START_TAG){
367                     //must be the type tag
368
String JavaDoc name = parser.getName();
369                     if (name.equalsIgnoreCase("provider")){
370                         String JavaDoc content = parser.nextText();
371                         if (content !=null){
372                             content = content.trim();
373                         }
374                         props.put("provider",content);
375                         parser.require(XmlPullParser.END_TAG,"","provider");
376                         event = parser.nextTag();
377                         name = parser.getName();
378                     }
379                     
380                     if (name.equalsIgnoreCase("init-properties")){
381                         
382                         if (!parser.isEmptyElementTag()){
383                             //loop through attributes
384
while (parser.nextTag() == XmlPullParser.START_TAG){
385                                 name = parser.getName();
386                                 if (name.equalsIgnoreCase("property")){
387                                     String JavaDoc temp = parser.getAttributeValue(0);
388                                     if (temp != null){
389                                         props.put(temp, parser.nextText().trim());
390                                     }
391                                 }
392                                 parser.require(XmlPullParser.END_TAG,"",name);
393                             }
394                             
395                             //assert the
396
parser.require(XmlPullParser.END_TAG,"","init-properties");
397                             event = parser.nextTag();
398                             
399                         }
400                         
401                     }
402                 }
403                 
404            
405
406             return config;
407         }
408  private IndexConfig processClasses(XmlPullParser parser, IndexConfig config)
409  throws IOException JavaDoc, XmlPullParserException, JoftiException{
410     
411     Map JavaDoc temp = new HashMap JavaDoc();
412     //loop through classes
413
while (parser.nextTag() == XmlPullParser.START_TAG){
414         String JavaDoc name = parser.getName();
415         if (name.equalsIgnoreCase("class")){
416             List JavaDoc list = new ArrayList JavaDoc();
417             String JavaDoc className = parser.getAttributeValue(0);
418             if (!parser.isEmptyElementTag()){
419                 ;
420                 //loop through attributes
421
while (parser.nextTag() == XmlPullParser.START_TAG){
422                     name = parser.getName();
423                     if (name.equalsIgnoreCase("property")){
424                         list.add(parser.nextText());
425                     }
426                     parser.require(XmlPullParser.END_TAG,"",name);
427                 }
428                 //assert the
429
parser.require(XmlPullParser.END_TAG,"","class");
430                 temp.put(className,list);
431             }
432             config.addMapping(className,list);
433         }
434     }
435     
436     return config;
437  }
438  
439  private IndexConfig processQueries(XmlPullParser parser, IndexConfig config)
440  throws IOException JavaDoc, XmlPullParserException, JoftiException{
441     
442     //loop through queries
443
while (parser.nextTag() == XmlPullParser.START_TAG){
444         String JavaDoc name = parser.getName();
445         if (name.equalsIgnoreCase("query")){
446             String JavaDoc queryText = null;
447             String JavaDoc queryName = parser.getAttributeValue(0);
448                 
449                 queryText = parser.nextText().trim();
450                 
451                 config.addQuery(queryName,queryText);
452                 parser.require(XmlPullParser.END_TAG,"","query");
453         }
454     }
455     
456     return config;
457  }
458  /**
459   * Used to parse only the classmappings fragment of an xml file. Mot common usage is when manual configuration
460   * is being used with no config file and a file containing only the classes is passed in.<p>
461  * @param config
462  * @param stream
463  * @return IndexConfig containing the parsed classes.
464  * @throws JoftiException
465  */

466 public IndexConfig parseClassMapping(IndexConfig config, InputStream JavaDoc stream) throws JoftiException{
467      return parseClassFromStream(config,stream);
468  }
469
470 /**
471  * Used to parse only the classmappings fragment of an xml file. Mot common usage is when manual configuration
472  * is being used with no config file and a file containing only the classes is passed in. Note: uses getResourceAsStream and so
473  * file name must be on classpath.<p>
474 * @param config
475 * @param classFileName
476 * @return IndexConfig containing the parsed classes.
477 * @throws JoftiException
478 */

479  public IndexConfig parseClassMapping(IndexConfig config, String JavaDoc classFileName) throws JoftiException{
480      InputStream JavaDoc stream =null;
481      try {
482             stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(classFileName);
483
484             if (stream == null){
485                 throw new JoftiException("Unable to find config file '" + classFileName +"' on classpath - ensure the file name is correctly configured");
486             }
487             config = parseClassFromStream(config,stream);
488      } catch(Throwable JavaDoc t){
489                 if(t instanceof JoftiException){
490                         throw (JoftiException )t;
491                 }else{
492                     throw new JoftiException(t);
493                 }
494     }
495      return config;
496     
497  }
498  
499  private IndexConfig parseClassFromStream(IndexConfig config, InputStream JavaDoc stream) throws JoftiException{
500         
501         
502         try {
503     
504             if (stream == null){
505                 throw new JoftiException("Input Stream for classes is null");
506             }
507             
508             XmlPullParser parser = getParser();
509             parser.setInput(stream, null);
510             
511             int eventType = parser.getEventType();
512             
513             do {
514                 if(eventType == XmlPullParser.START_DOCUMENT){
515                     
516                     eventType = parser.nextTag();
517                     String JavaDoc name = parser.getName();
518                     while (eventType == XmlPullParser.START_TAG) {
519                                
520                     
521                     if (eventType == XmlPullParser.START_TAG && ("classes".equalsIgnoreCase(name))
522                              ) {
523                 
524                     
525                         config = processClasses(parser, config);
526                         
527                         parser.require(XmlPullParser.END_TAG,"","classes");
528                         eventType =parser.next();
529                         name = parser.getName();
530                         
531                     }else if (eventType == XmlPullParser.START_TAG && ("queries".equalsIgnoreCase(name))
532                      ) {
533                         config = processQueries(parser, config);
534                         
535                         parser.require(XmlPullParser.END_TAG,"","queries");
536                         eventType =parser.next();
537                         name = parser.getName();
538                     }else if (eventType == XmlPullParser.START_TAG && ("disk-overflow".equalsIgnoreCase(name))
539                      ) {
540                         config =processOverflow(parser, config);
541                         parser.require(XmlPullParser.END_TAG,"","disk-overflow");
542                         eventType =parser.next();
543                         name = parser.getName();
544                     }else{
545                         throw new JoftiException("Classes definitions must contain only '<classes>' or '<queries>' or '<disk-overflow>' tag");
546                     }
547                     }
548                     
549                 }
550                 
551                 //eventType = parser.getEventType();
552
if (eventType !=XmlPullParser.END_DOCUMENT ){
553                     throw new JoftiException("Document not formatted correctly extra tag found "+ parser.getName());
554                 }
555             }while (eventType != XmlPullParser.END_DOCUMENT);
556             parser.setInput (null);
557         } catch (Exception JavaDoc e) {
558             
559             throw new JoftiException(e);
560         }finally{
561         
562             if (stream != null){
563                 try{
564                     stream.close();
565                 } catch(Exception JavaDoc e){
566                     // we do not care about this
567
}
568             }
569         }
570         return config;
571  }
572  
573  
574  
575 }
576
577
Popular Tags