Author: ema Date: Thu Jan 4 18:50:59 2007 New Revision: 492875 URL: http://svn.apache.org/viewvc?view=rev&rev=492875 Log: Added jaxbDatabinding in tools2
Added: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/ClassNameAllocatorImpl.java (with props) incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBBindErrorListener.java (with props) incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBDataBinding.java (with props) incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/Messages.properties (with props) incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/NamespaceContextImpl.java (with props) incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/TypesCodeWriter.java (with props) Added: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/ClassNameAllocatorImpl.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/ClassNameAllocatorImpl.java?view=auto&rev=492875 ============================================================================== --- incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/ClassNameAllocatorImpl.java (added) +++ incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/ClassNameAllocatorImpl.java Thu Jan 4 18:50:59 2007 @@ -0,0 +1,65 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +package org.apache.cxf.tools.wsdlto.databinding.jaxb; + +import javax.xml.namespace.QName; + +import com.sun.tools.xjc.api.ClassNameAllocator; + +import org.apache.cxf.service.model.InterfaceInfo; +import org.apache.cxf.tools.util.ClassCollector; +import org.apache.cxf.tools.util.ProcessorUtil; + +public class ClassNameAllocatorImpl implements ClassNameAllocator { + private static final String TYPE_SUFFIX = "_Type"; + private ClassCollector collector; + public ClassNameAllocatorImpl(ClassCollector classCollector) { + collector = classCollector; + } + + private boolean isNameCollision(String packageName, String className) { + return collector.containSeiClass(packageName, className); + } + + public String assignClassName(String packageName, String className) { + String fullClzName = className; + if (isNameCollision(packageName, className)) { + fullClzName = className + TYPE_SUFFIX; + } + collector.addTypesClassName(packageName, className, packageName + "." + fullClzName); + return fullClzName; + } + + public void setInterface(InterfaceInfo seiInfo, String packageName) { + QName portType = seiInfo.getName(); + String ns = portType.getNamespaceURI(); + String type = portType.getLocalPart(); + String pkgName = ProcessorUtil.parsePackageName(ns, packageName); + String className = ProcessorUtil.mangleNameToClassName(type); + String fullClassName = pkgName + "." + className; + if (packageName == null) { + packageName = pkgName; + } + collector.addSeiClassName(packageName, className, fullClassName); + + } + + +} Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/ClassNameAllocatorImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/ClassNameAllocatorImpl.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBBindErrorListener.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBBindErrorListener.java?view=auto&rev=492875 ============================================================================== --- incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBBindErrorListener.java (added) +++ incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBBindErrorListener.java Thu Jan 4 18:50:59 2007 @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +package org.apache.cxf.tools.wsdlto.databinding.jaxb; +import com.sun.tools.xjc.api.ErrorListener; + +import org.apache.cxf.tools.common.ToolContext; +import org.apache.cxf.tools.common.ToolException; + + + +public class JAXBBindErrorListener implements ErrorListener { + private ToolContext env; + + public JAXBBindErrorListener(ToolContext penv) { + env = penv; + } + + public void error(org.xml.sax.SAXParseException exception) { + throw new ToolException(exception.getLocalizedMessage()); + + } + + public void fatalError(org.xml.sax.SAXParseException exception) { + throw new ToolException(exception.getLocalizedMessage()); + } + + public void info(org.xml.sax.SAXParseException exception) { + + } + + public void warning(org.xml.sax.SAXParseException exception) { + if (this.env.isVerbose()) { + System.err.println("Parsing schema warning " + exception.toString()); + } + } +} Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBBindErrorListener.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBBindErrorListener.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBDataBinding.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBDataBinding.java?view=auto&rev=492875 ============================================================================== --- incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBDataBinding.java (added) +++ incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBDataBinding.java Thu Jan 4 18:50:59 2007 @@ -0,0 +1,168 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.cxf.tools.wsdlto.databinding.jaxb; + + +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.List; +import java.util.Set; +import java.util.logging.Logger; + +import javax.wsdl.Definition; +import javax.xml.namespace.QName; + +import org.w3c.dom.Element; + +import org.xml.sax.InputSource; + +import com.sun.codemodel.JCodeModel; +import com.sun.tools.xjc.api.S2JJAXBModel; +import com.sun.tools.xjc.api.TypeAndAnnotation; +import com.sun.tools.xjc.api.XJC; +import com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl; + +import org.apache.cxf.common.i18n.Message; +import org.apache.cxf.common.logging.LogUtils; +import org.apache.cxf.common.util.StringUtils; +import org.apache.cxf.service.model.SchemaInfo; +import org.apache.cxf.service.model.ServiceInfo; +import org.apache.cxf.tools.common.ToolConstants; +import org.apache.cxf.tools.common.ToolContext; +import org.apache.cxf.tools.common.ToolException; +import org.apache.cxf.tools.util.ClassCollector; +import org.apache.cxf.tools.util.ProcessorUtil; +import org.apache.cxf.tools.wsdlto.core.DataBindingProfile; +public class JAXBDataBinding implements DataBindingProfile { + private static final Logger LOG = LogUtils.getL7dLogger(JAXBDataBinding.class); + protected S2JJAXBModel rawJaxbModelGenCode; + //private Model model; + private ToolContext env; + private ServiceInfo serviceInfo; + private Definition def; + + @SuppressWarnings("unchecked") + public void initialize(ToolContext penv) throws ToolException { + env = penv; + serviceInfo = (ServiceInfo)env.get(ServiceInfo.class); + def = (Definition)env.get(Definition.class); + + Set<InputSource> jaxbBindings = (Set<InputSource>)env.get(ToolConstants.NS_JAXB_BINDINGS); + + + SchemaCompilerImpl schemaCompiler = (SchemaCompilerImpl)XJC.createSchemaCompiler(); + + ClassCollector classCollector = env.get(ClassCollector.class); + ClassNameAllocatorImpl allocator = new ClassNameAllocatorImpl(classCollector); + allocator.setInterface(serviceInfo.getInterface(), env.mapPackageName(def.getTargetNamespace())); + schemaCompiler.setClassNameAllocator(allocator); + + JAXBBindErrorListener listener = new JAXBBindErrorListener(env); + schemaCompiler.setErrorListener(listener); + + + Collection<SchemaInfo> schemas = serviceInfo.getTypeInfo().getSchemas(); + + for (SchemaInfo schema : schemas) { + Element element = schema.getElement(); + String tns = element.getAttribute("targetNamespace"); + schemaCompiler.parseSchema(tns, element); + } + + for (InputSource binding : jaxbBindings) { + schemaCompiler.parseSchema(binding); + } + + rawJaxbModelGenCode = schemaCompiler.bind(); + addedEnumClassToCollector(schemas, allocator); + } + + + // JAXB bug. JAXB ClassNameCollector may not be invoked when generated + // class is an enum. We need to use this method to add the missed file + // to classCollector. + private void addedEnumClassToCollector(Collection<SchemaInfo> schemaList, + ClassNameAllocatorImpl allocator) { + for (SchemaInfo schema : schemaList) { + Element schemaElement = schema.getElement(); + String targetNamespace = schemaElement.getAttribute("targetNamespace"); + if (StringUtils.isEmpty(targetNamespace)) { + continue; + } + String packageName = ProcessorUtil.parsePackageName(targetNamespace, null); + if (!addedToClassCollector(packageName)) { + allocator.assignClassName(packageName, "*"); + } + } + } + + private boolean addedToClassCollector(String packageName) { + ClassCollector classCollector = env.get(ClassCollector.class); + List<String> files = (List<String>)classCollector.getGeneratedFileInfo(); + for (String file : files) { + int dotIndex = file.lastIndexOf("."); + String sub = file.substring(0, dotIndex - 1); + if (sub.equals(packageName)) { + return true; + } + } + return false; + } + + + + public void generate() throws ToolException { + if (rawJaxbModelGenCode == null) { + return; + } + + try { + String dir = (String)env.get(ToolConstants.CFG_OUTPUTDIR); + + TypesCodeWriter fileCodeWriter = new TypesCodeWriter(new File(dir), env.getExcludePkgList()); + + if (rawJaxbModelGenCode instanceof S2JJAXBModel) { + S2JJAXBModel schem2JavaJaxbModel = (S2JJAXBModel)rawJaxbModelGenCode; + //TO DO.... + // enable jaxb plugin + JCodeModel jcodeModel = schem2JavaJaxbModel.generateCode(null, null); + + jcodeModel.build(fileCodeWriter); + env.put(JCodeModel.class, jcodeModel); + for (String str : fileCodeWriter.getExcludeFileList()) { + env.getExcludeFileList().add(str); + } + } + return; + } catch (IOException e) { + Message msg = new Message("FAIL_TO_GENERATE_TYPES", LOG); + throw new ToolException(msg); + } + } + + public String getType(QName qname) { + TypeAndAnnotation typeAnno = rawJaxbModelGenCode.getJavaType(qname); + if (typeAnno.getTypeClass() != null) { + return typeAnno.getTypeClass().fullName(); + } + return null; + + } +} Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBDataBinding.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/JAXBDataBinding.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/Messages.properties URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/Messages.properties?view=auto&rev=492875 ============================================================================== --- incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/Messages.properties (added) +++ incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/Messages.properties Thu Jan 4 18:50:59 2007 @@ -0,0 +1,26 @@ +# +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +# +GENERATE_TYPES_ERROR = Generate types error +FAIL_TO_GENERATE_TYPES = Fail to generate types + + + + Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/Messages.properties ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/Messages.properties ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/Messages.properties ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/NamespaceContextImpl.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/NamespaceContextImpl.java?view=auto&rev=492875 ============================================================================== --- incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/NamespaceContextImpl.java (added) +++ incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/NamespaceContextImpl.java Thu Jan 4 18:50:59 2007 @@ -0,0 +1,65 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.cxf.tools.wsdlto.databinding.jaxb; +import java.io.File; +import java.io.IOException; +import java.util.Iterator; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import org.xml.sax.SAXException; + +public class NamespaceContextImpl implements NamespaceContext { + private Element element; + + public NamespaceContextImpl(File file) { + Document document = null; + DocumentBuilderFactory docFact = DocumentBuilderFactory.newInstance(); + docFact.setNamespaceAware(true); + try { + document = docFact.newDocumentBuilder().parse(file); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + element = document.getDocumentElement(); + } + + public String getNamespaceURI(String prefix) { + return element.lookupNamespaceURI(prefix); + + } + + public String getPrefix(String namespaceURI) { + return element.lookupPrefix(namespaceURI); + } + + public Iterator getPrefixes(String namespaceURI) { + return null; + } + +} Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/NamespaceContextImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/NamespaceContextImpl.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/TypesCodeWriter.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/TypesCodeWriter.java?view=auto&rev=492875 ============================================================================== --- incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/TypesCodeWriter.java (added) +++ incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/TypesCodeWriter.java Thu Jan 4 18:50:59 2007 @@ -0,0 +1,73 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +package org.apache.cxf.tools.wsdlto.databinding.jaxb; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; + +import com.sun.codemodel.CodeWriter; +import com.sun.codemodel.JPackage; + +public class TypesCodeWriter extends CodeWriter { + + /** The target directory to put source code. */ + private File target; + + private List<String> excludeFileList = new ArrayList<String>(); + private List<String> excludePkgList; + + public TypesCodeWriter(File ftarget, List<String> excludePkgs) throws IOException { + target = ftarget; + excludePkgList = excludePkgs; + } + + public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { + return new FileOutputStream(getFile(pkg, fileName)); + } + + protected File getFile(JPackage pkg, String fileName) throws IOException { + String dirName = pkg.name().replace('.', File.separatorChar); + File dir = pkg.isUnnamed() ? target : new File(target, dirName); + if (!dir.exists()) { + dir.mkdirs(); + } + File fn = new File(dir, fileName); + if (excludePkgList.contains(pkg.name())) { + excludeFileList.add(dirName + File.separator + fileName); + } + if (fn.exists() && !fn.delete()) { + throw new IOException(fn + ": Can't delete previous version"); + } + return fn; + } + + public void close() throws IOException { + + } + + public List<String> getExcludeFileList() { + return excludeFileList; + } + +} Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/TypesCodeWriter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/trunk/tools2/wsdlto/databinding/jaxb/src/main/java/org/apache/cxf/tools/wsdlto/databinding/jaxb/TypesCodeWriter.java ------------------------------------------------------------------------------ svn:keywords = Rev Date