001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.xjc.reader.xmlschema.bindinfo;
038:
039: import javax.xml.bind.ValidationEventHandler;
040: import javax.xml.bind.annotation.DomHandler;
041: import javax.xml.parsers.ParserConfigurationException;
042: import javax.xml.transform.Source;
043: import javax.xml.transform.dom.DOMSource;
044: import javax.xml.transform.sax.SAXResult;
045:
046: import com.sun.xml.bind.marshaller.SAX2DOMEx;
047:
048: import org.w3c.dom.Document;
049: import org.w3c.dom.Element;
050: import org.xml.sax.Locator;
051: import org.xml.sax.helpers.LocatorImpl;
052: import org.xml.sax.helpers.XMLFilterImpl;
053:
054: /**
055: * {@link DomHandler} that produces a W3C DOM but with a location information.
056: *
057: * @author Kohsuke Kawaguchi
058: */
059: final class DomHandlerEx
060: implements
061: DomHandler<DomHandlerEx.DomAndLocation, DomHandlerEx.ResultImpl> {
062:
063: public static final class DomAndLocation {
064: public final Element element;
065: public final Locator loc;
066:
067: public DomAndLocation(Element element, Locator loc) {
068: this .element = element;
069: this .loc = loc;
070: }
071: }
072:
073: public ResultImpl createUnmarshaller(
074: ValidationEventHandler errorHandler) {
075: return new ResultImpl();
076: }
077:
078: public DomAndLocation getElement(ResultImpl r) {
079: return new DomAndLocation(((Document) r.s2d.getDOM())
080: .getDocumentElement(), r.location);
081: }
082:
083: public Source marshal(DomAndLocation domAndLocation,
084: ValidationEventHandler errorHandler) {
085: return new DOMSource(domAndLocation.element);
086: }
087:
088: public static final class ResultImpl extends SAXResult {
089: final SAX2DOMEx s2d;
090:
091: Locator location = null;
092:
093: ResultImpl() {
094: try {
095: s2d = new SAX2DOMEx();
096: } catch (ParserConfigurationException e) {
097: throw new AssertionError(e); // impossible
098: }
099:
100: XMLFilterImpl f = new XMLFilterImpl() {
101: public void setDocumentLocator(Locator locator) {
102: super .setDocumentLocator(locator);
103: location = new LocatorImpl(locator);
104: }
105: };
106: f.setContentHandler(s2d);
107:
108: setHandler(f);
109: }
110:
111: }
112: }
|