/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.javadoc.internal.doclets.formats.html.markup;
import jdk.javadoc.internal.doclets.toolkit.Content;
import java.io.IOException;
import java.io.Writer;
A representation of HTML entities.
/**
* A representation of HTML entities.
*/
public class Entity extends Content {
public static final Entity LESS_THAN = new Entity("<");
public static final Entity GREATER_THAN = new Entity(">");
public static final Entity AMPERSAND = new Entity("&");
public static final Entity NO_BREAK_SPACE = new Entity(" ");
public static final Entity ZERO_WIDTH_SPACE = new Entity("​") {
@Override
public int charCount() {
return 0;
}
};
public final String text;
private Entity(String text) {
this.text = text;
}
@Override
public void add(Content content) {
throw new UnsupportedOperationException();
}
@Override
public void add(CharSequence stringContent) {
throw new UnsupportedOperationException();
}
@Override
public boolean write(Writer writer, boolean atNewline) throws IOException {
writer.write(text);
return false;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public int charCount() {
return 1;
}
Escapes the special HTML characters in a given string using the appropriate
entities.
Params: - s – the string to escape
Returns: the string with all of the HTML characters escaped
/**
* Escapes the special HTML characters in a given string using the appropriate
* entities.
*
* @param s the string to escape
* @return the string with all of the HTML characters escaped
*/
static String escapeHtmlChars(CharSequence s) {
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
// only start building a new string if we need to
case '<': case '>': case '&':
StringBuilder sb = new StringBuilder(s.subSequence(0, i));
escapeHtmlChars(s, i, sb);
return sb.toString();
}
}
return s.toString();
}
Escapes the special HTML characters in a given string using the appropriate
entities, appending the results into a string builder.
Params: - s – the string
- sb – the string builder
/**
* Escapes the special HTML characters in a given string using the appropriate
* entities, appending the results into a string builder.
*
* @param s the string
* @param sb the string builder
*/
static void escapeHtmlChars(CharSequence s, StringBuilder sb) {
escapeHtmlChars(s, 0, sb);
}
private static void escapeHtmlChars(CharSequence s, int start, StringBuilder sb) {
for (int i = start ; i < s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
case '<': sb.append(Entity.LESS_THAN.text); break;
case '>': sb.append(Entity.GREATER_THAN.text); break;
case '&': sb.append(Entity.AMPERSAND.text); break;
default: sb.append(ch); break;
}
}
}
}