KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > XMLConfig


1 package org.exoplatform.services.xml.querying.impl.xtas;
2 import java.util.Collection JavaDoc;
3 import java.io.IOException JavaDoc;
4 import org.xml.sax.SAXException JavaDoc;
5 import javax.xml.parsers.ParserConfigurationException JavaDoc;
6 import org.exoplatform.services.xml.querying.ConfigException;
7 import org.exoplatform.services.xml.querying.impl.xtas.resource.ResourceDescriptor;
8 import org.exoplatform.services.xml.querying.impl.xtas.xml.Utils;
9 import org.w3c.dom.Document JavaDoc;
10 import org.w3c.dom.NodeList JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import org.w3c.dom.Node JavaDoc;
13 import org.w3c.dom.Text JavaDoc;
14
15
16 public class XMLConfig implements Config
17 {
18     private static XMLConfig config = null;
19     protected Collection JavaDoc resources;
20
21     protected XMLConfig(String JavaDoc fileName) throws IOException JavaDoc, SAXException JavaDoc,
22         ParserConfigurationException JavaDoc, ConfigException
23     {
24
25         Document JavaDoc doc = Utils.createDocument(this.getClass().getResourceAsStream(fileName));
26         NodeList JavaDoc list1 = doc.getElementsByTagName("resource");
27
28         resources = new ArrayList JavaDoc();
29
30         for(int i=0; i< list1.getLength(); i++) {
31             ResourceDescriptor descr = new ResourceDescriptor();
32             resources.add(descr);
33             Node JavaDoc params = list1.item(i);
34             NodeList JavaDoc list2 = params.getChildNodes();
35             for(int j=0;j <list2.getLength(); j++) {
36                 Node JavaDoc param = list2.item(j);
37
38                 if(param.hasChildNodes())
39                 if( param.getNodeName().equals("name") )
40                     descr.setName(((Text JavaDoc)param.getFirstChild()).getData());
41                 else if( param.getNodeName().equals("classname") )
42                     descr.setClassname(((Text JavaDoc)param.getFirstChild()).getData());
43                 else if( param.getNodeName().equals("prefix") )
44                     descr.setPrefix(((Text JavaDoc)param.getFirstChild()).getData());
45                 else if( param.getNodeName().equals("description") )
46                     descr.setDescription(((Text JavaDoc)param.getFirstChild()).getData());
47
48 //System.out.println(descr.getPrefix());
49

50             }
51         }
52
53         if (resources.size() == 0)
54             throw new ConfigException("Xtas Config Exception - there are No Resources in config!");
55        
56     }
57
58     public static XMLConfig getInstance() throws ConfigException
59     {
60        if (config == null) {
61           try {
62             config = new XMLConfig("/xtas-config.xml");
63           } catch (ConfigException e) {
64             throw e;
65           } catch (Exception JavaDoc e) {
66             throw new ConfigException("Error while creating XmlConfig! "+e);
67           }
68         }
69        return config;
70     }
71
72     public Collection JavaDoc getResources()
73     {
74        return resources;
75     }
76
77 }
78
Popular Tags