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();
}
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;
});
}
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();
}
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();
}
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;
});
}
Aggregations