Author: dims Date: Tue Jun 5 13:47:45 2007 New Revision: 544626 URL: http://svn.apache.org/viewvc?view=rev&rev=544626 Log: initial checkin for a skeleton jaxb schema generator. the idea is to collect the list of all the classes corresponding to the parameters and return types etc for all the methods, generate their schema using jaxbri schemagen and then call DefaultSchemaGenerator's generateSchemas to add whatever else is needed for the element wrappers for each method etc.
Added: webservices/axis2/trunk/java/modules/jaxbri/src/org/apache/axis2/jaxbri/JaxbSchemaGenerator.java Modified: webservices/axis2/trunk/java/modules/jaxbri/project.properties Modified: webservices/axis2/trunk/java/modules/jaxbri/project.properties URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxbri/project.properties?view=diff&rev=544626&r1=544625&r2=544626 ============================================================================== --- webservices/axis2/trunk/java/modules/jaxbri/project.properties (original) +++ webservices/axis2/trunk/java/modules/jaxbri/project.properties Tue Jun 5 13:47:45 2007 @@ -15,3 +15,5 @@ # ------------------------------------------------------------------- maven.multiproject.type=jar +maven.compile.target=1.5 +maven.compile.source=1.5 \ No newline at end of file Added: webservices/axis2/trunk/java/modules/jaxbri/src/org/apache/axis2/jaxbri/JaxbSchemaGenerator.java URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxbri/src/org/apache/axis2/jaxbri/JaxbSchemaGenerator.java?view=auto&rev=544626 ============================================================================== --- webservices/axis2/trunk/java/modules/jaxbri/src/org/apache/axis2/jaxbri/JaxbSchemaGenerator.java (added) +++ webservices/axis2/trunk/java/modules/jaxbri/src/org/apache/axis2/jaxbri/JaxbSchemaGenerator.java Tue Jun 5 13:47:45 2007 @@ -0,0 +1,196 @@ +package org.apache.axis2.jaxbri; + +import org.apache.axis2.description.java2wsdl.DefaultSchemaGenerator; +import org.apache.axis2.util.Loader; +import org.apache.ws.commons.schema.XmlSchema; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.SchemaOutputResolver; +import javax.xml.namespace.QName; +import javax.xml.transform.Result; +import javax.xml.transform.dom.DOMResult; +import java.io.IOException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/* +* Copyright 2004,2005 The Apache Software Foundation. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ +public class JaxbSchemaGenerator extends DefaultSchemaGenerator { + public JaxbSchemaGenerator(ClassLoader loader, String className, + String schematargetNamespace, + String schematargetNamespacePrefix) + throws Exception { + super(loader, className, schematargetNamespace, schematargetNamespacePrefix); + } + + public Collection generateSchema() throws Exception { + Set<Class<?>> classes = new HashSet<Class<?>>(); + classes.addAll(processMethods(serviceClass.getMethods())); + + if (extraClasses != null) { + for (Object extraClass : extraClasses) { + classes.add(Loader.loadClass(classLoader, (String) extraClass)); + } + } + + JAXBContext context = createJAXBContext(classes, + (isUseWSDLTypesNamespace() ? ((String) pkg2nsmap.get("all")) : null)); + + for (DOMResult r : generateJaxbSchemas(context)) { + Document d = (Document) r.getNode(); + String targetNamespace = d.getDocumentElement().getAttribute("targetNamespace"); + if ("".equals(targetNamespace)) { + targetNamespace = this.getSchemaTargetNameSpace(); + d.getDocumentElement().setAttribute("targetNamespace", targetNamespace); + } + + NodeList nodes = d.getDocumentElement().getChildNodes(); + for (int i = 0; i < nodes.getLength(); i++) { + Node n = nodes.item(i); + if (n instanceof Element) { + Element e = (Element) n; + if (e.getLocalName().equals("import")) { + d.getDocumentElement().removeChild(e); + } + } + } + + XmlSchema xmlSchema = xmlSchemaCollection.read(d.getDocumentElement()); + schemaMap.put(targetNamespace, xmlSchema); + } + + return super.generateSchema(); + } + + private List<Class<?>> processMethods(Method[] declaredMethods) throws Exception { + List<Class<?>> list = new ArrayList<Class<?>>(); + + for (int i = 0; i < declaredMethods.length; i++) { + Method jMethod = declaredMethods[i]; + + if (jMethod.getExceptionTypes().length > 0) { + Class[] extypes = jMethod.getExceptionTypes(); + for (int j = 0; j < extypes.length; j++) { + Class extype = extypes[j]; + if(!extype.getName().startsWith("java")||extype.getName().startsWith("javax.")){ + list.add(extype); + } + generateSchemaForType(list, extype); + } + } + Class[] paras = jMethod.getParameterTypes(); + for (int j = 0; j < paras.length; j++) { + Class paraType = paras[j]; + generateSchemaForType(list, paraType); + } + // for its return type + Class returnType = jMethod.getReturnType(); + + if (!(returnType == void.class)) { + generateSchemaForType(list, returnType); + + } + } + return list; + } + + private void generateSchemaForType(List<Class<?>> list, Class type) + throws Exception { + + boolean isArrayType = false; + if (type != null) { + isArrayType = type.isArray(); + } + if (isArrayType) { + type = type.getComponentType(); + } + String classTypeName; + if (type == null) { + classTypeName = "java.lang.Object"; + } else { + classTypeName = type.getName(); + } + if (isArrayType && "byte".equals(classTypeName)) { + classTypeName = "base64Binary"; + isArrayType = false; + } + if ("javax.activation.DataHandler".equals(classTypeName)) { + classTypeName = "base64Binary"; + } + QName schemaTypeName = typeTable.getSimpleSchemaTypeName(classTypeName); + if (schemaTypeName == null) { + list.add(type); + } + } + + protected List<DOMResult> generateJaxbSchemas(JAXBContext context) throws IOException { + final List<DOMResult> results = new ArrayList<DOMResult>(); + + context.generateSchema(new SchemaOutputResolver() { + @Override + public Result createOutput(String ns, String file) throws IOException { + DOMResult result = new DOMResult(); + result.setSystemId(file); + results.add(result); + return result; + } + }); + + return results; + } + + protected static JAXBContext createJAXBContext(Set<Class<?>> classes, + String defaultNs) throws JAXBException { + Iterator it = classes.iterator(); + String className = ""; + Object remoteExceptionObject; + while (it.hasNext()) { + remoteExceptionObject = it.next(); + className = remoteExceptionObject.toString(); + if (!("".equals(className)) && className.contains("RemoteException")) { + classes.remove(remoteExceptionObject); + } + } + + for (Class<?> cls : classes) { + if (cls.getName().endsWith("ObjectFactory")) { + //kind of a hack, but ObjectFactories may be created with empty namespaces + defaultNs = null; + } + } + + Map<String, Object> map = new HashMap<String, Object>(); + if (defaultNs != null) { + map.put("com.sun.xml.bind.defaultNamespaceRemap", defaultNs); + } + + return JAXBContext.newInstance(classes.toArray(new Class[classes.size()]), map); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]