325N/A/*
325N/A * Copyright (c) 1997, 2010, 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.xml.internal.xsom.util;
325N/A
325N/Aimport com.sun.xml.internal.xsom.XSAnnotation;
325N/Aimport com.sun.xml.internal.xsom.parser.AnnotationContext;
325N/Aimport com.sun.xml.internal.xsom.parser.AnnotationParser;
325N/Aimport com.sun.xml.internal.xsom.parser.AnnotationParserFactory;
325N/Aimport org.w3c.dom.Document;
325N/Aimport org.w3c.dom.Element;
325N/Aimport org.w3c.dom.Node;
325N/Aimport org.xml.sax.ContentHandler;
325N/Aimport org.xml.sax.EntityResolver;
325N/Aimport org.xml.sax.ErrorHandler;
325N/A
325N/Aimport javax.xml.transform.TransformerConfigurationException;
325N/Aimport javax.xml.transform.dom.DOMResult;
325N/Aimport javax.xml.transform.sax.SAXTransformerFactory;
325N/Aimport javax.xml.transform.sax.TransformerHandler;
325N/A
325N/A/**
325N/A * {@link AnnotationParserFactory} that parses annotations into a W3C DOM.
325N/A *
325N/A * <p>
325N/A * If you use this parser factory, you'll get {@link Element} that represents
325N/A * &lt;xs:annotation> from {@link XSAnnotation#getAnnotation()}.
325N/A *
325N/A * <p>
325N/A * When multiple &lt;xs:annotation>s are found for the given schema component,
325N/A * you'll see all &lt;xs:appinfo>s and &lt;xs:documentation>s combined under
325N/A * one &lt;xs:annotation> element.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Apublic class DomAnnotationParserFactory implements AnnotationParserFactory {
325N/A public AnnotationParser create() {
325N/A return new AnnotationParserImpl();
325N/A }
325N/A
325N/A private static final SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
325N/A
325N/A private static class AnnotationParserImpl extends AnnotationParser {
325N/A
325N/A /**
325N/A * Identity transformer used to parse SAX into DOM.
325N/A */
325N/A private final TransformerHandler transformer;
325N/A private DOMResult result;
325N/A
325N/A AnnotationParserImpl() {
325N/A try {
325N/A transformer = stf.newTransformerHandler();
325N/A } catch (TransformerConfigurationException e) {
325N/A throw new Error(e); // impossible
325N/A }
325N/A }
325N/A
325N/A public ContentHandler getContentHandler(AnnotationContext context, String parentElementName, ErrorHandler errorHandler, EntityResolver entityResolver) {
325N/A result = new DOMResult();
325N/A transformer.setResult(result);
325N/A return transformer;
325N/A }
325N/A
325N/A public Object getResult(Object existing) {
325N/A Document dom = (Document)result.getNode();
325N/A Element e = dom.getDocumentElement();
325N/A if(existing instanceof Element) {
325N/A // merge all the children
325N/A Element prev = (Element) existing;
325N/A Node anchor = e.getFirstChild();
325N/A while(prev.getFirstChild()!=null) {
325N/A Node move = prev.getFirstChild();
325N/A e.insertBefore(e.getOwnerDocument().adoptNode(move), anchor );
325N/A }
325N/A }
325N/A return e;
325N/A }
325N/A }
325N/A}