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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2010, 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  */
  25 
  26 package com.sun.xml.internal.xsom.util;
  27 
  28 import com.sun.xml.internal.xsom.XSAnnotation;
  29 import com.sun.xml.internal.xsom.parser.AnnotationContext;
  30 import com.sun.xml.internal.xsom.parser.AnnotationParser;
  31 import com.sun.xml.internal.xsom.parser.AnnotationParserFactory;

  32 import org.w3c.dom.Document;
  33 import org.w3c.dom.Element;
  34 import org.w3c.dom.Node;
  35 import org.xml.sax.ContentHandler;
  36 import org.xml.sax.EntityResolver;
  37 import org.xml.sax.ErrorHandler;
  38 
  39 import javax.xml.transform.TransformerConfigurationException;
  40 import javax.xml.transform.dom.DOMResult;
  41 import javax.xml.transform.sax.SAXTransformerFactory;
  42 import javax.xml.transform.sax.TransformerHandler;
  43 
  44 /**
  45  * {@link AnnotationParserFactory} that parses annotations into a W3C DOM.
  46  *
  47  * <p>
  48  * If you use this parser factory, you'll get {@link Element} that represents
  49  * &lt;xs:annotation> from {@link XSAnnotation#getAnnotation()}.
  50  *
  51  * <p>
  52  * When multiple &lt;xs:annotation>s are found for the given schema component,
  53  * you'll see all &lt;xs:appinfo>s and &lt;xs:documentation>s combined under
  54  * one &lt;xs:annotation> element.
  55  *
  56  * @author Kohsuke Kawaguchi
  57  */
  58 public class DomAnnotationParserFactory implements AnnotationParserFactory {

  59     public AnnotationParser create() {
  60         return new AnnotationParserImpl();
  61     }
  62 




  63     private static final SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
  64 
  65     private static class AnnotationParserImpl extends AnnotationParser {
  66 
  67         /**
  68          * Identity transformer used to parse SAX into DOM.
  69          */
  70         private final TransformerHandler transformer;
  71         private DOMResult result;
  72 
  73         AnnotationParserImpl() {




  74             try {
  75                 transformer = stf.newTransformerHandler();

  76             } catch (TransformerConfigurationException e) {
  77                 throw new Error(e); // impossible
  78             }
  79         }
  80 
  81         public ContentHandler getContentHandler(AnnotationContext context, String parentElementName, ErrorHandler errorHandler, EntityResolver entityResolver) {
  82             result = new DOMResult();
  83             transformer.setResult(result);
  84             return transformer;
  85         }
  86 
  87         public Object getResult(Object existing) {
  88             Document dom = (Document)result.getNode();
  89             Element e = dom.getDocumentElement();
  90             if(existing instanceof Element) {
  91                 // merge all the children
  92                 Element prev = (Element) existing;
  93                 Node anchor = e.getFirstChild();
  94                 while(prev.getFirstChild()!=null) {
  95                     Node move = prev.getFirstChild();
   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  */
  25 
  26 package com.sun.xml.internal.xsom.util;
  27 
  28 import com.sun.xml.internal.xsom.XSAnnotation;
  29 import com.sun.xml.internal.xsom.parser.AnnotationContext;
  30 import com.sun.xml.internal.xsom.parser.AnnotationParser;
  31 import com.sun.xml.internal.xsom.parser.AnnotationParserFactory;
  32 import javax.xml.XMLConstants;
  33 import org.w3c.dom.Document;
  34 import org.w3c.dom.Element;
  35 import org.w3c.dom.Node;
  36 import org.xml.sax.ContentHandler;
  37 import org.xml.sax.EntityResolver;
  38 import org.xml.sax.ErrorHandler;
  39 
  40 import javax.xml.transform.TransformerConfigurationException;
  41 import javax.xml.transform.dom.DOMResult;
  42 import javax.xml.transform.sax.SAXTransformerFactory;
  43 import javax.xml.transform.sax.TransformerHandler;
  44 
  45 /**
  46  * {@link AnnotationParserFactory} that parses annotations into a W3C DOM.
  47  *
  48  * <p>
  49  * If you use this parser factory, you'll get {@link Element} that represents
  50  * &lt;xs:annotation> from {@link XSAnnotation#getAnnotation()}.
  51  *
  52  * <p>
  53  * When multiple &lt;xs:annotation>s are found for the given schema component,
  54  * you'll see all &lt;xs:appinfo>s and &lt;xs:documentation>s combined under
  55  * one &lt;xs:annotation> element.
  56  *
  57  * @author Kohsuke Kawaguchi
  58  */
  59 public class DomAnnotationParserFactory implements AnnotationParserFactory {
  60 
  61     public AnnotationParser create() {
  62         return new AnnotationParserImpl();
  63     }
  64 
  65     public AnnotationParser create(boolean disableSecureProcessing) {
  66         return new AnnotationParserImpl();
  67     }
  68 
  69     private static final SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
  70 
  71     private static class AnnotationParserImpl extends AnnotationParser {
  72 
  73         /**
  74          * Identity transformer used to parse SAX into DOM.
  75          */
  76         private final TransformerHandler transformer;
  77         private DOMResult result;
  78 
  79         AnnotationParserImpl() {
  80             this(false);
  81         }
  82 
  83         AnnotationParserImpl(boolean disableSecureProcessing) {
  84             try {
  85                 transformer = stf.newTransformerHandler();
  86                 stf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, disableSecureProcessing);
  87             } catch (TransformerConfigurationException e) {
  88                 throw new Error(e); // impossible
  89             }
  90         }
  91 
  92         public ContentHandler getContentHandler(AnnotationContext context, String parentElementName, ErrorHandler errorHandler, EntityResolver entityResolver) {
  93             result = new DOMResult();
  94             transformer.setResult(result);
  95             return transformer;
  96         }
  97 
  98         public Object getResult(Object existing) {
  99             Document dom = (Document)result.getNode();
 100             Element e = dom.getDocumentElement();
 101             if(existing instanceof Element) {
 102                 // merge all the children
 103                 Element prev = (Element) existing;
 104                 Node anchor = e.getFirstChild();
 105                 while(prev.getFirstChild()!=null) {
 106                     Node move = prev.getFirstChild();