001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util;
011:
012: import java.io.*;
013:
014: import javax.xml.parsers.DocumentBuilder;
015:
016: import org.mmbase.util.logging.Logger;
017: import org.mmbase.util.logging.Logging;
018: import org.mmbase.util.xml.DocumentReader;
019: import org.xml.sax.*;
020:
021: /**
022: * XMLBasicReader has two goals.
023: * <ul>
024: * <li>It provides a way for parsing XML</li>
025: * <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.
026: * It uses dots to lay a path in the XML (XPath uses slashes).</li>
027: * </ul>
028: *
029: * @deprecated use DocumentReader or DocumentWriter. Some code may need to be moved to DocumentReader
030: * @author Case Roule
031: * @author Rico Jansen
032: * @author Pierre van Rooden
033: * @author Michiel Meeuwissen
034: * @version $Id: XMLBasicReader.java,v 1.46 2006/01/26 10:21:50 michiel Exp $
035: */
036: public class XMLBasicReader extends DocumentReader {
037:
038: private static Logger log = Logging
039: .getLoggerInstance(XMLBasicReader.class);
040:
041: public XMLBasicReader(String path) {
042: super (getInputSource(path));
043: }
044:
045: public XMLBasicReader(String path, boolean validating) {
046: super (getInputSource(path), validating, null);
047: }
048:
049: public XMLBasicReader(String path, Class resolveBase) {
050: super (getInputSource(path), DocumentReader.validate(),
051: resolveBase);
052: }
053:
054: public XMLBasicReader() {
055: super ();
056: }
057:
058: public XMLBasicReader(InputSource source, boolean validating,
059: Class resolveBase) {
060: super (source, validating, resolveBase);
061: }
062:
063: public XMLBasicReader(InputSource source, boolean validating) {
064: super (source, validating);
065: }
066:
067: public XMLBasicReader(InputSource source, Class resolveBase) {
068: super (source, resolveBase);
069: }
070:
071: public XMLBasicReader(InputSource source) {
072: super (source);
073: }
074:
075: /**
076: * Creates an input source for a document, based on a filepath
077: * If the file cannot be opened, the method returns an inputsource of an error document describing the condition
078: * under which this failed.
079: * @param path the path to the file containing the document
080: * @return the input source to the document.
081: * @deprecated
082: */
083: public static InputSource getInputSource(String path) {
084: InputSource is;
085: try {
086: // remove file protocol if present to avoid errors in accessing file
087: if (path.startsWith("file://")) {
088: try {
089: path = new java.net.URL(path).getPath();
090: } catch (java.net.MalformedURLException mfe) {
091: }
092: }
093: is = new InputSource(new FileInputStream(path));
094: try {
095: is.setSystemId(new File(path).toURL().toExternalForm());
096: } catch (java.net.MalformedURLException mfe) {
097: }
098: is.setSystemId("file://" + path);
099: } catch (java.io.FileNotFoundException e) {
100: log.error("Error reading " + path + ": " + e.toString());
101: log.service("Using empty source");
102: // try to handle more or less gracefully
103: is = new InputSource();
104: is.setSystemId(FILENOTFOUND + path);
105: is
106: .setCharacterStream(new StringReader(
107: "<?xml version=\"1.0\"?>\n"
108: + "<!DOCTYPE error PUBLIC \""
109: + PUBLIC_ID_ERROR
110: + "\""
111: + " \"http://www.mmbase.org/dtd/error_1_0.dtd\">\n"
112: + "<error>" + path
113: + " not found</error>"));
114: }
115: return is;
116: }
117:
118: /**
119: * Obtain a DocumentBuilder
120: * @deprecated use {!link DocumentReader.getDocumentBuilder(boolean, ErrorHandler, EntityResolver)}
121: */
122: public static DocumentBuilder getDocumentBuilder(
123: boolean validating, ErrorHandler handler) {
124: return DocumentReader.getDocumentBuilder(validating, handler,
125: null);
126: }
127:
128: /**
129: * Obtain a DocumentBuilder
130: * @deprecated use {!link DocumentReader.getDocumentBuilder(boolean, ErrorHandler, EntityResolver)}
131: */
132: public static DocumentBuilder getDocumentBuilder(
133: boolean validating, EntityResolver resolver) {
134: return DocumentReader.getDocumentBuilder(validating, null,
135: resolver);
136: }
137:
138: /**
139: * Obtain a DocumentBuilder
140: * @deprecated use {!link DocumentReader.getDocumentBuilder(boolean, ErrorHandler, EntityResolver)}
141: */
142: public static DocumentBuilder getDocumentBuilder(
143: boolean validating, ErrorHandler handler,
144: EntityResolver resolver) {
145: return DocumentReader.getDocumentBuilder(validating, handler,
146: resolver);
147: }
148:
149: /**
150: * Obtain a DocumentBuilder
151: * @deprecated use {!link DocumentReader.getDocumentBuilder(boolean, ErrorHandler, EntityResolver)}
152: */
153: public static DocumentBuilder getDocumentBuilder(Class refer) {
154: return DocumentReader.getDocumentBuilder(DocumentReader
155: .validate(), null, new XMLEntityResolver(DocumentReader
156: .validate(), refer));
157: }
158:
159: }
|