/*
* Copyright 2004-2010 the Seasar Foundation and the Others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.slim3.util;
/**
* A utility class for HTML.
*
* @author higa
* @since 1.0.0
*
*/
public final class HtmlUtil {
private static final int HIGHEST_SPECIAL = '>';
private static char[][] specialCharactersRepresentation =
new char[HIGHEST_SPECIAL + 1][];
static {
specialCharactersRepresentation['&'] = "&".toCharArray();
specialCharactersRepresentation['<'] = "<".toCharArray();
specialCharactersRepresentation['>'] = ">".toCharArray();
specialCharactersRepresentation['"'] = """.toCharArray();
specialCharactersRepresentation['\''] = "'".toCharArray();
}
/**
* Escapes string that could be interpreted as HTML.
*
* @param input
* the input value
* @return the escaped value
*/
public static String escape(String input) {
int start = 0;
char[] arrayBuffer = input.toCharArray();
int length = arrayBuffer.length;
StringBuilder escapedBuffer = null;
for (int i = 0; i < length; i++) {
char c = arrayBuffer[i];
if (c <= HIGHEST_SPECIAL) {
char[] escaped = specialCharactersRepresentation[c];
if (escaped != null) {
if (start == 0) {
escapedBuffer = new StringBuilder(length + 5);
}
if (start < i) {
escapedBuffer.append(arrayBuffer, start, i - start);
}
start = i + 1;
escapedBuffer.append(escaped);
}
}
}
if (start == 0) {
return input;
}
if (start < length) {
escapedBuffer.append(arrayBuffer, start, length - start);
}
return escapedBuffer.toString();
}
private HtmlUtil() {
}
}