KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > DomWaiverSource


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23
24 package org.hammurapi;
25
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.net.MalformedURLException JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.util.Date JavaDoc;
34
35 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
36 import javax.xml.parsers.ParserConfigurationException JavaDoc;
37 import javax.xml.transform.TransformerException JavaDoc;
38
39 import org.apache.xpath.XPathAPI;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.traversal.NodeIterator;
43 import org.xml.sax.SAXException JavaDoc;
44
45 /**
46  *
47  * @author Pavel Vlasov
48  * @version $Revision: 1.3 $
49  */

50 public class DomWaiverSource implements WaiverSource {
51     private Element JavaDoc holder;
52     
53     public DomWaiverSource(Element JavaDoc holder) {
54         this.holder=holder;
55     }
56     
57     public DomWaiverSource(InputStream JavaDoc in) throws HammurapiException {
58         load(in);
59     }
60     
61     public DomWaiverSource(File JavaDoc f) throws HammurapiException {
62         try {
63             load(new FileInputStream JavaDoc(f));
64         } catch (FileNotFoundException JavaDoc e) {
65             throw new HammurapiException("File not found: "+f.getAbsolutePath(), e);
66         }
67     }
68     
69     private void load(InputStream JavaDoc in) throws HammurapiException {
70         try {
71             Document JavaDoc document=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
72             holder=document.getDocumentElement();
73         } catch (ParserConfigurationException JavaDoc e) {
74             throw new HammurapiException(e.toString(), e);
75         } catch (SAXException JavaDoc e) {
76             throw new HammurapiException(e.toString(), e);
77         } catch (IOException JavaDoc e) {
78             throw new HammurapiException(e.toString(), e);
79         }
80     }
81     
82     public void loadWaivers(WaiverSet waiverSet, Date JavaDoc now) throws HammurapiException {
83         if (holder.hasAttribute("base")) {
84             try {
85                 URL JavaDoc baseURL=new URL JavaDoc(holder.getAttribute("base"));
86                 DomWaiverSource base=new DomWaiverSource(baseURL.openStream());
87                 base.loadWaivers(waiverSet, now);
88             } catch (MalformedURLException JavaDoc e) {
89                 throw new HammurapiException("Malformed base URL: "+e, e);
90             } catch (IOException JavaDoc e) {
91                 throw new HammurapiException(e.toString(), e);
92             }
93         }
94         
95         try {
96             NodeIterator waiverIterator=XPathAPI.selectNodeIterator(holder, "waiver");
97             Element JavaDoc waiverElement;
98             while ((waiverElement=(Element JavaDoc) waiverIterator.nextNode())!=null) {
99                 waiverSet.addWaiver(new DomWaiver(waiverElement), now);
100             }
101         } catch (TransformerException JavaDoc e) {
102             new HammurapiException(e);
103         }
104     }
105 }
106
Popular Tags