XMLFormatter.replace sometimes cuts the result
----------------------------------------------

         Key: CACTUS-210
         URL: http://issues.apache.org/jira/browse/CACTUS-210
     Project: Cactus
        Type: Bug
  Components: Framework  
    Versions: 1.7    
    Reporter: Thorsten Harders
    Priority: Minor


The replace method in the org.apache.cactus.internal.server.runner.XMLFormatter 
might cut off characters...

Example:
replace("12x456x89", 'x', "<X>")
Will return 12<X>456<X>

This method can even produce an infinite loop, if the char that is searched for 
is included in the result String.
replace("12x456x890", 'x', "<x>")
But that should not be the case within cactus.

Also the performance of this method is really bad, because String 
concatenations are used. A StringBuffer used to produce the result would be 
better.

Here is my suggestion for a replacement....

    private static String replace(String theBaseString, char theChar,
            String theNewString) {
        
        if (theBaseString == null) {
            return null;
        }

        int pos = theBaseString.indexOf(theChar);
        if (pos < 0) return theBaseString;
        int lastPos = 0;
        
        StringBuffer result = new StringBuffer();
            while (pos > -1) {
                result.append(theBaseString.substring(lastPos, pos));
                result.append(theNewString);

                lastPos = pos + 1;
                pos = theBaseString.indexOf(theChar, lastPos);
        }
        if (lastPos < theBaseString.length()) 
            result.append(theBaseString.substring(lastPos));
        return result.toString();
    }






-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to