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.TypeMapper;
025: import org.apache.axis2.wsdl.i18n.CodegenMessages;
026: import org.apache.axis2.wsdl.util.ConfigPropertyFileLoader;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029:
030: import javax.xml.parsers.DocumentBuilder;
031: import javax.xml.parsers.DocumentBuilderFactory;
032: import javax.xml.parsers.ParserConfigurationException;
033: import java.io.InputStream;
034: import java.lang.reflect.Method;
035: import java.util.ArrayList;
036: import java.util.Iterator;
037: import java.util.List;
038:
039: public class XMLBeansExtension extends AbstractDBProcessingExtension {
040: /** Name of "extra" option used to supply package name for xsb files. */
041: public static final String TYPESYSTEMNAME_OPTION = "typesystemname";
042: public static final String SCHEMA_FOLDER = "schemas";
043: public static final String XSDCONFIG_OPTION = "xc";
044: public static final String XSDCONFIG_OPTION_LONG = "xsdconfig";
045:
046: public static String MAPPINGS = "mappings";
047: public static String MAPPING = "mapping";
048: public static String MESSAGE = "message";
049: public static String JAVA_NAME = "javaclass";
050:
051: public static final String MAPPING_FOLDER = "Mapping";
052: public static final String MAPPER_FILE_NAME = "mapper";
053: public static final String SCHEMA_PATH = "/org/apache/axis2/wsdl/codegen/schema/";
054:
055: public static final String XMLBEANS_CONFIG_CLASS = "org.apache.xmlbeans.BindingConfig";
056: public static final String XMLBEANS_UTILITY_CLASS = "org.apache.axis2.xmlbeans.CodeGenerationUtility";
057: public static final String XMLBEANS_PROCESS_METHOD = "processSchemas";
058:
059: boolean debug = false;
060:
061: public void engage(CodeGenConfiguration configuration) {
062:
063: //test the databinding type. If not just fall through
064: if (testFallThrough(configuration.getDatabindingType())) {
065: return;
066: }
067:
068: // check the JiBX binding definition file specified
069: String typeSystemName = (String) configuration.getProperties()
070: .get(TYPESYSTEMNAME_OPTION);
071:
072: try {
073: // try dummy load of framework class first to check missing jars
074: try {
075: getClass().getClassLoader().loadClass(
076: XMLBEANS_CONFIG_CLASS);
077: } catch (ClassNotFoundException e) {
078: throw new RuntimeException(
079: "XMLBeans framework jars not in classpath");
080: }
081:
082: // load the actual utility class
083: Class clazz = null;
084: try {
085: clazz = getClass().getClassLoader().loadClass(
086: XMLBEANS_UTILITY_CLASS);
087: } catch (ClassNotFoundException e) {
088: throw new RuntimeException(
089: "XMLBeans binding extension not in classpath");
090: }
091:
092: // invoke utility class method for actual processing
093: Method method = clazz.getMethod(XMLBEANS_PROCESS_METHOD,
094: new Class[] { List.class, Element[].class,
095: CodeGenConfiguration.class, String.class });
096: List schemas = new ArrayList();
097: List axisServices = configuration.getAxisServices();
098: AxisService axisService = null;
099: for (Iterator iter = axisServices.iterator(); iter
100: .hasNext();) {
101: axisService = (AxisService) iter.next();
102: schemas.addAll(axisService.getSchema());
103: }
104:
105: Element[] additionalSchemas = loadAdditionalSchemas();
106: TypeMapper mapper = (TypeMapper) method.invoke(null,
107: new Object[] { schemas, additionalSchemas,
108: configuration, typeSystemName });
109:
110: // set the type mapper to the config
111: configuration.setTypeMapper(mapper);
112:
113: } catch (Exception e) {
114: if (e instanceof RuntimeException) {
115: throw (RuntimeException) e;
116: } else {
117: throw new RuntimeException(e);
118: }
119: }
120: }
121:
122: /**
123: * Loading the external schemas.
124: *
125: * @return element array consisting of the the DOM element objects that represent schemas
126: */
127: private Element[] loadAdditionalSchemas() {
128: //load additional schemas
129: String[] schemaNames = ConfigPropertyFileLoader
130: .getThirdPartySchemaNames();
131: Element[] schemaElements;
132:
133: try {
134: ArrayList additionalSchemaElements = new ArrayList();
135: DocumentBuilder documentBuilder = getNamespaceAwareDocumentBuilder();
136: for (int i = 0; i < schemaNames.length; i++) {
137: //the location for the third party schema;s is hardcoded
138: if (!"".equals(schemaNames[i].trim())) {
139: InputStream schemaStream = this .getClass()
140: .getResourceAsStream(
141: SCHEMA_PATH + schemaNames[i]);
142: Document doc = documentBuilder.parse(schemaStream);
143: additionalSchemaElements.add(doc
144: .getDocumentElement());
145: }
146: }
147:
148: //Create the Schema element array
149: schemaElements = new Element[additionalSchemaElements
150: .size()];
151: for (int i = 0; i < additionalSchemaElements.size(); i++) {
152: schemaElements[i] = (Element) additionalSchemaElements
153: .get(i);
154:
155: }
156: } catch (Exception e) {
157: throw new RuntimeException(CodegenMessages
158: .getMessage("extension.additionalSchemaFailure"), e);
159: }
160:
161: return schemaElements;
162: }
163:
164: private DocumentBuilder getNamespaceAwareDocumentBuilder()
165: throws ParserConfigurationException {
166: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
167: .newInstance();
168: documentBuilderFactory.setNamespaceAware(true);
169: return documentBuilderFactory.newDocumentBuilder();
170: }
171: }
|