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: import java.util.Date;
034:
035: import javax.xml.parsers.DocumentBuilderFactory;
036: import javax.xml.parsers.ParserConfigurationException;
037: import javax.xml.transform.TransformerException;
038:
039: import org.apache.xpath.XPathAPI;
040: import org.w3c.dom.Document;
041: import org.w3c.dom.Element;
042: import org.w3c.dom.traversal.NodeIterator;
043: import org.xml.sax.SAXException;
044:
045: /**
046: *
047: * @author Pavel Vlasov
048: * @version $Revision: 1.3 $
049: */
050: public class DomWaiverSource implements WaiverSource {
051: private Element holder;
052:
053: public DomWaiverSource(Element holder) {
054: this .holder = holder;
055: }
056:
057: public DomWaiverSource(InputStream in) throws HammurapiException {
058: load(in);
059: }
060:
061: public DomWaiverSource(File f) throws HammurapiException {
062: try {
063: load(new FileInputStream(f));
064: } catch (FileNotFoundException e) {
065: throw new HammurapiException("File not found: "
066: + f.getAbsolutePath(), e);
067: }
068: }
069:
070: private void load(InputStream in) throws HammurapiException {
071: try {
072: Document document = DocumentBuilderFactory.newInstance()
073: .newDocumentBuilder().parse(in);
074: holder = document.getDocumentElement();
075: } catch (ParserConfigurationException e) {
076: throw new HammurapiException(e.toString(), e);
077: } catch (SAXException e) {
078: throw new HammurapiException(e.toString(), e);
079: } catch (IOException e) {
080: throw new HammurapiException(e.toString(), e);
081: }
082: }
083:
084: public void loadWaivers(WaiverSet waiverSet, Date now)
085: throws HammurapiException {
086: if (holder.hasAttribute("base")) {
087: try {
088: URL baseURL = new URL(holder.getAttribute("base"));
089: DomWaiverSource base = new DomWaiverSource(baseURL
090: .openStream());
091: base.loadWaivers(waiverSet, now);
092: } catch (MalformedURLException e) {
093: throw new HammurapiException(
094: "Malformed base URL: " + e, e);
095: } catch (IOException e) {
096: throw new HammurapiException(e.toString(), e);
097: }
098: }
099:
100: try {
101: NodeIterator waiverIterator = XPathAPI.selectNodeIterator(
102: holder, "waiver");
103: Element waiverElement;
104: while ((waiverElement = (Element) waiverIterator.nextNode()) != null) {
105: waiverSet.addWaiver(new DomWaiver(waiverElement), now);
106: }
107: } catch (TransformerException e) {
108: new HammurapiException(e);
109: }
110: }
111: }
|