You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@excalibur.apache.org by le...@apache.org on 2005/01/06 08:52:48 UTC

svn commit: r124351 - /excalibur/trunk/containerkit/instrument/mgr-http/src/java/org/apache/excalibur/instrument/manager/http/AbstractXMLHandler.java

Author: leif
Date: Wed Jan  5 23:52:45 2005
New Revision: 124351

URL: http://svn.apache.org/viewcvs?view=rev&rev=124351
Log:
Add code donated by Tanuki Software for token replacement.  This was needed to generate XML safe attribute values in generated XML.
Modified:
   excalibur/trunk/containerkit/instrument/mgr-http/src/java/org/apache/excalibur/instrument/manager/http/AbstractXMLHandler.java

Modified: excalibur/trunk/containerkit/instrument/mgr-http/src/java/org/apache/excalibur/instrument/manager/http/AbstractXMLHandler.java
Url: http://svn.apache.org/viewcvs/excalibur/trunk/containerkit/instrument/mgr-http/src/java/org/apache/excalibur/instrument/manager/http/AbstractXMLHandler.java?view=diff&rev=124351&p1=excalibur/trunk/containerkit/instrument/mgr-http/src/java/org/apache/excalibur/instrument/manager/http/AbstractXMLHandler.java&r1=124350&p2=excalibur/trunk/containerkit/instrument/mgr-http/src/java/org/apache/excalibur/instrument/manager/http/AbstractXMLHandler.java&r2=124351
==============================================================================
--- excalibur/trunk/containerkit/instrument/mgr-http/src/java/org/apache/excalibur/instrument/manager/http/AbstractXMLHandler.java	(original)
+++ excalibur/trunk/containerkit/instrument/mgr-http/src/java/org/apache/excalibur/instrument/manager/http/AbstractXMLHandler.java	Wed Jan  5 23:52:45 2005
@@ -54,9 +54,71 @@
     /*---------------------------------------------------------------
      * Methods
      *-------------------------------------------------------------*/
-    protected String makeSafeAttribute( String attribute )
+    /**
+     * Replaces one token with another in a string.
+     *
+     * @param str String with tokens to be replaced.
+     * @param oldToken The token to be replaced.
+     * @param newToken The new token value.
+     *
+     * @return A new String that has had its tokens replaced.
+     */
+    protected final String replaceToken( String str, String oldToken, String newToken )
     {
-        // TODO:
+        int len = str.length();
+        int oldLen = oldToken.length();
+        if ( oldLen == 0 )
+        {
+            // Can't replace nothing.
+            return str;
+        }
+        int newLen = newToken.length();
+        int start = 0;
+        int pos;
+        while ( ( pos = str.indexOf( oldToken, start ) ) >= 0 )
+        {
+            String left;
+            String right;
+            int leftLen;
+            int rightLen;
+            
+            // Get the left side of the string
+            leftLen = pos;
+            if ( leftLen == 0 )
+            {
+                left = "";
+            }
+            else
+            {
+                left = str.substring( 0, pos );
+            }
+            
+            // Get the right side of the string
+            rightLen = len - pos - oldLen;
+            if ( len - pos - oldLen <= 0 )
+            {
+                right = "";
+            }
+            else
+            {
+                right = str.substring( pos + oldLen );
+            }
+            
+            // Rebuild the str variable
+            str = left + newToken + right;
+            len = leftLen + newLen + rightLen;
+            start = leftLen + newLen;
+        }
+        return str;
+    }
+
+    protected final String makeSafeAttribute( String attribute )
+    {
+        attribute = replaceToken( attribute, "&", "&amp;" ); // Must be done first.
+        attribute = replaceToken( attribute, "<", "&lt;" );
+        attribute = replaceToken( attribute, ">", "&gt;" );
+        attribute = replaceToken( attribute, "\"", "&quot;" );
+        
         return attribute;
     }
     

---------------------------------------------------------------------
To unsubscribe, e-mail: scm-unsubscribe@excalibur.apache.org
For additional commands, e-mail: scm-help@excalibur.apache.org