CPD Results

The following document contains the results of PMD's CPD 4.1.

Duplications

File Line
patterntesting/plugin/aspectj/StringUtil.java 36
patterntesting/plugin/aspectj/XMLFormatter.java 132
    private static String xmlEncode(String theString)
    {
        String newString;

        // It is important to replace the "&" first as the other replacements
        // also introduces "&" chars ...
        newString = replace(theString, '&', "&");

        newString = replace(newString, '<', "&lt;");
        newString = replace(newString, '>', "&gt;");
        newString = replace(newString, '\"', "&quot;");

        return newString;
    }

    /**
     * Replaces a character in a string by a substring.
     *
     * @param theBaseString the base string in which to perform replacements
     * @param theChar the char to look for
     * @param theNewString the string with which to replace the char
     * @return the string with replacements done or null if the input string
     *          was null
     */
    public static String replace(String theBaseString, char theChar, 
        String theNewString)
    {
        if (theBaseString == null)
        {
            return null;
        }

        final int len = theBaseString.length() - 1;
        int pos = -1;

        while ((pos = theBaseString.indexOf(theChar, pos + 1)) > -1)
        {
            if (pos == 0)
            {
                final String after = theBaseString.substring(1);

                theBaseString = theNewString + after;
            }
            else if (pos == len)
            {
                final String before = theBaseString.substring(0, pos);

                theBaseString = before + theNewString;
            }
            else
            {
                final String before = theBaseString.substring(0, pos);
                final String after = theBaseString.substring(pos + 1);

                theBaseString = before + theNewString + after;
            }
        }

        return theBaseString;
    }

}