| 1 10 package org.mmbase.util; 11 12 import java.io.*; 13 14 import javax.xml.parsers.DocumentBuilder ; 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 36 public class XMLBasicReader extends DocumentReader { 37 38 private static Logger log = Logging.getLoggerInstance(XMLBasicReader.class); 39 40 public XMLBasicReader(String path) { 41 super(getInputSource(path)); 42 } 43 44 public XMLBasicReader(String path, boolean validating) { 45 super(getInputSource(path), validating, null); 46 } 47 48 public XMLBasicReader(String path, Class 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 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 resolveBase) { 65 super(source, resolveBase); 66 } 67 68 public XMLBasicReader(InputSource source) { 69 super(source); 70 } 71 72 80 public static InputSource getInputSource(String path) { 81 InputSource is; 82 try { 83 if (path.startsWith("file://")) { 85 try { 86 path = new java.net.URL (path).getPath(); 87 } catch (java.net.MalformedURLException 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 mfe) { 94 } 95 is.setSystemId("file://" + path); 96 } catch (java.io.FileNotFoundException e) { 97 log.error("Error reading " + path + ": " + e.toString()); 98 log.service("Using empty source"); 99 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 114 public static DocumentBuilder getDocumentBuilder(boolean validating, ErrorHandler handler) { 115 return DocumentReader.getDocumentBuilder(validating, handler, null); 116 } 117 118 122 public static DocumentBuilder getDocumentBuilder(boolean validating, EntityResolver resolver) { 123 return DocumentReader.getDocumentBuilder(validating, null, resolver); 124 } 125 126 130 public static DocumentBuilder getDocumentBuilder(boolean validating, ErrorHandler handler, EntityResolver resolver) { 131 return DocumentReader.getDocumentBuilder(validating, handler, resolver); 132 } 133 134 138 public static DocumentBuilder getDocumentBuilder(Class refer) { 139 return DocumentReader.getDocumentBuilder(DocumentReader.validate(), null, new XMLEntityResolver(DocumentReader.validate(), refer)); 140 } 141 142 } 143 | Popular Tags |