You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2002/06/20 06:24:31 UTC

cvs commit: xml-cocoon2/src/java/org/apache/cocoon/components/xscript StringBufferContentHandler.java

vgritsenko    2002/06/19 21:24:30

  Modified:    src/java/org/apache/cocoon/components/xscript Tag:
                        cocoon_2_0_3_branch StringBufferContentHandler.java
  Log:
  Escape reserved XML symbols: <, >, & with entities &lt; &gt; &amp;
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.4.2.1   +53 -17    xml-cocoon2/src/java/org/apache/cocoon/components/xscript/StringBufferContentHandler.java
  
  Index: StringBufferContentHandler.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/xscript/StringBufferContentHandler.java,v
  retrieving revision 1.4
  retrieving revision 1.4.2.1
  diff -u -r1.4 -r1.4.2.1
  --- StringBufferContentHandler.java	22 Feb 2002 07:00:15 -0000	1.4
  +++ StringBufferContentHandler.java	20 Jun 2002 04:24:30 -0000	1.4.2.1
  @@ -67,6 +67,7 @@
    * a <code>StringBuffer</code> object.
    *
    * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
  + * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
    * @version CVS $Id$
    * @since August 30, 2001
    */
  @@ -75,7 +76,7 @@
     private Stack namespaces = new Stack();
     ContentHandler contentHandler;
     StringBuffer stringBuffer;
  -  static Marker marker = new Marker();
  +  static Object marker = new Object();
   
     public StringBufferContentHandler(StringBuffer stringBuffer)
     {
  @@ -106,13 +107,15 @@
   
       lastNamespaceIndex = namespaces.size() - 1;
       while(lastNamespaceIndex >= 0
  -          && namespaces.elementAt(lastNamespaceIndex) != marker)
  +          && namespaces.elementAt(lastNamespaceIndex) != marker) {
         lastNamespaceIndex--;
  +    }
   
  -    if (lastNamespaceIndex < 0)
  +    if (lastNamespaceIndex < 0) {
         lastNamespaceIndex = 0;
  -    else if (namespaces.elementAt(lastNamespaceIndex) == marker)
  +    } else if (namespaces.elementAt(lastNamespaceIndex) == marker) {
         lastNamespaceIndex++;
  +    }
   
       namespaces.push(marker);
   
  @@ -135,8 +138,9 @@
                j >= lastNamespaceIndex;
                j--) {
             Object obj = namespaces.elementAt(j);
  -          if (obj == marker)
  +          if (obj == marker) {
               continue;
  +          }
             NPU npu = (NPU)obj;
   
             if (name.equals(npu.prefix)) {
  @@ -148,19 +152,20 @@
           if (!found) {
             namespaces.push(new NPU(name, a.getValue(i)));
           }
  -      }
  -      else {
  +      } else {
           // Normal attribute
  -        stringBuffer.append(" ").append(a.getQName(i))
  -                            .append("=\"").append(a.getValue(i)).append("\"");
  +        stringBuffer.append(" ").append(a.getQName(i)).append("=\"");
  +        escape(stringBuffer, a.getValue(i));
  +        stringBuffer.append("\"");
         }
       }
   
       if (namespaces.size() != 0) {
         for (int i = namespaces.size() - 1; i >= lastNamespaceIndex; i--) {
           Object obj = namespaces.elementAt(i);
  -        if (obj == marker)
  +        if (obj == marker) {
             continue;
  +        }
           NPU npu = (NPU)obj;
           stringBuffer.append(" xmlns:").append(npu.prefix).append("=\"").append(npu.uri).append("\"");
         }
  @@ -182,8 +187,43 @@
     public void characters(char ch[], int start, int len)
       throws SAXException
     {
  -    stringBuffer.append(ch, start, len);
  +    escape(stringBuffer, ch, start, len);
     }
  +
  +
  +    /**
  +     * Copies string into buffer and
  +     * escapes all '<', '&' and '>' chars in the string with
  +     * corresponding entities.
  +     */
  +    private static void escape(StringBuffer buffer, String s) {
  +        char[] ch = s.toCharArray();
  +        escape(buffer, ch, 0, ch.length);
  +    }
  +
  +    /**
  +     * Copies characters from the char array into buffer and
  +     * escapes all '<', '&' and '>' chars with corresponding
  +     * entities.
  +     */
  +    private static void escape(StringBuffer buffer, char ch[], int start, int len) {
  +        for (int i = start; i < start + len; i++) {
  +            switch (ch[i]) {
  +                case '<':
  +                    buffer.append("&lt;");
  +                    break;
  +                case '&':
  +                    buffer.append("&amp;");
  +                    break;
  +                case '>':
  +                    buffer.append("&gt;");
  +                    break;
  +                default:
  +                    buffer.append(ch[i]);
  +                    break;
  +            }
  +        }
  +    }
   }
   
   class NPU
  @@ -199,10 +239,6 @@
   
     public String toString()
     {
  -    return new StringBuffer(this.prefix).append("=").append(this.uri).toString();
  +    return this.prefix + "=" + this.uri;
     }
  -}
  -
  -class Marker
  -{
   }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org