src/share/jaxws_classes/com/sun/xml/internal/xsom/util/DomAnnotationParserFactory.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/xsom/util/DomAnnotationParserFactory.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,119 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.xsom.util;
    1.30 +
    1.31 +import com.sun.xml.internal.xsom.XSAnnotation;
    1.32 +import com.sun.xml.internal.xsom.parser.AnnotationContext;
    1.33 +import com.sun.xml.internal.xsom.parser.AnnotationParser;
    1.34 +import com.sun.xml.internal.xsom.parser.AnnotationParserFactory;
    1.35 +import javax.xml.XMLConstants;
    1.36 +import org.w3c.dom.Document;
    1.37 +import org.w3c.dom.Element;
    1.38 +import org.w3c.dom.Node;
    1.39 +import org.xml.sax.ContentHandler;
    1.40 +import org.xml.sax.EntityResolver;
    1.41 +import org.xml.sax.ErrorHandler;
    1.42 +
    1.43 +import javax.xml.transform.TransformerConfigurationException;
    1.44 +import javax.xml.transform.dom.DOMResult;
    1.45 +import javax.xml.transform.sax.SAXTransformerFactory;
    1.46 +import javax.xml.transform.sax.TransformerHandler;
    1.47 +
    1.48 +/**
    1.49 + * {@link AnnotationParserFactory} that parses annotations into a W3C DOM.
    1.50 + *
    1.51 + * <p>
    1.52 + * If you use this parser factory, you'll get {@link Element} that represents
    1.53 + * &lt;xs:annotation> from {@link XSAnnotation#getAnnotation()}.
    1.54 + *
    1.55 + * <p>
    1.56 + * When multiple &lt;xs:annotation>s are found for the given schema component,
    1.57 + * you'll see all &lt;xs:appinfo>s and &lt;xs:documentation>s combined under
    1.58 + * one &lt;xs:annotation> element.
    1.59 + *
    1.60 + * @author Kohsuke Kawaguchi
    1.61 + */
    1.62 +public class DomAnnotationParserFactory implements AnnotationParserFactory {
    1.63 +
    1.64 +    public AnnotationParser create() {
    1.65 +        return new AnnotationParserImpl();
    1.66 +    }
    1.67 +
    1.68 +    public AnnotationParser create(boolean disableSecureProcessing) {
    1.69 +        return new AnnotationParserImpl(disableSecureProcessing);
    1.70 +    }
    1.71 +
    1.72 +    private static final ContextClassloaderLocal<SAXTransformerFactory> stf = new ContextClassloaderLocal<SAXTransformerFactory>() {
    1.73 +        @Override
    1.74 +        protected SAXTransformerFactory initialValue() throws Exception {
    1.75 +            return (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    1.76 +        }
    1.77 +    };
    1.78 +
    1.79 +    private static class AnnotationParserImpl extends AnnotationParser {
    1.80 +
    1.81 +        /**
    1.82 +         * Identity transformer used to parse SAX into DOM.
    1.83 +         */
    1.84 +        private final TransformerHandler transformer;
    1.85 +        private DOMResult result;
    1.86 +
    1.87 +        AnnotationParserImpl() {
    1.88 +            this(false);
    1.89 +        }
    1.90 +
    1.91 +        AnnotationParserImpl(boolean disableSecureProcessing) {
    1.92 +            try {
    1.93 +                SAXTransformerFactory factory = stf.get();
    1.94 +                factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, disableSecureProcessing);
    1.95 +                transformer = factory.newTransformerHandler();
    1.96 +            } catch (TransformerConfigurationException e) {
    1.97 +                throw new Error(e); // impossible
    1.98 +            }
    1.99 +        }
   1.100 +
   1.101 +        public ContentHandler getContentHandler(AnnotationContext context, String parentElementName, ErrorHandler errorHandler, EntityResolver entityResolver) {
   1.102 +            result = new DOMResult();
   1.103 +            transformer.setResult(result);
   1.104 +            return transformer;
   1.105 +        }
   1.106 +
   1.107 +        public Object getResult(Object existing) {
   1.108 +            Document dom = (Document)result.getNode();
   1.109 +            Element e = dom.getDocumentElement();
   1.110 +            if(existing instanceof Element) {
   1.111 +                // merge all the children
   1.112 +                Element prev = (Element) existing;
   1.113 +                Node anchor = e.getFirstChild();
   1.114 +                while(prev.getFirstChild()!=null) {
   1.115 +                    Node move = prev.getFirstChild();
   1.116 +                    e.insertBefore(e.getOwnerDocument().adoptNode(move), anchor );
   1.117 +                }
   1.118 +            }
   1.119 +            return e;
   1.120 +        }
   1.121 +    }
   1.122 +}

mercurial