1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/processor/modeler/wsdl/JAXBModelBuilder.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,148 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, 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.ws.processor.modeler.wsdl; 1.30 + 1.31 +import com.sun.tools.internal.ws.processor.model.ModelException; 1.32 +import com.sun.tools.internal.ws.processor.model.java.JavaSimpleType; 1.33 +import com.sun.tools.internal.ws.processor.model.java.JavaType; 1.34 +import com.sun.tools.internal.ws.processor.model.jaxb.JAXBMapping; 1.35 +import com.sun.tools.internal.ws.processor.model.jaxb.JAXBModel; 1.36 +import com.sun.tools.internal.ws.processor.model.jaxb.JAXBType; 1.37 +import com.sun.tools.internal.ws.processor.util.ClassNameCollector; 1.38 +import com.sun.tools.internal.ws.wscompile.AbortException; 1.39 +import com.sun.tools.internal.ws.wscompile.ErrorReceiver; 1.40 +import com.sun.tools.internal.ws.wscompile.WsimportOptions; 1.41 +import com.sun.tools.internal.ws.wsdl.parser.DOMForestScanner; 1.42 +import com.sun.tools.internal.ws.wsdl.parser.MetadataFinder; 1.43 +import com.sun.tools.internal.xjc.api.S2JJAXBModel; 1.44 +import com.sun.tools.internal.xjc.api.SchemaCompiler; 1.45 +import com.sun.tools.internal.xjc.api.TypeAndAnnotation; 1.46 +import org.w3c.dom.Element; 1.47 +import org.xml.sax.InputSource; 1.48 +import org.xml.sax.helpers.LocatorImpl; 1.49 + 1.50 +import javax.xml.namespace.QName; 1.51 + 1.52 +/** 1.53 + * @author Vivek Pandey 1.54 + * 1.55 + * Uses JAXB XJC apis to build JAXBModel and resolves xml to java type mapping from JAXBModel 1.56 + */ 1.57 +public class JAXBModelBuilder { 1.58 + 1.59 + private final ErrorReceiver errReceiver; 1.60 + private final WsimportOptions options; 1.61 + private final MetadataFinder forest; 1.62 + 1.63 + public JAXBModelBuilder(WsimportOptions options, ClassNameCollector classNameCollector, MetadataFinder finder, ErrorReceiver errReceiver) { 1.64 + this._classNameAllocator = new ClassNameAllocatorImpl(classNameCollector); 1.65 + this.errReceiver = errReceiver; 1.66 + this.options = options; 1.67 + this.forest = finder; 1.68 + 1.69 + internalBuildJAXBModel(); 1.70 + } 1.71 + 1.72 + /** 1.73 + * Builds model from WSDL document. Model contains abstraction which is used by the 1.74 + * generators to generate the stub/tie/serializers etc. code. 1.75 + * 1.76 + * @see com.sun.tools.internal.ws.processor.modeler.Modeler#buildModel() 1.77 + */ 1.78 + 1.79 + private void internalBuildJAXBModel(){ 1.80 + try { 1.81 + schemaCompiler = options.getSchemaCompiler(); 1.82 + schemaCompiler.resetSchema(); 1.83 + if(options.entityResolver != null) { 1.84 + //set if its not null so as not to override catalog option specified via xjc args 1.85 + schemaCompiler.setEntityResolver(options.entityResolver); 1.86 + } 1.87 + schemaCompiler.setClassNameAllocator(_classNameAllocator); 1.88 + schemaCompiler.setErrorListener(errReceiver); 1.89 + int schemaElementCount = 1; 1.90 + 1.91 + for (Element element : forest.getInlinedSchemaElement()) { 1.92 + String location = element.getOwnerDocument().getDocumentURI(); 1.93 + String systemId = location + "#types?schema" + schemaElementCount++; 1.94 + if(forest.isMexMetadata) 1.95 + schemaCompiler.parseSchema(systemId,element); 1.96 + else 1.97 + new DOMForestScanner(forest).scan(element,schemaCompiler.getParserHandler(systemId)); 1.98 + } 1.99 + 1.100 + //feed external jaxb:bindings file 1.101 + InputSource[] externalBindings = options.getSchemaBindings(); 1.102 + if(externalBindings != null){ 1.103 + for(InputSource jaxbBinding : externalBindings){ 1.104 + schemaCompiler.parseSchema(jaxbBinding); 1.105 + } 1.106 + } 1.107 + } catch (Exception e) { 1.108 + throw new ModelException(e); 1.109 + } 1.110 + } 1.111 + 1.112 + public JAXBType getJAXBType(QName qname){ 1.113 + JAXBMapping mapping = jaxbModel.get(qname); 1.114 + if (mapping == null){ 1.115 + return null; 1.116 + } 1.117 + JavaType javaType = new JavaSimpleType(mapping.getType()); 1.118 + return new JAXBType(qname, javaType, mapping, jaxbModel); 1.119 + } 1.120 + 1.121 + public TypeAndAnnotation getElementTypeAndAnn(QName qname){ 1.122 + JAXBMapping mapping = jaxbModel.get(qname); 1.123 + if (mapping == null){ 1.124 + return null; 1.125 + } 1.126 + return mapping.getType().getTypeAnn(); 1.127 + } 1.128 + 1.129 + protected void bind(){ 1.130 + S2JJAXBModel rawJaxbModel = schemaCompiler.bind(); 1.131 + if(rawJaxbModel == null) 1.132 + throw new AbortException(); 1.133 + options.setCodeModel(rawJaxbModel.generateCode(null, errReceiver)); 1.134 + jaxbModel = new JAXBModel(rawJaxbModel); 1.135 + jaxbModel.setGeneratedClassNames(_classNameAllocator.getJaxbGeneratedClasses()); 1.136 + } 1.137 + 1.138 + protected SchemaCompiler getJAXBSchemaCompiler(){ 1.139 + return schemaCompiler; 1.140 + } 1.141 + 1.142 + public JAXBModel getJAXBModel(){ 1.143 + return jaxbModel; 1.144 + } 1.145 + 1.146 + private JAXBModel jaxbModel; 1.147 + private SchemaCompiler schemaCompiler; 1.148 + private final ClassNameAllocatorImpl _classNameAllocator; 1.149 + protected static final LocatorImpl NULL_LOCATOR = new LocatorImpl(); 1.150 + 1.151 +}