📄 htmlfilter.java
字号:
/*
* Copyright 2003-2005 the original author or authors.
* 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 com.jdon.jivejdon.model.message.output.html;
import com.jdon.jivejdon.model.ForumMessage;
import com.jdon.jivejdon.model.message.MessageRendering;
/**
* A ForumMessageFilter that converts newline characters into HTML <br> tags.
* This filter should only be run after any HTML stripping filters.
*/
public class HTMLFilter extends MessageRendering {
private static final char[] QUOTE_ENCODE = """.toCharArray();
private static final char[] AMP_ENCODE = "&".toCharArray();
private static final char[] LT_ENCODE = "<".toCharArray();
private static final char[] GT_ENCODE = ">".toCharArray();
/**
* Clones a new filter that will have the same properties and that
* will wrap around the specified message.
*
* @param message the ForumMessage to wrap the new filter around.
*/
public MessageRendering clone(ForumMessage message){
HTMLFilter filter = new HTMLFilter();
filter.message = message;
return filter;
}
/**
* <b>Overloaded</b> to return the subject of the message with HTML tags
* escaped.
*/
public String applyFilteredSubject() {
return escapeHTMLTags(message.getFilteredSubject());
}
/**
* <b>Overloaded</b> to return the body of the message with HTML tags
* escaped.
*/
public String applyFilteredBody() {
return escapeHTMLTags(message.getFilteredBody());
}
/**
* This method takes a string which may contain HTML tags (ie, <b>,
* <table>, etc) and converts the '<'' and '>' characters to
* their HTML escape sequences.
*
* @param in the text to be converted.
* @return the input string with the characters '<' and '>' replaced
* with their HTML escape sequences.
*/
public static final String escapeHTMLTags(String in) {
if (in == null) {
return null;
}
char ch;
int i=0;
int last=0;
char[] input = in.toCharArray();
int len = input.length;
StringBuffer out = new StringBuffer((int)(len*1.3));
for (; i < len; i++) {
ch = input[i];
if (ch > '>') {
continue;
} else if (ch == '<') {
if (i > last) {
out.append(input, last, i - last);
}
last = i + 1;
out.append(LT_ENCODE);
} else if (ch == '>') {
if (i > last) {
out.append(input, last, i - last);
}
last = i + 1;
out.append(GT_ENCODE);
}
}
if (last == 0) {
return in;
}
if (i > last) {
out.append(input, last, i - last);
}
return out.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -