package net.sf.jasperreports.engine.util;

public class JRStringUtil {
  public static String replaceCRwithLF(String text) {
    if (text != null) {
      int length = text.length();
      char[] chars = text.toCharArray();
      int r = 0;
      boolean dirty = false;
      for (int i = 0; i < length; i++) {
        char ch = chars[i];
        if (ch == '\r') {
          dirty = true;
          if (i + 1 < length && chars[i + 1] == '\n') {
            r++;
          } else {
            chars[i - r] = '\n';
          } 
        } else {
          chars[i - r] = ch;
        } 
      } 
      return dirty ? new String(chars, 0, length - r) : text;
    } 
    return null;
  }
  
  public static String xmlEncode(String text) {
    int length = text.length();
    if (text != null && length > 0) {
      StringBuffer ret = new StringBuffer(length * 12 / 10);
      int last = 0;
      for (int i = 0; i < length; i++) {
        char c = text.charAt(i);
        switch (c) {
          case '&':
            if (last < i)
              ret.append(text.substring(last, i)); 
            last = i + 1;
            ret.append("&amp;");
            break;
          case '>':
            if (last < i)
              ret.append(text.substring(last, i)); 
            last = i + 1;
            ret.append("&gt;");
            break;
          case '<':
            if (last < i)
              ret.append(text.substring(last, i)); 
            last = i + 1;
            ret.append("&lt;");
            break;
          case '"':
            if (last < i)
              ret.append(text.substring(last, i)); 
            last = i + 1;
            ret.append("&quot;");
            break;
          case '\'':
            if (last < i)
              ret.append(text.substring(last, i)); 
            last = i + 1;
            ret.append("&apos;");
            break;
        } 
      } 
      if (last < length)
        ret.append(text.substring(last)); 
      return ret.toString();
    } 
    return text;
  }
  
  public static String htmlEncode(String text) {
    int length = text.length();
    if (text != null && length > 0) {
      StringBuffer ret = new StringBuffer(length * 12 / 10);
      boolean isEncodeSpace = true;
      int last = 0;
      for (int i = 0; i < length; i++) {
        char c = text.charAt(i);
        switch (c) {
          case ' ':
            if (isEncodeSpace) {
              if (last < i)
                ret.append(text.substring(last, i)); 
              last = i + 1;
              ret.append("&nbsp;");
              isEncodeSpace = false;
              break;
            } 
            isEncodeSpace = true;
            break;
          case '&':
            if (last < i)
              ret.append(text.substring(last, i)); 
            last = i + 1;
            ret.append("&amp;");
            isEncodeSpace = false;
            break;
          case '>':
            if (last < i)
              ret.append(text.substring(last, i)); 
            last = i + 1;
            ret.append("&gt;");
            isEncodeSpace = false;
            break;
          case '<':
            if (last < i)
              ret.append(text.substring(last, i)); 
            last = i + 1;
            ret.append("&lt;");
            isEncodeSpace = false;
            break;
          case '"':
            if (last < i)
              ret.append(text.substring(last, i)); 
            last = i + 1;
            ret.append("&quot;");
            isEncodeSpace = false;
            break;
          case '\n':
            if (last < i)
              ret.append(text.substring(last, i)); 
            last = i + 1;
            ret.append("<br/>");
            isEncodeSpace = false;
            break;
          default:
            isEncodeSpace = false;
            break;
        } 
      } 
      if (last < length)
        ret.append(text.substring(last)); 
      return ret.toString();
    } 
    return text;
  }
  
  public static String getLiteral(String name) {
    if (isValidLiteral(name))
      return name; 
    StringBuffer buffer = new StringBuffer(name.length() + 5);
    char[] literalChars = new char[name.length()];
    name.getChars(0, literalChars.length, literalChars, 0);
    for (int i = 0; i < literalChars.length; i++) {
      if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) {
        buffer.append(literalChars[i]);
      } else if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i])) {
        buffer.append(literalChars[i]);
      } else {
        buffer.append(literalChars[i]);
      } 
    } 
    return buffer.toString();
  }
  
  private static boolean isValidLiteral(String literal) {
    boolean result = true;
    char[] literalChars = new char[literal.length()];
    literal.getChars(0, literalChars.length, literalChars, 0);
    for (int i = 0; i < literalChars.length; i++) {
      if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) {
        result = false;
        break;
      } 
      if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i])) {
        result = false;
        break;
      } 
    } 
    return result;
  }
}
