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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/bindinfo/AnnotationParserFactoryImpl.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,159 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, 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.tools.internal.xjc.reader.xmlschema.bindinfo;
    1.30 +
    1.31 +import javax.xml.bind.JAXBException;
    1.32 +import javax.xml.bind.Unmarshaller;
    1.33 +import javax.xml.bind.UnmarshallerHandler;
    1.34 +import javax.xml.bind.helpers.DefaultValidationEventHandler;
    1.35 +import javax.xml.validation.ValidatorHandler;
    1.36 +
    1.37 +import com.sun.tools.internal.xjc.Options;
    1.38 +import com.sun.tools.internal.xjc.reader.Const;
    1.39 +import com.sun.xml.internal.xsom.parser.AnnotationContext;
    1.40 +import com.sun.xml.internal.xsom.parser.AnnotationParser;
    1.41 +import com.sun.xml.internal.xsom.parser.AnnotationParserFactory;
    1.42 +import com.sun.xml.internal.bind.v2.WellKnownNamespace;
    1.43 +
    1.44 +import org.xml.sax.Attributes;
    1.45 +import org.xml.sax.ContentHandler;
    1.46 +import org.xml.sax.EntityResolver;
    1.47 +import org.xml.sax.ErrorHandler;
    1.48 +import org.xml.sax.SAXException;
    1.49 +import org.xml.sax.SAXParseException;
    1.50 +import org.xml.sax.helpers.XMLFilterImpl;
    1.51 +
    1.52 +/**
    1.53 + * Implementation of XSOM {@link AnnotationParserFactory} that
    1.54 + * parses JAXB customization declarations.
    1.55 + *
    1.56 + * @author
    1.57 + *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    1.58 + */
    1.59 +public class AnnotationParserFactoryImpl implements AnnotationParserFactory {
    1.60 +    public AnnotationParserFactoryImpl(Options opts) {
    1.61 +        this.options=opts;
    1.62 +    }
    1.63 +
    1.64 +    private final Options options;
    1.65 +    /**
    1.66 +     * Lazily created validator, so that the schema for binding won't be
    1.67 +     * prepared unless absolutely necessary.
    1.68 +     */
    1.69 +    private ValidatorHandler validator;
    1.70 +
    1.71 +    public AnnotationParser create() {
    1.72 +        return new AnnotationParser() {
    1.73 +            private Unmarshaller u = BindInfo.getJAXBContext().createUnmarshaller();
    1.74 +
    1.75 +            private UnmarshallerHandler handler;
    1.76 +
    1.77 +            public ContentHandler getContentHandler(
    1.78 +                AnnotationContext context, String parentElementName,
    1.79 +                final ErrorHandler errorHandler, EntityResolver entityResolver ) {
    1.80 +
    1.81 +                // return a ContentHandler that validates the customization and also
    1.82 +                // parses them into the internal structure.
    1.83 +                if(handler!=null)
    1.84 +                    // interface contract violation.
    1.85 +                    // this method will be called only once.
    1.86 +                    throw new AssertionError();
    1.87 +
    1.88 +                if(options.debugMode)
    1.89 +                    try {
    1.90 +                        u.setEventHandler(new DefaultValidationEventHandler());
    1.91 +                    } catch (JAXBException e) {
    1.92 +                        throw new AssertionError(e);    // ridiculous!
    1.93 +                    }
    1.94 +
    1.95 +                handler = u.getUnmarshallerHandler();
    1.96 +
    1.97 +                // configure so that the validator will receive events for JAXB islands
    1.98 +                return new ForkingFilter(handler) {
    1.99 +                    @Override
   1.100 +                    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
   1.101 +                        super.startElement(uri, localName, qName, atts);
   1.102 +                        if((uri.equals(Const.JAXB_NSURI) || uri.equals(Const.XJC_EXTENSION_URI))
   1.103 +                        && getSideHandler()==null) {
   1.104 +                            // set up validator
   1.105 +                            if(validator==null)
   1.106 +                                validator = BindInfo.bindingFileSchema.newValidator();
   1.107 +                            validator.setErrorHandler(errorHandler);
   1.108 +                            startForking(uri,localName,qName,atts,new ValidatorProtecter(validator));
   1.109 +                        }
   1.110 +
   1.111 +                        // check for xmime:expectedContentTypes attributes in annotations and report them
   1.112 +                        for( int i=atts.getLength()-1; i>=0; i-- ) {
   1.113 +                            if(atts.getURI(i).equals(WellKnownNamespace.XML_MIME_URI)
   1.114 +                            && atts.getLocalName(i).equals(Const.EXPECTED_CONTENT_TYPES))
   1.115 +                                errorHandler.warning(new SAXParseException(
   1.116 +                                    com.sun.tools.internal.xjc.reader.xmlschema.Messages.format(
   1.117 +                                        com.sun.tools.internal.xjc.reader.xmlschema.Messages.WARN_UNUSED_EXPECTED_CONTENT_TYPES),
   1.118 +                                    getDocumentLocator()
   1.119 +                                ));
   1.120 +                        }
   1.121 +                    }
   1.122 +                };
   1.123 +            }
   1.124 +
   1.125 +            public BindInfo getResult( Object existing ) {
   1.126 +                if(handler==null)
   1.127 +                    // interface contract violation.
   1.128 +                    // the getContentHandler method must have been called.
   1.129 +                    throw new AssertionError();
   1.130 +
   1.131 +                try {
   1.132 +                    BindInfo result = (BindInfo)handler.getResult();
   1.133 +
   1.134 +                    if(existing!=null) {
   1.135 +                        BindInfo bie = (BindInfo)existing;
   1.136 +                        bie.absorb(result);
   1.137 +                        return bie;
   1.138 +                    } else {
   1.139 +                        if(!result.isPointless())
   1.140 +                            return result;   // just annotation. no meaningful customization
   1.141 +                        else
   1.142 +                            return null;
   1.143 +                    }
   1.144 +                } catch (JAXBException e) {
   1.145 +                    throw new AssertionError(e);
   1.146 +                }
   1.147 +            }
   1.148 +        };
   1.149 +    }
   1.150 +
   1.151 +    private static final class ValidatorProtecter extends XMLFilterImpl {
   1.152 +        public ValidatorProtecter(ContentHandler h) {
   1.153 +            setContentHandler(h);
   1.154 +        }
   1.155 +
   1.156 +        @Override
   1.157 +        public void startPrefixMapping(String prefix, String uri) throws SAXException {
   1.158 +            // work around a bug in the validator implementation in Tiger
   1.159 +            super.startPrefixMapping(prefix.intern(),uri);
   1.160 +        }
   1.161 +    }
   1.162 +}

mercurial