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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.tools.internal.xjc.reader.xmlschema.bindinfo;
ohair@286 27
ohair@286 28 import javax.xml.bind.JAXBException;
ohair@286 29 import javax.xml.bind.Unmarshaller;
ohair@286 30 import javax.xml.bind.UnmarshallerHandler;
ohair@286 31 import javax.xml.bind.helpers.DefaultValidationEventHandler;
ohair@286 32 import javax.xml.validation.ValidatorHandler;
ohair@286 33
ohair@286 34 import com.sun.tools.internal.xjc.Options;
ohair@286 35 import com.sun.tools.internal.xjc.reader.Const;
ohair@286 36 import com.sun.xml.internal.xsom.parser.AnnotationContext;
ohair@286 37 import com.sun.xml.internal.xsom.parser.AnnotationParser;
ohair@286 38 import com.sun.xml.internal.xsom.parser.AnnotationParserFactory;
ohair@286 39 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
ohair@286 40
ohair@286 41 import org.xml.sax.Attributes;
ohair@286 42 import org.xml.sax.ContentHandler;
ohair@286 43 import org.xml.sax.EntityResolver;
ohair@286 44 import org.xml.sax.ErrorHandler;
ohair@286 45 import org.xml.sax.SAXException;
ohair@286 46 import org.xml.sax.SAXParseException;
ohair@286 47 import org.xml.sax.helpers.XMLFilterImpl;
ohair@286 48
ohair@286 49 /**
ohair@286 50 * Implementation of XSOM {@link AnnotationParserFactory} that
ohair@286 51 * parses JAXB customization declarations.
ohair@286 52 *
ohair@286 53 * @author
ohair@286 54 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286 55 */
ohair@286 56 public class AnnotationParserFactoryImpl implements AnnotationParserFactory {
ohair@286 57 public AnnotationParserFactoryImpl(Options opts) {
ohair@286 58 this.options=opts;
ohair@286 59 }
ohair@286 60
ohair@286 61 private final Options options;
ohair@286 62 /**
ohair@286 63 * Lazily created validator, so that the schema for binding won't be
ohair@286 64 * prepared unless absolutely necessary.
ohair@286 65 */
ohair@286 66 private ValidatorHandler validator;
ohair@286 67
ohair@286 68 public AnnotationParser create() {
ohair@286 69 return new AnnotationParser() {
ohair@286 70 private Unmarshaller u = BindInfo.getJAXBContext().createUnmarshaller();
ohair@286 71
ohair@286 72 private UnmarshallerHandler handler;
ohair@286 73
ohair@286 74 public ContentHandler getContentHandler(
ohair@286 75 AnnotationContext context, String parentElementName,
ohair@286 76 final ErrorHandler errorHandler, EntityResolver entityResolver ) {
ohair@286 77
ohair@286 78 // return a ContentHandler that validates the customization and also
ohair@286 79 // parses them into the internal structure.
ohair@286 80 if(handler!=null)
ohair@286 81 // interface contract violation.
ohair@286 82 // this method will be called only once.
ohair@286 83 throw new AssertionError();
ohair@286 84
ohair@286 85 if(options.debugMode)
ohair@286 86 try {
ohair@286 87 u.setEventHandler(new DefaultValidationEventHandler());
ohair@286 88 } catch (JAXBException e) {
ohair@286 89 throw new AssertionError(e); // ridiculous!
ohair@286 90 }
ohair@286 91
ohair@286 92 handler = u.getUnmarshallerHandler();
ohair@286 93
ohair@286 94 // configure so that the validator will receive events for JAXB islands
ohair@286 95 return new ForkingFilter(handler) {
ohair@286 96 @Override
ohair@286 97 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
ohair@286 98 super.startElement(uri, localName, qName, atts);
ohair@286 99 if((uri.equals(Const.JAXB_NSURI) || uri.equals(Const.XJC_EXTENSION_URI))
ohair@286 100 && getSideHandler()==null) {
ohair@286 101 // set up validator
ohair@286 102 if(validator==null)
ohair@286 103 validator = BindInfo.bindingFileSchema.newValidator();
ohair@286 104 validator.setErrorHandler(errorHandler);
ohair@286 105 startForking(uri,localName,qName,atts,new ValidatorProtecter(validator));
ohair@286 106 }
ohair@286 107
ohair@286 108 // check for xmime:expectedContentTypes attributes in annotations and report them
ohair@286 109 for( int i=atts.getLength()-1; i>=0; i-- ) {
ohair@286 110 if(atts.getURI(i).equals(WellKnownNamespace.XML_MIME_URI)
ohair@286 111 && atts.getLocalName(i).equals(Const.EXPECTED_CONTENT_TYPES))
ohair@286 112 errorHandler.warning(new SAXParseException(
ohair@286 113 com.sun.tools.internal.xjc.reader.xmlschema.Messages.format(
ohair@286 114 com.sun.tools.internal.xjc.reader.xmlschema.Messages.WARN_UNUSED_EXPECTED_CONTENT_TYPES),
ohair@286 115 getDocumentLocator()
ohair@286 116 ));
ohair@286 117 }
ohair@286 118 }
ohair@286 119 };
ohair@286 120 }
ohair@286 121
ohair@286 122 public BindInfo getResult( Object existing ) {
ohair@286 123 if(handler==null)
ohair@286 124 // interface contract violation.
ohair@286 125 // the getContentHandler method must have been called.
ohair@286 126 throw new AssertionError();
ohair@286 127
ohair@286 128 try {
ohair@286 129 BindInfo result = (BindInfo)handler.getResult();
ohair@286 130
ohair@286 131 if(existing!=null) {
ohair@286 132 BindInfo bie = (BindInfo)existing;
ohair@286 133 bie.absorb(result);
ohair@286 134 return bie;
ohair@286 135 } else {
ohair@286 136 if(!result.isPointless())
ohair@286 137 return result; // just annotation. no meaningful customization
ohair@286 138 else
ohair@286 139 return null;
ohair@286 140 }
ohair@286 141 } catch (JAXBException e) {
ohair@286 142 throw new AssertionError(e);
ohair@286 143 }
ohair@286 144 }
ohair@286 145 };
ohair@286 146 }
ohair@286 147
ohair@286 148 private static final class ValidatorProtecter extends XMLFilterImpl {
ohair@286 149 public ValidatorProtecter(ContentHandler h) {
ohair@286 150 setContentHandler(h);
ohair@286 151 }
ohair@286 152
ohair@286 153 @Override
ohair@286 154 public void startPrefixMapping(String prefix, String uri) throws SAXException {
ohair@286 155 // work around a bug in the validator implementation in Tiger
ohair@286 156 super.startPrefixMapping(prefix.intern(),uri);
ohair@286 157 }
ohair@286 158 }
ohair@286 159 }

mercurial