001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependency;
034:
035: import java.io.*;
036:
037: import javax.xml.parsers.*;
038:
039: import org.apache.log4j.*;
040: import org.xml.sax.*;
041:
042: public class NodeLoader {
043: private static final boolean DEFAULT_VALIDATE = false;
044:
045: private NodeHandler handler;
046: private boolean validate;
047:
048: public NodeLoader() {
049: this (new NodeFactory(), DEFAULT_VALIDATE);
050: }
051:
052: public NodeLoader(NodeFactory factory) {
053: this (factory, DEFAULT_VALIDATE);
054: }
055:
056: public NodeLoader(boolean validate) {
057: this (new NodeFactory(), validate);
058: }
059:
060: public NodeLoader(NodeFactory factory, boolean validate) {
061: this .handler = new NodeHandler(factory);
062: this .validate = validate;
063: }
064:
065: public NodeFactory load(String filename) throws IOException,
066: SAXException, ParserConfigurationException {
067: NodeFactory result = null;
068:
069: FileReader in = null;
070: try {
071: in = new FileReader(filename);
072: result = load(in);
073: } finally {
074: if (in != null) {
075: in.close();
076: }
077: }
078:
079: return result;
080: }
081:
082: public NodeFactory load(InputStream in) throws IOException,
083: ParserConfigurationException, SAXException {
084: return load(new InputSource(in));
085: }
086:
087: public NodeFactory load(Reader in) throws IOException,
088: ParserConfigurationException, SAXException {
089: return load(new InputSource(in));
090: }
091:
092: public NodeFactory load(InputSource in) throws IOException,
093: ParserConfigurationException, SAXException {
094: XMLReader reader = SAXParserFactory.newInstance()
095: .newSAXParser().getXMLReader();
096: reader.setDTDHandler(handler);
097: reader.setContentHandler(handler);
098: reader.setErrorHandler(handler);
099:
100: try {
101: if (validate) {
102: Logger.getLogger(getClass()).warn(
103: "XML validation turned on");
104: reader.setFeature(
105: "http://xml.org/sax/features/validation", true);
106: reader
107: .setFeature(
108: "http://apache.org/xml/features/nonvalidating/load-external-dtd",
109: true);
110: } else {
111: Logger.getLogger(getClass()).info(
112: "XML validation turned off");
113: reader
114: .setFeature(
115: "http://xml.org/sax/features/validation",
116: false);
117: reader
118: .setFeature(
119: "http://apache.org/xml/features/nonvalidating/load-external-dtd",
120: false);
121: }
122: } catch (Exception ex) {
123: Logger.getLogger(getClass()).warn(
124: "Problem setting validation feature on XML reader",
125: ex);
126: }
127:
128: reader.parse(in);
129:
130: return handler.getFactory();
131: }
132:
133: public void addDependencyListener(DependencyListener listener) {
134: handler.addDependencyListener(listener);
135: }
136:
137: public void removeDependencyListener(DependencyListener listener) {
138: handler.removeDependencyListener(listener);
139: }
140: }
|