01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.tools.internal.xjc.reader.internalizer;
27:
28: import java.io.IOException;
29:
30: import com.sun.xml.internal.xsom.parser.XMLParser;
31:
32: import org.w3c.dom.Document;
33: import org.xml.sax.ContentHandler;
34: import org.xml.sax.EntityResolver;
35: import org.xml.sax.ErrorHandler;
36: import org.xml.sax.InputSource;
37: import org.xml.sax.SAXException;
38:
39: /**
40: * {@link XMLParser} implementation that
41: * parses XML from a DOM forest instead of parsing it from
42: * its original location.
43: *
44: * @author
45: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
46: */
47: class DOMForestParser implements XMLParser {
48:
49: /** DOM forest to be "parsed". */
50: private final DOMForest forest;
51:
52: /** Scanner object will do the actual SAX events generation. */
53: private final DOMForestScanner scanner;
54:
55: private final XMLParser fallbackParser;
56:
57: /**
58: * @param fallbackParser
59: * This parser will be used when DOMForestParser needs to parse
60: * documents that are not in the forest.
61: */
62: DOMForestParser(DOMForest forest, XMLParser fallbackParser) {
63: this .forest = forest;
64: this .scanner = new DOMForestScanner(forest);
65: this .fallbackParser = fallbackParser;
66: }
67:
68: public void parse(InputSource source,
69: ContentHandler contentHandler, ErrorHandler errorHandler,
70: EntityResolver entityResolver) throws SAXException,
71: IOException {
72:
73: String systemId = source.getSystemId();
74: Document dom = forest.get(systemId);
75:
76: if (dom == null) {
77: // if no DOM tree is built for it,
78: // let the fall back parser parse the original document.
79: //
80: // for example, XSOM parses datatypes.xsd (XML Schema part 2)
81: // but this will never be built into the forest.
82: fallbackParser.parse(source, contentHandler, errorHandler,
83: entityResolver);
84: return;
85: }
86:
87: scanner.scan(dom, contentHandler);
88: }
89: }
|