1 package org.exoplatform.services.xml.querying.impl.xtas; 2 import java.util.Collection ; 3 import java.io.IOException ; 4 import org.xml.sax.SAXException ; 5 import javax.xml.parsers.ParserConfigurationException ; 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 ; 10 import org.w3c.dom.NodeList ; 11 import java.util.ArrayList ; 12 import org.w3c.dom.Node ; 13 import org.w3c.dom.Text ; 14 15 16 public class XMLConfig implements Config 17 { 18 private static XMLConfig config = null; 19 protected Collection resources; 20 21 protected XMLConfig(String fileName) throws IOException , SAXException , 22 ParserConfigurationException , ConfigException 23 { 24 25 Document doc = Utils.createDocument(this.getClass().getResourceAsStream(fileName)); 26 NodeList list1 = doc.getElementsByTagName("resource"); 27 28 resources = new ArrayList (); 29 30 for(int i=0; i< list1.getLength(); i++) { 31 ResourceDescriptor descr = new ResourceDescriptor(); 32 resources.add(descr); 33 Node params = list1.item(i); 34 NodeList list2 = params.getChildNodes(); 35 for(int j=0;j <list2.getLength(); j++) { 36 Node param = list2.item(j); 37 38 if(param.hasChildNodes()) 39 if( param.getNodeName().equals("name") ) 40 descr.setName(((Text )param.getFirstChild()).getData()); 41 else if( param.getNodeName().equals("classname") ) 42 descr.setClassname(((Text )param.getFirstChild()).getData()); 43 else if( param.getNodeName().equals("prefix") ) 44 descr.setPrefix(((Text )param.getFirstChild()).getData()); 45 else if( param.getNodeName().equals("description") ) 46 descr.setDescription(((Text )param.getFirstChild()).getData()); 47 48 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 e) { 66 throw new ConfigException("Error while creating XmlConfig! "+e); 67 } 68 } 69 return config; 70 } 71 72 public Collection getResources() 73 { 74 return resources; 75 } 76 77 } 78 | Popular Tags |