| Method from org.apache.bsf.util.StringUtils Detail: |
public static String classNameToVarName(String className) {
// Might represent an array.
int arrayDim = 0;
while (className.endsWith("[]"))
{
className = className.substring(0, className.length() - 2);
arrayDim++;
}
int iLastPeriod = className.lastIndexOf('.");
String varName = Introspector.decapitalize(
iLastPeriod != -1
? className.substring(iLastPeriod + 1)
: className);
if (arrayDim > 0)
{
varName += "_" + arrayDim + "D";
}
return getValidIdentifierName(varName);
}
|
public static String cleanString(String str) {
if (str == null)
return null;
else
{
char[] charArray = str.toCharArray();
StringBuffer sBuf = new StringBuffer();
for (int i = 0; i < charArray.length; i++)
switch (charArray[i])
{
case '\"" : sBuf.append("\\\"");
break;
case '\\" : sBuf.append("\\\\");
break;
case '\n" : sBuf.append("\\n");
break;
case '\r" : sBuf.append("\\r");
break;
default : sBuf.append(charArray[i]);
break;
}
return sBuf.toString();
}
}
|
public static String getChars(int numberOfChars,
char theChar) {
if (numberOfChars < = 0)
return "";
StringBuffer sRet = new StringBuffer(numberOfChars);
for (int i = 0; i < numberOfChars; i++)
sRet.append(theChar);
return sRet.toString();
}
Get a string consisting of numberOfChars theChars. |
public static String getClassName(Class targetClass) {
String className = targetClass.getName();
return targetClass.isArray() ? parseDescriptor(className) : className;
}
|
public static String getCommaListFromVector(Vector sourceVector) {
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < sourceVector.size(); i++)
{
strBuf.append((i > 0 ? ", " : "") +
sourceVector.elementAt(i));
}
return strBuf.toString();
}
|
public static Reader getContentAsReader(URL url) throws IllegalArgumentException, IOException, SecurityException {
if (url == null)
{
throw new IllegalArgumentException("URL cannot be null.");
}
try
{
Object content = url.getContent();
if (content == null)
{
throw new IllegalArgumentException("No content.");
}
if (content instanceof InputStream)
{
Reader in = new InputStreamReader((InputStream)content);
if (in.ready())
{
return in;
}
else
{
throw new FileNotFoundException();
}
}
else
{
throw new IllegalArgumentException((content instanceof String)
? (String)content
: "This URL points to a: " +
StringUtils.getClassName(content.getClass()));
}
}
catch (SecurityException e)
{
throw new SecurityException("Your JVM's SecurityManager has disallowed this.");
}
catch (FileNotFoundException e)
{
throw new FileNotFoundException("This file was not found: " + url);
}
}
|
public static String getContentAsString(URL url) throws IllegalArgumentException, IOException, SecurityException {
return IOUtils.getStringFromReader(getContentAsReader(url));
}
|
public static String getSafeString(String scriptStr) {
BufferedReader in = new BufferedReader(new StringReader(scriptStr));
StringBuffer strBuf = new StringBuffer();
String tempLine,
previousLine = null;
try
{
while ((tempLine = in.readLine()) != null)
{
if (previousLine != null)
{
strBuf.append("\"" + previousLine + lineSeparatorStr + "\" +" +
lineSeparator);
}
previousLine = cleanString(tempLine);
}
}
catch (IOException e)
{
}
strBuf.append("\"" + (previousLine != null ? previousLine : "") + "\"" +
lineSeparator);
return strBuf.toString();
}
|
public static URL getURL(URL contextURL,
String spec) throws MalformedURLException {
return getURL(contextURL, spec, 1);
}
|
public static String getValidIdentifierName(String identifierName) {
if (identifierName == null || identifierName.length() == 0)
return null;
StringBuffer strBuf = new StringBuffer();
char[] chars = identifierName.toCharArray();
strBuf.append(Character.isJavaIdentifierStart(chars[0])
? chars[0]
: '_"
);
for (int i = 1; i < chars.length; i++)
{
strBuf.append(Character.isJavaIdentifierPart(chars[i])
? chars[i]
: '_"
);
}
return strBuf.toString();
}
|
public static boolean isValidIdentifierName(String identifierName) {
if (identifierName == null || identifierName.length() == 0)
return false;
char[] chars = identifierName.toCharArray();
if (!Character.isJavaIdentifierStart(chars[0]))
return false;
for (int i = 1; i < chars.length; i++)
if (!Character.isJavaIdentifierPart(chars[i]))
return false;
return true;
}
|
public static boolean isValidPackageName(String packageName) {
if (packageName == null)
return false;
else if (packageName.length() == 0)
// Empty is ok.
return true;
StringTokenizer strTok = new StringTokenizer(packageName, ".", true);
// Should have an odd number of tokens (including '.' delimiters).
if (strTok.countTokens() % 2 != 1)
return false;
// Must start with a valid identifier name.
if (!isValidIdentifierName(strTok.nextToken()))
return false;
// ... followed by 0 or more of ".ValidIdentifier".
while (strTok.hasMoreTokens())
{
// Must be a '.'.
if (!strTok.nextToken().equals("."))
return false;
// Must be a valid identifier name.
if (strTok.hasMoreTokens())
{
if (!isValidIdentifierName(strTok.nextToken()))
return false;
}
else
return false;
}
return true;
}
|