001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.wsdl.codegen.extension;
021:
022: import org.apache.axis2.description.AxisService;
023: import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
024: import org.apache.axis2.wsdl.databinding.DefaultTypeMapper;
025: import org.apache.axis2.wsdl.databinding.JavaTypeMapper;
026: import org.apache.axis2.wsdl.databinding.TypeMapper;
027: import org.apache.axis2.wsdl.i18n.CodegenMessages;
028: import org.apache.axis2.wsdl.util.ConfigPropertyFileLoader;
029: import org.apache.ws.commons.schema.XmlSchema;
030: import org.apache.ws.jaxme.generator.impl.GeneratorImpl;
031: import org.apache.ws.jaxme.generator.sg.GroupSG;
032: import org.apache.ws.jaxme.generator.sg.ObjectSG;
033: import org.apache.ws.jaxme.generator.sg.SchemaSG;
034: import org.apache.ws.jaxme.generator.sg.TypeSG;
035: import org.apache.ws.jaxme.generator.sg.impl.JAXBSchemaReader;
036: import org.apache.ws.jaxme.js.JavaQName;
037: import org.apache.ws.jaxme.xs.xml.XsQName;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Element;
040: import org.xml.sax.InputSource;
041:
042: import javax.xml.namespace.QName;
043: import javax.xml.parsers.DocumentBuilder;
044: import javax.xml.parsers.DocumentBuilderFactory;
045: import javax.xml.parsers.ParserConfigurationException;
046: import java.io.ByteArrayOutputStream;
047: import java.io.File;
048: import java.io.InputStream;
049: import java.io.StringReader;
050: import java.util.ArrayList;
051: import java.util.Iterator;
052: import java.util.List;
053: import java.util.Vector;
054:
055: public class JaxMeExtension extends AbstractDBProcessingExtension {
056: public static final String SCHEMA_FOLDER = "schemas";
057:
058: public static String MAPPINGS = "mappings";
059: public static String MAPPING = "mapping";
060: public static String MESSAGE = "message";
061: public static String JAVA_NAME = "javaclass";
062:
063: public static final String MAPPING_FOLDER = "Mapping";
064: public static final String MAPPER_FILE_NAME = "mapper";
065: public static final String SCHEMA_PATH = "/org/apache/axis2/wsdl/codegen/schema/";
066:
067: boolean debug = false;
068:
069: public void engage(CodeGenConfiguration configuration) {
070:
071: //test the databinding type. If not just fall through
072: if (testFallThrough(configuration.getDatabindingType())) {
073: return;
074: }
075:
076: try {
077: //get the types from the types section
078: List typesList = new ArrayList();
079: List axisServices = configuration.getAxisServices();
080: AxisService axisService = null;
081: for (Iterator iter = axisServices.iterator(); iter
082: .hasNext();) {
083: axisService = (AxisService) iter.next();
084: typesList.addAll(axisService.getSchema());
085: }
086:
087: //check for the imported types. Any imported types are supposed to be here also
088: if (typesList == null || typesList.isEmpty()) {
089: //there are no types to be code generated
090: //However if the type mapper is left empty it will be a problem for the other
091: //processes. Hence the default type mapper is set to the configuration
092: configuration.setTypeMapper(new DefaultTypeMapper());
093: return;
094: }
095:
096: Vector xmlObjectsVector = new Vector();
097: //create the type mapper
098: //First try to take the one that is already there
099: TypeMapper mapper = configuration.getTypeMapper();
100: if (mapper == null) {
101: mapper = new JavaTypeMapper();
102: }
103:
104: for (int i = 0; i < typesList.size(); i++) {
105: XmlSchema schema = (XmlSchema) typesList.get(i);
106: xmlObjectsVector.add(new InputSource(new StringReader(
107: getSchemaAsString(schema))));
108: }
109:
110: // TODO: FIXME
111: // Element[] additionalSchemas = loadAdditionalSchemas();
112: // // Need to add the third party schemas
113: // for (int i = 0; i < additionalSchemas.length; i++) {
114: // String s = DOM2Writer.nodeToString(additionalSchemas[i]);
115: // xmlObjectsVector.add(new InputSource(new StringReader(s)));
116: // }
117:
118: File outputDir = new File(
119: configuration.getOutputLocation(), configuration
120: .getSourceLocation());
121:
122: JAXBSchemaReader reader = new JAXBSchemaReader();
123: reader.setSupportingExtensions(true);
124:
125: GeneratorImpl generator = new GeneratorImpl();
126: generator.setTargetDirectory(outputDir);
127: generator.setForcingOverwrite(false);
128: generator.setSchemaReader(reader);
129:
130: for (int i = 0; i < xmlObjectsVector.size(); i++) {
131: SchemaSG sg = generator
132: .generate((InputSource) xmlObjectsVector
133: .elementAt(i));
134: ObjectSG[] elements = sg.getElements();
135: for (int j = 0; j < elements.length; j++) {
136: XsQName qName = elements[j].getName();
137: JavaQName name = elements[j].getClassContext()
138: .getXMLInterfaceName();
139: mapper.addTypeMappingName(new QName(qName
140: .getNamespaceURI(), qName.getLocalName()),
141: name.getPackageName() + '.'
142: + name.getClassName());
143: }
144: TypeSG[] types = sg.getTypes();
145: for (int j = 0; j < types.length; j++) {
146: XsQName qName = types[j].getName();
147: JavaQName name = types[j].getRuntimeType();
148: mapper.addTypeMappingName(new QName(qName
149: .getNamespaceURI(), qName.getLocalName()),
150: name.getPackageName() + '.'
151: + name.getClassName());
152: }
153: GroupSG[] groups = sg.getGroups();
154: for (int j = 0; j < groups.length; j++) {
155: XsQName qName = groups[j].getName();
156: JavaQName name = groups[j].getClassContext()
157: .getXMLInterfaceName();
158: mapper.addTypeMappingName(new QName(qName
159: .getNamespaceURI(), qName.getLocalName()),
160: name.getPackageName() + '.'
161: + name.getClassName());
162: }
163: }
164:
165: //set the type mapper to the config
166: configuration.setTypeMapper(mapper);
167:
168: } catch (Exception e) {
169: throw new RuntimeException(e);
170: }
171:
172: }
173:
174: /**
175: * Loading the external schemas.
176: *
177: * @return element array consisting of the the DOM element objects that represent schemas
178: */
179: private Element[] loadAdditionalSchemas() {
180: //load additional schemas
181: String[] schemaNames = ConfigPropertyFileLoader
182: .getThirdPartySchemaNames();
183: Element[] schemaElements;
184:
185: try {
186: ArrayList additionalSchemaElements = new ArrayList();
187: DocumentBuilder documentBuilder = getNamespaceAwareDocumentBuilder();
188: for (int i = 0; i < schemaNames.length; i++) {
189: //the location for the third party schema;s is hardcoded
190: if (!"".equals(schemaNames[i].trim())) {
191: InputStream schemaStream = this .getClass()
192: .getResourceAsStream(
193: SCHEMA_PATH + schemaNames[i]);
194: Document doc = documentBuilder.parse(schemaStream);
195: additionalSchemaElements.add(doc
196: .getDocumentElement());
197: }
198: }
199:
200: //Create the Schema element array
201: schemaElements = new Element[additionalSchemaElements
202: .size()];
203: for (int i = 0; i < additionalSchemaElements.size(); i++) {
204: schemaElements[i] = (Element) additionalSchemaElements
205: .get(i);
206:
207: }
208: } catch (Exception e) {
209: throw new RuntimeException(CodegenMessages
210: .getMessage("extension.additionalSchemaFailure"), e);
211: }
212:
213: return schemaElements;
214: }
215:
216: private DocumentBuilder getNamespaceAwareDocumentBuilder()
217: throws ParserConfigurationException {
218: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
219: .newInstance();
220: documentBuilderFactory.setNamespaceAware(true);
221: return documentBuilderFactory.newDocumentBuilder();
222: }
223:
224: private String getSchemaAsString(XmlSchema schema) {
225: ByteArrayOutputStream baos = new ByteArrayOutputStream();
226: schema.write(baos);
227: return baos.toString();
228: }
229: }
|