001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023:
024: package org.hammurapi;
025:
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.FileNotFoundException;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.net.MalformedURLException;
032: import java.net.URL;
033:
034: import javax.xml.parsers.DocumentBuilderFactory;
035: import javax.xml.parsers.ParserConfigurationException;
036: import javax.xml.transform.TransformerException;
037:
038: import org.apache.xpath.XPathAPI;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041: import org.w3c.dom.traversal.NodeIterator;
042: import org.xml.sax.SAXException;
043:
044: import com.pavelvlasov.config.ConfigurationException;
045:
046: /**
047: *
048: * @author Pavel Vlasov
049: * @version $Revision: 1.3 $
050: */
051: public class DomInspectorSource implements InspectorSource {
052: private Element holder;
053: private String source;
054:
055: /** Creates a new instance of DomInspectorSource */
056: public DomInspectorSource(Element holder, String source) {
057: this .holder = holder;
058: this .source = source;
059: }
060:
061: public DomInspectorSource(InputStream in, String source)
062: throws HammurapiException {
063: load(in);
064: this .source = source;
065: }
066:
067: public DomInspectorSource(File f, String source)
068: throws HammurapiException {
069: try {
070: load(new FileInputStream(f));
071: } catch (FileNotFoundException e) {
072: throw new HammurapiException("File not found: "
073: + f.getAbsolutePath(), e);
074: }
075: this .source = source;
076: }
077:
078: private void load(InputStream in) throws HammurapiException {
079: try {
080: Document document = DocumentBuilderFactory.newInstance()
081: .newDocumentBuilder().parse(in);
082: holder = document.getDocumentElement();
083: } catch (ParserConfigurationException e) {
084: throw new HammurapiException(e.toString(), e);
085: } catch (SAXException e) {
086: throw new HammurapiException(e.toString(), e);
087: } catch (IOException e) {
088: throw new HammurapiException(e.toString(), e);
089: }
090: }
091:
092: public void loadInspectors(InspectorSet inspectorSet)
093: throws HammurapiException {
094: if (holder.hasAttribute("base")) {
095: try {
096: URL baseURL = new URL(holder.getAttribute("base"));
097: DomInspectorSource base = new DomInspectorSource(
098: baseURL.openStream(), "URL: " + baseURL);
099: base.loadInspectors(inspectorSet);
100: } catch (MalformedURLException e) {
101: throw new HammurapiException(
102: "Malformed base URL: " + e, e);
103: } catch (IOException e) {
104: throw new HammurapiException(e.toString(), e);
105: }
106: }
107:
108: try {
109: NodeIterator inspectorsIterator = XPathAPI
110: .selectNodeIterator(holder, "inspector-descriptor");
111: Element inspectorElement;
112: while ((inspectorElement = (Element) inspectorsIterator
113: .nextNode()) != null) {
114: inspectorSet.addDescriptor(new DomInspectorDescriptor(
115: inspectorElement));
116: }
117: } catch (TransformerException e) {
118: new HammurapiException(e);
119: } catch (ConfigurationException e) {
120: new HammurapiException(e);
121: }
122:
123: inspectorSet.addInspectorSourceInfo(new InspectorSourceInfo(
124: holder.getAttribute("name"), source, holder
125: .getAttribute("revision")));
126: }
127: }
|