You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2004/03/24 23:46:16 UTC

cvs commit: jakarta-commons/betwixt/src/java/org/apache/commons/betwixt XMLUtils.java

rdonkin     2004/03/24 14:46:15

  Modified:    betwixt/src/test/org/apache/commons/betwixt
                        TestXMLUtils.java
               betwixt/src/java/org/apache/commons/betwixt XMLUtils.java
  Log:
  Added new methods which escape CDATA sections to XMLUtils
  
  Revision  Changes    Path
  1.6       +24 -1     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/TestXMLUtils.java
  
  Index: TestXMLUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/TestXMLUtils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestXMLUtils.java	28 Feb 2004 13:38:34 -0000	1.5
  +++ TestXMLUtils.java	24 Mar 2004 22:46:15 -0000	1.6
  @@ -88,5 +88,28 @@
               XMLUtils.escapeAttributeValue("a<>b''c\"e>f'&g"));
           
       }
  +    
  +    /** 
  +     * Test CDATA escaping 
  +     * Within a CDATA section, only the CDEnd
  +     * string ']]>' is recognized as markup.
  +     * Angle brackets and amphersands may occur in their literal form.
  +     */
  +    public void testCDATAEscaping() {
  +        assertEquals("Escaping: <", "<", XMLUtils.escapeCDATAContent("<"));
  +        assertEquals("Escaping: >", ">", XMLUtils.escapeCDATAContent(">"));
  +        assertEquals("Escaping: '", "'", XMLUtils.escapeCDATAContent("'"));
  +        assertEquals("Escaping: \"", "\"", XMLUtils.escapeCDATAContent("\""));
  +        assertEquals("Escaping: &", "&", XMLUtils.escapeCDATAContent("&"));
  +        assertEquals("Escaping: ]]", "]]", XMLUtils.escapeCDATAContent("]]"));
  +        assertEquals("Escaping: ]>", "]>", XMLUtils.escapeCDATAContent("]>"));
  +        assertEquals("Escaping: ]]>", "]]&gt;", XMLUtils.escapeCDATAContent("]]>"));
  +        assertEquals("Escaping: ]]>]]>", "]]&gt;]]&gt;", XMLUtils.escapeCDATAContent("]]>]]>"));
  +        assertEquals("Escaping: ]>]]>", "]>]]&gt;", XMLUtils.escapeCDATAContent("]>]]>"));
  +        assertEquals("Escaping: ]]>]]]>", "]]&gt;]]]&gt;", XMLUtils.escapeCDATAContent("]]>]]]>"));
  +        assertEquals("Escaping: ", "", XMLUtils.escapeCDATAContent(""));     
  +    }
  +    
  +    
   }
   
  
  
  
  1.9       +44 -1     jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/XMLUtils.java
  
  Index: XMLUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/XMLUtils.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- XMLUtils.java	28 Feb 2004 13:38:32 -0000	1.8
  +++ XMLUtils.java	24 Mar 2004 22:46:15 -0000	1.9
  @@ -329,6 +329,49 @@
           return buffer.toString();
       }    
       
  +    
  +    /**
  +     * Escapes the given content suitable for insertion within a
  +     * <code>CDATA</code> sequence.
  +     * Within a <code>CDATA</code> section, only the <code>CDEnd</code>
  +     * string ']]>' is recognized as markup.
  +     * @param content the body content whose character data should 
  +     * be escaped in a way appropriate for use within a <code>CDATA</code>
  +     * section of xml.
  +     * @return escaped character data, not null
  +     */
  +    public static final String escapeCDATAContent(String content) {
  +        StringBuffer buffer = new StringBuffer(content);
  +        escapeCDATAContent(buffer);
  +        return buffer.toString();
  +    }
  +     
  +    /**
  +     * Escapes the given content suitable for insertion within a
  +     * <code>CDATA</code> sequence.
  +     * Within a <code>CDATA</code> section, only the <code>CDEnd</code>
  +     * string ']]>' is recognized as markup.
  +     * @param bufferedContent the body content within a buffer 
  +     * whose character data should 
  +     * be escaped in a way appropriate for use within a <code>CDATA</code>
  +     * section of xml.
  +     * @return escaped character data, not null
  +     */
  +    public static final void escapeCDATAContent(StringBuffer bufferedContent) {
  +        for (int i=2, size = bufferedContent.length(); i<size; i++) {
  +            char at = bufferedContent.charAt(i);
  +            if ( at == '>' 
  +                && bufferedContent.charAt(i-1) == ']' 
  +                && bufferedContent.charAt(i-2) == ']') {
  +                    
  +                    bufferedContent.replace(i, i+1, GREATER_THAN_ENTITY);
  +                size += 3;
  +                i+=3;
  +            }
  +        }
  +    }    
  + 
  +    
       /**
        * <p>Is this string a well formed xml name?</p>
        *
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org