📄 cmshtmldecorator.java
字号:
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_HTML_DECORATOR_APPEND_TEXT_2, m_config, text));
}
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(text)) {
// split the input into single words
List wordList = splitAsList(text, delimiters, false, true);
for (int i = 0; i < wordList.size(); i++) {
String word = (String)wordList.get(i);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_HTML_DECORATOR_PROCESS_WORD_2,
word,
new Boolean(mustDecode(word, wordList, i))));
}
// test if the word must be decoded
if (mustDecode(word, wordList, i)) {
word = Translate.decode(word);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_HTML_DECORATOR_DECODED_WORD_1, word));
}
}
// test if the word is no delimiter
// try to get a decoration if it is not
CmsDecorationObject decObj = null;
if (!hasDelimiter(word, delimiters)) {
decObj = (CmsDecorationObject)m_decorations.get(word);
}
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_HTML_DECORATOR_DECORATION_FOUND_2,
decObj,
word));
}
// if there is a decoration obejct for this word, we must do the decoration
// if not, we must test if the word itself consits of several parts diveded by
// second level delimiters
if (decObj == null) {
if (hasDelimiter(word, DELIMITERS_SECOND_LEVEL) && recursive) {
// add the following symbol if possbile to allow the second level decoration
// test to make a forward lookup as well
String secondLevel = word;
if (i < wordList.size() - 1) {
if (!((String)wordList.get(i + 1)).equals(" ")) {
secondLevel = word + (String)wordList.get(i + 1);
i++;
}
}
appendText(secondLevel, DELIMITERS_SECOND_LEVEL, false);
} else {
// make a forward lookup to the next elements of the word list to check
// if the combination of word and delimiter can be found as a decoration key
// an example would be "Dr." wich must be decorated with "Doctor"
StringBuffer decKey = new StringBuffer();
decKey.append(word);
// calculate how much forward looking must be made
int forwardLookup = wordList.size() - i - 1;
if (forwardLookup > FORWARD_LOOKUP) {
forwardLookup = FORWARD_LOOKUP;
}
if (i < wordList.size() - forwardLookup) {
for (int j = 1; j <= forwardLookup; j++) {
decKey.append(wordList.get(i + j));
decObj = (CmsDecorationObject)m_decorations.get(decKey.toString());
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_HTML_DECORATOR_DECORATION_FOUND_FWL_3,
decObj,
word,
new Integer(j)));
}
if (decObj != null) {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_HTML_DECORATOR_DECORATION_APPEND_DECORATION_1,
decObj.getContentDecoration(m_config)));
}
// decorate the current word with the following delimiter
m_result.append(decObj.getContentDecoration(m_config));
// important, we must skip the next element of the list
i += j;
break;
}
}
}
if (decObj == null) {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_HTML_DECORATOR_DECORATION_APPEND_WORD_1,
word));
}
// no decoration was found, use the word alone
m_result.append(word);
}
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_HTML_DECORATOR_DECORATION_APPEND_DECORATION_1,
decObj.getContentDecoration(m_config)));
}
// decorate the current word
m_result.append(decObj.getContentDecoration(m_config));
}
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_HTML_DECORATOR_DECORATION_APPEND_ORIGINALTEXT_1,
text));
}
m_result.append(text);
}
}
/**
* Processes a HTML string and adds text decorations according to the decoration configuration.<p>
*
* @param html a string holding the HTML code that should be added with text decorations
* @param encoding the encoding to be used
* @return a HTML string with the decorations added.
* @throws Exception if something goes wrong
*/
public String doDecoration(String html, String encoding) throws Exception {
return process(html, encoding);
}
/**
* Checks if a word contains a given delimiter.<p>
*
* @param word the word to test
* @param delimiters array of delimiter strings
* @return true if the word contains the delimiter, false otherwiese
*/
private boolean hasDelimiter(String word, String[] delimiters) {
boolean delim = false;
for (int i = 0; i < delimiters.length; i++) {
if (word.indexOf(delimiters[i]) > -1) {
delim = true;
break;
}
}
return delim;
}
/**
* Checks if a word must be decoded.<p>
*
* The given word is compated to a negative list of words which must not be decoded.
* @param word the word to test
* @return true if the word must be decoded, false otherweise
*/
private boolean mustDecode(String word, List wordList, int count) {
boolean decode = true;
String nextWord = null;
if (count < wordList.size() - 1) {
nextWord = (String)wordList.get(count + 1);
}
// test if the current word contains a "&" and the following with a ";"
// if so, we must not decode the word
if (nextWord != null && word.indexOf("&") > -1 && nextWord.startsWith(";")) {
return false;
} else {
// now scheck if the word matches one of the non decoder tokens
for (int i = 0; i < NON_TRANSLATORS.length; i++) {
if (word.startsWith(NON_TRANSLATORS[i])) {
decode = false;
break;
}
}
}
return decode;
}
/**
* Resets the first occurance flags of all decoration objects.<p>
*
* This is nescessary if decoration objects should be used for processing more than once. *
*/
public void resetDecorationDefinitions() {
m_config.resetMarkedDecorations();
}
/**
* @see org.htmlparser.visitors.NodeVisitor#visitStringNode(org.htmlparser.Text)
*/
public void visitStringNode(Text text) {
appendText(text.toPlainTextString(), DELIMITERS, true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -