KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > DomInspectorSource


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
34 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36 import javax.xml.transform.TransformerException JavaDoc;
37
38 import org.apache.xpath.XPathAPI;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.traversal.NodeIterator;
42 import org.xml.sax.SAXException JavaDoc;
43
44 import com.pavelvlasov.config.ConfigurationException;
45
46 /**
47  *
48  * @author Pavel Vlasov
49  * @version $Revision: 1.3 $
50  */

51 public class DomInspectorSource implements InspectorSource {
52     private Element JavaDoc holder;
53     private String JavaDoc source;
54     
55     /** Creates a new instance of DomInspectorSource */
56     public DomInspectorSource(Element JavaDoc holder, String JavaDoc source) {
57         this.holder=holder;
58         this.source=source;
59     }
60     
61     public DomInspectorSource(InputStream JavaDoc in, String JavaDoc source) throws HammurapiException {
62         load(in);
63         this.source=source;
64     }
65     
66     public DomInspectorSource(File JavaDoc f, String JavaDoc source) throws HammurapiException {
67         try {
68             load(new FileInputStream JavaDoc(f));
69         } catch (FileNotFoundException JavaDoc e) {
70             throw new HammurapiException("File not found: "+f.getAbsolutePath(), e);
71         }
72         this.source=source;
73     }
74     
75     private void load(InputStream JavaDoc in) throws HammurapiException {
76         try {
77             Document JavaDoc document=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
78             holder=document.getDocumentElement();
79         } catch (ParserConfigurationException JavaDoc e) {
80             throw new HammurapiException(e.toString(), e);
81         } catch (SAXException JavaDoc e) {
82             throw new HammurapiException(e.toString(), e);
83         } catch (IOException JavaDoc 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 JavaDoc baseURL=new URL JavaDoc(holder.getAttribute("base"));
92                 DomInspectorSource base=new DomInspectorSource(baseURL.openStream(), "URL: "+baseURL);
93                 base.loadInspectors(inspectorSet);
94             } catch (MalformedURLException JavaDoc e) {
95                 throw new HammurapiException("Malformed base URL: "+e, e);
96             } catch (IOException JavaDoc e) {
97                 throw new HammurapiException(e.toString(), e);
98             }
99         }
100         
101         try {
102             NodeIterator inspectorsIterator=XPathAPI.selectNodeIterator(holder, "inspector-descriptor");
103             Element JavaDoc inspectorElement;
104             while ((inspectorElement=(Element JavaDoc) inspectorsIterator.nextNode())!=null) {
105                 inspectorSet.addDescriptor(new DomInspectorDescriptor(inspectorElement));
106             }
107         } catch (TransformerException JavaDoc e) {
108             new HammurapiException(e);
109         } catch (ConfigurationException JavaDoc e) {
110             new HammurapiException(e);
111         }
112
113         inspectorSet.addInspectorSourceInfo(new InspectorSourceInfo(holder.getAttribute("name"), source, holder.getAttribute("revision")));
114     }
115 }
116
Popular Tags