001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.tools.internal.ws.processor.modeler.wsdl;
026:
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Properties;
030: import java.util.Set;
031:
032: import javax.xml.namespace.QName;
033:
034: import org.w3c.dom.Element;
035: import org.xml.sax.InputSource;
036: import org.xml.sax.SAXParseException;
037:
038: import com.sun.tools.internal.xjc.api.ErrorListener;
039: import com.sun.tools.internal.xjc.api.SchemaCompiler;
040: import com.sun.tools.internal.xjc.api.XJC;
041: import com.sun.tools.internal.xjc.api.TypeAndAnnotation;
042: import com.sun.tools.internal.ws.processor.ProcessorOptions;
043: import com.sun.tools.internal.ws.processor.config.ModelInfo;
044: import com.sun.tools.internal.ws.processor.config.WSDLModelInfo;
045: import com.sun.tools.internal.ws.processor.model.ModelException;
046: import com.sun.tools.internal.ws.processor.model.java.JavaSimpleType;
047: import com.sun.tools.internal.ws.processor.model.java.JavaType;
048: import com.sun.tools.internal.ws.processor.model.jaxb.JAXBMapping;
049: import com.sun.tools.internal.ws.processor.model.jaxb.JAXBModel;
050: import com.sun.tools.internal.ws.processor.model.jaxb.JAXBType;
051: import com.sun.tools.internal.ws.processor.modeler.JavaSimpleTypeCreator;
052: import com.sun.tools.internal.ws.processor.util.ClassNameCollector;
053: import com.sun.tools.internal.ws.processor.util.ProcessorEnvironment;
054: import com.sun.xml.internal.ws.util.JAXWSUtils;
055: import com.sun.xml.internal.ws.util.localization.LocalizableMessageFactory;
056:
057: /**
058: * @author Kathy Walsh, Vivek Pandey
059: *
060: * Uses JAXB XJC apis to build JAXBModel and resolves xml to java type mapping from JAXBModel
061: */
062: public class JAXBModelBuilder {
063: public JAXBModelBuilder(ModelInfo modelInfo, Properties options,
064: ClassNameCollector classNameCollector, List elements) {
065: _messageFactory = new LocalizableMessageFactory(
066: "com.sun.tools.internal.ws.resources.model");
067: _modelInfo = modelInfo;
068: _env = (ProcessorEnvironment) modelInfo.getParent()
069: .getEnvironment();
070: _classNameAllocator = new ClassNameAllocatorImpl(
071: classNameCollector);
072:
073: printstacktrace = Boolean
074: .valueOf(options
075: .getProperty(ProcessorOptions.PRINT_STACK_TRACE_PROPERTY));
076: consoleErrorReporter = new ConsoleErrorReporter(_env, false);
077: internalBuildJAXBModel(elements);
078: }
079:
080: /**
081: * Builds model from WSDL document. Model contains abstraction which is used by the
082: * generators to generate the stub/tie/serializers etc. code.
083: *
084: * @see com.sun.tools.internal.ws.processor.modeler.Modeler#buildModel()
085: */
086:
087: private void internalBuildJAXBModel(List elements) {
088: try {
089: schemaCompiler = XJC.createSchemaCompiler();
090: schemaCompiler.setClassNameAllocator(_classNameAllocator);
091: schemaCompiler.setErrorListener(consoleErrorReporter);
092: schemaCompiler.setEntityResolver(_modelInfo
093: .getEntityResolver());
094: int schemaElementCount = 1;
095: for (Iterator iter = elements.iterator(); iter.hasNext();) {
096: Element schemaElement = (Element) iter.next();
097: String location = schemaElement.getOwnerDocument()
098: .getDocumentURI();
099: String systemId = new String(location + "#types?schema"
100: + schemaElementCount++);
101: schemaCompiler.parseSchema(systemId, schemaElement);
102: }
103:
104: //feed external jaxb:bindings file
105: Set<InputSource> externalBindings = ((WSDLModelInfo) _modelInfo)
106: .getJAXBBindings();
107: if (externalBindings != null) {
108: for (InputSource jaxbBinding : externalBindings) {
109: schemaCompiler.parseSchema(jaxbBinding);
110: }
111: }
112: } catch (Exception e) {
113: throw new ModelException(e);
114: }
115: }
116:
117: public JAXBType getJAXBType(QName qname) {
118: JAXBMapping mapping = jaxbModel.get(qname);
119: if (mapping == null) {
120: fail("model.schema.elementNotFound", new Object[] { qname });
121: }
122:
123: JavaType javaType = new JavaSimpleType(mapping.getType());
124: JAXBType type = new JAXBType(qname, javaType, mapping,
125: jaxbModel);
126: return type;
127: }
128:
129: public TypeAndAnnotation getElementTypeAndAnn(QName qname) {
130: JAXBMapping mapping = jaxbModel.get(qname);
131: if (mapping == null) {
132: fail("model.schema.elementNotFound", new Object[] { qname });
133: }
134: return mapping.getType().getTypeAnn();
135: }
136:
137: protected void bind() {
138: com.sun.tools.internal.xjc.api.JAXBModel rawJaxbModel = schemaCompiler
139: .bind();
140: if (consoleErrorReporter.hasError()) {
141: throw new ModelException(consoleErrorReporter
142: .getException());
143: }
144: jaxbModel = new JAXBModel(rawJaxbModel);
145: jaxbModel.setGeneratedClassNames(_classNameAllocator
146: .getJaxbGeneratedClasses());
147: }
148:
149: protected SchemaCompiler getJAXBSchemaCompiler() {
150: return schemaCompiler;
151: }
152:
153: protected void fail(String key, Object[] arg) {
154: throw new ModelException(key, arg);
155: }
156:
157: protected void error(String key, Object[] args) {
158: _env.error(_messageFactory.getMessage(key, args));
159: }
160:
161: protected void warn(String key, Object[] args) {
162: _env.warn(_messageFactory.getMessage(key, args));
163: }
164:
165: protected void inform(String key, Object[] args) {
166: _env.info(_messageFactory.getMessage(key, args));
167: }
168:
169: public JAXBModel getJAXBModel() {
170: return jaxbModel;
171: }
172:
173: private JAXBModel jaxbModel;
174: private SchemaCompiler schemaCompiler;
175: private final LocalizableMessageFactory _messageFactory;
176: private final ModelInfo _modelInfo;
177: private final ProcessorEnvironment _env;
178: private final boolean printstacktrace;
179: private final ClassNameAllocatorImpl _classNameAllocator;
180: private final ConsoleErrorReporter consoleErrorReporter;
181: }
|