首页

关于apache的bsf提供的StringUtils字符串工具类对常用字符串转换、校验判断等操作

标签:StringUtils,字符串工具类,bsf,apache     发布时间:2018-02-25   

一、前言

关于apachebsf源码包中的org.apache.bsf.util.StringUtils字符串工具类,对类名称格式转换classNameToVarName、字符串指定特殊字符串去除cleanString、指定url字符串内容获取getContentAsString等,详情见源码说明。

二、源码说明

1.StringUtils工具类

package org.apache.bsf.util;@b@@b@import java.beans.Introspector;@b@import java.io.BufferedReader;@b@import java.io.File;@b@import java.io.FileNotFoundException;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.io.Reader;@b@import java.io.StringReader;@b@import java.net.MalformedURLException;@b@import java.net.URL;@b@import java.util.StringTokenizer;@b@import java.util.Vector;@b@@b@public class StringUtils@b@{@b@  public static final String lineSeparator = System.getProperty("line.separator", "\n");@b@  public static final String lineSeparatorStr = cleanString(lineSeparator);@b@@b@  public static String classNameToVarName(String className)@b@  {@b@    int arrayDim = 0;@b@@b@    while (className.endsWith("[]"))@b@    {@b@      className = className.substring(0, className.length() - 2);@b@      ++arrayDim;@b@    }@b@@b@    int iLastPeriod = className.lastIndexOf(46);@b@    String varName = Introspector.decapitalize((iLastPeriod != -1) ? className.substring(iLastPeriod + 1) : className);@b@@b@    if (arrayDim > 0)@b@    {@b@      varName = varName + "_" + arrayDim + "D";@b@    }@b@@b@    return getValidIdentifierName(varName);@b@  }@b@@b@  public static String cleanString(String str)@b@  {@b@    if (str == null) {@b@      return null;@b@    }@b@@b@    char[] charArray = str.toCharArray();@b@    StringBuffer sBuf = new StringBuffer();@b@@b@    for (int i = 0; i < charArray.length; ++i)@b@      switch (charArray[i])@b@      {@b@      case '"':@b@        sBuf.append("\\\"");@b@        break;@b@      case '\\':@b@        sBuf.append("\\\\");@b@        break;@b@      case '\n':@b@        sBuf.append("\\n");@b@        break;@b@      case '\r':@b@        sBuf.append("\\r");@b@        break;@b@      default:@b@        sBuf.append(charArray[i]);@b@      }@b@@b@@b@    return sBuf.toString();@b@  }@b@@b@  public static String getChars(int numberOfChars, char theChar)@b@  {@b@    if (numberOfChars <= 0)@b@      return "";@b@@b@    StringBuffer sRet = new StringBuffer(numberOfChars);@b@@b@    for (int i = 0; i < numberOfChars; ++i)@b@      sRet.append(theChar);@b@@b@    return sRet.toString();@b@  }@b@@b@  public static String getClassName(Class targetClass)@b@  {@b@    String className = targetClass.getName();@b@@b@    return ((targetClass.isArray()) ? parseDescriptor(className) : className);@b@  }@b@@b@  public static String getCommaListFromVector(Vector sourceVector) {@b@    StringBuffer strBuf = new StringBuffer();@b@@b@    for (int i = 0; i < sourceVector.size(); ++i)@b@    {@b@      strBuf.append(((i > 0) ? ", " : "") + sourceVector.elementAt(i));@b@    }@b@@b@    return strBuf.toString();@b@  }@b@@b@  public static Reader getContentAsReader(URL url)@b@    throws SecurityException, IllegalArgumentException, IOException@b@  {@b@    if (url == null)@b@    {@b@      throw new IllegalArgumentException("URL cannot be null.");@b@    }@b@@b@    try@b@    {@b@      Object content = url.getContent();@b@@b@      if (content == null)@b@      {@b@        throw new IllegalArgumentException("No content.");@b@      }@b@@b@      if (content instanceof InputStream)@b@      {@b@        Reader in = new InputStreamReader((InputStream)content);@b@@b@        if (in.ready())@b@        {@b@          return in;@b@        }@b@@b@        throw new FileNotFoundException();@b@      }@b@@b@      throw new IllegalArgumentException("This URL points to a: " + getClassName(content.getClass()));@b@    }@b@    catch (SecurityException e)@b@    {@b@      throw new SecurityException("Your JVM's SecurityManager has disallowed this.");@b@    }@b@    catch (FileNotFoundException e)@b@    {@b@      throw new FileNotFoundException("This file was not found: " + url);@b@    }@b@  }@b@@b@  public static String getContentAsString(URL url)@b@    throws SecurityException, IllegalArgumentException, IOException@b@  {@b@    return IOUtils.getStringFromReader(getContentAsReader(url));@b@  }@b@@b@  public static String getSafeString(String scriptStr)@b@  {@b@    BufferedReader in = new BufferedReader(new StringReader(scriptStr));@b@    StringBuffer strBuf = new StringBuffer();@b@@b@    String previousLine = null;@b@    try@b@    {@b@      while ((tempLine = in.readLine()) != null)@b@      {@b@        String tempLine;@b@        if (previousLine != null)@b@        {@b@          strBuf.append("\"" + previousLine + lineSeparatorStr + "\" +" + lineSeparator);@b@        }@b@@b@        previousLine = cleanString(tempLine);@b@      }@b@    }@b@    catch (IOException e)@b@    {@b@    }@b@@b@    strBuf.append("\"" + ((previousLine != null) ? previousLine : "") + "\"" + lineSeparator);@b@@b@    return strBuf.toString();@b@  }@b@@b@  public static URL getURL(URL contextURL, String spec)@b@    throws MalformedURLException@b@  {@b@    return getURL(contextURL, spec, 1);@b@  }@b@@b@  private static URL getURL(URL contextURL, String spec, int recursiveDepth)@b@    throws MalformedURLException@b@  {@b@    URL url = null;@b@    try@b@    {@b@      url = new URL(contextURL, spec);@b@      try@b@      {@b@        url.openStream();@b@      }@b@      catch (IOException ioe1)@b@      {@b@        throw new MalformedURLException("This file was not found: " + url);@b@      }@b@    }@b@    catch (MalformedURLException e1)@b@    {@b@      url = new URL("file", "", spec);@b@      try@b@      {@b@        url.openStream();@b@      }@b@      catch (IOException ioe2)@b@      {@b@        if (contextURL != null)@b@        {@b@          String contextFileName = contextURL.getFile();@b@          String parentName = new File(contextFileName).getParent();@b@@b@          if ((parentName != null) && (recursiveDepth < 3))@b@          {@b@            return getURL(new URL("file", "", parentName + '/'), spec, recursiveDepth + 1);@b@          }@b@@b@        }@b@@b@        throw new MalformedURLException("This file was not found: " + url);@b@      }@b@    }@b@@b@    return url;@b@  }@b@@b@  public static String getValidIdentifierName(String identifierName) {@b@    if ((identifierName == null) || (identifierName.length() == 0))@b@      return null;@b@@b@    StringBuffer strBuf = new StringBuffer();@b@@b@    char[] chars = identifierName.toCharArray();@b@@b@    strBuf.append((Character.isJavaIdentifierStart(chars[0])) ? chars[0] : '_');@b@@b@    for (int i = 1; i < chars.length; ++i)@b@    {@b@      strBuf.append((Character.isJavaIdentifierPart(chars[i])) ? chars[i] : '_');@b@    }@b@@b@    return strBuf.toString();@b@  }@b@@b@  public static boolean isValidIdentifierName(String identifierName) {@b@    if ((identifierName == null) || (identifierName.length() == 0))@b@      return false;@b@@b@    char[] chars = identifierName.toCharArray();@b@@b@    if (!(Character.isJavaIdentifierStart(chars[0])))@b@      return false;@b@@b@    for (int i = 1; i < chars.length; ++i)@b@      if (!(Character.isJavaIdentifierPart(chars[i])))@b@        return false;@b@@b@    return true;@b@  }@b@@b@  public static boolean isValidPackageName(String packageName) {@b@    if (packageName == null)@b@      return false;@b@    if (packageName.length() == 0)@b@    {@b@      return true;@b@    }@b@    StringTokenizer strTok = new StringTokenizer(packageName, ".", true);@b@@b@    if (strTok.countTokens() % 2 != 1) {@b@      return false;@b@    }@b@@b@    if (!(isValidIdentifierName(strTok.nextToken())))@b@      return false;@b@    do@b@    {@b@      if (!(strTok.hasMoreTokens()))@b@        break label93;@b@@b@      if (!(strTok.nextToken().equals("."))) {@b@        return false;@b@      }@b@@b@      if (!(strTok.hasMoreTokens())) break label91;@b@    }@b@    while (isValidIdentifierName(strTok.nextToken()));@b@    return false;@b@@b@    label91: return false;@b@@b@    label93: return true;@b@  }@b@@b@  private static String parseDescriptor(String className)@b@  {@b@    char[] classNameChars = className.toCharArray();@b@    int arrayDim = 0;@b@    int i = 0;@b@@b@    while (classNameChars[i] == '[')@b@    {@b@      ++arrayDim;@b@      ++i;@b@    }@b@@b@    StringBuffer classNameBuf = new StringBuffer();@b@@b@    switch (classNameChars[(i++)]) { case 'B':@b@      classNameBuf.append("byte");@b@      break;@b@    case 'C':@b@      classNameBuf.append("char");@b@      break;@b@    case 'D':@b@      classNameBuf.append("double");@b@      break;@b@    case 'F':@b@      classNameBuf.append("float");@b@      break;@b@    case 'I':@b@      classNameBuf.append("int");@b@      break;@b@    case 'J':@b@      classNameBuf.append("long");@b@      break;@b@    case 'S':@b@      classNameBuf.append("short");@b@      break;@b@    case 'Z':@b@      classNameBuf.append("boolean");@b@      break;@b@    case 'L':@b@      classNameBuf.append(classNameChars, i, classNameChars.length - i - 1);@b@    case 'E':@b@    case 'G':@b@    case 'H':@b@    case 'K':@b@    case 'M':@b@    case 'N':@b@    case 'O':@b@    case 'P':@b@    case 'Q':@b@    case 'R':@b@    case 'T':@b@    case 'U':@b@    case 'V':@b@    case 'W':@b@    case 'X':@b@    case 'Y': } for (i = 0; i < arrayDim; ++i)@b@      classNameBuf.append("[]");@b@@b@    return classNameBuf.toString();@b@  }@b@}

2.IOUtils类

package org.apache.bsf.util;@b@@b@import java.io.BufferedReader;@b@import java.io.IOException;@b@import java.io.PrintWriter;@b@import java.io.Reader;@b@import java.io.StringWriter;@b@@b@public class IOUtils@b@{@b@  static boolean debug = false;@b@@b@  public static String getStringFromReader(Reader reader)@b@    throws IOException@b@  {@b@    BufferedReader bufIn = new BufferedReader(reader);@b@    StringWriter swOut = new StringWriter();@b@    PrintWriter pwOut = new PrintWriter(swOut);@b@@b@    while ((tempLine = bufIn.readLine()) != null) {@b@      String tempLine;@b@      pwOut.println(tempLine);@b@    }@b@@b@    pwOut.flush();@b@@b@    return swOut.toString();@b@  }@b@}