1 23 24 package org.hammurapi; 25 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.net.MalformedURLException ; 32 import java.net.URL ; 33 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 import javax.xml.transform.TransformerException ; 37 38 import org.apache.xpath.XPathAPI; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.traversal.NodeIterator; 42 import org.xml.sax.SAXException ; 43 44 import com.pavelvlasov.config.ConfigurationException; 45 46 51 public class DomInspectorSource implements InspectorSource { 52 private Element holder; 53 private String source; 54 55 56 public DomInspectorSource(Element holder, String source) { 57 this.holder=holder; 58 this.source=source; 59 } 60 61 public DomInspectorSource(InputStream in, String source) throws HammurapiException { 62 load(in); 63 this.source=source; 64 } 65 66 public DomInspectorSource(File f, String source) throws HammurapiException { 67 try { 68 load(new FileInputStream (f)); 69 } catch (FileNotFoundException e) { 70 throw new HammurapiException("File not found: "+f.getAbsolutePath(), e); 71 } 72 this.source=source; 73 } 74 75 private void load(InputStream in) throws HammurapiException { 76 try { 77 Document document=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); 78 holder=document.getDocumentElement(); 79 } catch (ParserConfigurationException e) { 80 throw new HammurapiException(e.toString(), e); 81 } catch (SAXException e) { 82 throw new HammurapiException(e.toString(), e); 83 } catch (IOException e) { 84 throw new HammurapiException(e.toString(), e); 85 } 86 } 87 88 public void loadInspectors(InspectorSet inspectorSet) throws HammurapiException { 89 if (holder.hasAttribute("base")) { 90 try { 91 URL baseURL=new URL (holder.getAttribute("base")); 92 DomInspectorSource base=new DomInspectorSource(baseURL.openStream(), "URL: "+baseURL); 93 base.loadInspectors(inspectorSet); 94 } catch (MalformedURLException e) { 95 throw new HammurapiException("Malformed base URL: "+e, e); 96 } catch (IOException e) { 97 throw new HammurapiException(e.toString(), e); 98 } 99 } 100 101 try { 102 NodeIterator inspectorsIterator=XPathAPI.selectNodeIterator(holder, "inspector-descriptor"); 103 Element inspectorElement; 104 while ((inspectorElement=(Element ) inspectorsIterator.nextNode())!=null) { 105 inspectorSet.addDescriptor(new DomInspectorDescriptor(inspectorElement)); 106 } 107 } catch (TransformerException e) { 108 new HammurapiException(e); 109 } catch (ConfigurationException e) { 110 new HammurapiException(e); 111 } 112 113 inspectorSet.addInspectorSourceInfo(new InspectorSourceInfo(holder.getAttribute("name"), source, holder.getAttribute("revision"))); 114 } 115 } 116 | Popular Tags |