KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > XMLBasicReader


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.io.*;
13
14 import javax.xml.parsers.DocumentBuilder JavaDoc;
15
16 import org.mmbase.util.logging.Logger;
17 import org.mmbase.util.logging.Logging;
18 import org.mmbase.util.xml.DocumentReader;
19 import org.xml.sax.*;
20
21 /**
22  * XMLBasicReader has two goals.
23  * <ul>
24  * <li>It provides a way for parsing XML</li>
25  * <li>It provides a way for searching in this XML, without the need for an XPath implementation, and without the hassle of org.w3c.dom alone.
26  * It uses dots to lay a path in the XML (XPath uses slashes).</li>
27  * </ul>
28  *
29  * @deprecated use DocumentReader or DocumentWriter. Some code may need to be moved to DocumentReader
30  * @author Case Roule
31  * @author Rico Jansen
32  * @author Pierre van Rooden
33  * @author Michiel Meeuwissen
34  * @version $Id: XMLBasicReader.java,v 1.46 2006/01/26 10:21:50 michiel Exp $
35  */

36 public class XMLBasicReader extends DocumentReader {
37
38     private static Logger log = Logging.getLoggerInstance(XMLBasicReader.class);
39
40     public XMLBasicReader(String JavaDoc path) {
41         super(getInputSource(path));
42     }
43
44     public XMLBasicReader(String JavaDoc path, boolean validating) {
45         super(getInputSource(path), validating, null);
46     }
47
48     public XMLBasicReader(String JavaDoc path, Class JavaDoc resolveBase) {
49         super(getInputSource(path), DocumentReader.validate(), resolveBase);
50     }
51
52     public XMLBasicReader() {
53         super();
54     }
55
56     public XMLBasicReader(InputSource source, boolean validating, Class JavaDoc resolveBase) {
57         super(source, validating, resolveBase);
58     }
59
60     public XMLBasicReader(InputSource source, boolean validating) {
61         super(source, validating);
62     }
63
64     public XMLBasicReader(InputSource source, Class JavaDoc resolveBase) {
65         super(source, resolveBase);
66     }
67
68     public XMLBasicReader(InputSource source) {
69         super(source);
70     }
71
72     /**
73      * Creates an input source for a document, based on a filepath
74      * If the file cannot be opened, the method returns an inputsource of an error document describing the condition
75      * under which this failed.
76      * @param path the path to the file containing the document
77      * @return the input source to the document.
78      * @deprecated
79      */

80     public static InputSource getInputSource(String JavaDoc path) {
81         InputSource is;
82         try {
83             // remove file protocol if present to avoid errors in accessing file
84
if (path.startsWith("file://")) {
85                 try {
86                     path = new java.net.URL JavaDoc(path).getPath();
87                 } catch (java.net.MalformedURLException JavaDoc mfe) {
88                 }
89             }
90             is = new InputSource(new FileInputStream(path));
91             try {
92                 is.setSystemId(new File(path).toURL().toExternalForm());
93             } catch (java.net.MalformedURLException JavaDoc mfe) {
94             }
95             is.setSystemId("file://" + path);
96         } catch (java.io.FileNotFoundException JavaDoc e) {
97             log.error("Error reading " + path + ": " + e.toString());
98             log.service("Using empty source");
99             // try to handle more or less gracefully
100
is = new InputSource();
101             is.setSystemId(FILENOTFOUND + path);
102             is.setCharacterStream(new StringReader("<?xml version=\"1.0\"?>\n" +
103                                                    "<!DOCTYPE error PUBLIC \"" + PUBLIC_ID_ERROR + "\"" +
104                                                    " \"http://www.mmbase.org/dtd/error_1_0.dtd\">\n" +
105                                                    "<error>" + path + " not found</error>"));
106          }
107         return is;
108     }
109
110     /**
111      * Obtain a DocumentBuilder
112      * @deprecated use {!link DocumentReader.getDocumentBuilder(boolean, ErrorHandler, EntityResolver)}
113      */

114     public static DocumentBuilder JavaDoc getDocumentBuilder(boolean validating, ErrorHandler handler) {
115         return DocumentReader.getDocumentBuilder(validating, handler, null);
116     }
117
118     /**
119      * Obtain a DocumentBuilder
120      * @deprecated use {!link DocumentReader.getDocumentBuilder(boolean, ErrorHandler, EntityResolver)}
121      */

122     public static DocumentBuilder JavaDoc getDocumentBuilder(boolean validating, EntityResolver resolver) {
123         return DocumentReader.getDocumentBuilder(validating, null, resolver);
124     }
125
126     /**
127      * Obtain a DocumentBuilder
128      * @deprecated use {!link DocumentReader.getDocumentBuilder(boolean, ErrorHandler, EntityResolver)}
129      */

130     public static DocumentBuilder JavaDoc getDocumentBuilder(boolean validating, ErrorHandler handler, EntityResolver resolver) {
131         return DocumentReader.getDocumentBuilder(validating, handler, resolver);
132     }
133
134     /**
135      * Obtain a DocumentBuilder
136      * @deprecated use {!link DocumentReader.getDocumentBuilder(boolean, ErrorHandler, EntityResolver)}
137      */

138     public static DocumentBuilder JavaDoc getDocumentBuilder(Class JavaDoc refer) {
139         return DocumentReader.getDocumentBuilder(DocumentReader.validate(), null, new XMLEntityResolver(DocumentReader.validate(), refer));
140     }
141
142 }
143
Popular Tags