Search in sources :

Example 1 with ParserDelegator

use of javax.swing.text.html.parser.ParserDelegator in project languagetool by languagetool-org.

the class RetainLineBreakTransferHandler method extractText.

private String extractText(Reader reader) {
    StringBuilder result = new StringBuilder();
    HTMLEditorKit.ParserCallback parserCallback = new HTMLEditorKit.ParserCallback() {

        @Override
        public void handleText(char[] data, int pos) {
            result.append(data);
        }

        @Override
        public void handleStartTag(HTML.Tag tag, MutableAttributeSet attribute, int pos) {
        }

        @Override
        public void handleEndTag(HTML.Tag tag, int pos) {
        }

        @Override
        public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet a, int pos) {
            if (tag.equals(HTML.Tag.BR)) {
                result.append('\n');
            }
        }

        @Override
        public void handleComment(char[] data, int pos) {
        }

        @Override
        public void handleError(String errMsg, int pos) {
        }
    };
    try {
        new ParserDelegator().parse(reader, parserCallback, true);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return result.toString();
}
Also used : MutableAttributeSet(javax.swing.text.MutableAttributeSet) ParserDelegator(javax.swing.text.html.parser.ParserDelegator) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) IOException(java.io.IOException)

Example 2 with ParserDelegator

use of javax.swing.text.html.parser.ParserDelegator in project intellij-community by JetBrains.

the class PyPIPackageUtil method parsePackageVersionsFromArchives.

@NotNull
private static List<String> parsePackageVersionsFromArchives(@NotNull String archivesUrl) throws IOException {
    return HttpRequests.request(archivesUrl).userAgent(getUserAgent()).connect(request -> {
        final List<String> versions = new ArrayList<>();
        final Reader reader = request.getReader();
        new ParserDelegator().parse(reader, new HTMLEditorKit.ParserCallback() {

            HTML.Tag myTag;

            @Override
            public void handleStartTag(HTML.Tag tag, MutableAttributeSet set, int i) {
                myTag = tag;
            }

            @Override
            public void handleText(@NotNull char[] data, int pos) {
                if (myTag != null && "a".equals(myTag.toString())) {
                    String packageVersion = String.valueOf(data);
                    final String suffix = ".tar.gz";
                    if (!packageVersion.endsWith(suffix))
                        return;
                    packageVersion = StringUtil.trimEnd(packageVersion, suffix);
                    versions.add(splitNameVersion(packageVersion).second);
                }
            }
        }, true);
        versions.sort(PackageVersionComparator.VERSION_COMPARATOR.reversed());
        return versions;
    });
}
Also used : ParserDelegator(javax.swing.text.html.parser.ParserDelegator) Reader(java.io.Reader) FileReader(java.io.FileReader) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) HTML(javax.swing.text.html.HTML) MutableAttributeSet(javax.swing.text.MutableAttributeSet) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ParserDelegator

use of javax.swing.text.html.parser.ParserDelegator in project intellij-community by JetBrains.

the class HyperlinkLabel method setHtmlText.

public void setHtmlText(String text) {
    HTMLEditorKit.Parser parse = new ParserDelegator();
    final HighlightedText highlightedText = new HighlightedText();
    try {
        parse.parse(new StringReader(text), new HTMLEditorKit.ParserCallback() {

            private TextAttributes currentAttributes = null;

            @Override
            public void handleText(char[] data, int pos) {
                highlightedText.appendText(data, currentAttributes);
            }

            @Override
            public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
                if (t == HTML.Tag.B) {
                    currentAttributes = BOLD_ATTRIBUTES;
                } else if (t == HTML.Tag.A) {
                    currentAttributes = myAnchorAttributes;
                }
            }

            @Override
            public void handleEndTag(HTML.Tag t, int pos) {
                currentAttributes = null;
            }
        }, false);
    } catch (IOException e) {
        LOG.error(e);
    }
    highlightedText.applyToComponent(this);
    final JComponent parent = (JComponent) getParent();
    parent.revalidate();
    parent.repaint();
    adjustSize();
}
Also used : ParserDelegator(javax.swing.text.html.parser.ParserDelegator) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) HTML(javax.swing.text.html.HTML) IOException(java.io.IOException) MutableAttributeSet(javax.swing.text.MutableAttributeSet) StringReader(java.io.StringReader) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 4 with ParserDelegator

use of javax.swing.text.html.parser.ParserDelegator in project jdk8u_jdk by JetBrains.

the class bug7165725 method getParsedContentOneLine.

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();
}
Also used : ParserDelegator(javax.swing.text.html.parser.ParserDelegator) FileReader(java.io.FileReader) File(java.io.File)

Example 5 with ParserDelegator

use of javax.swing.text.html.parser.ParserDelegator in project intellij-community by JetBrains.

the class PyPIPackageUtil method parsePyPIListFromWeb.

@NotNull
private static List<String> parsePyPIListFromWeb(@NotNull String url, boolean isSimpleIndex) throws IOException {
    LOG.debug("Fetching index of all packages available on " + url);
    return HttpRequests.request(url).userAgent(getUserAgent()).connect(request -> {
        final List<String> packages = new ArrayList<>();
        final Reader reader = request.getReader();
        new ParserDelegator().parse(reader, new HTMLEditorKit.ParserCallback() {

            boolean inTable = false;

            HTML.Tag myTag;

            @Override
            public void handleStartTag(@NotNull HTML.Tag tag, @NotNull MutableAttributeSet set, int i) {
                myTag = tag;
                if (!isSimpleIndex) {
                    if ("table".equals(tag.toString())) {
                        inTable = !inTable;
                    }
                    if (inTable && "a".equals(tag.toString())) {
                        packages.add(String.valueOf(set.getAttribute(HTML.Attribute.HREF)));
                    }
                }
            }

            @Override
            public void handleText(@NotNull char[] data, int pos) {
                if (isSimpleIndex) {
                    if (myTag != null && "a".equals(myTag.toString())) {
                        packages.add(String.valueOf(data));
                    }
                }
            }

            @Override
            public void handleEndTag(@NotNull HTML.Tag tag, int i) {
                if (!isSimpleIndex) {
                    if ("table".equals(tag.toString())) {
                        inTable = !inTable;
                    }
                }
            }
        }, true);
        return packages;
    });
}
Also used : ParserDelegator(javax.swing.text.html.parser.ParserDelegator) Reader(java.io.Reader) FileReader(java.io.FileReader) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) HTML(javax.swing.text.html.HTML) MutableAttributeSet(javax.swing.text.MutableAttributeSet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ParserDelegator (javax.swing.text.html.parser.ParserDelegator)6 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)5 MutableAttributeSet (javax.swing.text.MutableAttributeSet)4 FileReader (java.io.FileReader)3 HTML (javax.swing.text.html.HTML)3 IOException (java.io.IOException)2 Reader (java.io.Reader)2 NotNull (org.jetbrains.annotations.NotNull)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 File (java.io.File)1 StringReader (java.io.StringReader)1 DTD (javax.swing.text.html.parser.DTD)1 AppContext (sun.awt.AppContext)1