Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, 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.ws.processor.modeler.wsdl; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.tools.internal.ws.processor.model.ModelException; |
aoqi@0 | 29 | import com.sun.tools.internal.ws.processor.model.java.JavaSimpleType; |
aoqi@0 | 30 | import com.sun.tools.internal.ws.processor.model.java.JavaType; |
aoqi@0 | 31 | import com.sun.tools.internal.ws.processor.model.jaxb.JAXBMapping; |
aoqi@0 | 32 | import com.sun.tools.internal.ws.processor.model.jaxb.JAXBModel; |
aoqi@0 | 33 | import com.sun.tools.internal.ws.processor.model.jaxb.JAXBType; |
aoqi@0 | 34 | import com.sun.tools.internal.ws.processor.util.ClassNameCollector; |
aoqi@0 | 35 | import com.sun.tools.internal.ws.wscompile.AbortException; |
aoqi@0 | 36 | import com.sun.tools.internal.ws.wscompile.ErrorReceiver; |
aoqi@0 | 37 | import com.sun.tools.internal.ws.wscompile.WsimportOptions; |
aoqi@0 | 38 | import com.sun.tools.internal.ws.wsdl.parser.DOMForestScanner; |
aoqi@0 | 39 | import com.sun.tools.internal.ws.wsdl.parser.MetadataFinder; |
aoqi@0 | 40 | import com.sun.tools.internal.xjc.api.S2JJAXBModel; |
aoqi@0 | 41 | import com.sun.tools.internal.xjc.api.SchemaCompiler; |
aoqi@0 | 42 | import com.sun.tools.internal.xjc.api.TypeAndAnnotation; |
aoqi@0 | 43 | import org.w3c.dom.Element; |
aoqi@0 | 44 | import org.xml.sax.InputSource; |
aoqi@0 | 45 | import org.xml.sax.helpers.LocatorImpl; |
aoqi@0 | 46 | |
aoqi@0 | 47 | import javax.xml.namespace.QName; |
aoqi@0 | 48 | |
aoqi@0 | 49 | /** |
aoqi@0 | 50 | * @author Vivek Pandey |
aoqi@0 | 51 | * |
aoqi@0 | 52 | * Uses JAXB XJC apis to build JAXBModel and resolves xml to java type mapping from JAXBModel |
aoqi@0 | 53 | */ |
aoqi@0 | 54 | public class JAXBModelBuilder { |
aoqi@0 | 55 | |
aoqi@0 | 56 | private final ErrorReceiver errReceiver; |
aoqi@0 | 57 | private final WsimportOptions options; |
aoqi@0 | 58 | private final MetadataFinder forest; |
aoqi@0 | 59 | |
aoqi@0 | 60 | public JAXBModelBuilder(WsimportOptions options, ClassNameCollector classNameCollector, MetadataFinder finder, ErrorReceiver errReceiver) { |
aoqi@0 | 61 | this._classNameAllocator = new ClassNameAllocatorImpl(classNameCollector); |
aoqi@0 | 62 | this.errReceiver = errReceiver; |
aoqi@0 | 63 | this.options = options; |
aoqi@0 | 64 | this.forest = finder; |
aoqi@0 | 65 | |
aoqi@0 | 66 | internalBuildJAXBModel(); |
aoqi@0 | 67 | } |
aoqi@0 | 68 | |
aoqi@0 | 69 | /** |
aoqi@0 | 70 | * Builds model from WSDL document. Model contains abstraction which is used by the |
aoqi@0 | 71 | * generators to generate the stub/tie/serializers etc. code. |
aoqi@0 | 72 | * |
aoqi@0 | 73 | * @see com.sun.tools.internal.ws.processor.modeler.Modeler#buildModel() |
aoqi@0 | 74 | */ |
aoqi@0 | 75 | |
aoqi@0 | 76 | private void internalBuildJAXBModel(){ |
aoqi@0 | 77 | try { |
aoqi@0 | 78 | schemaCompiler = options.getSchemaCompiler(); |
aoqi@0 | 79 | schemaCompiler.resetSchema(); |
aoqi@0 | 80 | if(options.entityResolver != null) { |
aoqi@0 | 81 | //set if its not null so as not to override catalog option specified via xjc args |
aoqi@0 | 82 | schemaCompiler.setEntityResolver(options.entityResolver); |
aoqi@0 | 83 | } |
aoqi@0 | 84 | schemaCompiler.setClassNameAllocator(_classNameAllocator); |
aoqi@0 | 85 | schemaCompiler.setErrorListener(errReceiver); |
aoqi@0 | 86 | int schemaElementCount = 1; |
aoqi@0 | 87 | |
aoqi@0 | 88 | for (Element element : forest.getInlinedSchemaElement()) { |
aoqi@0 | 89 | String location = element.getOwnerDocument().getDocumentURI(); |
aoqi@0 | 90 | String systemId = location + "#types?schema" + schemaElementCount++; |
aoqi@0 | 91 | if(forest.isMexMetadata) |
aoqi@0 | 92 | schemaCompiler.parseSchema(systemId,element); |
aoqi@0 | 93 | else |
aoqi@0 | 94 | new DOMForestScanner(forest).scan(element,schemaCompiler.getParserHandler(systemId)); |
aoqi@0 | 95 | } |
aoqi@0 | 96 | |
aoqi@0 | 97 | //feed external jaxb:bindings file |
aoqi@0 | 98 | InputSource[] externalBindings = options.getSchemaBindings(); |
aoqi@0 | 99 | if(externalBindings != null){ |
aoqi@0 | 100 | for(InputSource jaxbBinding : externalBindings){ |
aoqi@0 | 101 | schemaCompiler.parseSchema(jaxbBinding); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | } |
aoqi@0 | 104 | } catch (Exception e) { |
aoqi@0 | 105 | throw new ModelException(e); |
aoqi@0 | 106 | } |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | public JAXBType getJAXBType(QName qname){ |
aoqi@0 | 110 | JAXBMapping mapping = jaxbModel.get(qname); |
aoqi@0 | 111 | if (mapping == null){ |
aoqi@0 | 112 | return null; |
aoqi@0 | 113 | } |
aoqi@0 | 114 | JavaType javaType = new JavaSimpleType(mapping.getType()); |
aoqi@0 | 115 | return new JAXBType(qname, javaType, mapping, jaxbModel); |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | public TypeAndAnnotation getElementTypeAndAnn(QName qname){ |
aoqi@0 | 119 | JAXBMapping mapping = jaxbModel.get(qname); |
aoqi@0 | 120 | if (mapping == null){ |
aoqi@0 | 121 | return null; |
aoqi@0 | 122 | } |
aoqi@0 | 123 | return mapping.getType().getTypeAnn(); |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | protected void bind(){ |
aoqi@0 | 127 | S2JJAXBModel rawJaxbModel = schemaCompiler.bind(); |
aoqi@0 | 128 | if(rawJaxbModel == null) |
aoqi@0 | 129 | throw new AbortException(); |
aoqi@0 | 130 | options.setCodeModel(rawJaxbModel.generateCode(null, errReceiver)); |
aoqi@0 | 131 | jaxbModel = new JAXBModel(rawJaxbModel); |
aoqi@0 | 132 | jaxbModel.setGeneratedClassNames(_classNameAllocator.getJaxbGeneratedClasses()); |
aoqi@0 | 133 | } |
aoqi@0 | 134 | |
aoqi@0 | 135 | protected SchemaCompiler getJAXBSchemaCompiler(){ |
aoqi@0 | 136 | return schemaCompiler; |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | public JAXBModel getJAXBModel(){ |
aoqi@0 | 140 | return jaxbModel; |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | private JAXBModel jaxbModel; |
aoqi@0 | 144 | private SchemaCompiler schemaCompiler; |
aoqi@0 | 145 | private final ClassNameAllocatorImpl _classNameAllocator; |
aoqi@0 | 146 | protected static final LocatorImpl NULL_LOCATOR = new LocatorImpl(); |
aoqi@0 | 147 | |
aoqi@0 | 148 | } |