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