javax.swing.text.html.parser.ParserDelegator Java Examples

The following examples show how to use javax.swing.text.html.parser.ParserDelegator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@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("%s@id=%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);
  }
}
 
Example #2
Source File: ChannelFinderException.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
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";
    }
}
 
Example #3
Source File: bug7165725.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #4
Source File: bug7165725.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #5
Source File: Tools.java    From Zettelkasten with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #6
Source File: bug7165725.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #7
Source File: bug7165725.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #8
Source File: AnalizeWebParse.java    From howsun-javaee-framework with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #9
Source File: bug7165725.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #10
Source File: OlogException.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
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";
           }
}
 
Example #11
Source File: bug7165725.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #12
Source File: bug7165725.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #13
Source File: bug7165725.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #14
Source File: Html2String.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
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");
}
 
Example #15
Source File: bug7165725.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #16
Source File: bug7165725.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #17
Source File: bug7165725.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #18
Source File: bug7165725.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #19
Source File: bug7165725.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #20
Source File: RefactoringPanel.java    From netbeans with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example #21
Source File: RefactoringTestCase.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void parse(StringReader in) throws IOException {
    new ParserDelegator().parse(in, this, Boolean.TRUE);
}
 
Example #22
Source File: bug7011777.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
 
Example #23
Source File: bug7011777.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
 
Example #24
Source File: Html2Text.java    From ObjectLogger with Apache License 2.0 4 votes vote down vote up
private void parse(String html) throws IOException {
    Reader reader = new StringReader(html);
    ParserDelegator parsers = new ParserDelegator();
    parsers.parse(reader, this, Boolean.TRUE);
}
 
Example #25
Source File: bug7011777.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
 
Example #26
Source File: bug7011777.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
 
Example #27
Source File: bug7011777.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
 
Example #28
Source File: bug7011777.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
 
Example #29
Source File: bug7011777.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}
 
Example #30
Source File: bug7011777.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        new ParserDelegator().parse(new StringReader(html), new MyParserCallback(), true);
}