325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.ws.processor.modeler.wsdl;
325N/A
325N/Aimport com.sun.tools.internal.ws.processor.model.ModelException;
325N/Aimport com.sun.tools.internal.ws.processor.model.java.JavaSimpleType;
325N/Aimport com.sun.tools.internal.ws.processor.model.java.JavaType;
325N/Aimport com.sun.tools.internal.ws.processor.model.jaxb.JAXBMapping;
325N/Aimport com.sun.tools.internal.ws.processor.model.jaxb.JAXBModel;
325N/Aimport com.sun.tools.internal.ws.processor.model.jaxb.JAXBType;
325N/Aimport com.sun.tools.internal.ws.processor.util.ClassNameCollector;
325N/Aimport com.sun.tools.internal.ws.wscompile.AbortException;
325N/Aimport com.sun.tools.internal.ws.wscompile.ErrorReceiver;
325N/Aimport com.sun.tools.internal.ws.wscompile.WsimportOptions;
325N/Aimport com.sun.tools.internal.ws.wsdl.parser.DOMForestScanner;
325N/Aimport com.sun.tools.internal.ws.wsdl.parser.MetadataFinder;
325N/Aimport com.sun.tools.internal.xjc.api.S2JJAXBModel;
325N/Aimport com.sun.tools.internal.xjc.api.SchemaCompiler;
325N/Aimport com.sun.tools.internal.xjc.api.TypeAndAnnotation;
325N/Aimport org.w3c.dom.Element;
325N/Aimport org.xml.sax.InputSource;
325N/Aimport org.xml.sax.helpers.LocatorImpl;
325N/A
325N/Aimport javax.xml.namespace.QName;
325N/A
325N/A/**
325N/A * @author Vivek Pandey
325N/A *
325N/A * Uses JAXB XJC apis to build JAXBModel and resolves xml to java type mapping from JAXBModel
325N/A */
325N/Apublic class JAXBModelBuilder {
325N/A
325N/A private final ErrorReceiver errReceiver;
325N/A private final WsimportOptions options;
325N/A private final MetadataFinder forest;
325N/A
325N/A public JAXBModelBuilder(WsimportOptions options, ClassNameCollector classNameCollector, MetadataFinder finder, ErrorReceiver errReceiver) {
325N/A this._classNameAllocator = new ClassNameAllocatorImpl(classNameCollector);
325N/A this.errReceiver = errReceiver;
325N/A this.options = options;
325N/A this.forest = finder;
325N/A
325N/A internalBuildJAXBModel();
325N/A }
325N/A
325N/A /**
325N/A * Builds model from WSDL document. Model contains abstraction which is used by the
325N/A * generators to generate the stub/tie/serializers etc. code.
325N/A *
325N/A * @see com.sun.tools.internal.ws.processor.modeler.Modeler#buildModel()
325N/A */
325N/A
325N/A private void internalBuildJAXBModel(){
325N/A try {
325N/A schemaCompiler = options.getSchemaCompiler();
325N/A schemaCompiler.resetSchema();
325N/A if(options.entityResolver != null) {
325N/A //set if its not null so as not to override catalog option specified via xjc args
325N/A schemaCompiler.setEntityResolver(options.entityResolver);
325N/A }
325N/A schemaCompiler.setClassNameAllocator(_classNameAllocator);
325N/A schemaCompiler.setErrorListener(errReceiver);
325N/A int schemaElementCount = 1;
325N/A
325N/A for (Element element : forest.getInlinedSchemaElement()) {
325N/A String location = element.getOwnerDocument().getDocumentURI();
325N/A String systemId = location + "#types?schema" + schemaElementCount++;
325N/A if(forest.isMexMetadata)
325N/A schemaCompiler.parseSchema(systemId,element);
325N/A else
325N/A new DOMForestScanner(forest).scan(element,schemaCompiler.getParserHandler(systemId));
325N/A }
325N/A
325N/A //feed external jaxb:bindings file
325N/A InputSource[] externalBindings = options.getSchemaBindings();
325N/A if(externalBindings != null){
325N/A for(InputSource jaxbBinding : externalBindings){
325N/A schemaCompiler.parseSchema(jaxbBinding);
325N/A }
325N/A }
325N/A } catch (Exception e) {
325N/A throw new ModelException(e);
325N/A }
325N/A }
325N/A
325N/A public JAXBType getJAXBType(QName qname){
325N/A JAXBMapping mapping = jaxbModel.get(qname);
325N/A if (mapping == null){
325N/A return null;
325N/A }
325N/A JavaType javaType = new JavaSimpleType(mapping.getType());
325N/A return new JAXBType(qname, javaType, mapping, jaxbModel);
325N/A }
325N/A
325N/A public TypeAndAnnotation getElementTypeAndAnn(QName qname){
325N/A JAXBMapping mapping = jaxbModel.get(qname);
325N/A if (mapping == null){
325N/A return null;
325N/A }
325N/A return mapping.getType().getTypeAnn();
325N/A }
325N/A
325N/A protected void bind(){
325N/A S2JJAXBModel rawJaxbModel = schemaCompiler.bind();
325N/A if(rawJaxbModel == null)
325N/A throw new AbortException();
325N/A options.setCodeModel(rawJaxbModel.generateCode(null, errReceiver));
325N/A jaxbModel = new JAXBModel(rawJaxbModel);
325N/A jaxbModel.setGeneratedClassNames(_classNameAllocator.getJaxbGeneratedClasses());
325N/A }
325N/A
325N/A protected SchemaCompiler getJAXBSchemaCompiler(){
325N/A return schemaCompiler;
325N/A }
325N/A
325N/A public JAXBModel getJAXBModel(){
325N/A return jaxbModel;
325N/A }
325N/A
325N/A private JAXBModel jaxbModel;
325N/A private SchemaCompiler schemaCompiler;
325N/A private final ClassNameAllocatorImpl _classNameAllocator;
325N/A protected static final LocatorImpl NULL_LOCATOR = new LocatorImpl();
325N/A
325N/A}