src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/bindinfo/AnnotationParserFactoryImpl.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2011, 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.JAXBException;
    29 import javax.xml.bind.Unmarshaller;
    30 import javax.xml.bind.UnmarshallerHandler;
    31 import javax.xml.bind.helpers.DefaultValidationEventHandler;
    32 import javax.xml.validation.ValidatorHandler;
    34 import com.sun.tools.internal.xjc.Options;
    35 import com.sun.tools.internal.xjc.reader.Const;
    36 import com.sun.xml.internal.xsom.parser.AnnotationContext;
    37 import com.sun.xml.internal.xsom.parser.AnnotationParser;
    38 import com.sun.xml.internal.xsom.parser.AnnotationParserFactory;
    39 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
    41 import org.xml.sax.Attributes;
    42 import org.xml.sax.ContentHandler;
    43 import org.xml.sax.EntityResolver;
    44 import org.xml.sax.ErrorHandler;
    45 import org.xml.sax.SAXException;
    46 import org.xml.sax.SAXParseException;
    47 import org.xml.sax.helpers.XMLFilterImpl;
    49 /**
    50  * Implementation of XSOM {@link AnnotationParserFactory} that
    51  * parses JAXB customization declarations.
    52  *
    53  * @author
    54  *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    55  */
    56 public class AnnotationParserFactoryImpl implements AnnotationParserFactory {
    57     public AnnotationParserFactoryImpl(Options opts) {
    58         this.options=opts;
    59     }
    61     private final Options options;
    62     /**
    63      * Lazily created validator, so that the schema for binding won't be
    64      * prepared unless absolutely necessary.
    65      */
    66     private ValidatorHandler validator;
    68     public AnnotationParser create() {
    69         return new AnnotationParser() {
    70             private Unmarshaller u = BindInfo.getCustomizationUnmarshaller();
    72             private UnmarshallerHandler handler;
    74             public ContentHandler getContentHandler(
    75                 AnnotationContext context, String parentElementName,
    76                 final ErrorHandler errorHandler, EntityResolver entityResolver ) {
    78                 // return a ContentHandler that validates the customization and also
    79                 // parses them into the internal structure.
    80                 if(handler!=null)
    81                     // interface contract violation.
    82                     // this method will be called only once.
    83                     throw new AssertionError();
    85                 if(options.debugMode)
    86                     try {
    87                         u.setEventHandler(new DefaultValidationEventHandler());
    88                     } catch (JAXBException e) {
    89                         throw new AssertionError(e);    // ridiculous!
    90                     }
    92                 handler = u.getUnmarshallerHandler();
    94                 // configure so that the validator will receive events for JAXB islands
    95                 return new ForkingFilter(handler) {
    96                     @Override
    97                     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    98                         super.startElement(uri, localName, qName, atts);
    99                         if((uri.equals(Const.JAXB_NSURI) || uri.equals(Const.XJC_EXTENSION_URI))
   100                         && getSideHandler()==null) {
   101                             // set up validator
   102                             if(validator==null)
   103                                 validator = BindInfo.bindingFileSchema.newValidator();
   104                             validator.setErrorHandler(errorHandler);
   105                             startForking(uri,localName,qName,atts,new ValidatorProtecter(validator));
   106                         }
   108                         // check for xmime:expectedContentTypes attributes in annotations and report them
   109                         for( int i=atts.getLength()-1; i>=0; i-- ) {
   110                             if(atts.getURI(i).equals(WellKnownNamespace.XML_MIME_URI)
   111                             && atts.getLocalName(i).equals(Const.EXPECTED_CONTENT_TYPES))
   112                                 errorHandler.warning(new SAXParseException(
   113                                     com.sun.tools.internal.xjc.reader.xmlschema.Messages.format(
   114                                         com.sun.tools.internal.xjc.reader.xmlschema.Messages.WARN_UNUSED_EXPECTED_CONTENT_TYPES),
   115                                     getDocumentLocator()
   116                                 ));
   117                         }
   118                     }
   119                 };
   120             }
   122             public BindInfo getResult( Object existing ) {
   123                 if(handler==null)
   124                     // interface contract violation.
   125                     // the getContentHandler method must have been called.
   126                     throw new AssertionError();
   128                 try {
   129                     BindInfo result = (BindInfo)handler.getResult();
   131                     if(existing!=null) {
   132                         BindInfo bie = (BindInfo)existing;
   133                         bie.absorb(result);
   134                         return bie;
   135                     } else {
   136                         if(!result.isPointless())
   137                             return result;   // just annotation. no meaningful customization
   138                         else
   139                             return null;
   140                     }
   141                 } catch (JAXBException e) {
   142                     throw new AssertionError(e);
   143                 }
   144             }
   145         };
   146     }
   148     private static final class ValidatorProtecter extends XMLFilterImpl {
   149         public ValidatorProtecter(ContentHandler h) {
   150             setContentHandler(h);
   151         }
   153         @Override
   154         public void startPrefixMapping(String prefix, String uri) throws SAXException {
   155             // work around a bug in the validator implementation in Tiger
   156             super.startPrefixMapping(prefix.intern(),uri);
   157         }
   158     }
   159 }

mercurial