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 import java.util.Date ; 34 35 import javax.xml.parsers.DocumentBuilderFactory ; 36 import javax.xml.parsers.ParserConfigurationException ; 37 import javax.xml.transform.TransformerException ; 38 39 import org.apache.xpath.XPathAPI; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.traversal.NodeIterator; 43 import org.xml.sax.SAXException ; 44 45 50 public class DomWaiverSource implements WaiverSource { 51 private Element holder; 52 53 public DomWaiverSource(Element holder) { 54 this.holder=holder; 55 } 56 57 public DomWaiverSource(InputStream in) throws HammurapiException { 58 load(in); 59 } 60 61 public DomWaiverSource(File f) throws HammurapiException { 62 try { 63 load(new FileInputStream (f)); 64 } catch (FileNotFoundException e) { 65 throw new HammurapiException("File not found: "+f.getAbsolutePath(), e); 66 } 67 } 68 69 private void load(InputStream in) throws HammurapiException { 70 try { 71 Document document=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); 72 holder=document.getDocumentElement(); 73 } catch (ParserConfigurationException e) { 74 throw new HammurapiException(e.toString(), e); 75 } catch (SAXException e) { 76 throw new HammurapiException(e.toString(), e); 77 } catch (IOException e) { 78 throw new HammurapiException(e.toString(), e); 79 } 80 } 81 82 public void loadWaivers(WaiverSet waiverSet, Date now) throws HammurapiException { 83 if (holder.hasAttribute("base")) { 84 try { 85 URL baseURL=new URL (holder.getAttribute("base")); 86 DomWaiverSource base=new DomWaiverSource(baseURL.openStream()); 87 base.loadWaivers(waiverSet, now); 88 } catch (MalformedURLException e) { 89 throw new HammurapiException("Malformed base URL: "+e, e); 90 } catch (IOException e) { 91 throw new HammurapiException(e.toString(), e); 92 } 93 } 94 95 try { 96 NodeIterator waiverIterator=XPathAPI.selectNodeIterator(holder, "waiver"); 97 Element waiverElement; 98 while ((waiverElement=(Element ) waiverIterator.nextNode())!=null) { 99 waiverSet.addWaiver(new DomWaiver(waiverElement), now); 100 } 101 } catch (TransformerException e) { 102 new HammurapiException(e); 103 } 104 } 105 } 106 | Popular Tags |