1 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 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 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 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 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 readerClassName, boolean validate) { 77 this(new NodeFactory(), readerClassName, validate); 78 } 79 80 public NodeLoader(NodeFactory factory, String readerClassName, boolean validate) { 81 this.handler = new NodeHandler(factory); 82 this.readerClassName = readerClassName; 83 this.validate = validate; 84 } 85 86 public NodeFactory load(String 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 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 |