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