KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > NodeLoader


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependency;
34
35 import java.io.*;
36 import java.net.*;
37 import java.util.*;
38
39 import org.apache.log4j.*;
40
41 import org.xml.sax.*;
42 import org.xml.sax.helpers.*;
43
44 public class NodeLoader {
45     private static final String JavaDoc DEFAULT_READER_CLASS_NAME = "org.apache.xerces.parsers.SAXParser";
46     private static final boolean DEFAULT_VALIDATE = false;
47
48     private NodeHandler handler;
49     private String JavaDoc readerClassName;
50     private boolean validate;
51
52     public NodeLoader() {
53         this(new NodeFactory(), DEFAULT_READER_CLASS_NAME, DEFAULT_VALIDATE);
54     }
55
56     public NodeLoader(NodeFactory factory) {
57         this(factory, DEFAULT_READER_CLASS_NAME, DEFAULT_VALIDATE);
58     }
59
60     public NodeLoader(String JavaDoc readerClassName) {
61         this(new NodeFactory(), readerClassName, DEFAULT_VALIDATE);
62     }
63
64     public NodeLoader(boolean validate) {
65         this(new NodeFactory(), DEFAULT_READER_CLASS_NAME, validate);
66     }
67
68     public NodeLoader(NodeFactory factory, String JavaDoc readerClassName) {
69         this(factory, readerClassName, DEFAULT_VALIDATE);
70     }
71
72     public NodeLoader(NodeFactory factory, boolean validate) {
73         this(factory, DEFAULT_READER_CLASS_NAME, validate);
74     }
75
76     public NodeLoader(String JavaDoc readerClassName, boolean validate) {
77         this(new NodeFactory(), readerClassName, validate);
78     }
79     
80     public NodeLoader(NodeFactory factory, String JavaDoc readerClassName, boolean validate) {
81         this.handler = new NodeHandler(factory);
82         this.readerClassName = readerClassName;
83         this.validate = validate;
84     }
85
86     public NodeFactory load(String JavaDoc filename) throws IOException, SAXException {
87         NodeFactory result = null;
88
89         FileReader in = null;
90         try {
91             in = new FileReader(filename);
92             result = load(in);
93         } finally {
94             if (in != null) {
95                 in.close();
96             }
97         }
98
99         return result;
100     }
101
102     public NodeFactory load(InputStream in) throws IOException, SAXException {
103         return load(new InputSource(in));
104     }
105
106     public NodeFactory load(Reader in) throws IOException, SAXException {
107         return load(new InputSource(in));
108     }
109
110     public NodeFactory load(InputSource in) throws IOException, SAXException {
111         XMLReader reader = XMLReaderFactory.createXMLReader(readerClassName);
112         reader.setDTDHandler(handler);
113         reader.setContentHandler(handler);
114         reader.setErrorHandler(handler);
115
116         try {
117             if (validate) {
118                 Logger.getLogger(getClass()).warn("XML validation turned on");
119                 reader.setFeature("http://xml.org/sax/features/validation", true);
120                 reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true);
121             } else {
122                 Logger.getLogger(getClass()).info("XML validation turned off");
123                 reader.setFeature("http://xml.org/sax/features/validation", false);
124                 reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
125             }
126         } catch (Exception JavaDoc ex) {
127             Logger.getLogger(getClass()).warn("Problem setting validation feature on XML reader",ex);
128         }
129     
130         reader.parse(in);
131     
132         return handler.getFactory();
133     }
134
135     public void addDependencyListener(DependencyListener listener) {
136         handler.addDependencyListener(listener);
137     }
138
139     public void removeDependencyListener(DependencyListener listener) {
140         handler.removeDependencyListener(listener);
141     }
142 }
143
Popular Tags