cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r738210 - in /cxf/trunk: common/common/src/main/java/org/apache/cxf/staxutils/ rt/core/src/main/java/org/apache/cxf/databinding/source/ rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/xml/ rt/frontend/jaxws/src/main/java/org/ap...
Date Tue, 27 Jan 2009 20:06:58 GMT
Author: dkulp
Date: Tue Jan 27 20:06:57 2009
New Revision: 738210

URL: http://svn.apache.org/viewvc?rev=738210&view=rev
Log:
[CXF-2007] Add support for Source objects that have a XMLReader, but not really a valid InputSource.
 JAXBSource objects do this.

Added:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java
  (with props)
Modified:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/NodeDataWriter.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataWriter.java
    cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/xml/SourceType.java
    cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java
    cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/SwAOutInterceptor.java

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?rev=738210&r1=738209&r2=738210&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Tue Jan
27 20:06:57 2009
@@ -48,6 +48,7 @@
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
 import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
 
 import org.w3c.dom.Attr;
 import org.w3c.dom.CDATASection;
@@ -60,6 +61,9 @@
 import org.w3c.dom.ProcessingInstruction;
 import org.w3c.dom.Text;
 
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.DOMUtils;
@@ -287,6 +291,30 @@
         }
         return false;
     }
+    public static void copy(Source source, XMLStreamWriter writer) throws XMLStreamException
{
+        if (source instanceof SAXSource) {
+            InputSource src = ((SAXSource)source).getInputSource();
+            if (src.getByteStream() == null && src.getCharacterStream() == null
+                && src.getSystemId() == null && src.getPublicId() == null
+                && ((SAXSource)source).getXMLReader() != null) {
+                //OK - reader is OK.  We'll dump that out
+                StreamWriterContentHandler ch = new StreamWriterContentHandler(writer);
+                XMLReader reader = ((SAXSource)source).getXMLReader();
+                reader.setContentHandler(ch);
+                try {
+                    reader.parse(((SAXSource)source).getInputSource());
+                    return;
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    throw new XMLStreamException(e);
+                }
+            }
+        }
+        
+        XMLStreamReader reader = createXMLStreamReader(source);
+        copy(reader, writer);
+        reader.close();
+    }
 
     public static Document copy(Document doc) 
         throws XMLStreamException, ParserConfigurationException {

Added: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java?rev=738210&view=auto
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java
(added)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java
Tue Jan 27 20:06:57 2009
@@ -0,0 +1,221 @@
+/**
+ * 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.staxutils;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+
+/**
+ * 
+ */
+public class StreamWriterContentHandler implements ContentHandler {
+    
+    XMLStreamWriter writer;
+    Map<String, String> mapping = new LinkedHashMap<String, String>();
+    
+    public StreamWriterContentHandler(XMLStreamWriter w) {
+        writer = w;
+    }
+
+    /**
+     * Method endDocument.
+     *
+     * @throws SAXException
+     */
+    public void endDocument() throws SAXException {
+        // do nothing
+    }
+
+    /**
+     * Method startDocument.
+     *
+     * @throws SAXException
+     */
+    public void startDocument() throws SAXException {
+        // 
+    }
+
+    /**
+     * Method characters.
+     *
+     * @param ch
+     * @param start
+     * @param length
+     * @throws SAXException
+     */
+    public void characters(char ch[], int start, int length) throws SAXException {
+        try {
+            writer.writeCharacters(ch, start, length);
+        } catch (XMLStreamException e) {
+            throw new SAXException(e);
+        }
+    }
+
+    /**
+     * Method ignorableWhitespace.
+     *
+     * @param ch
+     * @param start
+     * @param length
+     * @throws SAXException
+     */
+    public void ignorableWhitespace(char ch[], int start, int length) throws SAXException
{
+    }
+
+    /**
+     * Method endPrefixMapping.
+     *
+     * @param prefix
+     * @throws SAXException
+     */
+    public void endPrefixMapping(String prefix) throws SAXException {
+    }
+
+    /**
+     * Method skippedEntity.
+     *
+     * @param name
+     * @throws SAXException
+     */
+    public void skippedEntity(String name) throws SAXException {
+    }
+
+    /**
+     * Method setDocumentLocator.
+     *
+     * @param locator
+     */
+    public void setDocumentLocator(Locator locator) {
+    }
+
+    /**
+     * Method processingInstruction.
+     *
+     * @param target
+     * @param data
+     * @throws SAXException
+     */
+    public void processingInstruction(String target, String data)
+        throws SAXException {
+    }
+
+    /**
+     * Method startPrefixMapping.
+     *
+     * @param prefix
+     * @param uri
+     * @throws SAXException
+     */
+    public void startPrefixMapping(String prefix, String uri)
+        throws SAXException {
+        mapping.put(prefix, uri);
+        try {
+            writer.setPrefix(prefix, uri);
+        } catch (XMLStreamException e) {
+            throw new SAXException(e);
+        }
+    }
+
+    /**
+     * Method endElement.
+     *
+     * @param namespaceURI
+     * @param localName
+     * @param qName
+     * @throws SAXException
+     */
+    public void endElement(String namespaceURI,
+                           String localName,
+                           String qName) throws SAXException {
+        try {
+            writer.writeEndElement();
+        } catch (XMLStreamException e) {
+            throw new SAXException(e);
+        }
+    }
+
+    /**
+     * Method getPrefix.
+     *
+     * @param qname
+     * @return Returns String.
+     */
+    private String getPrefix(String ns) {
+        int idx = ns.indexOf(':');
+        if (idx != -1) {
+            return ns.substring(0, idx);
+        }
+        return null;
+    }
+
+    /**
+     * Method startElement.
+     *
+     * @param namespaceURI
+     * @param localName
+     * @param qName
+     * @param atts
+     * @throws SAXException
+     */
+    public void startElement(String namespaceURI,
+                             String localName,
+                             String qName,
+                             Attributes atts) throws SAXException {
+        try {
+            String prefix = getPrefix(qName);
+            
+            // it is only the prefix we want to learn from the QName! so we can get rid of
the
+            // spliting QName
+            if (prefix == null) {
+                writer.writeStartElement(namespaceURI, localName);
+            } else {
+                writer.writeStartElement(prefix, localName, namespaceURI);
+            }
+            for (Map.Entry<String, String> e : mapping.entrySet()) {
+                if ("".equals(e.getKey())) {
+                    writer.writeDefaultNamespace(e.getValue());
+                } else {
+                    writer.writeNamespace(e.getKey(), e.getValue());
+                }
+            }
+            mapping.clear();
+            if (atts != null) {
+                int attCount = atts.getLength();
+                for (int i = 0; i < attCount; i++) {
+                    writer.writeAttribute(atts.getURI(i), localName,
+                                          atts.getValue(i));
+                }
+            }
+        } catch (XMLStreamException e) {
+            throw new SAXException(e);
+        }
+    }
+    
+    
+
+}

Propchange: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/NodeDataWriter.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/NodeDataWriter.java?rev=738210&r1=738209&r2=738210&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/NodeDataWriter.java
(original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/NodeDataWriter.java
Tue Jan 27 20:06:57 2009
@@ -22,7 +22,6 @@
 import java.util.logging.Logger;
 
 import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.Source;
 import javax.xml.transform.dom.DOMSource;
@@ -56,9 +55,7 @@
             }
             
             XMLStreamWriter writer = new W3CDOMStreamWriter((Element)n);
-            XMLStreamReader reader = StaxUtils.createXMLStreamReader(s);
-            StaxUtils.copy(reader, writer);
-            reader.close();
+            StaxUtils.copy(s, writer);
         } catch (XMLStreamException e) {
             throw new Fault(new Message("COULD_NOT_READ_XML_STREAM", LOG), e);
         }

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataWriter.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataWriter.java?rev=738210&r1=738209&r2=738210&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataWriter.java
(original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/XMLStreamDataWriter.java
Tue Jan 27 20:06:57 2009
@@ -51,17 +51,16 @@
             if (obj instanceof DataSource) {
                 DataSource ds = (DataSource)obj;
                 reader = StaxUtils.createXMLStreamReader(ds.getInputStream());
+                StaxUtils.copy(reader, writer);
+                reader.close();
             } else {
                 Source s = (Source) obj;
                 if (s instanceof DOMSource
                     && ((DOMSource) s).getNode() == null) {
                     return;
                 }
-            
-                reader = StaxUtils.createXMLStreamReader(s);
+                StaxUtils.copy(s, writer);
             }
-            StaxUtils.copy(reader, writer);
-            reader.close();
         } catch (XMLStreamException e) {
             throw new Fault(new Message("COULD_NOT_READ_XML_STREAM", LOG), e);
         } catch (IOException e) {

Modified: cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/xml/SourceType.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/xml/SourceType.java?rev=738210&r1=738209&r2=738210&view=diff
==============================================================================
--- cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/xml/SourceType.java
(original)
+++ cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/xml/SourceType.java
Tue Jan 27 20:06:57 2009
@@ -20,7 +20,6 @@
 
 import javax.xml.stream.FactoryConfigurationError;
 import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.Source;
 import javax.xml.transform.dom.DOMSource;
@@ -98,8 +97,7 @@
 
             StaxUtils.writeElement(element, writer, false);
         } else {
-            XMLStreamReader reader = StaxUtils.createXMLStreamReader((Source)object);
-            StaxUtils.copy(reader, writer);
+            StaxUtils.copy((Source)object, writer);
         }
     }
 

Modified: cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java?rev=738210&r1=738209&r2=738210&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java
Tue Jan 27 20:06:57 2009
@@ -246,9 +246,7 @@
             }
 
             XMLStreamWriter writer = new W3CDOMStreamWriter((Element)n);
-            XMLStreamReader reader = StaxUtils.createXMLStreamReader(source);
-            StaxUtils.copy(reader, writer);
-            reader.close();
+            StaxUtils.copy(source, writer);
         } catch (XMLStreamException e) {
             throw new Fault(e);
         }

Modified: cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/SwAOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/SwAOutInterceptor.java?rev=738210&r1=738209&r2=738210&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/SwAOutInterceptor.java
(original)
+++ cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/SwAOutInterceptor.java
Tue Jan 27 20:06:57 2009
@@ -43,7 +43,6 @@
 import javax.mail.util.ByteArrayDataSource;
 import javax.xml.bind.JAXBContext;
 import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.Source;
 import javax.xml.transform.stream.StreamSource;
@@ -233,11 +232,10 @@
                 throw new Fault(e);
             }
         } else {
-            XMLStreamReader reader = StaxUtils.createXMLStreamReader((Source)o);
             StringWriter stringWriter = new StringWriter();
             XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(stringWriter);
             try {
-                StaxUtils.copy(reader, writer);
+                StaxUtils.copy((Source)o, writer);
                 writer.flush();
                 ds = new ByteArrayDataSource(stringWriter.toString(), ct);
             } catch (XMLStreamException e1) {



Mime
View raw message