Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.internal.xjc.reader.xmlschema.bindinfo;
28 import javax.xml.bind.ValidationEventHandler;
29 import javax.xml.bind.annotation.DomHandler;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.transform.Source;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.sax.SAXResult;
35 import com.sun.xml.internal.bind.marshaller.SAX2DOMEx;
37 import com.sun.xml.internal.bind.v2.util.XmlFactory;
38 import javax.xml.parsers.DocumentBuilderFactory;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.Element;
41 import org.xml.sax.Locator;
42 import org.xml.sax.helpers.LocatorImpl;
43 import org.xml.sax.helpers.XMLFilterImpl;
45 /**
46 * {@link DomHandler} that produces a W3C DOM but with a location information.
47 *
48 * @author Kohsuke Kawaguchi
49 */
50 final class DomHandlerEx implements DomHandler<DomHandlerEx.DomAndLocation,DomHandlerEx.ResultImpl> {
52 public static final class DomAndLocation {
53 public final Element element;
54 public final Locator loc;
56 public DomAndLocation(Element element, Locator loc) {
57 this.element = element;
58 this.loc = loc;
59 }
60 }
62 public ResultImpl createUnmarshaller(ValidationEventHandler errorHandler) {
63 return new ResultImpl();
64 }
66 public DomAndLocation getElement(ResultImpl r) {
67 return new DomAndLocation( ((Document)r.s2d.getDOM()).getDocumentElement(), r.location );
68 }
70 public Source marshal(DomAndLocation domAndLocation, ValidationEventHandler errorHandler) {
71 return new DOMSource(domAndLocation.element);
72 }
74 public static final class ResultImpl extends SAXResult {
75 final SAX2DOMEx s2d;
77 Locator location = null;
79 ResultImpl() {
80 try {
81 DocumentBuilderFactory factory = XmlFactory.createDocumentBuilderFactory(false); // safe - only used for BI
82 s2d = new SAX2DOMEx(factory);
83 } catch (ParserConfigurationException e) {
84 throw new AssertionError(e); // impossible
85 }
87 XMLFilterImpl f = new XMLFilterImpl() {
88 @Override
89 public void setDocumentLocator(Locator locator) {
90 super.setDocumentLocator(locator);
91 location = new LocatorImpl(locator);
92 }
93 };
94 f.setContentHandler(s2d);
96 setHandler(f);
97 }
99 }
100 }