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-sandbox/betwixt/src/java/org/apache/commons/betwixt/io BeanWriter.java
Date Sun, 10 Feb 2002 19:18:42 GMT
rdonkin     02/02/10 11:18:42

  Modified:    betwixt/src/java/org/apache/commons/betwixt/io
                        BeanWriter.java
  Log:
  Added code to escape text in attribute values and element body text
  
  Revision  Changes    Path
  1.13      +85 -7     jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java
  
  Index: BeanWriter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- BeanWriter.java	30 Jan 2002 19:35:37 -0000	1.12
  +++ BeanWriter.java	10 Feb 2002 19:18:42 -0000	1.13
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java,v
1.12 2002/01/30 19:35:37 rdonkin Exp $
  - * $Revision: 1.12 $
  - * $Date: 2002/01/30 19:35:37 $
  + * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java,v
1.13 2002/02/10 19:18:42 rdonkin Exp $
  + * $Revision: 1.13 $
  + * $Date: 2002/02/10 19:18:42 $
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id: BeanWriter.java,v 1.12 2002/01/30 19:35:37 rdonkin Exp $
  + * $Id: BeanWriter.java,v 1.13 2002/02/10 19:18:42 rdonkin Exp $
    */
   package org.apache.commons.betwixt.io;
   
  @@ -98,10 +98,21 @@
     * The indent string used is set by {@link #setIndent}.
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.12 $
  +  * @version $Revision: 1.13 $
     */
   public class BeanWriter {
   
  +    /** Escaped <code>&lt;</code> entity */
  +    private final static String LESS_THAN_ENTITY = "&lt;";
  +    /** Escaped <code>&gt;</code> entity */
  +    private final static String GREATER_THAN_ENTITY = "&gt;";
  +    /** Escaped <code>&amp;</code> entity */
  +    private final static String AMPERSAND_ENTITY = "&amp;";
  +    /** Escaped <code>'</code> entity */
  +    private final static String APOSTROPHE_ENTITY = "&apos;";
  +    /** Escaped <code>"</code> entity */
  +    private final static String QUOTE_ENTITY = "&quot;";
  +
       /** Introspector used */
       private XMLIntrospector introspector = new XMLIntrospector();
       /** Where the output goes */
  @@ -344,7 +355,7 @@
               if ( expression != null ) {
                   Object value = expression.evaluate( context );
                   if ( value != null ) {
  -                    String text = value.toString();
  +                    String text = escapeBodyValue(value);
                       if ( text != null && text.length() > 0 ) {
                           writer.write( ">" );
                           writer.write( text );
  @@ -382,7 +393,7 @@
           if ( expression != null ) {
               Object value = expression.evaluate( context );
               if ( value != null ) {
  -                String text = value.toString();
  +                String text = escapeAttributeValue(value);
                   if ( text != null && text.length() > 0 ) {
                       writer.write( " " );
                       writer.write( attributeDescriptor.getQualifiedName() );
  @@ -412,4 +423,71 @@
               }
           }
       }
  +    
  +    /** 
  +     * <p>Escape the <code>toString</code> of the given object.
  +     * For use as body text.</p>
  +     */
  +    protected String escapeBodyValue(Object value) {
  +        StringBuffer buffer = new StringBuffer(value.toString());
  +        for (int i=0, size = buffer.length(); i <size; i++) {
  +            switch (buffer.charAt(i)) {
  +                case '<':
  +                    buffer.replace(i, i+1, LESS_THAN_ENTITY);
  +                    size += 3;
  +                    i+=3;
  +                    break;
  +                 case '>':
  +                    buffer.replace(i, i+1, GREATER_THAN_ENTITY);
  +                    size += 3;
  +                    i += 3;
  +                    break;
  +                 case '&':
  +                    buffer.replace(i, i+1, AMPERSAND_ENTITY);
  +                    size += 4;
  +                    i += 4;
  +                    break;        
  +            }
  +        }
  +        return buffer.toString();
  +    }
  +
  +    /** 
  +     * <p>Escape the <code>toString</code> of the given object.
  +     * For use in an attribute value.</p>
  +     */
  +    protected String escapeAttributeValue(Object value) {
  +        StringBuffer buffer = new StringBuffer(value.toString());
  +        for (int i=0, size = buffer.length(); i <size; i++) {
  +            switch (buffer.charAt(i)) {
  +                case '<':
  +                    buffer.replace(i, i+1, LESS_THAN_ENTITY);
  +                    size += 3;
  +                    i+=3;
  +                    break;
  +                 case '>':
  +                    buffer.replace(i, i+1, GREATER_THAN_ENTITY);
  +                    size += 3;
  +                    i += 3;
  +                    break;
  +                 case '&':
  +                    buffer.replace(i, i+1, AMPERSAND_ENTITY);
  +                    size += 4;
  +                    i += 4;
  +                    break;
  +                 case '\'':
  +                    buffer.replace(i, i+1, APOSTROPHE_ENTITY);
  +                    size += 4;
  +                    i += 4;
  +                    break;
  +                 case '\"':
  +                    buffer.replace(i, i+1, QUOTE_ENTITY);
  +                    size += 5;
  +                    i += 5;
  +                    break;           
  +            }
  +        }
  +        return buffer.toString();
  +    }
  +
   }
  
  
  

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


Mime
View raw message