commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/strategy TestMixedContentEncoding.java
Date Sat, 10 Apr 2004 09:54:07 GMT
rdonkin     2004/04/10 02:54:07

  Modified:    betwixt/src/java/org/apache/commons/betwixt/strategy
                        MixedContentEncodingStrategy.java
               betwixt/src/test/org/apache/commons/betwixt/strategy
                        TestMixedContentEncoding.java
  Log:
  Added support for per-property CDATA encoding to the dot betwixt file
  
  Revision  Changes    Path
  1.3       +27 -3     jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/MixedContentEncodingStrategy.java
  
  Index: MixedContentEncodingStrategy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/MixedContentEncodingStrategy.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MixedContentEncodingStrategy.java	4 Apr 2004 09:23:10 -0000	1.2
  +++ MixedContentEncodingStrategy.java	10 Apr 2004 09:54:07 -0000	1.3
  @@ -36,17 +36,41 @@
    */
   public abstract class MixedContentEncodingStrategy {
   
  +    /** 
  +     * The name of the option used to specify encoding on a per-element
  +     * basis is
  +     * <code>org.apache.commons.betwixt.mixed-content-encoding</code> 
  +     */
  +    public static final String ENCODING_OPTION_NAME 
  +        = "org.apache.commons.betwixt.mixed-content-encoding";
  +    /** The option value for CDATA */
  +    public static final String CDATA_ENCODING = "CDATA";
  +
       /**
        * The standard implementation used by Betwixt by default.
  +     * The default is to escape as character data unless
  +     * the <code>ElementDescriptor</code> contains
  +     * an option with name 
  +     * <code>org.apache.commons.betwixt.mixed-content-encoding</code>
  +     * and value <code>CDATA</code>.
        * This is a singleton.
        */
       public static final MixedContentEncodingStrategy DEFAULT 
               = new BaseMixedContentEncodingStrategy() {
           /**
  -         * Always encode by escaping character data.
  +         * Encode by escaping character data unless
  +         * the <code>ElementDescriptor</code> contains
  +         * an option with name 
  +         * <code>org.apache.commons.betwixt.mixed-content-encoding</code>
  +         * and value <code>CDATA</code>.
            */
           protected boolean encodeAsCDATA(ElementDescriptor element) {
  -            return false;       
  +            boolean result = false;
  +            if (element != null ) {
  +                String optionValue = element.getOptions().getValue(ENCODING_OPTION_NAME);
  +                result = CDATA_ENCODING.equals(optionValue);        
  +            }
  +            return result;       
           }
       };
       
  
  
  
  1.3       +63 -2     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/strategy/TestMixedContentEncoding.java
  
  Index: TestMixedContentEncoding.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/strategy/TestMixedContentEncoding.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestMixedContentEncoding.java	4 Apr 2004 09:23:10 -0000	1.2
  +++ TestMixedContentEncoding.java	10 Apr 2004 09:54:07 -0000	1.3
  @@ -21,6 +21,7 @@
   
   import org.apache.commons.betwixt.AbstractTestCase;
   import org.apache.commons.betwixt.ElementDescriptor;
  +import org.apache.commons.betwixt.XMLBeanInfo;
   import org.apache.commons.betwixt.io.BeanWriter;
   
   
  @@ -104,7 +105,7 @@
               MixedContentEncodingStrategy.CDATA.encode("<proclaim>The King Is Dead,
Long Live The King</proclaim>", descriptor));
       }
       
  -    public void testdefaultOutput() throws Exception {
  +    public void testDefaultOutput() throws Exception {
           Element element = new Element();
           element.setValue("<greeting>What Ho Jeeves!</greeting>");
           
  @@ -119,6 +120,66 @@
            
           assertEquals(expected,xml); 
                               
  +    }
  +    
  +    /** Unit test for default output when CDATA option is set */
  +    public void testDefaultOutputWithCDATAOption() throws Exception {
  +        Element element = new Element();
  +        element.setValue("<greeting>What Ho Jeeves!</greeting>");
  +        
  +        StringWriter out = new StringWriter();
  +        out.write("<?xml version='1.0'?>");
  +        BeanWriter writer = new BeanWriter(out);
  +        writer.getXMLIntrospector().setAttributesForPrimitives(false);
  +        XMLBeanInfo elementInfo = writer.getXMLIntrospector().introspect(Element.class);
  +        elementInfo.getElementDescriptor().getElementDescriptors()[0]
  +            .getOptions().addOption(MixedContentEncodingStrategy.ENCODING_OPTION_NAME,
"CDATA");  
  +         
  +        writer.write(element);
  +        
  +        String expected = "<?xml version='1.0'?><Element>\n<value><![CDATA[<greeting>What
Ho Jeeves!</greeting>]]></value>\n</Element>\n";
  +        String xml = out.getBuffer().toString();
  +         
  +        assertEquals(expected,xml); 
  +                            
  +    }
  +    
  +    /** Unit test for default output when character escaping option is set */
  +    public void testDefaultOutputWithCharacterEscapingOption() throws Exception {
  +        Element element = new Element();
  +        element.setValue("<greeting>What Ho Jeeves!</greeting>");
  +        
  +        StringWriter out = new StringWriter();
  +        out.write("<?xml version='1.0'?>");
  +        BeanWriter writer = new BeanWriter(out);
  +        writer.getXMLIntrospector().setAttributesForPrimitives(false);
  +        XMLBeanInfo elementInfo = writer.getXMLIntrospector().introspect(Element.class);
  +        elementInfo.getElementDescriptor().getElementDescriptors()[0]
  +            .getOptions().addOption("org.apache.commons.betwixt.mixed-content-encoding",
"escaped");
  +        writer.write(element);
  +        
  +        String expected = "<?xml version='1.0'?><Element>\n<value>&lt;greeting&gt;What
Ho Jeeves!&lt;/greeting&gt;</value>\n</Element>\n";
  +        String xml = out.getBuffer().toString();
  +         
  +        assertEquals(expected,xml); 
  +    }
  +    
  +    public void testDefaultOutputWithDotBetwixtOptions() throws Exception {
  +        ABCBean bean = new ABCBean();
  +        bean.setA("<strong>weak</strong>");
  +        bean.setB("<strong>weak</strong>");
  +        bean.setC("<strong>weak</strong>");
  +        
  +        StringWriter out = new StringWriter();
  +        out.write("<?xml version='1.0'?>");
  +        BeanWriter writer = new BeanWriter(out);
  +        writer.getXMLIntrospector().setAttributesForPrimitives(false);
  +        writer.write(bean);
  +        
  +        String expected = "<?xml version='1.0'?>" +
            "<greek-abc>\n"
+
            "<alpha><![CDATA[<strong>weak</strong>]]></alpha>\n"
+
            "<beta>&lt;strong&gt;weak&lt;/strong&gt;</beta>\n"
+
            "<gamma>&lt;strong&gt;weak&lt;/strong&gt;</gamma>\n"
+
            "</greek-abc>\n";
  +        String xml = out.getBuffer().toString();
  +         
  +        assertEquals(expected,xml); 
       }
       
       public void testEscapedOutput() throws Exception {
  
  
  

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


Mime
View raw message