StringReader sourceReader = new StringReader(content);
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument)htmlKit.createDefaultDocument();
HTMLEditorKit.Parser parser = new ParserDelegator();
final HTMLEditorKit.ParserCallback docCallback = htmlDoc.getReader(0);
HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback() {
@Override
public void flush() throws BadLocationException {
docCallback.flush();
}
@Override
public void handleComment(char[] data, int pos) {
docCallback.handleComment(data, pos);
}
@Override
public void handleEndOfLineString(String eol) {
docCallback.handleEndOfLineString(eol);
}
@Override
public void handleEndTag(Tag t, int pos) {
docCallback.handleEndTag(t, pos);
}
@Override
public void handleError(String errorMsg, int pos) {
docCallback.handleError(errorMsg, pos);
if (errorMsg.startsWith("req.att") ||
errorMsg.startsWith("invalid.tagatt") ||
errorMsg.startsWith("javascript") ||
errorMsg.startsWith("tag.unrecognized") ||
errorMsg.startsWith("end.unrecognized") ||
// Java 5
errorMsg.startsWith("tag.ignore") ||
// Java 6 (beta2)
errorMsg.startsWith("unmatched.endtag script")) {
if (SHOW_DEBUGGING_OUTPUT) {
System.out.println("Spurious error: " + errorMsg);
}
} else {
fail("Failed to parse: " + errorMsg);
}
}
@Override
public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos) {
docCallback.handleSimpleTag(t, a, pos);
}
@Override
public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
docCallback.handleStartTag(t, a, pos);
}
@Override
public void handleText(char[] data, int pos) {
docCallback.handleText(data, pos);
}
};
try {
parser.parse(sourceReader, callback, true);
} catch (IOException e) {
throw new RuntimeException(e);
}
////