下面列出了怎么用javax.swing.text.html.parser.ParserDelegator的API类实例代码及写法,或者点击链接到github查看源代码。
@Override public void actionPerformed(ActionEvent e) {
textArea.append(String.format("----%n%s%n", getValue(Action.NAME)));
String id = field.getText().trim();
String text = editorPane.getText();
ParserDelegator delegator = new ParserDelegator();
try {
delegator.parse(new StringReader(text), new HTMLEditorKit.ParserCallback() {
@Override public void handleStartTag(HTML.Tag tag, MutableAttributeSet a, int pos) {
Object attrId = a.getAttribute(HTML.Attribute.ID);
textArea.append(String.format("%[email protected]=%s%n", tag, attrId));
if (id.equals(attrId)) {
textArea.append(String.format("found: pos=%d%n", pos));
int endOffs = text.indexOf('>', pos);
textArea.append(String.format("%s%n", text.substring(pos, endOffs + 1)));
}
}
}, Boolean.TRUE);
} catch (IOException ex) {
ex.printStackTrace();
textArea.append(String.format("%s%n", ex.getMessage()));
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
}
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
private static String parseErrorMsg(UniformInterfaceException ex) {
String entity = ex.getResponse().getEntity(String.class);
try {
ClientResponseParser callback = new ClientResponseParser();
Reader reader = new StringReader(entity);
new ParserDelegator().parse(reader, callback, false);
return callback.getMessage();
} catch (IOException e) {
return "Could not retrieve message from server";
}
}
private static String parseErrorMsg(UniformInterfaceException ex) {
String entity = ex.getResponse().getEntity(String.class);
try {
ClientResponseParser callback = new ClientResponseParser();
Reader reader = new StringReader(entity);
new ParserDelegator().parse(reader, callback, false);
return callback.getMessage();
} catch (IOException e) {
return "Could not retrieve message from server";
}
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
public void parse() throws IOException {
logger.debug("IN");
// put a capo
toConvert = toConvert.replaceAll("<BR>", "|*|");
toConvert = toConvert.replaceAll("<BR/>", "|*|");
toConvert = toConvert.replaceAll("<br>", "|*|");
toConvert = toConvert.replaceAll("<br/>", "|*|");
StringReader stringReader = new StringReader(toConvert);
buffer = new StringBuffer();
ParserDelegator delegator = new ParserDelegator();
// the third parameter is TRUE to ignore charset directive
delegator.parse(stringReader, this, Boolean.FALSE);
stringReader.close();
logger.debug("OUT");
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
static String getParsedContentOneLine(String path) throws Exception {
File f = new File(path);
FileReader fr = new FileReader(f);
ParserDelegator pd = new ParserDelegator();
SBParserCallback sbcallback = new SBParserCallback();
pd.parse(fr, sbcallback, true);
fr.close();
return sbcallback.getStringOneLine();
}
/**
* This method checks the content of {@code content} for valid HTML and
* returns {@code true} if the content could be parsed t HTML. With this, we
* check whether an entry makes use of correct or irregular nested tags.
*
* @param content the html-page which should be checked for correctly nested
* tags, usually an entry's content
* @param zettelnummer the number of the entry that is checked for valid
* html-tags
* @return {@code true} when the content could be successfully parsed to
* HTML, false otherwise
*/
public static boolean isValidHTML(String content, final int zettelnummer) {
// check for valid html
validhtml = true;
// first, we parse the created web-page to catch errors that might occure when parsing
// the entry-content. this might happen when tags are not properly used.
HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback() {
// in case the parsing was not succssful, log that error message.
@Override
public void handleError(String errorMsg, int pos) {
if (errorMsg.toLowerCase().contains("unmatched") || errorMsg.toLowerCase().contains("missing")) {
// if body tag is missing (which is true for all entries), don't log that message
if (!errorMsg.toLowerCase().contains("body")) {
// tell function that HTML is invalid.
validhtml = false;
errorMsg = System.lineSeparator() + "Error when parsing the entry " + String.valueOf(zettelnummer) + "!" + System.lineSeparator() + errorMsg + System.lineSeparator();
Constants.zknlogger.log(Level.SEVERE, errorMsg);
}
}
}
};
// create a string-reader that reads the entry's html-content
Reader reader = new StringReader(content);
// try to parse the html-page
try {
new ParserDelegator().parse(reader, callback, false);
} catch (IOException ex) {
Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
}
return validhtml;
}
public List<String> parse(Reader file) throws Exception {
if (file == null) {
return null;
}
ParserDelegator pd = new ParserDelegator();
try {
pd.parse(file, this, true);
} catch (Exception e) {
throw e;
}
return imgs;
}
private void parse(String html) throws IOException {
Reader reader = new StringReader(html);
ParserDelegator parsers = new ParserDelegator();
parsers.parse(reader, this, Boolean.TRUE);
}
public static void main(String[] args) throws Exception {
new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
public static void main(String[] args) throws Exception {
new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
public static void main(String[] args) throws Exception {
new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
public static void main(String[] args) throws Exception {
new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
public void parse(StringReader in) throws IOException {
new ParserDelegator().parse(in, this, Boolean.TRUE);
}
void parse() throws IOException {
ParserDelegator pd = new ParserDelegator();
formParser = new FormHTMLParser();
Reader r = new StringReader(definition);
pd.parse(r, formParser, true);
}
public void parse(Reader in) throws IOException {
s = new StringBuffer();
ParserDelegator delegator = new ParserDelegator();
// the third parameter is TRUE to ignore charset directive
delegator.parse(in, this, Boolean.TRUE);
}
public static void main(String[] args) throws Exception {
new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
public static void main(String[] args) throws Exception {
new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
public static void main(String[] args) throws Exception {
new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}