KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > rentacar > util > ConfigHelper


1 /*
2  * Created on 26 juil. 2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package org.objectweb.rentacar.util;
8
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11
12 import org.jdom.Document;
13 import org.jdom.JDOMException;
14 import org.jdom.input.SAXBuilder;
15
16 /**
17  * @author mcarpentier
18  *
19  * Load an XML configuration file
20  */

21 public class ConfigHelper {
22
23     
24     /**
25      * Read the portal configuration Document
26      * @return the portal configuration Document
27      * @throws ConfigHelperException if configuration file was not found
28      * @throws JDOMException if an error occured while parsing the configuration file
29      */

30     public static Document getConfig(String JavaDoc XMLfile) throws RentacarUtilException {
31         Document doc = null;
32         String JavaDoc myFile = StringHelper.isEmpty(XMLfile) ? "rentacar.cfg.xml" : XMLfile;
33         InputStream JavaDoc myInputStream = ConfigHelper.class.getResourceAsStream("/"+myFile);
34         if (myInputStream == null) {
35             //TODO : log
36
throw new ExceptionInInitializerError JavaDoc("unable to find configuration file");
37         }
38         try {
39             doc =parseXmlFile(myInputStream);
40         } catch (JDOMException e) {
41             throw new RentacarUtilException("Error in JDOM parsing", e);
42         } catch (IOException JavaDoc e) {
43             throw new RentacarUtilException("Error in JDOM parsing", e);
44         }
45         return doc;
46         
47     }
48     
49     /**
50      * TODO : check if XML file is following its DTD ?
51      */

52     public boolean isValid(String JavaDoc XMLfile) {
53         return true;
54     }
55     
56     /**
57      * Parse an xml file to produce a Document object mapping this xml template
58      * @param xmlURL the InputStream loaded from the XML document
59      * @return the JDOM document
60      * @throws IOException
61      */

62     private static Document parseXmlFile(InputStream JavaDoc xmlURL) throws JDOMException, IOException JavaDoc {
63         Document document = null;
64         SAXBuilder sxb = new SAXBuilder();
65         document = sxb.build(xmlURL);
66         return document;
67     }
68
69 }
70
Popular Tags