cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r889383 - in /cxf/trunk: common/common/src/main/java/org/apache/cxf/helpers/ rt/core/src/main/java/org/apache/cxf/wsdl11/ tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/generator/wsdl11/ tools/javato/ws/src/test/java/org/apach...
Date Thu, 10 Dec 2009 19:45:38 GMT
Author: dkulp
Date: Thu Dec 10 19:45:37 2009
New Revision: 889383

URL: http://svn.apache.org/viewvc?rev=889383&view=rev
Log:
[CXF-1818] Make sure schemaLocations are set for the imported xsds

Modified:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/XMLUtils.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/SchemaSerializer.java
    cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/generator/wsdl11/WSDL11Generator.java
    cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/JavaToProcessorTest.java

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/XMLUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/XMLUtils.java?rev=889383&r1=889382&r2=889383&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/XMLUtils.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/XMLUtils.java Thu Dec 10
19:45:37 2009
@@ -114,6 +114,19 @@
     public static Transformer newTransformer() throws TransformerConfigurationException {
         return getTransformerFactory().newTransformer();
     }
+    public static Transformer newTransformer(int indent) throws TransformerConfigurationException
{
+        if (indent > 0) {
+            TransformerFactory f = TransformerFactory.newInstance();
+            try {
+                //sun way of setting indent
+                f.setAttribute("indent-number", Integer.toString(indent));
+            } catch (Throwable t) {
+                //ignore
+            }
+            return f.newTransformer();
+        }
+        return getTransformerFactory().newTransformer();
+    }
 
     public static DocumentBuilder getParser() throws ParserConfigurationException {
         return getDocumentBuilderFactory().newDocumentBuilder();
@@ -161,6 +174,15 @@
     public static void writeTo(Source src, OutputStream os) {
         writeTo(src, os, -1);
     }
+    public static void writeTo(Node node, Writer os) {
+        writeTo(new DOMSource(node), os);
+    }
+    public static void writeTo(Node node, Writer os, int indent) {
+        writeTo(new DOMSource(node), os, indent);
+    }
+    public static void writeTo(Source src, Writer os) {
+        writeTo(src, os, -1);
+    }
     public static void writeTo(Source src, OutputStream os, int indent) {
         String enc = null;
         if (src instanceof DOMSource
@@ -173,6 +195,18 @@
         }
         writeTo(src, os, indent, enc, "no");
     }
+    public static void writeTo(Source src, Writer os, int indent) {
+        String enc = null;
+        if (src instanceof DOMSource
+            && ((DOMSource)src).getNode() instanceof Document) {
+            try {
+                enc = ((Document)((DOMSource)src).getNode()).getXmlEncoding();
+            } catch (Exception ex) {
+                //ignore - not DOM level 3
+            }
+        }
+        writeTo(src, os, indent, enc, "no");
+    }
     public static void writeTo(Source src,
                                OutputStream os,
                                int indent,
@@ -184,7 +218,7 @@
                 charset = "utf-8"; 
             }
 
-            it = newTransformer();
+            it = newTransformer(indent);
             it.setOutputProperty(OutputKeys.METHOD, "xml");
             if (indent > -1) {
                 it.setOutputProperty(OutputKeys.INDENT, "yes");
@@ -197,7 +231,31 @@
         } catch (TransformerException e) {
             throw new RuntimeException("Failed to configure TRaX", e);
         }
+    }
+    public static void writeTo(Source src,
+                               Writer os,
+                               int indent,
+                               String charset,
+                               String omitXmlDecl) {
+        Transformer it;
+        try {
+            if (StringUtils.isEmpty(charset)) {
+                charset = "utf-8"; 
+            }
 
+            it = newTransformer(indent);
+            it.setOutputProperty(OutputKeys.METHOD, "xml");
+            if (indent > -1) {
+                it.setOutputProperty(OutputKeys.INDENT, "yes");
+                it.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
+                                     Integer.toString(indent));
+            }
+            it.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDecl);
+            it.setOutputProperty(OutputKeys.ENCODING, charset);
+            it.transform(src, new StreamResult(os));
+        } catch (TransformerException e) {
+            throw new RuntimeException("Failed to configure TRaX", e);
+        }
     }
     public static String toString(Source source) throws TransformerException, IOException
{
         return toString(source, null);

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/SchemaSerializer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/SchemaSerializer.java?rev=889383&r1=889382&r2=889383&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/SchemaSerializer.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/SchemaSerializer.java Thu Dec 10
19:45:37 2009
@@ -31,12 +31,13 @@
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 
 import org.w3c.dom.Node;
 
+import org.apache.cxf.helpers.XMLUtils;
+
 /**
  * A custom Schema serializer because WSDL4J's is buggy.
  */
@@ -52,9 +53,7 @@
     }
 
     private void writeXml(Node n, PrintWriter pw) throws TransformerException {
-        TransformerFactory tf = TransformerFactory.newInstance();
-
-        Transformer t = tf.newTransformer();
+        Transformer t = XMLUtils.newTransformer(2);
         t.setOutputProperty(OutputKeys.INDENT, "yes");
         t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
         t.transform(new DOMSource(n), new StreamResult(pw));

Modified: cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/generator/wsdl11/WSDL11Generator.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/generator/wsdl11/WSDL11Generator.java?rev=889383&r1=889382&r2=889383&view=diff
==============================================================================
--- cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/generator/wsdl11/WSDL11Generator.java
(original)
+++ cxf/trunk/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/generator/wsdl11/WSDL11Generator.java
Thu Dec 10 19:45:37 2009
@@ -27,6 +27,7 @@
 import java.io.OutputStream;
 import java.io.Writer;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import javax.wsdl.Definition;
@@ -35,7 +36,12 @@
 import javax.wsdl.factory.WSDLFactory;
 import javax.wsdl.xml.WSDLWriter;
 
+import org.w3c.dom.Element;
+
+import org.apache.cxf.common.WSDLConstants;
 import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.helpers.DOMUtils;
+import org.apache.cxf.helpers.XMLUtils;
 import org.apache.cxf.service.model.SchemaInfo;
 import org.apache.cxf.tools.common.ToolException;
 import org.apache.cxf.tools.java2wsdl.generator.AbstractGenerator;
@@ -93,8 +99,10 @@
 
             for (Map.Entry<String, SchemaInfo> imp : imports.entrySet()) {
                 File impfile = new File(file.getParentFile(), imp.getKey());
+                Element el = imp.getValue().getElement();
+                updateImports(el, imports);
                 os = FileWriterUtil.getWriter(impfile);
-                imp.getValue().getSchema().write(os);
+                XMLUtils.writeTo(el, os, 2);
                 os.close();
             }
 
@@ -109,6 +117,20 @@
         return def;
     }
 
+    private void updateImports(Element el, Map<String, SchemaInfo> imports) {
+        List<Element> imps = DOMUtils.getChildrenWithName(el,
+                                                          WSDLConstants.NS_SCHEMA_XSD, 
+                                                          "import");
+        for (Element e : imps) {
+            String ns = e.getAttribute("namespace");
+            for (Map.Entry<String, SchemaInfo> ent : imports.entrySet()) {
+                if (ent.getValue().getNamespaceURI().equals(ns)) {
+                    e.setAttribute("schemaLocation", ent.getKey());
+                }
+            }
+        }
+    }
+
     private void customizing(final File outputdir,
                              final String wsdlName,
                              final Set<String> imports) {

Modified: cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/JavaToProcessorTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/JavaToProcessorTest.java?rev=889383&r1=889382&r2=889383&view=diff
==============================================================================
--- cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/JavaToProcessorTest.java
(original)
+++ cxf/trunk/tools/javato/ws/src/test/java/org/apache/cxf/tools/java2wsdl/processor/JavaToProcessorTest.java
Thu Dec 10 19:45:37 2009
@@ -21,14 +21,21 @@
 
 import java.io.File;
 import java.net.URI;
+import java.util.List;
 
 import javax.wsdl.Definition;
 import javax.wsdl.Port;
 import javax.wsdl.Service;
 import javax.xml.namespace.QName;
 
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
 import org.apache.cxf.common.WSDLConstants;
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.helpers.WSDLHelper;
+import org.apache.cxf.helpers.XMLUtils;
 import org.apache.cxf.tools.common.ProcessorTestBase;
 import org.apache.cxf.tools.common.ToolConstants;
 import org.apache.cxf.tools.common.ToolContext;
@@ -49,6 +56,7 @@
     public void startUp() throws Exception {
         env = new ToolContext();
         env.put(ToolConstants.CFG_WSDL, ToolConstants.CFG_WSDL);
+
         classPath = System.getProperty("java.class.path");
         System.setProperty("java.class.path", getClassPath());
     }
@@ -328,9 +336,38 @@
         assertTrue("Generate Wsdl Fail", wsdlFile.exists());
         URI expectedFile = getClass().getResource("expected/xml-bare-expected.wsdl").toURI();
         assertWsdlEquals(new File(expectedFile), new File(output, "/xml-bare.wsdl"));
-
     }
+    @Test
+    public void testXSDImports() throws Exception {
+        //Testcase for CXF-1818
+        env.put(ToolConstants.CFG_OUTPUTFILE, output.getPath() + "/xml-bare.wsdl");
+        env.put(ToolConstants.CFG_CLASSNAME, "org.apache.xml_bare.Greeter");
+        env.put(ToolConstants.CFG_CREATE_XSD_IMPORTS, ToolConstants.CFG_CREATE_XSD_IMPORTS);
+        processor.setEnvironment(env);
+        processor.process();
 
+        File xsd1 = new File(output, "xml-bare_schema1.xsd");
+        File xsd2 = new File(output, "xml-bare_schema2.xsd");
+        assertTrue("Generate xsd1 Fail", xsd1.exists());
+        assertTrue("Generate xsd2 Fail", xsd2.exists());
+        Document doc1 = XMLUtils.parse(xsd1);
+        Document doc2 = XMLUtils.parse(xsd2);
+        String imp = findImport(doc2);
+        if (StringUtils.isEmpty(imp)) {
+            imp = findImport(doc1);
+        }
+        assertNotNull(imp);
+        assertTrue(imp.contains("xml-bare_schema"));
+    }
+    private String findImport(Document doc) {
+        List<Element> lst = DOMUtils.getChildrenWithName(doc.getDocumentElement(),
+                                                         WSDLConstants.NS_SCHEMA_XSD, 
+                                                         "import");
+        for (Element el : lst) {
+            return el.getAttribute("schemaLocation");
+        }
+        return null;
+    }
 
     @Test
     public void testFault() throws Exception {



Mime
View raw message