Example usage for java.io CharConversionException CharConversionException

List of usage examples for java.io CharConversionException CharConversionException

Introduction

In this page you can find the example usage for java.io CharConversionException CharConversionException.

Prototype

public CharConversionException(String s) 

Source Link

Document

This provides a detailed message.

Usage

From source file:Main.java

/**
 * Escape passed string as XML element content (<code>&lt;</code>, 
 * <code>&amp;</code> and <code>><code> in <code>]]></code> sequences).
 *
 * @param val a string to be escaped//w w  w .ja  va  2 s . c o  m
 *
 * @return escaped value
 * @throws CharConversionException if val contains an improper XML character
 *
 * @since 1.40
 */
public static String toElementContent(String val) throws CharConversionException {
    if (val == null)
        throw new CharConversionException("null"); // NOI18N

    if (checkContentCharacters(val))
        return val;

    StringBuffer buf = new StringBuffer();

    for (int i = 0; i < val.length(); i++) {
        char ch = val.charAt(i);
        if ('<' == ch) {
            buf.append("&lt;");
            continue;
        } else if ('&' == ch) {
            buf.append("&amp;");
            continue;
        } else if ('>' == ch && i > 1 && val.charAt(i - 2) == ']' && val.charAt(i - 1) == ']') {
            buf.append("&gt;");
            continue;
        }
        buf.append(ch);
    }
    return buf.toString();

}

From source file:Main.java

/** 
 * Escape passed string as XML attibute value
 * (<code>&lt;</code>, <code>&amp;</code>, <code>'</code> and <code>"</code>
 * will be escaped./*from  www  . j  a v  a2  s  .com*/
 * Note: An XML processor returns normalized value that can be different.
 *
 * @param val a string to be escaped
 *
 * @return escaped value
 * @throws CharConversionException if val contains an improper XML character
 *
 * @since 1.40
 */
public static String toAttributeValue(String val) throws CharConversionException {

    if (val == null)
        throw new CharConversionException("null"); // NOI18N

    if (checkAttributeCharacters(val))
        return val;

    StringBuffer buf = new StringBuffer();

    for (int i = 0; i < val.length(); i++) {
        char ch = val.charAt(i);
        if ('<' == ch) {
            buf.append("&lt;");
            continue;
        } else if ('&' == ch) {
            buf.append("&amp;");
            continue;
        } else if ('\'' == ch) {
            buf.append("&apos;");
            continue;
        } else if ('"' == ch) {
            buf.append("&quot;");
            continue;
        }
        buf.append(ch);
    }
    return buf.toString();

}

From source file:Main.java

/**
 * Check if all passed characters match XML expression [2].
 * @return true if no escaping necessary
 * @throws CharConversionException if contains invalid chars
 *///from w w  w.  j a v  a 2 s  . c  o m
private static boolean checkAttributeCharacters(String chars) throws CharConversionException {
    boolean escape = false;
    for (int i = 0; i < chars.length(); i++) {
        char ch = chars.charAt(i);
        if (((int) ch) <= 93) { // we are UNICODE ']'
            switch (ch) {
            case 0x9:
            case 0xA:
            case 0xD:
                continue;
            case '\'':
            case '"':
            case '<':
            case '&':
                escape = true;
                continue;
            default:
                if (((int) ch) < 0x20) {
                    throw new CharConversionException("Invalid XML character &#" + ((int) ch) + ";.");
                }
            }
        }
    }
    return escape == false;
}

From source file:Main.java

/**
 * Check if all passed characters match XML expression [2].
 * @return true if no escaping necessary
 * @throws CharConversionException if contains invalid chars
 *///from   w  w  w .j  av  a2s  .  c  om
private static boolean checkContentCharacters(String chars) throws CharConversionException {
    boolean escape = false;
    for (int i = 0; i < chars.length(); i++) {
        char ch = chars.charAt(i);
        if (((int) ch) <= 93) { // we are UNICODE ']'
            switch (ch) {
            case 0x9:
            case 0xA:
            case 0xD:
                continue;
            case '>': // only ]]> is dangerous
                if (escape)
                    continue;
                escape = i > 0 && (chars.charAt(i - 1) == ']');
                continue;
            case '<':
            case '&':
                escape = true;
                continue;
            default:
                if (((int) ch) < 0x20) {
                    throw new CharConversionException("Invalid XML character &#" + ((int) ch) + ";.");
                }
            }
        }
    }
    return escape == false;
}

From source file:Main.java

/**
 * Escape passed string as XML element content (<code>&lt;</code>,
 * <code>&amp;</code> and <code>&gt;</code> in <code>]]&gt;</code>
 * sequences)./*from  w w  w .jav  a2s.c  o m*/
 *
 * @param val a string to be escaped
 *
 * @return escaped value
 * @throws CharConversionException if val contains an improper XML character
 *
 * @since 1.40
 */
public static String toElementContent(String val) throws CharConversionException {
    if (val == null) {
        throw new CharConversionException("null"); // NOI18N
    }

    if (checkContentCharacters(val)) {
        return val;
    }

    StringBuilder buf = new StringBuilder();

    for (int i = 0; i < val.length(); i++) {
        char ch = val.charAt(i);

        if ('<' == ch) {
            buf.append("&lt;");

            continue;
        } else if ('&' == ch) {
            buf.append("&amp;");

            continue;
        } else if (('>' == ch) && (i > 1) && (val.charAt(i - 2) == ']') && (val.charAt(i - 1) == ']')) {
            buf.append("&gt;");

            continue;
        }

        buf.append(ch);
    }

    return buf.toString();
}

From source file:Main.java

/**
 * Escape passed string as XML attibute value (<code>&lt;</code>,
 * <code>&amp;</code>, <code>'</code> and <code>"</code> will be escaped.
 * Note: An XML processor returns normalized value that can be different.
 *
 * @param val a string to be escaped/*from   www .  ja  va2  s  .  co  m*/
 *
 * @return escaped value
 * @throws CharConversionException if val contains an improper XML character
 *
 * @since 1.40
 */
public static String toAttributeValue(String val) throws CharConversionException {
    if (val == null) {
        throw new CharConversionException("null"); // NOI18N
    }

    if (checkAttributeCharacters(val)) {
        return val;
    }

    StringBuilder buf = new StringBuilder();

    for (int i = 0; i < val.length(); i++) {
        char ch = val.charAt(i);

        if ('<' == ch) {
            buf.append("&lt;");

            continue;
        } else if ('&' == ch) {
            buf.append("&amp;");

            continue;
        } else if ('\'' == ch) {
            buf.append("&apos;");

            continue;
        } else if ('"' == ch) {
            buf.append("&quot;");

            continue;
        }

        buf.append(ch);
    }

    return buf.toString();
}

From source file:Main.java

/**
 * Check if all passed characters match XML expression [2].
 *
 * @return true if no escaping necessary
 * @throws CharConversionException if contains invalid chars
 *//* w  w  w .  j  ava  2  s .com*/
private static boolean checkAttributeCharacters(String chars) throws CharConversionException {
    boolean escape = false;

    for (int i = 0; i < chars.length(); i++) {
        char ch = chars.charAt(i);

        if (ch <= 93) { // we are UNICODE ']'

            switch (ch) {
            case 0x9:
            case 0xA:
            case 0xD:

                continue;

            case '\'':
            case '"':
            case '<':
            case '&':
                escape = true;

                continue;

            default:

                if (ch < 0x20) {
                    throw new CharConversionException("Invalid XML character &#" + ((int) ch) + ";.");
                }
            }
        }
    }

    return escape == false;
}

From source file:Main.java

/**
 * Check if all passed characters match XML expression [2].
 *
 * @return true if no escaping necessary
 * @throws CharConversionException if contains invalid chars
 *///from   w  ww  .ja  va  2s .c  o m
private static boolean checkContentCharacters(String chars) throws CharConversionException {
    boolean escape = false;

    for (int i = 0; i < chars.length(); i++) {
        char ch = chars.charAt(i);

        if (ch <= 93) { // we are UNICODE ']'

            switch (ch) {
            case 0x9:
            case 0xA:
            case 0xD:

                continue;

            case '>': // only ]]> is dangerous

                if (escape) {
                    continue;
                }

                escape = (i > 0) && (chars.charAt(i - 1) == ']');

                continue;

            case '<':
            case '&':
                escape = true;

                continue;

            default:

                if (ch < 0x20) {
                    throw new CharConversionException("Invalid XML character &#" + ((int) ch) + ";.");
                }
            }
        }
    }

    return escape == false;
}

From source file:org.apache.jk.common.HandlerRequest.java

/**
 * Parse host.// ww w . ja v  a 2  s. c  om
 */
private void parseHost(MessageBytes valueMB, Request request) throws IOException {

    if (valueMB == null || valueMB.isNull()) {
        // HTTP/1.0
        // Default is what the socket tells us. Overriden if a host is 
        // found/parsed
        request.setServerPort(request.getLocalPort());
        request.serverName().duplicate(request.localName());
        return;
    }

    ByteChunk valueBC = valueMB.getByteChunk();
    byte[] valueB = valueBC.getBytes();
    int valueL = valueBC.getLength();
    int valueS = valueBC.getStart();
    int colonPos = -1;
    CharChunk hostNameC = (CharChunk) request.getNote(HOSTBUFFER);
    if (hostNameC == null) {
        hostNameC = new CharChunk(valueL);
        request.setNote(HOSTBUFFER, hostNameC);
    }
    hostNameC.recycle();

    boolean ipv6 = (valueB[valueS] == '[');
    boolean bracketClosed = false;
    for (int i = 0; i < valueL; i++) {
        char b = (char) valueB[i + valueS];
        hostNameC.append(b);
        if (b == ']') {
            bracketClosed = true;
        } else if (b == ':') {
            if (!ipv6 || bracketClosed) {
                colonPos = i;
                break;
            }
        }
    }

    if (colonPos < 0) {
        if (request.scheme().equalsIgnoreCase("https")) {
            // 80 - Default HTTTP port
            request.setServerPort(443);
        } else {
            // 443 - Default HTTPS port
            request.setServerPort(80);
        }
        request.serverName().setChars(hostNameC.getChars(), hostNameC.getStart(), hostNameC.getLength());
    } else {

        request.serverName().setChars(hostNameC.getChars(), hostNameC.getStart(), colonPos);

        int port = 0;
        int mult = 1;
        for (int i = valueL - 1; i > colonPos; i--) {
            int charValue = HexUtils.DEC[(int) valueB[i + valueS]];
            if (charValue == -1) {
                // Invalid character
                throw new CharConversionException("Invalid char in port: " + valueB[i + valueS]);
            }
            port = port + (charValue * mult);
            mult = 10 * mult;
        }
        request.setServerPort(port);

    }

}