
syntaxhighlighter.SyntaxHighlighterParser Maven / Gradle / Ivy
The newest version!
package syntaxhighlighter;
import syntaxhighlight.ParseResult;
import syntaxhighlight.Parser;
import syntaxhighlighter.brush.Brush;
import syntaxhighlighter.parser.MatchResult;
import syntaxhighlighter.parser.SyntaxHighlighter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* The SyntaxHighlighter parser for syntax highlight.
* @author Chan Wai Shing
*/
public class SyntaxHighlighterParser implements Parser {
protected SyntaxHighlighter syntaxHighlighter;
/**
* The brush to use for this syntax highlighter.
*/
protected Brush brush;
/**
* Indicate whether the HTML-Script option is turned on or not.
*/
private boolean htmlScript;
/**
* The brushes list for HTML-Script.
*/
protected final List htmlScriptBrushesList;
/**
* Constructor.
* @param brush the brush to use
*/
public SyntaxHighlighterParser(Brush brush) {
if (brush == null) {
throw new NullPointerException("argument 'brush' cannot be null");
}
syntaxHighlighter = new SyntaxHighlighter();
this.brush = brush;
htmlScript = false;
htmlScriptBrushesList = new ArrayList();
}
/**
* Get the brush.
* @return brush the brush
*/
public Brush getBrush() {
return brush;
}
/**
* Set the brush to use.
* @param brush the brush
*/
public void setBrush(Brush brush) {
this.brush = brush;
}
/**
* Get the list of HTML Script brushes.
* See also {@link #setHtmlScript(boolean)}.
* @return a copy of the list
*/
public List getHTMLScriptBrushesList() {
return new ArrayList(htmlScriptBrushesList);
}
/**
* Set HTML Script brushes. Note that this will clear all previous recorded
* HTML Script brushes. See also {@link #setHtmlScript(boolean)}.
* The highlighter will re-render the script text pane every time this
* function is invoked (if there is any content).
*
* @param htmlScriptBrushesList the list that contain the brushes
*/
public void setHTMLScriptBrushes(List htmlScriptBrushesList) {
synchronized (this.htmlScriptBrushesList) {
this.htmlScriptBrushesList.clear();
if (htmlScriptBrushesList != null) {
this.htmlScriptBrushesList.addAll(htmlScriptBrushesList);
}
}
}
public void addHTMLScriptBrush(Brush brush) {
if (brush == null) {
return;
}
htmlScriptBrushesList.add(brush);
}
/**
* Clear all HTML Script brushes.
*/
public void clearHTMLScriptBrushes() {
htmlScriptBrushesList.clear();
}
public boolean isHtmlScript() {
return htmlScript;
}
public void setHtmlScript(boolean htmlScript) {
if (this.htmlScript != htmlScript) {
this.htmlScript = htmlScript;
}
}
@Override
public List parse(String content) {
return parse(null, content);
}
@Override
public List parse(String fileExtension, String content) {
List returnList = new ArrayList();
syntaxHighlighter.setHTMLScriptBrushList(htmlScriptBrushesList);
Map> parsedResult = syntaxHighlighter.parse(brush, htmlScript, content.toCharArray(), 0, content.length());
for (List resultList : parsedResult.values()) {
for (MatchResult result : resultList) {
List styleKeyList = null;
if (result.isBold() == Boolean.TRUE) {
styleKeyList = new ArrayList(2);
styleKeyList.add(result.getStyleKey());
styleKeyList.add("bold");
} else {
styleKeyList = Arrays.asList(new String[]{result.getStyleKey()});
}
returnList.add(new ParseResult(result.getOffset(), result.getLength(), styleKeyList));
}
}
return returnList;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy