001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.xjc.reader.internalizer;
027:
028: import java.util.Set;
029:
030: import com.sun.tools.internal.xjc.reader.Const;
031: import com.sun.xml.internal.bind.marshaller.SAX2DOMEx;
032:
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Element;
035: import org.w3c.dom.Node;
036: import org.xml.sax.Attributes;
037: import org.xml.sax.Locator;
038:
039: /**
040: * Builds DOM while keeping the location information.
041: *
042: * <p>
043: * This class also looks for outer most <jaxb:bindings>
044: * customizations.
045: *
046: * @author
047: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
048: */
049: class DOMBuilder extends SAX2DOMEx {
050: /**
051: * Grows a DOM tree under the given document, and
052: * stores location information to the given table.
053: *
054: * @param outerMostBindings
055: * This set will receive newly found outermost
056: * jaxb:bindings customizations.
057: */
058: public DOMBuilder(Document dom, LocatorTable ltable,
059: Set outerMostBindings) {
060: super (dom);
061: this .locatorTable = ltable;
062: this .outerMostBindings = outerMostBindings;
063: }
064:
065: /** Location information will be stored into this object. */
066: private final LocatorTable locatorTable;
067:
068: private final Set outerMostBindings;
069:
070: private Locator locator;
071:
072: public void setDocumentLocator(Locator locator) {
073: this .locator = locator;
074: super .setDocumentLocator(locator);
075: }
076:
077: public void startElement(String namespaceURI, String localName,
078: String qName, Attributes atts) {
079: super .startElement(namespaceURI, localName, qName, atts);
080:
081: Element e = getCurrentElement();
082: locatorTable.storeStartLocation(e, locator);
083:
084: // check if this element is an outer-most <jaxb:bindings>
085: if (Const.JAXB_NSURI.equals(e.getNamespaceURI())
086: && "bindings".equals(e.getLocalName())) {
087:
088: // if this is the root node (meaning that this file is an
089: // external binding file) or if the parent is XML Schema element
090: // (meaning that this is an "inlined" external binding)
091: Node p = e.getParentNode();
092: if (p instanceof Document
093: || (p instanceof Element && !e.getNamespaceURI()
094: .equals(p.getNamespaceURI()))) {
095: outerMostBindings.add(e); // remember this value
096: }
097: }
098: }
099:
100: public void endElement(String namespaceURI, String localName,
101: String qName) {
102: locatorTable.storeEndLocation(getCurrentElement(), locator);
103: super.endElement(namespaceURI, localName, qName);
104: }
105: }
|