src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/internalizer/DOMForest.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 408
b0610cd08440
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.internal.xjc.reader.internalizer;
aoqi@0 27
aoqi@0 28 import com.sun.istack.internal.NotNull;
aoqi@0 29 import com.sun.istack.internal.XMLStreamReaderToContentHandler;
aoqi@0 30 import com.sun.tools.internal.xjc.ErrorReceiver;
aoqi@0 31 import com.sun.tools.internal.xjc.Options;
aoqi@0 32 import com.sun.tools.internal.xjc.reader.Const;
aoqi@0 33 import com.sun.tools.internal.xjc.util.ErrorReceiverFilter;
aoqi@0 34 import com.sun.xml.internal.bind.marshaller.DataWriter;
aoqi@0 35 import com.sun.xml.internal.bind.v2.util.XmlFactory;
aoqi@0 36 import com.sun.xml.internal.xsom.parser.JAXPParser;
aoqi@0 37 import com.sun.xml.internal.xsom.parser.XMLParser;
aoqi@0 38 import org.w3c.dom.Document;
aoqi@0 39 import org.w3c.dom.Element;
aoqi@0 40 import org.xml.sax.*;
aoqi@0 41 import org.xml.sax.helpers.XMLFilterImpl;
aoqi@0 42
aoqi@0 43 import javax.xml.parsers.DocumentBuilder;
aoqi@0 44 import javax.xml.parsers.DocumentBuilderFactory;
aoqi@0 45 import javax.xml.parsers.ParserConfigurationException;
aoqi@0 46 import javax.xml.parsers.SAXParserFactory;
aoqi@0 47 import javax.xml.stream.XMLStreamException;
aoqi@0 48 import javax.xml.stream.XMLStreamReader;
aoqi@0 49 import javax.xml.transform.Source;
aoqi@0 50 import javax.xml.transform.Transformer;
aoqi@0 51 import javax.xml.transform.TransformerException;
aoqi@0 52 import javax.xml.transform.TransformerFactory;
aoqi@0 53 import javax.xml.transform.dom.DOMSource;
aoqi@0 54 import javax.xml.transform.sax.SAXResult;
aoqi@0 55 import javax.xml.transform.sax.SAXSource;
aoqi@0 56 import javax.xml.validation.SchemaFactory;
aoqi@0 57 import java.io.IOException;
aoqi@0 58 import java.io.OutputStream;
aoqi@0 59 import java.io.OutputStreamWriter;
aoqi@0 60 import java.util.*;
aoqi@0 61
aoqi@0 62 import static com.sun.xml.internal.bind.v2.util.XmlFactory.allowExternalAccess;
aoqi@0 63 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
aoqi@0 64
aoqi@0 65
aoqi@0 66 /**
aoqi@0 67 * Builds a DOM forest and maintains association from
aoqi@0 68 * system IDs to DOM trees.
aoqi@0 69 *
aoqi@0 70 * <p>
aoqi@0 71 * A forest is a transitive reflexive closure of referenced documents.
aoqi@0 72 * IOW, if a document is in a forest, all the documents referenced from
aoqi@0 73 * it is in a forest, too. To support this semantics, {@link DOMForest}
aoqi@0 74 * uses {@link InternalizationLogic} to find referenced documents.
aoqi@0 75 *
aoqi@0 76 * <p>
aoqi@0 77 * Some documents are marked as "root"s, meaning those documents were
aoqi@0 78 * put into a forest explicitly, not because it is referenced from another
aoqi@0 79 * document. (However, a root document can be referenced from other
aoqi@0 80 * documents, too.)
aoqi@0 81 *
aoqi@0 82 * @author
aoqi@0 83 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
aoqi@0 84 */
aoqi@0 85 public final class DOMForest {
aoqi@0 86 /** actual data storage map&lt;SystemId,Document>. */
aoqi@0 87 private final Map<String,Document> core = new HashMap<String,Document>();
aoqi@0 88
aoqi@0 89 /**
aoqi@0 90 * To correctly feed documents to a schema parser, we need to remember
aoqi@0 91 * which documents (of the forest) were given as the root
aoqi@0 92 * documents, and which of them are read as included/imported
aoqi@0 93 * documents.
aoqi@0 94 *
aoqi@0 95 * <p>
aoqi@0 96 * Set of system ids as strings.
aoqi@0 97 */
aoqi@0 98 private final Set<String> rootDocuments = new HashSet<String>();
aoqi@0 99
aoqi@0 100 /** Stores location information for all the trees in this forest. */
aoqi@0 101 public final LocatorTable locatorTable = new LocatorTable();
aoqi@0 102
aoqi@0 103 /** Stores all the outer-most &lt;jaxb:bindings> customizations. */
aoqi@0 104 public final Set<Element> outerMostBindings = new HashSet<Element>();
aoqi@0 105
aoqi@0 106 /** Used to resolve references to other schema documents. */
aoqi@0 107 private EntityResolver entityResolver = null;
aoqi@0 108
aoqi@0 109 /** Errors encountered during the parsing will be sent to this object. */
aoqi@0 110 private ErrorReceiver errorReceiver = null;
aoqi@0 111
aoqi@0 112 /** Schema language dependent part of the processing. */
aoqi@0 113 protected final InternalizationLogic logic;
aoqi@0 114
aoqi@0 115 private final SAXParserFactory parserFactory;
aoqi@0 116 private final DocumentBuilder documentBuilder;
aoqi@0 117
aoqi@0 118 private final Options options;
aoqi@0 119
aoqi@0 120 public DOMForest(
aoqi@0 121 SAXParserFactory parserFactory, DocumentBuilder documentBuilder,
aoqi@0 122 InternalizationLogic logic ) {
aoqi@0 123
aoqi@0 124 this.parserFactory = parserFactory;
aoqi@0 125 this.documentBuilder = documentBuilder;
aoqi@0 126 this.logic = logic;
aoqi@0 127 this.options = null;
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 public DOMForest( InternalizationLogic logic, Options opt ) {
aoqi@0 131
aoqi@0 132 if (opt == null) throw new AssertionError("Options object null");
aoqi@0 133 this.options = opt;
aoqi@0 134
aoqi@0 135 try {
aoqi@0 136 DocumentBuilderFactory dbf = XmlFactory.createDocumentBuilderFactory(opt.disableXmlSecurity);
aoqi@0 137 this.documentBuilder = dbf.newDocumentBuilder();
aoqi@0 138 this.parserFactory = XmlFactory.createParserFactory(opt.disableXmlSecurity);
aoqi@0 139 } catch( ParserConfigurationException e ) {
aoqi@0 140 throw new AssertionError(e);
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 this.logic = logic;
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 /**
aoqi@0 147 * Gets the DOM tree associated with the specified system ID,
aoqi@0 148 * or null if none is found.
aoqi@0 149 */
aoqi@0 150 public Document get( String systemId ) {
aoqi@0 151 Document doc = core.get(systemId);
aoqi@0 152
aoqi@0 153 if( doc==null && systemId.startsWith("file:/") && !systemId.startsWith("file://") ) {
aoqi@0 154 // As of JDK1.4, java.net.URL.toExternal method returns URLs like
aoqi@0 155 // "file:/abc/def/ghi" which is an incorrect file protocol URL according to RFC1738.
aoqi@0 156 // Some other correctly functioning parts return the correct URLs ("file:///abc/def/ghi"),
aoqi@0 157 // and this descripancy breaks DOM look up by system ID.
aoqi@0 158
aoqi@0 159 // this extra check solves this problem.
aoqi@0 160 doc = core.get( "file://"+systemId.substring(5) );
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 if( doc==null && systemId.startsWith("file:") ) {
aoqi@0 164 // on Windows, filenames are case insensitive.
aoqi@0 165 // perform case-insensitive search for improved user experience
aoqi@0 166 String systemPath = getPath(systemId);
aoqi@0 167 for (String key : core.keySet()) {
aoqi@0 168 if(key.startsWith("file:") && getPath(key).equalsIgnoreCase(systemPath)) {
aoqi@0 169 doc = core.get(key);
aoqi@0 170 break;
aoqi@0 171 }
aoqi@0 172 }
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 return doc;
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 /**
aoqi@0 179 * Strips off the leading 'file:///' portion from an URL.
aoqi@0 180 */
aoqi@0 181 private String getPath(String key) {
aoqi@0 182 key = key.substring(5); // skip 'file:'
aoqi@0 183 while(key.length()>0 && key.charAt(0)=='/') {
aoqi@0 184 key = key.substring(1);
aoqi@0 185 }
aoqi@0 186 return key;
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 /**
aoqi@0 190 * Returns a read-only set of root document system IDs.
aoqi@0 191 */
aoqi@0 192 public Set<String> getRootDocuments() {
aoqi@0 193 return Collections.unmodifiableSet(rootDocuments);
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 /**
aoqi@0 197 * Picks one document at random and returns it.
aoqi@0 198 */
aoqi@0 199 public Document getOneDocument() {
aoqi@0 200 for (Document dom : core.values()) {
aoqi@0 201 if (!dom.getDocumentElement().getNamespaceURI().equals(Const.JAXB_NSURI))
aoqi@0 202 return dom;
aoqi@0 203 }
aoqi@0 204 // we should have caught this error very early on
aoqi@0 205 throw new AssertionError();
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 /**
aoqi@0 209 * Checks the correctness of the XML Schema documents and return true
aoqi@0 210 * if it's OK.
aoqi@0 211 *
aoqi@0 212 * <p>
aoqi@0 213 * This method performs a weaker version of the tests where error messages
aoqi@0 214 * are provided without line number information. So whenever possible
aoqi@0 215 * use {@link SchemaConstraintChecker}.
aoqi@0 216 *
aoqi@0 217 * @see SchemaConstraintChecker
aoqi@0 218 */
aoqi@0 219 public boolean checkSchemaCorrectness(ErrorReceiver errorHandler) {
aoqi@0 220 try {
aoqi@0 221 boolean disableXmlSecurity = false;
aoqi@0 222 if (options != null) {
aoqi@0 223 disableXmlSecurity = options.disableXmlSecurity;
aoqi@0 224 }
aoqi@0 225 SchemaFactory sf = XmlFactory.createSchemaFactory(W3C_XML_SCHEMA_NS_URI, disableXmlSecurity);
aoqi@0 226 ErrorReceiverFilter filter = new ErrorReceiverFilter(errorHandler);
aoqi@0 227 sf.setErrorHandler(filter);
aoqi@0 228 Set<String> roots = getRootDocuments();
aoqi@0 229 Source[] sources = new Source[roots.size()];
aoqi@0 230 int i=0;
aoqi@0 231 for (String root : roots) {
aoqi@0 232 sources[i++] = new DOMSource(get(root),root);
aoqi@0 233 }
aoqi@0 234 sf.newSchema(sources);
aoqi@0 235 return !filter.hadError();
aoqi@0 236 } catch (SAXException e) {
aoqi@0 237 // the errors should have been reported
aoqi@0 238 return false;
aoqi@0 239 }
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 /**
aoqi@0 243 * Gets the system ID from which the given DOM is parsed.
aoqi@0 244 * <p>
aoqi@0 245 * Poor-man's base URI.
aoqi@0 246 */
aoqi@0 247 public String getSystemId( Document dom ) {
aoqi@0 248 for (Map.Entry<String,Document> e : core.entrySet()) {
aoqi@0 249 if (e.getValue() == dom)
aoqi@0 250 return e.getKey();
aoqi@0 251 }
aoqi@0 252 return null;
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 public Document parse( InputSource source, boolean root ) throws SAXException {
aoqi@0 256 if( source.getSystemId()==null )
aoqi@0 257 throw new IllegalArgumentException();
aoqi@0 258
aoqi@0 259 return parse( source.getSystemId(), source, root );
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 /**
aoqi@0 263 * Parses an XML at the given location (
aoqi@0 264 * and XMLs referenced by it) into DOM trees
aoqi@0 265 * and stores them to this forest.
aoqi@0 266 *
aoqi@0 267 * @return the parsed DOM document object.
aoqi@0 268 */
aoqi@0 269 public Document parse( String systemId, boolean root ) throws SAXException, IOException {
aoqi@0 270
aoqi@0 271 systemId = Options.normalizeSystemId(systemId);
aoqi@0 272
aoqi@0 273 if( core.containsKey(systemId) )
aoqi@0 274 // this document has already been parsed. Just ignore.
aoqi@0 275 return core.get(systemId);
aoqi@0 276
aoqi@0 277 InputSource is=null;
aoqi@0 278
aoqi@0 279 // allow entity resolver to find the actual byte stream.
aoqi@0 280 if( entityResolver!=null )
aoqi@0 281 is = entityResolver.resolveEntity(null,systemId);
aoqi@0 282 if( is==null )
aoqi@0 283 is = new InputSource(systemId);
aoqi@0 284
aoqi@0 285 // but we still use the original system Id as the key.
aoqi@0 286 return parse( systemId, is, root );
aoqi@0 287 }
aoqi@0 288
aoqi@0 289 /**
aoqi@0 290 * Returns a {@link ContentHandler} to feed SAX events into.
aoqi@0 291 *
aoqi@0 292 * <p>
aoqi@0 293 * The client of this class can feed SAX events into the handler
aoqi@0 294 * to parse a document into this DOM forest.
aoqi@0 295 *
aoqi@0 296 * This version requires that the DOM object to be created and registered
aoqi@0 297 * to the map beforehand.
aoqi@0 298 */
aoqi@0 299 private ContentHandler getParserHandler( Document dom ) {
aoqi@0 300 ContentHandler handler = new DOMBuilder(dom,locatorTable,outerMostBindings);
aoqi@0 301 handler = new WhitespaceStripper(handler,errorReceiver,entityResolver);
aoqi@0 302 handler = new VersionChecker(handler,errorReceiver,entityResolver);
aoqi@0 303
aoqi@0 304 // insert the reference finder so that
aoqi@0 305 // included/imported schemas will be also parsed
aoqi@0 306 XMLFilterImpl f = logic.createExternalReferenceFinder(this);
aoqi@0 307 f.setContentHandler(handler);
aoqi@0 308
aoqi@0 309 if(errorReceiver!=null)
aoqi@0 310 f.setErrorHandler(errorReceiver);
aoqi@0 311 if(entityResolver!=null)
aoqi@0 312 f.setEntityResolver(entityResolver);
aoqi@0 313
aoqi@0 314 return f;
aoqi@0 315 }
aoqi@0 316
aoqi@0 317 public interface Handler extends ContentHandler {
aoqi@0 318 /**
aoqi@0 319 * Gets the DOM that was built.
aoqi@0 320 */
aoqi@0 321 public Document getDocument();
aoqi@0 322 }
aoqi@0 323
aoqi@0 324 private static abstract class HandlerImpl extends XMLFilterImpl implements Handler {
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 /**
aoqi@0 328 * Returns a {@link ContentHandler} to feed SAX events into.
aoqi@0 329 *
aoqi@0 330 * <p>
aoqi@0 331 * The client of this class can feed SAX events into the handler
aoqi@0 332 * to parse a document into this DOM forest.
aoqi@0 333 */
aoqi@0 334 public Handler getParserHandler( String systemId, boolean root ) {
aoqi@0 335 final Document dom = documentBuilder.newDocument();
aoqi@0 336 core.put( systemId, dom );
aoqi@0 337 if(root)
aoqi@0 338 rootDocuments.add(systemId);
aoqi@0 339
aoqi@0 340 ContentHandler handler = getParserHandler(dom);
aoqi@0 341
aoqi@0 342 // we will register the DOM to the map once the system ID becomes available.
aoqi@0 343 // but the SAX allows the event source to not to provide that information,
aoqi@0 344 // so be prepared for such case.
aoqi@0 345 HandlerImpl x = new HandlerImpl() {
aoqi@0 346 public Document getDocument() {
aoqi@0 347 return dom;
aoqi@0 348 }
aoqi@0 349 };
aoqi@0 350 x.setContentHandler(handler);
aoqi@0 351
aoqi@0 352 return x;
aoqi@0 353 }
aoqi@0 354
aoqi@0 355 /**
aoqi@0 356 * Parses the given document and add it to the DOM forest.
aoqi@0 357 *
aoqi@0 358 * @return
aoqi@0 359 * null if there was a parse error. otherwise non-null.
aoqi@0 360 */
aoqi@0 361 public Document parse( String systemId, InputSource inputSource, boolean root ) throws SAXException {
aoqi@0 362 Document dom = documentBuilder.newDocument();
aoqi@0 363
aoqi@0 364 systemId = Options.normalizeSystemId(systemId);
aoqi@0 365
aoqi@0 366 // put into the map before growing a tree, to
aoqi@0 367 // prevent recursive reference from causing infinite loop.
aoqi@0 368 core.put( systemId, dom );
aoqi@0 369 if(root)
aoqi@0 370 rootDocuments.add(systemId);
aoqi@0 371
aoqi@0 372 try {
aoqi@0 373 XMLReader reader = parserFactory.newSAXParser().getXMLReader();
aoqi@0 374 reader.setContentHandler(getParserHandler(dom));
aoqi@0 375 if(errorReceiver!=null)
aoqi@0 376 reader.setErrorHandler(errorReceiver);
aoqi@0 377 if(entityResolver!=null)
aoqi@0 378 reader.setEntityResolver(entityResolver);
aoqi@0 379 reader.parse(inputSource);
aoqi@0 380 } catch( ParserConfigurationException e ) {
aoqi@0 381 // in practice, this exception won't happen.
aoqi@0 382 errorReceiver.error(e.getMessage(),e);
aoqi@0 383 core.remove(systemId);
aoqi@0 384 rootDocuments.remove(systemId);
aoqi@0 385 return null;
aoqi@0 386 } catch( IOException e ) {
aoqi@0 387 errorReceiver.error(Messages.format(Messages.DOMFOREST_INPUTSOURCE_IOEXCEPTION, systemId, e.toString()),e);
aoqi@0 388 core.remove(systemId);
aoqi@0 389 rootDocuments.remove(systemId);
aoqi@0 390 return null;
aoqi@0 391 }
aoqi@0 392
aoqi@0 393 return dom;
aoqi@0 394 }
aoqi@0 395
aoqi@0 396 public Document parse( String systemId, XMLStreamReader parser, boolean root ) throws XMLStreamException {
aoqi@0 397 Document dom = documentBuilder.newDocument();
aoqi@0 398
aoqi@0 399 systemId = Options.normalizeSystemId(systemId);
aoqi@0 400
aoqi@0 401 if(root)
aoqi@0 402 rootDocuments.add(systemId);
aoqi@0 403
aoqi@0 404 if(systemId==null)
aoqi@0 405 throw new IllegalArgumentException("system id cannot be null");
aoqi@0 406 core.put( systemId, dom );
aoqi@0 407
aoqi@0 408 new XMLStreamReaderToContentHandler(parser,getParserHandler(dom),false,false).bridge();
aoqi@0 409
aoqi@0 410 return dom;
aoqi@0 411 }
aoqi@0 412
aoqi@0 413 /**
aoqi@0 414 * Performs internalization.
aoqi@0 415 *
aoqi@0 416 * This method should be called only once, only after all the
aoqi@0 417 * schemas are parsed.
aoqi@0 418 *
aoqi@0 419 * @return
aoqi@0 420 * the returned bindings need to be applied after schema
aoqi@0 421 * components are built.
aoqi@0 422 */
aoqi@0 423 public SCDBasedBindingSet transform(boolean enableSCD) {
aoqi@0 424 return Internalizer.transform(this, enableSCD, options.disableXmlSecurity);
aoqi@0 425 }
aoqi@0 426
aoqi@0 427 /**
aoqi@0 428 * Performs the schema correctness check by using JAXP 1.3.
aoqi@0 429 *
aoqi@0 430 * <p>
aoqi@0 431 * This is "weak", because {@link SchemaFactory#newSchema(Source[])}
aoqi@0 432 * doesn't handle inclusions very correctly (it ends up parsing it
aoqi@0 433 * from its original source, not in this tree), and because
aoqi@0 434 * it doesn't handle two documents for the same namespace very
aoqi@0 435 * well.
aoqi@0 436 *
aoqi@0 437 * <p>
aoqi@0 438 * We should eventually fix JAXP (and Xerces), but meanwhile
aoqi@0 439 * this weaker and potentially wrong correctness check is still
aoqi@0 440 * better than nothing when used inside JAX-WS (JAXB CLI and Ant
aoqi@0 441 * does a better job of checking this.)
aoqi@0 442 *
aoqi@0 443 * <p>
aoqi@0 444 * To receive errors, use {@link SchemaFactory#setErrorHandler(ErrorHandler)}.
aoqi@0 445 */
aoqi@0 446 public void weakSchemaCorrectnessCheck(SchemaFactory sf) {
aoqi@0 447 List<SAXSource> sources = new ArrayList<SAXSource>();
aoqi@0 448 for( String systemId : getRootDocuments() ) {
aoqi@0 449 Document dom = get(systemId);
aoqi@0 450 if (dom.getDocumentElement().getNamespaceURI().equals(Const.JAXB_NSURI))
aoqi@0 451 continue; // this isn't a schema. we have to do a negative check because if we see completely unrelated ns, we want to report that as an error
aoqi@0 452
aoqi@0 453 SAXSource ss = createSAXSource(systemId);
aoqi@0 454 try {
aoqi@0 455 ss.getXMLReader().setFeature("http://xml.org/sax/features/namespace-prefixes",true);
aoqi@0 456 } catch (SAXException e) {
aoqi@0 457 throw new AssertionError(e); // Xerces wants this. See 6395322.
aoqi@0 458 }
aoqi@0 459 sources.add(ss);
aoqi@0 460 }
aoqi@0 461
aoqi@0 462 try {
aoqi@0 463 allowExternalAccess(sf, "file,http", options.disableXmlSecurity).newSchema(sources.toArray(new SAXSource[0]));
aoqi@0 464 } catch (SAXException e) {
aoqi@0 465 // error should have been reported.
aoqi@0 466 } catch (RuntimeException re) {
aoqi@0 467 // JAXP RI isn't very trustworthy when it comes to schema error check,
aoqi@0 468 // and we know some cases where it just dies with NPE. So handle it gracefully.
aoqi@0 469 // this masks a bug in the JAXP RI, but we need a release that we have to make.
aoqi@0 470 try {
aoqi@0 471 sf.getErrorHandler().warning(
aoqi@0 472 new SAXParseException(Messages.format(
aoqi@0 473 Messages.ERR_GENERAL_SCHEMA_CORRECTNESS_ERROR,re.getMessage()),
aoqi@0 474 null,null,-1,-1,re));
aoqi@0 475 } catch (SAXException e) {
aoqi@0 476 // ignore
aoqi@0 477 }
aoqi@0 478 }
aoqi@0 479 }
aoqi@0 480
aoqi@0 481 /**
aoqi@0 482 * Creates a {@link SAXSource} that, when parsed, reads from this {@link DOMForest}
aoqi@0 483 * (instead of parsing the original source identified by the system ID.)
aoqi@0 484 */
aoqi@0 485 public @NotNull SAXSource createSAXSource(String systemId) {
aoqi@0 486 ContentHandlerNamespacePrefixAdapter reader = new ContentHandlerNamespacePrefixAdapter(new XMLFilterImpl() {
aoqi@0 487 // XMLReader that uses XMLParser to parse. We need to use XMLFilter to indrect
aoqi@0 488 // handlers, since SAX allows handlers to be changed while parsing.
aoqi@0 489 @Override
aoqi@0 490 public void parse(InputSource input) throws SAXException, IOException {
aoqi@0 491 createParser().parse(input, this, this, this);
aoqi@0 492 }
aoqi@0 493
aoqi@0 494 @Override
aoqi@0 495 public void parse(String systemId) throws SAXException, IOException {
aoqi@0 496 parse(new InputSource(systemId));
aoqi@0 497 }
aoqi@0 498 });
aoqi@0 499
aoqi@0 500 return new SAXSource(reader,new InputSource(systemId));
aoqi@0 501 }
aoqi@0 502
aoqi@0 503 /**
aoqi@0 504 * Creates {@link XMLParser} for XSOM which reads documents from
aoqi@0 505 * this DOMForest rather than doing a fresh parse.
aoqi@0 506 *
aoqi@0 507 * The net effect is that XSOM will read transformed XML Schemas
aoqi@0 508 * instead of the original documents.
aoqi@0 509 */
aoqi@0 510 public XMLParser createParser() {
aoqi@0 511 return new DOMForestParser(this, new JAXPParser(XmlFactory.createParserFactory(options.disableXmlSecurity)));
aoqi@0 512 }
aoqi@0 513
aoqi@0 514 public EntityResolver getEntityResolver() {
aoqi@0 515 return entityResolver;
aoqi@0 516 }
aoqi@0 517
aoqi@0 518 public void setEntityResolver(EntityResolver entityResolver) {
aoqi@0 519 this.entityResolver = entityResolver;
aoqi@0 520 }
aoqi@0 521
aoqi@0 522 public ErrorReceiver getErrorHandler() {
aoqi@0 523 return errorReceiver;
aoqi@0 524 }
aoqi@0 525
aoqi@0 526 public void setErrorHandler(ErrorReceiver errorHandler) {
aoqi@0 527 this.errorReceiver = errorHandler;
aoqi@0 528 }
aoqi@0 529
aoqi@0 530 /**
aoqi@0 531 * Gets all the parsed documents.
aoqi@0 532 */
aoqi@0 533 public Document[] listDocuments() {
aoqi@0 534 return core.values().toArray(new Document[core.size()]);
aoqi@0 535 }
aoqi@0 536
aoqi@0 537 /**
aoqi@0 538 * Gets all the system IDs of the documents.
aoqi@0 539 */
aoqi@0 540 public String[] listSystemIDs() {
aoqi@0 541 return core.keySet().toArray(new String[core.keySet().size()]);
aoqi@0 542 }
aoqi@0 543
aoqi@0 544 /**
aoqi@0 545 * Dumps the contents of the forest to the specified stream.
aoqi@0 546 *
aoqi@0 547 * This is a debug method. As such, error handling is sloppy.
aoqi@0 548 */
aoqi@0 549 @SuppressWarnings("CallToThreadDumpStack")
aoqi@0 550 public void dump( OutputStream out ) throws IOException {
aoqi@0 551 try {
aoqi@0 552 // create identity transformer
aoqi@0 553 boolean disableXmlSecurity = false;
aoqi@0 554 if (options != null) {
aoqi@0 555 disableXmlSecurity = options.disableXmlSecurity;
aoqi@0 556 }
aoqi@0 557 TransformerFactory tf = XmlFactory.createTransformerFactory(disableXmlSecurity);
aoqi@0 558 Transformer it = tf.newTransformer();
aoqi@0 559
aoqi@0 560 for (Map.Entry<String, Document> e : core.entrySet()) {
aoqi@0 561 out.write( ("---<< "+e.getKey()+'\n').getBytes() );
aoqi@0 562
aoqi@0 563 DataWriter dw = new DataWriter(new OutputStreamWriter(out),null);
aoqi@0 564 dw.setIndentStep(" ");
aoqi@0 565 it.transform( new DOMSource(e.getValue()),
aoqi@0 566 new SAXResult(dw));
aoqi@0 567
aoqi@0 568 out.write( "\n\n\n".getBytes() );
aoqi@0 569 }
aoqi@0 570 } catch( TransformerException e ) {
aoqi@0 571 e.printStackTrace();
aoqi@0 572 }
aoqi@0 573 }
aoqi@0 574 }

mercurial