Example #1
0
  /**
   * Perform a (configurable) CSS String <strong>escape</strong> operation on a <tt>char[]</tt>
   * input.
   *
   * <p>This method will perform an escape operation according to the specified {@link
   * CssStringEscapeType} and {@link CssStringEscapeLevel} argument values.
   *
   * <p>All other <tt>char[]</tt>-based <tt>escapeCssString*(...)</tt> methods call this one with
   * preconfigured <tt>type</tt> and <tt>level</tt> values.
   *
   * <p>This method is <strong>thread-safe</strong>.
   *
   * @param text the <tt>char[]</tt> to be escaped.
   * @param offset the position in <tt>text</tt> at which the escape operation should start.
   * @param len the number of characters in <tt>text</tt> that should be escaped.
   * @param writer the <tt>java.io.Writer</tt> to which the escaped result will be written. Nothing
   *     will be written at all to this writer if <tt>text</tt> is <tt>null</tt>.
   * @param type the type of escape operation to be performed, see {@link CssStringEscapeType}.
   * @param level the escape level to be applied, see {@link CssStringEscapeLevel}.
   * @throws IOException if an input/output exception occurs
   */
  public static void escapeCssString(
      final char[] text,
      final int offset,
      final int len,
      final Writer writer,
      final CssStringEscapeType type,
      final CssStringEscapeLevel level)
      throws IOException {

    if (writer == null) {
      throw new IllegalArgumentException("Argument 'writer' cannot be null");
    }

    if (type == null) {
      throw new IllegalArgumentException("The 'type' argument cannot be null");
    }

    if (level == null) {
      throw new IllegalArgumentException("The 'level' argument cannot be null");
    }

    final int textLen = (text == null ? 0 : text.length);

    if (offset < 0 || offset > textLen) {
      throw new IllegalArgumentException(
          "Invalid (offset, len). offset=" + offset + ", len=" + len + ", text.length=" + textLen);
    }

    if (len < 0 || (offset + len) > textLen) {
      throw new IllegalArgumentException(
          "Invalid (offset, len). offset=" + offset + ", len=" + len + ", text.length=" + textLen);
    }

    CssStringEscapeUtil.escape(text, offset, len, writer, type, level);
  }
Example #2
0
  /**
   * Perform a (configurable) CSS String <strong>escape</strong> operation on a <tt>String</tt>
   * input.
   *
   * <p>This method will perform an escape operation according to the specified {@link
   * CssStringEscapeType} and {@link CssStringEscapeLevel} argument values.
   *
   * <p>All other <tt>String</tt>-based <tt>escapeCssString*(...)</tt> methods call this one with
   * preconfigured <tt>type</tt> and <tt>level</tt> values.
   *
   * <p>This method is <strong>thread-safe</strong>.
   *
   * @param text the <tt>String</tt> to be escaped.
   * @param type the type of escape operation to be performed, see {@link CssStringEscapeType}.
   * @param level the escape level to be applied, see {@link CssStringEscapeLevel}.
   * @return The escaped result <tt>String</tt>. As a memory-performance improvement, will return
   *     the exact same object as the <tt>text</tt> input argument if no escaping modifications were
   *     required (and no additional <tt>String</tt> objects will be created during processing).
   *     Will return <tt>null</tt> if <tt>text</tt> is <tt>null</tt>.
   */
  public static String escapeCssString(
      final String text, final CssStringEscapeType type, final CssStringEscapeLevel level) {

    if (type == null) {
      throw new IllegalArgumentException("The 'type' argument cannot be null");
    }

    if (level == null) {
      throw new IllegalArgumentException("The 'level' argument cannot be null");
    }

    return CssStringEscapeUtil.escape(text, type, level);
  }