
patterntesting.tool.aspectj.StringUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-tools
Show all versions of patterntesting-tools
PatternTesting Tools (patterntesting-tools) is the container for
tools around PatternTesting like the Ant extensions and Maven plugin.
The newest version!
/*
*========================================================================
*
* Copyright 2001-2004 Vincent Massol & Matt Smith.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*========================================================================
*/
package patterntesting.tool.aspectj;
/**
* @author Matt Smith
*
* @version $Id: StringUtil.java,v 1.1 2008/12/07 14:13:55 oboehm Exp $
*
*/
public class StringUtil
{
/**
* Escapes reserved XML characters.
*
* @param theString the string to escape
* @return the escaped string
*/
public 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, '<', "<");
newString = replace(newString, '>', ">");
newString = replace(newString, '\"', """);
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;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy