001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.tools.ws.processor.modeler.wsdl;
037:
038: import com.sun.tools.ws.processor.model.ModelException;
039: import com.sun.tools.ws.processor.model.java.JavaSimpleType;
040: import com.sun.tools.ws.processor.model.java.JavaType;
041: import com.sun.tools.ws.processor.model.jaxb.JAXBMapping;
042: import com.sun.tools.ws.processor.model.jaxb.JAXBModel;
043: import com.sun.tools.ws.processor.model.jaxb.JAXBType;
044: import com.sun.tools.ws.processor.util.ClassNameCollector;
045: import com.sun.tools.ws.wscompile.AbortException;
046: import com.sun.tools.ws.wscompile.ErrorReceiver;
047: import com.sun.tools.ws.wscompile.WsimportOptions;
048: import com.sun.tools.ws.wsdl.parser.DOMForestScanner;
049: import com.sun.tools.ws.wsdl.parser.MetadataFinder;
050: import com.sun.tools.xjc.api.S2JJAXBModel;
051: import com.sun.tools.xjc.api.SchemaCompiler;
052: import com.sun.tools.xjc.api.TypeAndAnnotation;
053: import org.w3c.dom.Element;
054: import org.xml.sax.InputSource;
055: import org.xml.sax.helpers.LocatorImpl;
056:
057: import javax.xml.namespace.QName;
058:
059: /**
060: * @author Vivek Pandey
061: *
062: * Uses JAXB XJC apis to build JAXBModel and resolves xml to java type mapping from JAXBModel
063: */
064: public class JAXBModelBuilder {
065:
066: private final ErrorReceiver errReceiver;
067: private final WsimportOptions options;
068: private final MetadataFinder forest;
069:
070: public JAXBModelBuilder(WsimportOptions options,
071: ClassNameCollector classNameCollector,
072: MetadataFinder finder, ErrorReceiver errReceiver) {
073: this ._classNameAllocator = new ClassNameAllocatorImpl(
074: classNameCollector);
075: this .errReceiver = errReceiver;
076: this .options = options;
077: this .forest = finder;
078:
079: internalBuildJAXBModel();
080: }
081:
082: /**
083: * Builds model from WSDL document. Model contains abstraction which is used by the
084: * generators to generate the stub/tie/serializers etc. code.
085: *
086: * @see com.sun.tools.ws.processor.modeler.Modeler#buildModel()
087: */
088:
089: private void internalBuildJAXBModel() {
090: try {
091: schemaCompiler = options.getSchemaCompiler();
092: schemaCompiler.resetSchema();
093: schemaCompiler.setEntityResolver(options.entityResolver);
094: schemaCompiler.setClassNameAllocator(_classNameAllocator);
095: schemaCompiler.setErrorListener(errReceiver);
096: int schemaElementCount = 1;
097:
098: for (Element element : forest.getInlinedSchemaElement()) {
099: String location = element.getOwnerDocument()
100: .getDocumentURI();
101: String systemId = location + "#types?schema"
102: + schemaElementCount++;
103: if (forest.isMexMetadata)
104: schemaCompiler.parseSchema(systemId, element);
105: else
106: new DOMForestScanner(forest).scan(element,
107: schemaCompiler.getParserHandler(systemId));
108: }
109:
110: //feed external jaxb:bindings file
111: InputSource[] externalBindings = options
112: .getSchemaBindings();
113: if (externalBindings != null) {
114: for (InputSource jaxbBinding : externalBindings) {
115: schemaCompiler.parseSchema(jaxbBinding);
116: }
117: }
118: } catch (Exception e) {
119: throw new ModelException(e);
120: }
121: }
122:
123: public JAXBType getJAXBType(QName qname) {
124: JAXBMapping mapping = jaxbModel.get(qname);
125: if (mapping == null) {
126: return null;
127: }
128: JavaType javaType = new JavaSimpleType(mapping.getType());
129: return new JAXBType(qname, javaType, mapping, jaxbModel);
130: }
131:
132: public TypeAndAnnotation getElementTypeAndAnn(QName qname) {
133: JAXBMapping mapping = jaxbModel.get(qname);
134: if (mapping == null) {
135: return null;
136: }
137: return mapping.getType().getTypeAnn();
138: }
139:
140: protected void bind() {
141: S2JJAXBModel rawJaxbModel = schemaCompiler.bind();
142: if (rawJaxbModel == null)
143: throw new AbortException();
144: options.setCodeModel(rawJaxbModel.generateCode(null,
145: errReceiver));
146: jaxbModel = new JAXBModel(rawJaxbModel);
147: jaxbModel.setGeneratedClassNames(_classNameAllocator
148: .getJaxbGeneratedClasses());
149: }
150:
151: protected SchemaCompiler getJAXBSchemaCompiler() {
152: return schemaCompiler;
153: }
154:
155: public JAXBModel getJAXBModel() {
156: return jaxbModel;
157: }
158:
159: private JAXBModel jaxbModel;
160: private SchemaCompiler schemaCompiler;
161: private final ClassNameAllocatorImpl _classNameAllocator;
162: protected static final LocatorImpl NULL_LOCATOR = new LocatorImpl();
163:
164: }
|