325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.xjc.reader.internalizer;
325N/A
325N/Aimport com.sun.xml.internal.bind.unmarshaller.DOMScanner;
325N/A
325N/Aimport org.w3c.dom.Document;
325N/Aimport org.w3c.dom.Element;
325N/Aimport org.w3c.dom.Node;
325N/Aimport org.xml.sax.Attributes;
325N/Aimport org.xml.sax.ContentHandler;
325N/Aimport org.xml.sax.Locator;
325N/Aimport org.xml.sax.SAXException;
325N/Aimport org.xml.sax.helpers.XMLFilterImpl;
325N/A
325N/A/**
325N/A * Produces a complete series of SAX events from any DOM node
325N/A * in the DOMForest.
325N/A *
325N/A * <p>
325N/A * This class hides a logic of re-associating {@link Locator}
325N/A * to the generated SAX event stream.
325N/A *
325N/A * @author
325N/A * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
325N/A */
325N/Apublic class DOMForestScanner {
325N/A
325N/A private final DOMForest forest;
325N/A
325N/A /**
325N/A * Scans DOM nodes of the given forest.
325N/A *
325N/A * DOM node parameters to the scan method must be a part of
325N/A * this forest.
325N/A */
325N/A public DOMForestScanner( DOMForest _forest ) {
325N/A this.forest = _forest;
325N/A }
325N/A
325N/A /**
325N/A * Generates the whole set of SAX events by treating
325N/A * element e as if it's a root element.
325N/A */
325N/A public void scan( Element e, ContentHandler contentHandler ) throws SAXException {
325N/A DOMScanner scanner = new DOMScanner();
325N/A
325N/A // insert the location resolver into the pipe line
325N/A LocationResolver resolver = new LocationResolver(scanner);
325N/A resolver.setContentHandler(contentHandler);
325N/A
325N/A // parse this DOM.
325N/A scanner.setContentHandler(resolver);
325N/A scanner.scan(e);
325N/A }
325N/A
325N/A /**
325N/A * Generates the whole set of SAX events from the given Document
325N/A * in the DOMForest.
325N/A */
325N/A public void scan( Document d, ContentHandler contentHandler ) throws SAXException {
325N/A scan( d.getDocumentElement(), contentHandler );
325N/A }
325N/A
325N/A /**
325N/A * Intercepts the invocation of the setDocumentLocator method
325N/A * and passes itself as the locator.
325N/A *
325N/A * If the client calls one of the methods on the Locator interface,
325N/A * use the LocatorTable to resolve the source location.
325N/A */
325N/A private class LocationResolver extends XMLFilterImpl implements Locator {
325N/A LocationResolver( DOMScanner _parent ) {
325N/A this.parent = _parent;
325N/A }
325N/A
325N/A private final DOMScanner parent;
325N/A
325N/A /**
325N/A * Flag that tells us whether we are processing a start element event
325N/A * or an end element event.
325N/A *
325N/A * DOMScanner's getCurrentLocation method doesn't tell us which, but
325N/A * this information is necessary to return the correct source line information.
325N/A *
325N/A * Thus we set this flag appropriately before we pass an event to
325N/A * the next ContentHandler, thereby making it possible to figure
325N/A * out which location to return.
325N/A */
325N/A private boolean inStart = false;
325N/A
325N/A @Override
325N/A public void setDocumentLocator(Locator locator) {
325N/A // ignore one set by the parent.
325N/A
325N/A super.setDocumentLocator(this);
325N/A }
325N/A
325N/A @Override
325N/A public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
325N/A inStart = false;
325N/A super.endElement(namespaceURI, localName, qName);
325N/A }
325N/A
325N/A @Override
325N/A public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
325N/A throws SAXException {
325N/A inStart = true;
325N/A super.startElement(namespaceURI, localName, qName, atts);
325N/A }
325N/A
325N/A private Locator findLocator() {
325N/A Node n = parent.getCurrentLocation();
325N/A if( n instanceof Element ) {
325N/A Element e = (Element)n;
325N/A if( inStart )
325N/A return forest.locatorTable.getStartLocation( e );
325N/A else
325N/A return forest.locatorTable.getEndLocation( e );
325N/A }
325N/A return null;
325N/A }
325N/A
325N/A //
325N/A //
325N/A // Locator methods
325N/A //
325N/A //
325N/A public int getColumnNumber() {
325N/A Locator l = findLocator();
325N/A if(l!=null) return l.getColumnNumber();
325N/A return -1;
325N/A }
325N/A
325N/A public int getLineNumber() {
325N/A Locator l = findLocator();
325N/A if(l!=null) return l.getLineNumber();
325N/A return -1;
325N/A }
325N/A
325N/A public String getPublicId() {
325N/A Locator l = findLocator();
325N/A if(l!=null) return l.getPublicId();
325N/A return null;
325N/A }
325N/A
325N/A public String getSystemId() {
325N/A Locator l = findLocator();
325N/A if(l!=null) return l.getSystemId();
325N/A return null;
325N/A }
325N/A
325N/A }
325N/A}