1 58 59 60 package org.openlaszlo.iv.flash.xml.apache; 61 62 import java.io.IOException ; 63 import javax.xml.parsers.DocumentBuilder ; 64 import javax.xml.parsers.DocumentBuilderFactory ; 65 import javax.xml.parsers.ParserConfigurationException ; 66 67 import org.w3c.dom.Document ; 68 import org.w3c.dom.DOMImplementation ; 69 import org.w3c.dom.DocumentType ; 70 71 import org.xml.sax.XMLReader ; 72 import org.xml.sax.InputSource ; 73 import org.xml.sax.SAXException ; 74 import org.xml.sax.SAXParseException ; 75 import org.xml.sax.EntityResolver ; 76 import org.xml.sax.ErrorHandler ; 77 import org.xml.sax.helpers.DefaultHandler ; 78 79 import org.apache.xerces.parsers.DOMParser; 80 import org.apache.xerces.dom.DOMImplementationImpl; 81 82 93 public class DocumentBuilderImpl extends DocumentBuilder { 94 95 96 private static final int MAX_PARSERS = 5; 97 98 99 private static final String XERCES_FEATURE_PREFIX = "http://apache.org/xml/features/"; 100 private static final String CREATE_ENTITY_REF_NODES_FEATURE = "dom/create-entity-ref-nodes"; 101 private static final String INCLUDE_IGNORABLE_WHITESPACE = "dom/include-ignorable-whitespace"; 102 private static final String CONTINUE_AFTER_FATAL_ERROR = "continue-after-fatal-error"; 103 private static final String VALIDATING_PARSER = "http://xml.org/sax/features/validation"; 104 private static final String NAMESPACE_AWARE = "http://xml.org/sax/features/namespaces"; 105 106 private DocumentBuilderFactory dbf; 107 108 private EntityResolver er = null; 109 private ErrorHandler eh = null; 110 111 112 private DOMParser[] parsers = new DOMParser[MAX_PARSERS]; 113 private boolean[] locks = new boolean[MAX_PARSERS]; 114 115 private boolean namespaceAware = false; 116 private boolean validating = false; 117 118 DocumentBuilderImpl(DocumentBuilderFactory dbf) 119 throws ParserConfigurationException 120 { 121 this.dbf = dbf; 122 this.validating = dbf.isValidating(); 123 this.namespaceAware = dbf.isNamespaceAware(); 124 } 125 126 131 public Document newDocument() { 132 return new org.apache.xerces.dom.DocumentImpl(); 133 } 134 135 public DOMImplementation getDOMImplementation() { 136 return DOMImplementationImpl.getDOMImplementation(); 137 } 138 139 public Document parse(InputSource is) throws SAXException , IOException { 140 if( is == null ) { 141 throw new IllegalArgumentException ("InputSource cannot be null"); 142 } 143 144 DOMParser parser = lockDOMParser(); 145 try { 146 parser.parse(is); 147 Document doc = parser.getDocument(); 148 return doc; 149 } finally { 150 releaseDOMParser(parser); 151 } 152 } 153 154 public boolean isNamespaceAware() { 155 return namespaceAware; 156 } 157 158 public boolean isValidating() { 159 return validating; 160 } 161 162 public void setEntityResolver( org.xml.sax.EntityResolver er ) { 163 this.er = er; 164 } 165 166 public void setErrorHandler( org.xml.sax.ErrorHandler eh ) { 167 this.eh = (eh == null) ? new DefaultHandler () : eh; 170 } 171 172 178 private DOMParser createDOMParser() throws SAXException { 179 DOMParser domParser = new DOMParser(); 180 181 domParser.setFeature(VALIDATING_PARSER, false); 182 183 domParser.setFeature(NAMESPACE_AWARE, namespaceAware); 185 186 domParser.setFeature(XERCES_FEATURE_PREFIX+INCLUDE_IGNORABLE_WHITESPACE, 188 !dbf.isIgnoringElementContentWhitespace()); 189 domParser.setFeature(XERCES_FEATURE_PREFIX+CREATE_ENTITY_REF_NODES_FEATURE, 190 !dbf.isExpandEntityReferences()); 191 domParser.setFeature(XERCES_FEATURE_PREFIX+CONTINUE_AFTER_FATAL_ERROR, true ); 192 193 196 if( er != null ) domParser.setEntityResolver(er); 197 if( eh != null ) domParser.setErrorHandler(eh); 198 199 return domParser; 200 } 201 202 private synchronized DOMParser lockDOMParser() throws SAXException { 203 for(;;) { 204 for( int i=0; i<MAX_PARSERS; i++ ) { 205 if( !locks[i] ) { 206 if( parsers[i] == null ) { 207 parsers[i] = createDOMParser(); 208 } 209 locks[i] = true; 210 return parsers[i]; 211 } 212 } 213 try { 214 wait(); 215 } catch( InterruptedException e ) { 216 } 217 } 218 } 219 220 private synchronized void releaseDOMParser( DOMParser p ) { 221 for( int i=0; i<MAX_PARSERS; i++ ) { 222 if( parsers[i] == p ) { 223 locks[i] = false; 224 notify(); 225 break; 226 } 227 } 228 } 229 230 } 231 | Popular Tags |