ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From veit...@apache.org
Subject svn commit: r1795664 - in /webservices/axiom/trunk/components/core-streams/src: main/java/org/apache/axiom/core/stream/serializer/Serializer.java test/java/org/apache/axiom/core/stream/serializer/SerializerTest.java
Date Sat, 20 May 2017 15:30:03 GMT
Author: veithen
Date: Sat May 20 15:30:03 2017
New Revision: 1795664

URL: http://svn.apache.org/viewvc?rev=1795664&view=rev
Log:
Ensure that '>' in ']]>' is escaped as '>', as required by the XML specs.

Modified:
    webservices/axiom/trunk/components/core-streams/src/main/java/org/apache/axiom/core/stream/serializer/Serializer.java
    webservices/axiom/trunk/components/core-streams/src/test/java/org/apache/axiom/core/stream/serializer/SerializerTest.java

Modified: webservices/axiom/trunk/components/core-streams/src/main/java/org/apache/axiom/core/stream/serializer/Serializer.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/components/core-streams/src/main/java/org/apache/axiom/core/stream/serializer/Serializer.java?rev=1795664&r1=1795663&r2=1795664&view=diff
==============================================================================
--- webservices/axiom/trunk/components/core-streams/src/main/java/org/apache/axiom/core/stream/serializer/Serializer.java
(original)
+++ webservices/axiom/trunk/components/core-streams/src/main/java/org/apache/axiom/core/stream/serializer/Serializer.java
Sat May 20 15:30:03 2017
@@ -53,6 +53,13 @@ public final class Serializer implements
 
     private Context context = Context.MIXED_CONTENT;
     private int matchedIllegalCharacters;
+
+    /**
+     * Tracks the number of consecutive square brackets so that the '>' in ']]>' can
be replaced by
+     * a character reference.
+     */
+    private int squareBrackets;
+
     private String[] elementNameStack = new String[8];
     private int depth;
     private boolean startTagOpen;
@@ -83,6 +90,7 @@ public final class Serializer implements
             throw new StreamException(ex);
         }
         matchedIllegalCharacters = 0;
+        squareBrackets = 0;
     }
 
     /**
@@ -398,6 +406,11 @@ public final class Serializer implements
                         case '<':
                             replacement = "&lt;";
                             break;
+                        case '>':
+                            if (context == Context.MIXED_CONTENT && squareBrackets
>= 2) {
+                                replacement = "&gt;";
+                            }
+                            break;
                         case '&':
                             replacement = "&amp;";
                             break;
@@ -427,6 +440,10 @@ public final class Serializer implements
                         writer.writeCharacterReference(ch);
                     }
                     lastDirtyCharProcessed = i;
+                } else if (ch == ']') {
+                    squareBrackets++;
+                } else {
+                    squareBrackets = 0;
                 }
             }
             

Modified: webservices/axiom/trunk/components/core-streams/src/test/java/org/apache/axiom/core/stream/serializer/SerializerTest.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/components/core-streams/src/test/java/org/apache/axiom/core/stream/serializer/SerializerTest.java?rev=1795664&r1=1795663&r2=1795664&view=diff
==============================================================================
--- webservices/axiom/trunk/components/core-streams/src/test/java/org/apache/axiom/core/stream/serializer/SerializerTest.java
(original)
+++ webservices/axiom/trunk/components/core-streams/src/test/java/org/apache/axiom/core/stream/serializer/SerializerTest.java
Sat May 20 15:30:03 2017
@@ -160,4 +160,18 @@ public class SerializerTest {
         handler.endCDATASection();
         handler.completed();
     }
+
+    @Test
+    public void testGTEscapedAfterSquareBrackets() throws Exception {
+        String sequence = "xxx]]>yyy";
+        for (int i=1; i<sequence.length()-1; i++) {
+            StringWriter sw = new StringWriter();
+            Serializer handler = new Serializer(sw);
+            handler.startFragment();
+            handler.processCharacterData(sequence.substring(0, i), false);
+            handler.processCharacterData(sequence.substring(i), false);
+            handler.completed();
+            assertThat(sw.toString()).matches("xxx]]&gt;yyy");
+        }
+    }
 }



Mime
View raw message