Author: scheu
Date: Tue Aug  3 19:18:17 2010
New Revision: 982006

URL: http://svn.apache.org/viewvc?rev=982006&view=rev
Log:
AXIS2-4791
Contributor:Rich Scheuerle
Added a XMLStreamWriter filter that can be set on the OMOutputFormat to 
intercept the outbound write.
Added a XMLStreamWriterRemoveIllegalChars that is a derivation of the filter.
Added a test to ensure that illegal chars are removed from the outbound message 
when the filter is requested.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterFilter.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterFilterBase.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterRemoveIllegalChars.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/XMLStreamWriterFilterTestCase.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/soap/test.xml
    
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/test/java/org/apache/axiom/om/impl/llom/XMLStreamWriterFilterTest.java
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMOutputFormat.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/TestConstants.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMOutputFormat.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMOutputFormat.java?rev=982006&r1=982005&r2=982006&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMOutputFormat.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMOutputFormat.java
 Tue Aug  3 19:18:17 2010
@@ -22,6 +22,7 @@ package org.apache.axiom.om;
 import java.util.HashMap;
 
 import org.apache.axiom.om.impl.MTOMConstants;
+import org.apache.axiom.om.util.XMLStreamWriterFilter;
 import org.apache.axiom.soap.SOAP11Constants;
 import org.apache.axiom.soap.SOAP12Constants;
 import org.apache.axiom.util.UIDGenerator;
@@ -59,6 +60,8 @@ public class OMOutputFormat {
 
     public static final String ACTION_PROPERTY = "action";
     
+    private XMLStreamWriterFilter xmlStreamWriterFilter = null;
+    
     // The value of this property is a Boolean.  
     // A missing value indicates the default action, which is Boolean.FALSE
     // If Boolean.TRUE, attachments that are "non textual" are written out 
with 
@@ -388,4 +391,17 @@ public class OMOutputFormat {
         return optimizedThreshold;
     }
     
+    /**
+     * @return the xmlStreamWriterFilter
+     */
+    public XMLStreamWriterFilter getXmlStreamWriterFilter() {
+        return xmlStreamWriterFilter;
+    }
+
+    /**
+     * @param xmlStreamWriterFilter the xmlStreamWriterFilter to set
+     */
+    public void setXmlStreamWriterFilter(XMLStreamWriterFilter 
xmlStreamWriterFilter) {
+        this.xmlStreamWriterFilter = xmlStreamWriterFilter;
+    }
 }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java?rev=982006&r1=982005&r2=982006&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
 Tue Aug  3 19:18:17 2010
@@ -36,6 +36,7 @@ import org.apache.axiom.om.OMOutputForma
 import org.apache.axiom.om.OMText;
 import org.apache.axiom.om.util.CommonUtils;
 import org.apache.axiom.om.util.StAXUtils;
+import org.apache.axiom.om.util.XMLStreamWriterFilter;
 import org.apache.axiom.util.stax.XMLStreamWriterUtils;
 import org.apache.axiom.util.stax.xop.ContentIDGenerator;
 import org.apache.axiom.util.stax.xop.OptimizationPolicy;
@@ -66,7 +67,10 @@ public class MTOMXMLStreamWriter impleme
     // State variables
     private boolean isEndDocument = false; // has endElement been called
     private boolean isComplete = false;    // have the attachments been written
-    private int depth = 0;                 // current eleement depth
+    private int depth = 0;                 // current element depth
+    
+    // Set the filter object if provided
+    private XMLStreamWriterFilter xmlStreamWriterFilter  = null;
 
     public MTOMXMLStreamWriter(XMLStreamWriter xmlWriter) {
         this.xmlWriter = xmlWriter;
@@ -88,6 +92,7 @@ public class MTOMXMLStreamWriter impleme
     public MTOMXMLStreamWriter(OutputStream outStream, OMOutputFormat format)
             throws XMLStreamException, FactoryConfigurationError {
         if (isDebugEnabled) {
+            log.debug("Creating MTOMXMLStreamWriter");
             log.debug("OutputStream =" + outStream.getClass());
             log.debug("OMFormat = " + format.toString());
         }
@@ -122,6 +127,14 @@ public class MTOMXMLStreamWriter impleme
             xmlWriter = StAXUtils.createXMLStreamWriter(outStream,
                                                         
format.getCharSetEncoding());
         }
+        xmlStreamWriterFilter = format.getXmlStreamWriterFilter();
+        if (xmlStreamWriterFilter != null) {
+            if (log.isDebugEnabled()) {
+                log.debug("Installing XMLStreamWriterFilter " + 
xmlStreamWriterFilter);
+            }
+            xmlStreamWriterFilter.setDelegate(xmlWriter);
+            xmlWriter = xmlStreamWriterFilter;
+        }
     }
 
     public void writeStartElement(String string) throws XMLStreamException {
@@ -453,6 +466,15 @@ public class MTOMXMLStreamWriter impleme
      * @return the underlying byte stream, or <code>null</code> if the stream 
is not accessible
      */
     public OutputStream getOutputStream() throws XMLStreamException {  
+        
+        if (xmlStreamWriterFilter != null) {
+            if (log.isDebugEnabled()) {
+                log.debug("getOutputStream returning null due to presence of 
XMLStreamWriterFilter " + 
+                        xmlStreamWriterFilter);
+            }
+            return null;
+        }
+        
         OutputStream os = null;
         if (rootPartOutputStream != null) {
             os = rootPartOutputStream;
@@ -496,4 +518,31 @@ public class MTOMXMLStreamWriter impleme
             writeEntityRef(textNode.getText());
         }
     }
+    
+    public void setFilter(XMLStreamWriterFilter filter) {
+        if (filter != null) {
+            if (log.isDebugEnabled()) {
+                log.debug("setting filter " + filter.getClass());
+            }
+            xmlStreamWriterFilter = filter;
+            filter.setDelegate(xmlWriter);
+            xmlWriter = filter;
+        }
+    }
+    
+    public XMLStreamWriterFilter removeFilter() {
+        XMLStreamWriterFilter filter = null;
+        if (xmlStreamWriterFilter != null) {
+            filter = xmlStreamWriterFilter;
+            if (log.isDebugEnabled()) {
+                log.debug("removing filter " + filter.getClass());
+            }
+            xmlWriter = xmlStreamWriterFilter.getDelegate();
+            filter.setDelegate(null);
+            xmlStreamWriterFilter = (xmlWriter instanceof 
XMLStreamWriterFilter) ? 
+                        (XMLStreamWriterFilter) xmlWriter : 
+                                null;
+        }
+        return filter;
+    }
 }

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterFilter.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterFilter.java?rev=982006&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterFilter.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterFilter.java
 Tue Aug  3 19:18:17 2010
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+package org.apache.axiom.om.util;
+
+import javax.xml.stream.XMLStreamWriter;
+
+/**
+ * An interface used to identify a filter class for an XMLStreamWriter
+ * The filter receives XMLStreamWriter events (and can change or log them).
+ * The filter then sends the events to the delegate XMLStreamWriter
+ * @see XMLStreamWriterFilterBase
+ */
+public interface XMLStreamWriterFilter extends XMLStreamWriter {
+       
+       /**
+        * Set a new delegate writer
+        * @param writer
+        */
+       public void setDelegate(XMLStreamWriter writer);
+       
+       /**
+        * @return XMLStreamWriter delegate
+        */
+       public XMLStreamWriter getDelegate();
+}

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterFilterBase.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterFilterBase.java?rev=982006&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterFilterBase.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterFilterBase.java
 Tue Aug  3 19:18:17 2010
@@ -0,0 +1,197 @@
+/*
+ * 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.
+ */
+package org.apache.axiom.om.util;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+/**
+ * The base class for classes that are XMLStreamWriterFilters
+ * Each of the XMLStreamWriter events is intercepted and passed to the 
delegate XMLStreamWriter
+ * 
+ * Character data is sent to the xmlData abstract method.  Derived classes may 
+ * log or change the xml data.
+ * 
+ * @see XMLStreamWriterRemoveIllegalChars
+ */
+public abstract class XMLStreamWriterFilterBase implements 
XMLStreamWriterFilter {
+
+       XMLStreamWriter delegate = null;
+
+       public XMLStreamWriterFilterBase() {
+       }
+       
+       public void setDelegate(XMLStreamWriter writer) {
+               this.delegate = writer;
+       }
+
+       public XMLStreamWriter getDelegate() {
+               return delegate;
+       }
+
+       public void close() throws XMLStreamException {
+               delegate.close();
+       }
+
+       public void flush() throws XMLStreamException {
+               delegate.flush();
+       }
+
+       public NamespaceContext getNamespaceContext() {
+               return delegate.getNamespaceContext();
+       }
+
+       public String getPrefix(String uri) throws XMLStreamException {
+               return delegate.getPrefix(uri);
+       }
+
+       public Object getProperty(String name) throws IllegalArgumentException {
+               return delegate.getProperty(name);
+       }
+
+       public void setDefaultNamespace(String uri) throws XMLStreamException {
+               delegate.setDefaultNamespace(uri);
+       }
+
+       public void setNamespaceContext(NamespaceContext context)
+                       throws XMLStreamException {
+               delegate.setNamespaceContext(context);
+       }
+
+       public void setPrefix(String prefix, String uri) throws 
XMLStreamException {
+               delegate.setPrefix(prefix, uri);
+       }
+
+       public void writeAttribute(String prefix, String namespaceURI,
+                       String localName, String value) throws 
XMLStreamException {
+               delegate.writeAttribute(prefix, namespaceURI, localName, 
xmlData(value));
+       }
+
+       public void writeAttribute(String namespaceURI, String localName,
+                       String value) throws XMLStreamException {
+               delegate.writeAttribute(namespaceURI, localName, 
xmlData(value));
+       }
+
+       public void writeAttribute(String localName, String value)
+                       throws XMLStreamException {
+               delegate.writeAttribute(localName, xmlData(value));
+       }
+
+       public void writeCData(String data) throws XMLStreamException {
+               delegate.writeCData(xmlData(data));
+       }
+
+       public void writeCharacters(char[] text, int start, int len)
+                       throws XMLStreamException {
+           // Adapt to writeCharacters that takes a String value
+           String value = new String(text, start, len);
+               writeCharacters(value);
+       }
+
+       public void writeCharacters(String text) throws XMLStreamException {
+               delegate.writeCharacters(xmlData(text));
+       }
+
+       public void writeComment(String data) throws XMLStreamException {
+               delegate.writeComment(data);
+       }
+
+       public void writeDTD(String dtd) throws XMLStreamException {
+               delegate.writeDTD(dtd);
+       }
+
+       public void writeDefaultNamespace(String namespaceURI)
+                       throws XMLStreamException {
+               delegate.writeDefaultNamespace(namespaceURI);
+       }
+
+       public void writeEmptyElement(String prefix, String localName,
+                       String namespaceURI) throws XMLStreamException {
+               delegate.writeEmptyElement(prefix, localName, namespaceURI);
+       }
+
+       public void writeEmptyElement(String namespaceURI, String localName)
+                       throws XMLStreamException {
+               delegate.writeEmptyElement(namespaceURI, localName);
+       }
+
+       public void writeEmptyElement(String localName) throws 
XMLStreamException {
+               delegate.writeEmptyElement(localName);
+       }
+
+       public void writeEndDocument() throws XMLStreamException {
+               delegate.writeEndDocument();
+       }
+
+       public void writeEndElement() throws XMLStreamException {
+               delegate.writeEndElement();
+       }
+
+       public void writeEntityRef(String name) throws XMLStreamException {
+               delegate.writeEntityRef(name);
+       }
+
+       public void writeNamespace(String prefix, String namespaceURI)
+                       throws XMLStreamException {
+               delegate.writeNamespace(prefix, namespaceURI);
+       }
+
+       public void writeProcessingInstruction(String target, String data)
+                       throws XMLStreamException {
+               delegate.writeProcessingInstruction(target, data);
+       }
+
+       public void writeProcessingInstruction(String target)
+                       throws XMLStreamException {
+               delegate.writeProcessingInstruction(target);
+       }
+
+       public void writeStartDocument() throws XMLStreamException {
+               delegate.writeStartDocument();
+       }
+
+       public void writeStartDocument(String encoding, String version)
+                       throws XMLStreamException {
+               delegate.writeStartDocument(encoding, version);
+       }
+
+       public void writeStartDocument(String version) throws 
XMLStreamException {
+               delegate.writeStartDocument(version);
+       }
+
+       public void writeStartElement(String prefix, String localName,
+                       String namespaceURI) throws XMLStreamException {
+               delegate.writeStartElement(prefix, localName, namespaceURI);
+       }
+
+       public void writeStartElement(String namespaceURI, String localName)
+                       throws XMLStreamException {
+               delegate.writeStartElement(namespaceURI, localName);
+       }
+
+       public void writeStartElement(String localName) throws 
XMLStreamException {
+               delegate.writeStartElement(localName);
+       }
+
+       /**
+        * Derived classes extend the method.  A derived class may log or 
modify the xml data
+        * @param value
+        * @return value
+        */
+       protected abstract String xmlData(String value);
+
+}

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterRemoveIllegalChars.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterRemoveIllegalChars.java?rev=982006&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterRemoveIllegalChars.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/XMLStreamWriterRemoveIllegalChars.java
 Tue Aug  3 19:18:17 2010
@@ -0,0 +1,159 @@
+/*
+ * 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.
+ */
+package org.apache.axiom.om.util;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * This is an XMLStreamWriterFilter that removes illegal characters.
+ * 
+ * Valid and invalid character ranges are defined by:
+ * http://www.w3.org/TR/2008/REC-xml-20081126/#NT-Char
+ *
+ *
+ */
+public class XMLStreamWriterRemoveIllegalChars extends
+               XMLStreamWriterFilterBase {
+
+    private static Log log = 
LogFactory.getLog(XMLStreamWriterRemoveIllegalChars.class);
+    
+    public XMLStreamWriterRemoveIllegalChars() {
+        super();
+        if (log.isDebugEnabled()) {
+            log.debug("Creating XMLStreamWriterRemoveIllegalChars object " + 
this);
+        }
+    }
+    // Characters less than 0x20 may be control characters and should be 
removed
+    // Note the non-initialized bytes in this array are zero
+    private static byte[] REMOVE = new byte[32];
+    static {
+        REMOVE[0x00] = 1;
+        REMOVE[0x01] = 1;
+        REMOVE[0x02] = 1;
+        REMOVE[0x03] = 1;
+        REMOVE[0x04] = 1;
+        REMOVE[0x05] = 1;
+        REMOVE[0x06] = 1;
+        REMOVE[0x07] = 1;
+        REMOVE[0x08] = 1;
+        // 0x09 is TAB...which is allowed
+        // 0x0A is LINEFEED...which is allowed
+        REMOVE[0x0B] = 1;
+        REMOVE[0x0C] = 1;
+        // 0x0D is CARRIAGE RETURN, which is allowed
+        REMOVE[0x0E] = 1;
+        REMOVE[0x0F] = 1;
+        REMOVE[0x10] = 1;
+        REMOVE[0x11] = 1;
+        REMOVE[0x12] = 1;
+        REMOVE[0x13] = 1;
+        REMOVE[0x14] = 1;
+        REMOVE[0x15] = 1;
+        REMOVE[0x16] = 1;
+        REMOVE[0x17] = 1;
+        REMOVE[0x18] = 1;
+        REMOVE[0x19] = 1;
+        REMOVE[0x1A] = 1;
+        REMOVE[0x1B] = 1;
+        REMOVE[0x1C] = 1;
+        REMOVE[0x1D] = 1;
+        REMOVE[0x1E] = 1;
+        REMOVE[0x1F] = 1;
+    }
+    
+    // These two characters are not allowed
+    private final int FFFE = 0xFFFE;
+    private final char FFFF = 0xFFFF;
+    
+    // Characters in the surrogate range are not allowed
+    // (unless the result is a valid supplemental character)
+    private final char SURROGATE_START = 0xD800;
+    private final char SURROGATE_END =   0xDFFF;
+    
+    
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.util.XMLStreamWriterFilterBase#xmlData(java.lang.String)
+     */
+    protected String xmlData(String value) {
+        
+        char[] buffer = null;
+        int len = value.length();
+        int srcI = 0;
+        int tgtI = 0;
+        int copyLength = 0;
+        int i = 0;
+        
+        // Traverse all of the characters in the input String (value)
+        while (i < len) {
+            
+            // Get the codepoint of the character at the index
+            // Note that the code point may be two characters long (a 
supplemental character)
+            int cp = value.codePointAt(i);
+            
+            if (cp > FFFF) {
+                // Supplemental Character...Increase index by 2
+                // Increase the length of good characters to copy by 2
+                i = i+2;
+                copyLength = copyLength+2;
+            } else {
+                // See if the character is invalid
+                if ((cp < 0x20 && (REMOVE[cp] > 0)) || // Control Character
+                        (cp >= SURROGATE_START && cp <= SURROGATE_END ) ||  // 
Bad surrogate
+                        (cp == FFFF || cp == FFFE)) {  // or illegal character
+                    // Flow to here indicates that the character is not 
allowed.  
+                    // The good characters (up to this point) are copied into 
the buffer.
+                    
+                    // Note that the buffer is initialized with the original 
characters.
+                    // Thus the buffer copy is always done on the same buffer 
(saving
+                    // both time and space).
+                    
+                    
+                    // Make the buffer on demand
+                    if (buffer == null) {
+                        if (log.isDebugEnabled()) {
+                            log.debug("One or more illegal characterss found.  
Codepoint=" + cp);
+                        }
+                        buffer = value.toCharArray();
+                    }
+                    
+                    // Copy the good characters into the buffer
+                    System.arraycopy(buffer, srcI, buffer, tgtI, copyLength);
+                    tgtI = tgtI + copyLength;  // Update the target location 
in the array
+                    srcI = i + 1;  // Skip over the current character
+                    copyLength = 0; // reset new copy length
+                } else {
+                    // Valid character, increase copy length
+                    copyLength = copyLength+1;
+                }
+                // Single bit16 character, increase index by 1
+                i = i+1;
+            }
+        }
+        
+        if (buffer == null) {
+            // Normal case, no illegal characters removed..No buffer
+            return value;
+        } else {
+            // Move the final valid characters to the buffer
+            // and return a string representing the value
+            System.arraycopy(buffer, srcI, buffer, tgtI, copyLength);
+            String newValue = new String(buffer, 0, tgtI + copyLength);
+            return newValue;
+        }
+        
+    }
+}

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/TestConstants.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/TestConstants.java?rev=982006&r1=982005&r2=982006&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/TestConstants.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/TestConstants.java
 Tue Aug  3 19:18:17 2010
@@ -25,6 +25,7 @@ public class TestConstants {
     public static final String SOAP_SOAPMESSAGE = "soap/soapmessage.xml";
     public static final String SOAP_SOAPMESSAGE1 = "soap/soapmessage1.xml";
     public static final String SAMPLE1 = "soap/sample1.xml";
+    public static final String TEST = "soap/test.xml";
     public static final String WHITESPACE_MESSAGE = 
"soap/whitespacedMessage.xml";
     public static final String MINIMAL_MESSAGE = "soap/minimalMessage.xml";
     public static final String REALLY_BIG_MESSAGE = 
"soap/reallyReallyBigMessage.xml";

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/XMLStreamWriterFilterTestCase.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/XMLStreamWriterFilterTestCase.java?rev=982006&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/XMLStreamWriterFilterTestCase.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/XMLStreamWriterFilterTestCase.java
 Tue Aug  3 19:18:17 2010
@@ -0,0 +1,128 @@
+/*
+ * 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.axiom.om.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileReader;
+import java.io.InputStream;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.transform.TransformerFactory;
+
+import junit.framework.TestSuite;
+
+import org.apache.axiom.om.AbstractTestCase;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.OMXMLParserWrapper;
+import org.apache.axiom.om.TestConstants;
+import org.apache.axiom.om.util.XMLStreamWriterRemoveIllegalChars;
+import org.apache.axiom.soap.SOAPBody;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
+
+import com.ibm.jvm.util.ByteArrayOutputStream;
+
+public class XMLStreamWriterFilterTestCase extends AbstractTestCase {
+       
+       private char ILLEGAL_CHAR = 0x15;
+       private String ILLEGAL_ENTITY = "&#x15;";
+       
+       private char NULL_CHAR = 0x00;
+       private String NULL_ENTITY = "&#x00;";
+       
+       private final OMMetaFactory omMetaFactory;
+    
+    protected XMLStreamWriterFilterTestCase(OMMetaFactory omMetaFactory) {
+        this.omMetaFactory = omMetaFactory;
+    }
+       
+       public void test01() throws Exception {
+               char[] chars = new char[] {ILLEGAL_CHAR};
+               String insert = new String(chars);
+               testInsert(insert);
+       }
+       
+       public void test02() throws Exception {
+               char[] chars = new char[] {NULL_CHAR};
+               String insert = new String(chars);
+               testInsert(insert);
+       }
+       
+       public void test03() throws Exception {
+               
+               testInsert(ILLEGAL_ENTITY);
+       }
+       
+       public void test04() throws Exception {
+               
+               testInsert(NULL_ENTITY);
+       }
+       
+       
+       
+       private void testInsert(String insert) throws Exception {
+               
+               // Read XML
+               InputStream is = getTestResource(TestConstants.TEST);
+               
+               // Build SOAP OM
+               SOAPEnvelope env1 = createEnvelope(is);
+               
+               // Add illegal character
+               SOAPBody body = env1.getBody();
+               OMElement omElement = body.getFirstElement();
+               String text = omElement.getText();
+               text = text + "[" + insert + "]";
+               System.out.println("New Text = " + text);
+               omElement.setText(text);
+               
+               // Serialize
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               OMOutputFormat omFormat = new OMOutputFormat();
+               omFormat.setXmlStreamWriterFilter(new 
XMLStreamWriterRemoveIllegalChars());
+               env1.serialize(baos, omFormat);
+               
+               String xmlText = baos.toString();
+               System.out.println("Serialized Text = " + xmlText);
+               
+               ByteArrayInputStream bais = new 
ByteArrayInputStream(xmlText.getBytes("UTF-8"));
+               
+               SOAPEnvelope env2 = createEnvelope(bais);
+               env2.build();
+       }
+       
+    /**
+     * Create SOAPEnvelope from the test in the indicated file
+     * @param input stream
+     * @return
+     * @throws Exception
+     */
+    protected SOAPEnvelope createEnvelope(InputStream is) throws Exception {
+        XMLStreamReader parser =
+            XMLInputFactory.newInstance().createXMLStreamReader(is);
+        OMXMLParserWrapper builder = new StAXSOAPModelBuilder(omMetaFactory, 
parser, null);
+        SOAPEnvelope sourceEnv = (SOAPEnvelope) builder.getDocumentElement();
+        return sourceEnv;
+    }
+    
+}

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/soap/test.xml
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/soap/test.xml?rev=982006&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/soap/test.xml
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/soap/test.xml
 Tue Aug  3 19:18:17 2010
@@ -0,0 +1,6 @@
+<?xml version='1.0' ?>
+<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/";>
+    <env:Body>
+        <p:echo xmlns:p="urn://sample">Hello World</p:echo>
+    </env:Body>
+</env:Envelope>
\ No newline at end of file

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/test/java/org/apache/axiom/om/impl/llom/XMLStreamWriterFilterTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/test/java/org/apache/axiom/om/impl/llom/XMLStreamWriterFilterTest.java?rev=982006&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/test/java/org/apache/axiom/om/impl/llom/XMLStreamWriterFilterTest.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/test/java/org/apache/axiom/om/impl/llom/XMLStreamWriterFilterTest.java
 Tue Aug  3 19:18:17 2010
@@ -0,0 +1,29 @@
+/*
+ * 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.axiom.om.impl.llom;
+
+
+import org.apache.axiom.om.impl.XMLStreamWriterFilterTestCase;
+import org.apache.axiom.om.impl.llom.factory.OMLinkedListMetaFactory;
+
+public class XMLStreamWriterFilterTest extends XMLStreamWriterFilterTestCase {
+    public XMLStreamWriterFilterTest() {
+        super(new OMLinkedListMetaFactory());
+    }
+}


Reply via email to