Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
refactoring and extension of html parser to create markdown
Michael Peter Christen7aa11433fdc9
diff --git a/source/net/yacy/document/parser/html/AbstractScraper.java b/source/net/yacy/document/parser/html/AbstractScraper.javaindex 1f4a5fd0b..7c3f58b82 100644--- a/source/net/yacy/document/parser/html/AbstractScraper.java+++ b/source/net/yacy/document/parser/html/AbstractScraper.java@@ -56,12 +56,12 @@ public abstract class AbstractScraper implements Scraper {}@Override- public boolean isTag0(final String tag) {+ public boolean isSingetonTag(final String tag) {return (this.tags0 != null) && (this.tags0.contains(tag.toLowerCase()));}@Override- public boolean isTag1(final String tag) {+ public boolean isPairedTag(final String tag) {return (this.tags1 != null) && (this.tags1.contains(tag.toLowerCase()));}diff --git a/source/net/yacy/document/parser/html/ContentScraper.java b/source/net/yacy/document/parser/html/ContentScraper.javaindex b107167d0..e684ef296 100644--- a/source/net/yacy/document/parser/html/ContentScraper.java+++ b/source/net/yacy/document/parser/html/ContentScraper.java@@ -87,7 +87,7 @@ public class ContentScraper extends AbstractScraper implements Scraper {private static final Pattern LB = Pattern.compile("\n");- public enum TagType {+ public enum TagOp {/** Tag with no end tag (see https://www.w3.org/TR/html51/syntax.html#void-elements),* optional end tag (see https://www.w3.org/TR/html51/syntax.html#optional-tags),* or where processing directly only the start tag is desired. */@@ -96,84 +96,97 @@ public class ContentScraper extends AbstractScraper implements Scraper {pair;}- public enum TagName {- html(TagType.singleton), // scraped as singleton to get attached properties like 'lang'- body(TagType.singleton), // scraped as singleton to get attached properties like 'class'- img(TagType.singleton),- base(TagType.singleton),- frame(TagType.singleton),- meta(TagType.singleton),- area(TagType.singleton),- link(TagType.singleton),- embed(TagType.singleton), //added by [MN]- param(TagType.singleton), //added by [MN]- iframe(TagType.singleton), // scraped as singleton to get such iframes that have no closing tag- source(TagType.singleton), // html5 (part of <video> <audio>) - scaped like embed-- a(TagType.pair),- h1(TagType.pair),- h2(TagType.pair),- h3(TagType.pair),- h4(TagType.pair),- h5(TagType.pair),- h6(TagType.pair),- title(TagType.pair),- b(TagType.pair),- em(TagType.pair),- strong(TagType.pair),- u(TagType.pair),- i(TagType.pair),- li(TagType.pair),- dt(TagType.pair),- dd(TagType.pair),- script(TagType.pair),- span(TagType.pair),- div(TagType.pair),- nav(TagType.pair),- article(TagType.pair), // html5- time(TagType.pair), // html5 <time datetime>+ public enum TagType {+ html(TagOp.singleton, "", ""), // scraped as singleton to get attached properties like 'lang'+ body(TagOp.singleton, "", ""), // scraped as singleton to get attached properties like 'class'+ img(TagOp.singleton, "", ""),+ base(TagOp.singleton, "", ""),+ frame(TagOp.singleton, "", ""),+ meta(TagOp.singleton, "", ""),+ area(TagOp.singleton, "", ""),+ link(TagOp.singleton, "", ""),+ embed(TagOp.singleton, "", ""), //added by [MN]+ param(TagOp.singleton, "", ""), //added by [MN]+ iframe(TagOp.singleton, "", ""), // scraped as singleton to get such iframes that have no closing tag+ source(TagOp.singleton, "", ""), // html5 (part of <video> <audio>) - scaped like embed+ hr(TagOp.singleton, "\n---\n", ""),++ a(TagOp.pair, "", ""),+ p(TagOp.pair, "\n\n", ""),+ h1(TagOp.pair, "\n\n# ", "\n\n"),+ h2(TagOp.pair, "\n\n## ", "\n\n"),+ h3(TagOp.pair, "\n\n### ", "\n\n"),+ h4(TagOp.pair, "\n\n#### ", "\n\n"),+ h5(TagOp.pair, "\n\n##### ", "\n\n"),+ h6(TagOp.pair, "\n\n###### ", "\n\n"),+ title(TagOp.pair, "", ""),+ i(TagOp.pair, "*", "*"),+ em(TagOp.pair, "*", "*"),+ b(TagOp.pair, "**", "**"),+ strong(TagOp.pair, "**", "**"),+ u(TagOp.pair, "_", "_"),+ li(TagOp.pair, "\n- ", "\n"),+ dt(TagOp.pair, "", ""),+ dd(TagOp.pair, "", ""),+ script(TagOp.pair, "", ""),+ pre(TagOp.pair, "\n\n^^^\n", "\n^^^\n\n"),+ span(TagOp.pair, "", ""),+ div(TagOp.pair, "\n\n", ""),+ nav(TagOp.pair, "", ""),+ article(TagOp.pair, "", ""), // html5+ time(TagOp.pair, "", ""), // html5 <time datetime>// tags used to capture tag content- // TODO: considere to use </head> or <body> as trigger to scape for text content- style(TagType.pair); // embedded css (if not declared as tag content is parsed as text)+ // TODO: consider to use </head> or <body> as trigger to scrape for text content+ style(TagOp.pair, "", ""), // embedded css (if not declared as tag content is parsed as text)++ unknown(TagOp.singleton, "", ""); // used when a tag is not recognized- public TagType type;- private TagName(final TagType type) {+ public TagOp type;+ public String mdPrefix, mdSuffix;+ private TagType(final TagOp type, String mdPrefix, String mdSuffix) {this.type = type;+ this.mdPrefix = mdPrefix;+ this.mdSuffix = mdSuffix;}}public static class Tag {- public String name;+ public TagType tagType;+ public String tagName;public Properties opts;public CharBuffer content;private TagValency tv;public Tag(final String name, final TagValency defaultValency) {- this.name = name;+ this.tagName = name;+ try {this.tagType = TagType.valueOf(name.toLowerCase());} catch (final IllegalArgumentException e) {this.tagType = TagType.unknown;}this.tv = defaultValency;this.opts = new Properties();this.content = new CharBuffer(MAX_TAGSIZE);}public Tag(final String name, final TagValency defaultValency, final Properties opts) {- this.name = name;+ this.tagName = name;+ try {this.tagType = TagType.valueOf(name.toLowerCase());} catch (final IllegalArgumentException e) {this.tagType = TagType.unknown;}this.tv = defaultValency;this.opts = opts;this.content = new CharBuffer(MAX_TAGSIZE);}public Tag(final String name, final TagValency defaultValency, final Properties opts, final CharBuffer content) {- this.name = name;+ this.tagName = name;+ try {this.tagType = TagType.valueOf(name.toLowerCase());} catch (final IllegalArgumentException e) {this.tagType = TagType.unknown;}this.tv = defaultValency;this.opts = opts;this.content = content;}public void close() {- this.name = null;+ this.tagType = null;+ this.tagName = null;this.opts = null;if (this.content != null) this.content.close();this.content = null;}@Overridepublic String toString() {- return "<" + this.name + " " + this.opts + ">" + this.content + "</" + this.name + ">";+ return "<" + this.tagName + " " + this.opts + ">" + this.content + "</" + this.tagName + ">";}/** @return true when this tag should be ignored from scraping */@@ -190,9 +203,9 @@ public class ContentScraper extends AbstractScraper implements Scraper {// all these tags must be given in lowercase, because the tags from the files are compared in lowercasestatic {- for (final TagName tag: TagName.values()) {- if (tag.type == TagType.singleton) linkTags0.add(tag.name());- if (tag.type == TagType.pair) linkTags1.add(tag.name());+ for (final TagType tag: TagType.values()) {+ if (tag.type == TagOp.singleton) linkTags0.add(tag.name());+ if (tag.type == TagOp.pair) linkTags1.add(tag.name());}//<iframe src="../../../index.htm" name="SELFHTML_in_a_box" width="90%" height="400">}@@ -219,7 +232,7 @@ public class ContentScraper extends AbstractScraper implements Scraper {private List<String>[] headlines;private final ClusteredScoreMap<String> bold, italic, underline;private final List<String> li, dt, dd;- private final CharBuffer content;+ final CharBuffer content;private final EventListenerList htmlFilterEventListeners;private double lon, lat;private AnchorURL canonical, publisher;@@ -356,7 +369,7 @@ public class ContentScraper extends AbstractScraper implements Scraper {if (insideTag.tv == TagValency.IGNORE) {return;}- if ((TagName.script.name().equals(insideTag.name) || TagName.style.name().equals(insideTag.name))) {+ if ((TagType.script.name().equals(insideTag.tagName) || TagType.style.name().equals(insideTag.tagName))) {return;}}@@ -383,44 +396,48 @@ public class ContentScraper extends AbstractScraper implements Scraper {if (q < 0) break location;int r = p;while (r-- > 1) {- if (newtext[r] == ' ') {- r--;- if (newtext[r] == 'N') {- this.lat = Double.parseDouble(new String(newtext, r + 2, p - r - 2)) +- Double.parseDouble(new String(newtext, p + pl + 1, q - p - pl - 1)) / 60.0d;- if (this.lon != 0.0d) break location;- s = q + 6;- continue location;- }- if (newtext[r] == 'S') {- this.lat = -Double.parseDouble(new String(newtext, r + 2, p - r - 2)) -- Double.parseDouble(new String(newtext, p + pl + 1, q - p - pl - 1)) / 60.0d;- if (this.lon != 0.0d) break location;- s = q + 6;- continue location;- }- if (newtext[r] == 'E') {- this.lon = Double.parseDouble(new String(newtext, r + 2, p - r - 2)) +- Double.parseDouble(new String(newtext, p + pl + 1, q - p - pl - 1)) / 60.0d;- if (this.lat != 0.0d) break location;- s = q + 6;- continue location;- }- if (newtext[r] == 'W') {- this.lon = -Double.parseDouble(new String(newtext, r + 2, p - r - 2)) -- Double.parseDouble(new String(newtext, p + 2, q - p - pl - 1)) / 60.0d;- if (this.lat != 0.0d) break location;- s = q + 6;- continue location;+ try {+ if (newtext[r] == ' ') {+ r--;+ if (newtext[r] == 'N') {+ this.lat = Double.parseDouble(new String(newtext, r + 2, p - r - 2)) ++ Double.parseDouble(new String(newtext, p + pl + 1, q - p - pl - 1)) / 60.0d;+ if (this.lon != 0.0d) break location;+ s = q + 6;+ continue location;+ }+ if (newtext[r] == 'S') {+ this.lat = -Double.parseDouble(new String(newtext, r + 2, p - r - 2)) -+ Double.parseDouble(new String(newtext, p + pl + 1, q - p - pl - 1)) / 60.0d;+ if (this.lon != 0.0d) break location;+ s = q + 6;+ continue location;+ }+ if (newtext[r] == 'E') {+ this.lon = Double.parseDouble(new String(newtext, r + 2, p - r - 2)) ++ Double.parseDouble(new String(newtext, p + pl + 1, q - p - pl - 1)) / 60.0d;+ if (this.lat != 0.0d) break location;+ s = q + 6;+ continue location;+ }+ if (newtext[r] == 'W') {+ this.lon = -Double.parseDouble(new String(newtext, r + 2, p - r - 2)) -+ Double.parseDouble(new String(newtext, p + 2, q - p - pl - 1)) / 60.0d;+ if (this.lat != 0.0d) break location;+ s = q + 6;+ continue location;+ }+ break location;}- break location;+ } catch (final NumberFormatException e) {+ break location;}}break location;}// find tags inside textString b = cleanLine(stripAllTags(newtext));- if ((insideTag != null) && (!(insideTag.name.equals(TagName.a.name())))) {+ if ((insideTag != null) && (insideTag.tagType != TagType.a)) {// texts inside tags sometimes have no punctuation at the line end// this is bad for the text semantics, because it is not possible for the// condenser to distinguish headlines from text beginnings.@@ -739,16 +756,18 @@ public class ContentScraper extends AbstractScraper implements Scraper {* @param tag the tag to parse. Must not be null.*/@Override- public void scrapeTag0(final Tag tag) {+ public void scrapeSingletonTag(final Tag tag) {if (tag.tv == TagValency.IGNORE) {return;}- checkOpts(tag);- if (tag.name.equalsIgnoreCase("img")) {++ // scrape tag attributes+ this.checkOpts(tag);+ if (tag.tagType == TagType.img) {final String src = tag.opts.getProperty("src", EMPTY_STRING);try {if (src.length() > 0) {- final DigestURL url = absolutePath(src);+ final DigestURL url = this.absolutePath(src);if (url != null) {// use to allow parse of "550px", with better performance as Numberformat.parsefinal int width = NumberTools.parseIntDecSubstring(tag.opts.getProperty("width", "-1")); // Integer.parseInt fails on "200px"@@ -759,7 +778,7 @@ public class ContentScraper extends AbstractScraper implements Scraper {}} catch (final NumberFormatException e) {}this.evaluationScores.match(Element.imgpath, src);- } else if(tag.name.equalsIgnoreCase("base")) {+ } else if(tag.tagType == TagType.base) {final String baseHref = tag.opts.getProperty("href", EMPTY_STRING);if(!baseHref.isEmpty()) {/* We must use here AnchorURL.newAnchor as the base href may also be an URL relative to the document URL */@@ -769,8 +788,8 @@ public class ContentScraper extends AbstractScraper implements Scraper {/* Nothing more to do when the base URL is malformed */}}- } else if (tag.name.equalsIgnoreCase("frame")) {- final AnchorURL src = absolutePath(tag.opts.getProperty("src", EMPTY_STRING));+ } else if (tag.tagType == TagType.frame) {+ final AnchorURL src = this.absolutePath(tag.opts.getProperty("src", EMPTY_STRING));if(src != null) {tag.opts.put("src", src.toNormalform(true));src.setAll(tag.opts);@@ -778,10 +797,10 @@ public class ContentScraper extends AbstractScraper implements Scraper {this.frames.add(src);this.evaluationScores.match(Element.framepath, src.toNormalform(true));}- } else if (tag.name.equalsIgnoreCase("body")) {+ } else if (tag.tagType == TagType.body) {final String classprop = tag.opts.getProperty("class", EMPTY_STRING);this.evaluationScores.match(Element.bodyclass, classprop);- } else if (tag.name.equalsIgnoreCase("meta")) {+ } else if (tag.tagType == TagType.meta) {final String content = tag.opts.getProperty("content", EMPTY_STRING);String name = tag.opts.getProperty("name", EMPTY_STRING);if (name.length() > 0) {@@ -798,22 +817,22 @@ public class ContentScraper extends AbstractScraper implements Scraper {if (name.length() > 0) {this.metas.put(name.toLowerCase(), content);}- } else if (tag.name.equalsIgnoreCase("area")) {+ } else if (tag.tagType == TagType.area) {final String areatitle = cleanLine(tag.opts.getProperty("title", EMPTY_STRING));//String alt = tag.opts.getProperty("alt",EMPTY_STRING);final String href = tag.opts.getProperty("href", EMPTY_STRING);if (href.length() > 0) {tag.opts.put("name", areatitle);- final AnchorURL url = absolutePath(href);+ final AnchorURL url = this.absolutePath(href);if(url != null) {tag.opts.put("href", url.toNormalform(true));url.setAll(tag.opts);this.addAnchor(url);}}- } else if (tag.name.equalsIgnoreCase("link")) {+ } else if (tag.tagType == TagType.link) {final String href = tag.opts.getProperty("href", EMPTY_STRING);- final AnchorURL newLink = absolutePath(href);+ final AnchorURL newLink = this.absolutePath(href);if (newLink != null) {tag.opts.put("href", newLink.toNormalform(true));@@ -825,7 +844,7 @@ public class ContentScraper extends AbstractScraper implements Scraper {final String type = tag.opts.getProperty("type", EMPTY_STRING);final String hreflang = tag.opts.getProperty("hreflang", EMPTY_STRING);- final Set<String> iconRels = retainIconRelations(relTokens);+ final Set<String> iconRels = this.retainIconRelations(relTokens);/* Distinguish icons from images. It will enable for example to later search only images and no icons */if (!iconRels.isEmpty()) {final String sizesAttr = tag.opts.getProperty("sizes", EMPTY_STRING);@@ -862,11 +881,11 @@ public class ContentScraper extends AbstractScraper implements Scraper {this.addAnchor(newLink);}}- } else if(tag.name.equalsIgnoreCase("embed") || tag.name.equalsIgnoreCase("source")) { //html5 tag+ } else if(tag.tagType == TagType.embed || tag.tagType == TagType.source) { //html5 tagfinal String src = tag.opts.getProperty("src", EMPTY_STRING);try {if (src.length() > 0) {- final AnchorURL url = absolutePath(src);+ final AnchorURL url = this.absolutePath(src);if (url != null) {final int width = Integer.parseInt(tag.opts.getProperty("width", "-1"));final int height = Integer.parseInt(tag.opts.getProperty("height", "-1"));@@ -878,33 +897,33 @@ public class ContentScraper extends AbstractScraper implements Scraper {}}} catch (final NumberFormatException e) {}- } else if(tag.name.equalsIgnoreCase("param")) {+ } else if(tag.tagType == TagType.param) {final String name = tag.opts.getProperty("name", EMPTY_STRING);if (name.equalsIgnoreCase("movie")) {- final AnchorURL url = absolutePath(tag.opts.getProperty("value", EMPTY_STRING));+ final AnchorURL url = this.absolutePath(tag.opts.getProperty("value", EMPTY_STRING));if(url != null) {tag.opts.put("value", url.toNormalform(true));url.setAll(tag.opts);this.addAnchor(url);}}- } else if (tag.name.equalsIgnoreCase("iframe")) {- final AnchorURL src = absolutePath(tag.opts.getProperty("src", EMPTY_STRING));- if(src != null) {+ } else if (tag.tagType == TagType.iframe) {+ final AnchorURL src = this.absolutePath(tag.opts.getProperty("src", EMPTY_STRING));+ if (src != null) {tag.opts.put("src", src.toNormalform(true));src.setAll(tag.opts);// this.addAnchor(src); // don't add the iframe to the anchors because the webgraph should not contain such links (by definition)this.iframes.add(src);this.evaluationScores.match(Element.iframepath, src.toNormalform(true));}- } else if (tag.name.equalsIgnoreCase("html")) {+ } else if (tag.tagType == TagType.html) {final String lang = tag.opts.getProperty("lang", EMPTY_STRING);- if (!lang.isEmpty()) // fake a language meta to preserv detection from <html lang="xx" />- this.metas.put("dc.language",lang.substring(0,2)); // fix found entries like "hu-hu"+ if (lang.length() >= 2) // fake a language meta to preserv detection from <html lang="xx" />+ this.metas.put("dc.language", lang.substring(0,2)); // fix found entries like "hu-hu"}// fire event- this.fireScrapeTag0(tag.name, tag.opts);+ this.fireScrapeTag0(tag.tagName, tag.opts);}/**@@ -912,17 +931,18 @@ public class ContentScraper extends AbstractScraper implements Scraper {* @param tag the tag to process. Must not be null.*/@Override- public void scrapeTag1(final Tag tag) {+ public void scrapePairedTag(final Tag tag) {if (tag.tv == TagValency.IGNORE) {return;}- checkOpts(tag);++ this.checkOpts(tag);// System.out.println("ScrapeTag1: tag.tagname=" + tag.tagname + ", opts=" + tag.opts.toString() + ", text=" + UTF8.String(text));- if (tag.name.equalsIgnoreCase("a") && tag.content.length() < 2048) {+ if (tag.tagType == TagType.a && tag.content.length() < 2048) {final String href = tag.opts.getProperty("href", EMPTY_STRING);AnchorURL url;- if ((href.length() > 0) && ((url = absolutePath(href)) != null)) {- if (followDenied()) {+ if ((href.length() > 0) && ((url = this.absolutePath(href)) != null)) {+ if (this.followDenied()) {String rel = tag.opts.getProperty("rel", EMPTY_STRING);if (rel.length() == 0) rel = "nofollow"; else if (rel.indexOf("nofollow") < 0) rel += ",nofollow";tag.opts.put("rel", rel);@@ -935,63 +955,63 @@ public class ContentScraper extends AbstractScraper implements Scraper {this.evaluationScores.match(Element.apath, href);}final String h;- if (tag.name.equalsIgnoreCase("div")) {+ if (tag.tagType == TagType.div) {final String id = tag.opts.getProperty("id", EMPTY_STRING);this.evaluationScores.match(Element.divid, id);final String itemtype = tag.opts.getProperty("itemtype", EMPTY_STRING);if (itemtype.equals("http://data-vocabulary.org/Breadcrumb")) {this.breadcrumbs++;}- } else if ((tag.name.equalsIgnoreCase("h1")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.h1) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.headlines[0].add(h);- } else if((tag.name.equalsIgnoreCase("h2")) && (tag.content.length() < 1024)) {+ } else if((tag.tagType == TagType.h2) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.headlines[1].add(h);- } else if ((tag.name.equalsIgnoreCase("h3")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.h3) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.headlines[2].add(h);- } else if ((tag.name.equalsIgnoreCase("h4")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.h4) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.headlines[3].add(h);- } else if ((tag.name.equalsIgnoreCase("h5")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.h5) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.headlines[4].add(h);- } else if ((tag.name.equalsIgnoreCase("h6")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.h6) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.headlines[5].add(h);- } else if ((tag.name.equalsIgnoreCase("title")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.title) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));this.titles.add(h);this.evaluationScores.match(Element.title, h);- } else if ((tag.name.equalsIgnoreCase("b")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.b) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.bold.inc(h);- } else if ((tag.name.equalsIgnoreCase("strong")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.strong) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.bold.inc(h);- } else if ((tag.name.equalsIgnoreCase("em")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.em) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.bold.inc(h);- } else if ((tag.name.equalsIgnoreCase("i")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.i) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.italic.inc(h);- } else if ((tag.name.equalsIgnoreCase("u")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.u) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.underline.inc(h);- } else if ((tag.name.equalsIgnoreCase("li")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.li) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.li.add(h);- } else if ((tag.name.equalsIgnoreCase("dt")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.dt) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.dt.add(h);- } else if ((tag.name.equalsIgnoreCase("dd")) && (tag.content.length() < 1024)) {+ } else if ((tag.tagType == TagType.dd) && (tag.content.length() < 1024)) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.dd.add(h);- } else if (tag.name.equalsIgnoreCase("script")) {+ } else if (tag.tagType == TagType.script) {final String src = tag.opts.getProperty("src", EMPTY_STRING);if (src.length() > 0) {- final AnchorURL absoluteSrc = absolutePath(src);+ final AnchorURL absoluteSrc = this.absolutePath(src);if(absoluteSrc != null) {this.script.add(absoluteSrc);}@@ -999,10 +1019,10 @@ public class ContentScraper extends AbstractScraper implements Scraper {} else {this.evaluationScores.match(Element.scriptcode, LB.matcher(new String(tag.content.getChars())).replaceAll(" "));}- } else if (tag.name.equalsIgnoreCase("article")) {+ } else if (tag.tagType == TagType.article) {h = cleanLine(CharacterCoding.html2unicode(stripAllTags(tag.content.getChars())));if (h.length() > 0) this.articles.add(h);- } else if (tag.name.equalsIgnoreCase(TagName.time.name())) { // html5 tag <time datetime="2016-12-23">Event</time>+ } else if (tag.tagType == TagType.time) { // html5 tag <time datetime="2016-12-23">Event</time>h = tag.opts.getProperty("datetime"); // TODO: checkOpts() also parses datetime property if in combination with schema.org itemprop=startDate/endDateif (h != null) { // datetime property is optionaltry {@@ -1013,7 +1033,7 @@ public class ContentScraper extends AbstractScraper implements Scraper {}// fire event- this.fireScrapeTag1(tag.name, tag.opts, tag.content.getChars());+ this.fireScrapeTag1(tag.tagName, tag.opts, tag.content.getChars());}/**@@ -1028,7 +1048,7 @@ public class ContentScraper extends AbstractScraper implements Scraper {* HTML microdata can be annotated on any kind of tag, so we don't restrict this* scraping to the limited sets in linkTags0 and linkTags1*/- this.linkedDataTypes.addAll(parseMicrodataItemType(tag.opts));+ this.linkedDataTypes.addAll(this.parseMicrodataItemType(tag.opts));}}@@ -1038,7 +1058,7 @@ public class ContentScraper extends AbstractScraper implements Scraper {if (this.valencySwitchTagNames != null &&tag != null &&- (TagName.div.name().equals(tag.name) || TagName.nav.name().equals(tag.name))) {+ (TagType.div.name().equals(tag.tagName) || TagType.nav.name().equals(tag.tagName))) {final String classAttr = tag.opts.getProperty("class", EMPTY_STRING);final Set<String> classes = ContentScraper.parseSpaceSeparatedTokens(classAttr);if (!Collections.disjoint(this.valencySwitchTagNames, classes)) return this.defaultValency.reverse();@@ -1178,9 +1198,18 @@ public class ContentScraper extends AbstractScraper implements Scraper {return this.breadcrumbs;}++ private final Pattern tripleN = Pattern.compile("\n\n\n");+ private final Pattern doubleNd = Pattern.compile("\n\n- ");+public String getText() {try {- return this.content.trim().toString();+ // finalize the content+ String text = this.content.trim().toString();+ Matcher m;+ while ((m = this.tripleN.matcher(text)).find()) text = m.replaceAll("\n\n");+ while ((m = this.doubleNd.matcher(text)).find()) text = m.replaceAll("\\n- ");+ return text;} catch (final OutOfMemoryError e) {ConcurrentLog.logException(e);return "";@@ -1493,7 +1522,7 @@ public class ContentScraper extends AbstractScraper implements Scraper {public double getLat() {if (this.lat != 0.0d) return this.lat;- getLon(); // parse with getLon() method which creates also the lat value+ this.getLon(); // parse with getLon() method which creates also the lat valuereturn this.lat;}@@ -1643,7 +1672,7 @@ public class ContentScraper extends AbstractScraper implements Scraper {final ScraperInputStream htmlFilter = new ScraperInputStream(new ByteArrayInputStream(page),StandardCharsets.UTF_8.name(),- new HashSet<String>(), TagValency.EVAL,+ new HashSet<>(), TagValency.EVAL,new VocabularyScraper(),new DigestURL("http://localhost"),false, maxLinks, timezoneOffset);@@ -1655,7 +1684,7 @@ public class ContentScraper extends AbstractScraper implements Scraper {final ContentScraper scraper = new ContentScraper(new DigestURL("http://localhost"),maxLinks,- new HashSet<String>(),+ new HashSet<>(),TagValency.EVAL,new VocabularyScraper(),timezoneOffset);diff --git a/source/net/yacy/document/parser/html/Scraper.java b/source/net/yacy/document/parser/html/Scraper.javaindex e2eb68b27..eacf30feb 100644--- a/source/net/yacy/document/parser/html/Scraper.java+++ b/source/net/yacy/document/parser/html/Scraper.java@@ -32,10 +32,10 @@ public interface Scraper {* @return true when the tag name belongs to the first category of tags* according to the Scraper implementation, and is therefore candidate* for processing by- * {@link #scrapeTag0(net.yacy.document.parser.html.ContentScraper.Tag)}+ * {@link #scrapeSingletonTag(net.yacy.document.parser.html.ContentScraper.Tag)}* implementation*/- public boolean isTag0(String tag);+ public boolean isSingetonTag(String tag);/*** @param tag@@ -43,10 +43,10 @@ public interface Scraper {* @return true when the tag name belongs to the second category of tags* according to the Scraper implementation, and is therefore candidate* for processing by- * {@link #scrapeTag0(net.yacy.document.parser.html.ContentScraper.Tag)}+ * {@link #scrapeSingletonTag(net.yacy.document.parser.html.ContentScraper.Tag)}* implementation*/- public boolean isTag1(String tag);+ public boolean isPairedTag(String tag);/*** Process plain text@@ -59,13 +59,13 @@ public interface Scraper {* Process a tag belonging to the first category of tags according to the Scraper implementation* @param tag a parsed tag*/- public void scrapeTag0(ContentScraper.Tag tag);+ public void scrapeSingletonTag(ContentScraper.Tag tag);/*** Process a tag belonging to the second category of tags according to the Scraper implementation* @param tag a parsed tag*/- public void scrapeTag1(ContentScraper.Tag tag);+ public void scrapePairedTag(ContentScraper.Tag tag);/*** Processing applied to any kind of tag opening.diff --git a/source/net/yacy/document/parser/html/ScraperListener.java b/source/net/yacy/document/parser/html/ScraperListener.javaindex b40833b61..3fa65f359 100644--- a/source/net/yacy/document/parser/html/ScraperListener.java+++ b/source/net/yacy/document/parser/html/ScraperListener.java@@ -31,14 +31,14 @@ import java.util.Properties;*/public interface ScraperListener extends java.util.EventListener {/**- * Triggered by {@link Scraper#scrapeTag0(net.yacy.document.parser.html.ContentScraper.Tag)} implementations+ * Triggered by {@link Scraper#scrapeSingletonTag(net.yacy.document.parser.html.ContentScraper.Tag)} implementations* @param tagname tag name* @param tagopts tag attributes*/public void scrapeTag0(String tagname, Properties tagopts);/**- * Triggered by {@link Scraper#scrapeTag1(net.yacy.document.parser.html.ContentScraper.Tag)} implementations+ * Triggered by {@link Scraper#scrapePairedTag(net.yacy.document.parser.html.ContentScraper.Tag)} implementations* @param tagname tag name* @param tagopts tag attributes* @param text tag content textdiff --git a/source/net/yacy/document/parser/html/TransformerWriter.java b/source/net/yacy/document/parser/html/TransformerWriter.javaindex 192f28ecd..177d0584e 100644--- a/source/net/yacy/document/parser/html/TransformerWriter.java+++ b/source/net/yacy/document/parser/html/TransformerWriter.java@@ -40,8 +40,8 @@ import java.util.Enumeration;import java.util.Locale;import java.util.Properties;import java.util.Stack;-import net.yacy.document.parser.html.ContentScraper.TagName;+import net.yacy.document.parser.html.ContentScraper.TagType;import net.yacy.kelondro.io.CharBuffer;@@ -58,7 +58,7 @@ public final class TransformerWriter extends Writer {private OutputStreamWriter out;private CharBuffer buffer;private Stack<ContentScraper.Tag> tagStack;- private final Scraper scraper;+ private final ContentScraper scraper;private boolean inSingleQuote;private boolean inDoubleQuote;private boolean inComment;@@ -68,7 +68,7 @@ public final class TransformerWriter extends Writer {public TransformerWriter(final OutputStream outStream,final Charset charSet,- final Scraper scraper,+ final ContentScraper scraper,final boolean passbyIfBinarySuspect) {this(outStream, charSet, scraper, passbyIfBinarySuspect, 64);@@ -77,14 +77,14 @@ public final class TransformerWriter extends Writer {public TransformerWriter(final OutputStream outStream,final Charset charSet,- final Scraper scraper,+ final ContentScraper scraper,final boolean passbyIfBinarySuspect,final int initialBufferSize) {this.outStream = outStream;this.scraper = scraper;this.buffer = new CharBuffer(ContentScraper.MAX_DOCSIZE, initialBufferSize);- this.tagStack = new Stack<ContentScraper.Tag>();+ this.tagStack = new Stack<>();this.inSingleQuote = false;this.inDoubleQuote = false;this.inComment = false;@@ -96,7 +96,7 @@ public final class TransformerWriter extends Writer {}}- public static char[] genTag0raw(final String tagname, final boolean opening, final char[] tagopts) {+ public static char[] generateSingletonTagRaw(final String tagname, final boolean opening, final char[] tagopts) {final CharBuffer bb = new CharBuffer(ContentScraper.MAX_DOCSIZE, tagname.length() + tagopts.length + 3);bb.append('<');if (!opening) {@@ -114,38 +114,38 @@ public final class TransformerWriter extends Writer {return result;}- public static char[] genTag1raw(final String tagname, final char[] tagopts, final char[] text) {- final CharBuffer bb = new CharBuffer(ContentScraper.MAX_DOCSIZE, 2 * tagname.length() + tagopts.length + text.length + 5);+ public static char[] generateSingletonTag(final String tagname, final Properties tagopts, final char quotechar) {+ final char[] tagoptsx = (tagopts.isEmpty()) ? null : genOpts(tagopts, quotechar);+ final CharBuffer bb = new CharBuffer(ContentScraper.MAX_DOCSIZE, tagname.length() + ((tagoptsx == null) ? 0 : (tagoptsx.length + 1)) + tagname.length() + 2);bb.append('<').append(tagname);- if (tagopts.length > 0) {-// if (tagopts[0] == (byte) 32)- bb.append(tagopts);-// else bb.append((byte) 32).append(tagopts);+ if (tagoptsx != null) {+ bb.appendSpace();+ bb.append(tagoptsx);}bb.append('>');- bb.append(text);- bb.append('<').append('/').append(tagname).append('>');final char[] result = bb.getChars();bb.close();return result;}- public static char[] genTag0(final String tagname, final Properties tagopts, final char quotechar) {- final char[] tagoptsx = (tagopts.isEmpty()) ? null : genOpts(tagopts, quotechar);- final CharBuffer bb = new CharBuffer(ContentScraper.MAX_DOCSIZE, tagname.length() + ((tagoptsx == null) ? 0 : (tagoptsx.length + 1)) + tagname.length() + 2);++ public static char[] generatePairedTagRaw(final String tagname, final char[] tagopts, final char[] text) {+ final CharBuffer bb = new CharBuffer(ContentScraper.MAX_DOCSIZE, 2 * tagname.length() + tagopts.length + text.length + 5);bb.append('<').append(tagname);- if (tagoptsx != null) {- bb.appendSpace();- bb.append(tagoptsx);+ if (tagopts.length > 0) {+// if (tagopts[0] == (byte) 32)+ bb.append(tagopts);+// else bb.append((byte) 32).append(tagopts);}bb.append('>');+ bb.append(text);+ bb.append('<').append('/').append(tagname).append('>');final char[] result = bb.getChars();bb.close();return result;}-- public static char[] genTag1(final String tagname, final Properties tagopts, final char[] text, final char quotechar) {- final char[] gt0 = genTag0(tagname, tagopts, quotechar);+ public static char[] generatePairedTag(final String tagname, final Properties tagopts, final char[] text, final char quotechar) {+ final char[] gt0 = generateSingletonTag(tagname, tagopts, quotechar);final CharBuffer cb = new CharBuffer(ContentScraper.MAX_DOCSIZE, gt0, gt0.length + text.length + tagname.length() + 3);cb.append(text).append('<').append('/').append(tagname).append('>');final char[] result = cb.getChars();@@ -181,9 +181,9 @@ public final class TransformerWriter extends Writer {*/private char[] tokenProcessor(final char[] in, final char quotechar) {if (in.length == 0) return in;-+// scan the string and parse structure- if (in.length <= 2 || in[0] != lb) return filterTag(in); // this is a text+ if (in.length <= 2 || in[0] != lb) return this.filterTag(in); // this is a text// this is a tagString tag;@@ -194,11 +194,11 @@ public final class TransformerWriter extends Writer {tag = new String(in, 2, tagend - 2).toLowerCase(Locale.ROOT);final char[] text = new char[in.length - tagend - 1];System.arraycopy(in, tagend, text, 0, in.length - tagend - 1);- return filterTag(text, quotechar, tag, false);+ return this.filterTag(text, quotechar, tag, false);}// don't add text from within <script> section, here e.g. a "if 1<a" expression could confuse tag detection- if (this.tagStack.size()>0 && this.tagStack.lastElement().name.equals(TagName.script.name())) {+ if (this.tagStack.size()>0 && this.tagStack.lastElement().tagType == TagType.script) {return new char[0];}@@ -207,9 +207,21 @@ public final class TransformerWriter extends Writer {tag = new String(in, 1, tagend - 1).toLowerCase(Locale.ROOT);final char[] text = new char[in.length - tagend - 1];System.arraycopy(in, tagend, text, 0, in.length - tagend - 1);- return filterTag(text, quotechar, tag, true);+ return this.filterTag(text, quotechar, tag, true);+ }++ /* inside TransformerWriter */+ private static void mdPrefix(final ContentScraper.Tag tag, final CharBuffer target) {+ if (tag != null && tag.tagType != null && tag.tagType.mdPrefix != null && tag.tagType.mdPrefix.length() > 0) {+ target.append(tag.tagType.mdPrefix);+ }+ }+ private static void mdSuffix(final ContentScraper.Tag tag, final CharBuffer target) {+ if (tag != null && tag.tagType != null && tag.tagType.mdSuffix != null && tag.tagType.mdSuffix.length() > 0) {+ target.append(tag.tagType.mdSuffix);+ }}-+// distinguish the following cases:// - (1) not collecting data for a tag and getting no tag (not opener and not close)// - (2) not collecting data for a tag and getting a tag opener@@ -218,9 +230,9 @@ public final class TransformerWriter extends Writer {// - (5) collecting data for a tag and getting a new/different tag opener without closing the previous tag// - (6) collecting data for a tag and getting a tag close for the wrong tag (a different than the opener)// - (7) collecting data for a tag and getting the correct close tag for that collecting tag-+/**- *+ ** @param content* @return content or empty array*/@@ -242,91 +254,97 @@ public final class TransformerWriter extends Writer {this.tagStack.lastElement().content.append(content);return new char[0];}-+private char[] filterTag(final char[] content, final char quotechar, final String tagname, final boolean opening) {assert tagname != null;-+if (this.tagStack.size() == 0) {// we are not collection tag text -> case (1) - (3)// we have a new tagif (opening) {// case (2):- return filterTagOpening(tagname, content);+ return this.filterTagOpening(tagname, content);}// its a close tag where no should be// case (3): we ignore that thing and return it again- return genTag0raw(tagname, false, content);+ return generateSingletonTagRaw(tagname, false, content);}// we are collection tag text for the tag 'filterTag' -> case (4) - (7)- if (tagname.equals("!")) filterTag(content);+ if (tagname.equals("!")) this.filterTag(content);// it's a tag! which one?if (opening) {// case (5): the opening should not be here. But we keep the order anyway- this.tagStack.lastElement().content.append(filterTagOpening(tagname, content));+ this.tagStack.lastElement().content.append(this.filterTagOpening(tagname, content));return new char[0];}- if (!tagname.equalsIgnoreCase(this.tagStack.lastElement().name)) {+ if (!tagname.equalsIgnoreCase(this.tagStack.lastElement().tagName)) {// case (6): its a closing tag, but the wrong one. just add it.- this.tagStack.lastElement().content.append(genTag0raw(tagname, opening, content));+ this.tagStack.lastElement().content.append(generateSingletonTagRaw(tagname, opening, content));return new char[0];}// it's our closing tag! return complete result.- return filterTagCloseing(quotechar);+ return this.filterTagCloseing(quotechar);}private char[] filterTagOpening(final String tagname, final char[] content) {final CharBuffer charBuffer = new CharBuffer(ContentScraper.MAX_DOCSIZE, content);- ContentScraper.Tag tag = new ContentScraper.Tag(tagname, this.scraper.defaultValency(), charBuffer.propParser());+ final ContentScraper.Tag tag = new ContentScraper.Tag(tagname, this.scraper.defaultValency(), charBuffer.propParser());charBuffer.close();-+final ContentScraper.Tag parentTag;if(this.tagStack.size() > 0) {parentTag = this.tagStack.lastElement();} else {parentTag = null;}-+/* Check scraper ignoring rules */if (this.scraper != null) {tag.setValency(this.scraper.tagValency(tag, parentTag));}-++ // add markdown for the tag+ mdPrefix(tag, this.scraper.content);+/* Apply processing relevant for any kind of tag opening */if(this.scraper != null) {this.scraper.scrapeAnyTagOpening(tag);}-- if (this.scraper != null && this.scraper.isTag0(tagname)) {++ if (this.scraper != null && this.scraper.isSingetonTag(tagname)) {// this single tag is collected at once here- this.scraper.scrapeTag0(tag);+ this.scraper.scrapeSingletonTag(tag);}- if (this.scraper != null && this.scraper.isTag1(tagname)) {+ if (this.scraper != null && this.scraper.isPairedTag(tagname)) {// ok, start collecting; we don't push this here to the scraper or transformer; we do that when the tag is closed.this.tagStack.push(tag);return new char[0];}// we ignore that thing and return it again- return genTag0raw(tagname, true, content);+ return generateSingletonTagRaw(tagname, true, content);}private char[] filterTagCloseing(final char quotechar) {char[] ret;- ContentScraper.Tag tag = this.tagStack.lastElement();- if (this.scraper != null) this.scraper.scrapeTag1(tag);- ret = genTag1(tag.name, tag.opts, tag.content.getChars(), quotechar);- if (this.scraper != null && this.scraper.isTag1(tag.name)) {+ final ContentScraper.Tag tag = this.tagStack.lastElement();+ if (this.scraper != null) this.scraper.scrapePairedTag(tag);+ ret = generatePairedTag(tag.tagName, tag.opts, tag.content.getChars(), quotechar);+ if (this.scraper != null && this.scraper.isPairedTag(tag.tagName)) {// remove the tag from the stack as soon as the tag is processedthis.tagStack.pop();// at this point the characters from the recently processed tag must be attached to the previous tagif (this.tagStack.size() > 0) this.tagStack.lastElement().content.append(ret);}++ // add markdown for the tag+ mdSuffix(tag, this.scraper.content);return ret;}@@ -337,8 +355,8 @@ public final class TransformerWriter extends Writer {// it's our closing tag! return complete result.char[] ret;- if (this.scraper != null) this.scraper.scrapeTag1(this.tagStack.lastElement());- ret = genTag1(this.tagStack.lastElement().name, this.tagStack.lastElement().opts, this.tagStack.lastElement().content.getChars(), quotechar);+ if (this.scraper != null) this.scraper.scrapePairedTag(this.tagStack.lastElement());+ ret = generatePairedTag(this.tagStack.lastElement().tagName, this.tagStack.lastElement().opts, this.tagStack.lastElement().content.getChars(), quotechar);this.tagStack.pop();return ret;}@@ -369,7 +387,7 @@ public final class TransformerWriter extends Writer {//System.out.println((char) c);if ((this.binaryUnsuspect) && (binaryHint((char)c))) {this.binaryUnsuspect = false;- if (this.passbyIfBinarySuspect) close();+ if (this.passbyIfBinarySuspect) this.close();}if (this.binaryUnsuspect || !this.passbyIfBinarySuspect) {@@ -381,7 +399,7 @@ public final class TransformerWriter extends Writer {if ((c == rb) && (this.buffer.length() > 0 && this.buffer.charAt(0) == lb)) {this.inSingleQuote = false;// the tag ends here. after filtering: pass on- filtered = tokenProcessor(this.buffer.getChars(), singlequote);+ filtered = this.tokenProcessor(this.buffer.getChars(), singlequote);if (this.out != null) { this.out.write(filtered); }// this.buffer = new serverByteBuffer();this.buffer.reset();@@ -393,7 +411,7 @@ public final class TransformerWriter extends Writer {if (c == rb && this.buffer.length() > 0 && this.buffer.charAt(0) == lb) {this.inDoubleQuote = false;// the tag ends here. after filtering: pass on- filtered = tokenProcessor(this.buffer.getChars(), doublequote);+ filtered = this.tokenProcessor(this.buffer.getChars(), doublequote);if (this.out != null) this.out.write(filtered);// this.buffer = new serverByteBuffer();this.buffer.reset();@@ -431,7 +449,7 @@ public final class TransformerWriter extends Writer {} else if (c == rb) {this.buffer.append(c);// the tag ends here. after filtering: pass on- filtered = tokenProcessor(this.buffer.getChars(), doublequote);+ filtered = this.tokenProcessor(this.buffer.getChars(), doublequote);if (this.out != null) this.out.write(filtered);// this.buffer = new serverByteBuffer();this.buffer.reset();@@ -439,7 +457,7 @@ public final class TransformerWriter extends Writer {// this is an error case// we consider that there is one rb missingif (this.buffer.length() > 0) {- filtered = tokenProcessor(this.buffer.getChars(), doublequote);+ filtered = this.tokenProcessor(this.buffer.getChars(), doublequote);if (this.out != null) this.out.write(filtered);}// this.buffer = new serverByteBuffer();@@ -453,7 +471,7 @@ public final class TransformerWriter extends Writer {if (c == lb) {// the text ends hereif (this.buffer.length() > 0) {- filtered = tokenProcessor(this.buffer.getChars(), doublequote);+ filtered = this.tokenProcessor(this.buffer.getChars(), doublequote);if (this.out != null) this.out.write(filtered);}// this.buffer = new serverByteBuffer();@@ -472,7 +490,7 @@ public final class TransformerWriter extends Writer {@Overridepublic void write(final char b[]) throws IOException {- write(b, 0, b.length);+ this.write(b, 0, b.length);}@Override@@ -494,17 +512,17 @@ public final class TransformerWriter extends Writer {@Overridepublic void close() throws IOException {- flush();+ this.flush();final char quotechar = (this.inSingleQuote) ? singlequote : doublequote;if (this.buffer != null) {if (this.buffer.length() > 0) {- final char[] filtered = tokenProcessor(this.buffer.getChars(), quotechar);+ final char[] filtered = this.tokenProcessor(this.buffer.getChars(), quotechar);if (this.out != null) this.out.write(filtered);}this.buffer.close();this.buffer = null;}- final char[] finalized = filterFinalize(quotechar);+ final char[] finalized = this.filterFinalize(quotechar);if (this.out != null) {if (finalized != null) this.out.write(finalized);this.out.flush();diff --git a/source/net/yacy/document/parser/htmlParser.java b/source/net/yacy/document/parser/htmlParser.javaindex 78dce898f..edf85cbb0 100644--- a/source/net/yacy/document/parser/htmlParser.java+++ b/source/net/yacy/document/parser/htmlParser.java@@ -108,12 +108,12 @@ public class htmlParser extends AbstractParser implements Parser {final int timezoneOffset,final InputStream sourceStream) throws Parser.Failure, InterruptedException {- return parseWithLimits(+ return this.parseWithLimits(location,mimeType,documentCharset,TagValency.EVAL,- new HashSet<String>(),+ new HashSet<>(),vocscraper,timezoneOffset,sourceStream,@@ -133,7 +133,7 @@ public class htmlParser extends AbstractParser implements Parser {final int timezoneOffset,final InputStream sourceStream) throws Parser.Failure, InterruptedException {- return parseWithLimits(+ return this.parseWithLimits(location, mimeType,documentCharset,defaultValency,@@ -145,12 +145,12 @@ public class htmlParser extends AbstractParser implements Parser {DEFAULT_MAX_LINKS,Long.MAX_VALUE);}-+@Overridepublic boolean isParseWithLimitsSupported() {return true;}-+@Overridepublic Document[] parseWithLimits(final DigestURL location,@@ -164,7 +164,7 @@ public class htmlParser extends AbstractParser implements Parser {final int maxLinks,final long maxBytes)throws Failure {- return parseWithLimits(+ return this.parseWithLimits(location,mimeType,documentCharset,@@ -177,7 +177,7 @@ public class htmlParser extends AbstractParser implements Parser {maxLinks,maxBytes);}-+private Document[] parseWithLimits(final DigestURL location,final String mimeType,@@ -196,17 +196,17 @@ public class htmlParser extends AbstractParser implements Parser {Charset[] detectedcharsetcontainer = new Charset[]{null};ContentScraper scraper = parseToScraper(location, documentCharset, defaultValency, valencySwitchTagNames, vocscraper, detectedcharsetcontainer, timezoneOffset, sourceStream, maxAnchors, maxLinks, maxBytes);// parseToScraper also detects/corrects/sets charset from html content tag- final Document document = transformScraper(location, mimeType, detectedcharsetcontainer[0].name(), scraper);+ final Document document = this.transformScraper(location, mimeType, detectedcharsetcontainer[0].name(), scraper);Document documentSnapshot = null;try {// check for ajax crawling scheme (https://developers.google.com/webmasters/ajax-crawling/docs/specification)// and create a sub-document for snapshot page (which will be merged by loader)// TODO: as a crawl request removes anchor part from original url getRef() is never successful - considere other handling as removeRef() in crawlerif (location.getRef() != null && location.getRef().startsWith("!")) {- documentSnapshot = parseAlternativeSnapshot(location, mimeType, documentCharset, defaultValency, valencySwitchTagNames, vocscraper, timezoneOffset, maxAnchors, maxLinks, maxBytes);+ documentSnapshot = this.parseAlternativeSnapshot(location, mimeType, documentCharset, defaultValency, valencySwitchTagNames, vocscraper, timezoneOffset, maxAnchors, maxLinks, maxBytes);} else { // head tag fragment only allowed on url without anchor hashfragment, but there are discussions that existence of hashfragment anchor takes preference (means allow both)if (scraper.getMetas().containsKey("fragment") && scraper.getMetas().get("fragment").equals("!")) {- documentSnapshot = parseAlternativeSnapshot(location, mimeType, documentCharset, defaultValency, valencySwitchTagNames, vocscraper, timezoneOffset, maxAnchors, maxLinks, maxBytes);+ documentSnapshot = this.parseAlternativeSnapshot(location, mimeType, documentCharset, defaultValency, valencySwitchTagNames, vocscraper, timezoneOffset, maxAnchors, maxLinks, maxBytes);}}} catch (Exception ex1) { // ignore any exception for any issue with snapshot@@ -218,8 +218,8 @@ public class htmlParser extends AbstractParser implements Parser {throw new Parser.Failure("IOException in htmlParser: " + e.getMessage(), location);}}--++/*** the transformScraper method transforms a scraper object into a document object@@ -268,13 +268,13 @@ public class htmlParser extends AbstractParser implements Parser {ppd.setIcons(scraper.getIcons());ppd.setLinkedDataTypes(scraper.getLinkedDataTypes());ppd.setPartiallyParsed(scraper.isLimitsExceeded());-+return ppd;}public static ContentScraper parseToScraper(final DigestURL location,- final String documentCharset,+ final String documentCharset,final TagValency defaultValency,final Set<String> valencySwitchTagNames,final VocabularyScraper vocabularyScraper,@@ -297,7 +297,7 @@ public class htmlParser extends AbstractParser implements Parser {}return scraper;}-+/*** Parse the resource at location and return the resulting ContentScraper* @param location the URL of the resource to parse@@ -325,7 +325,7 @@ public class htmlParser extends AbstractParser implements Parser {final int maxAnchors,final int maxLinks,final long maxBytes) throws Parser.Failure, IOException {-+// make a scraperString charset = null;@@ -336,24 +336,20 @@ public class htmlParser extends AbstractParser implements Parser {// nothing found: try to find a meta-tagif (charset == null) {- ScraperInputStream htmlFilter = null;- try {- htmlFilter = new ScraperInputStream(- sourceStream,- documentCharset,- valencySwitchTagNames,- defaultValency,- vocabularyScraper,- location,- false,- maxLinks,- timezoneOffset);+ try (ScraperInputStream htmlFilter = new ScraperInputStream(+ sourceStream,+ documentCharset,+ valencySwitchTagNames,+ defaultValency,+ vocabularyScraper,+ location,+ false,+ maxLinks,+ timezoneOffset)) {sourceStream = htmlFilter;charset = htmlFilter.detectCharset();} catch (final IOException e1) {throw new Parser.Failure("Charset error:" + e1.getMessage(), location);- } finally {- if (htmlFilter != null) htmlFilter.close();}}@@ -379,7 +375,7 @@ public class htmlParser extends AbstractParser implements Parser {detectedcharsetcontainer[0] = Charset.defaultCharset();}}-+// parsing the content// for this static method no need to init local this.scraperObject herefinal ContentScraper scraper = new ContentScraper(@@ -405,7 +401,10 @@ public class htmlParser extends AbstractParser implements Parser {}}} catch (final IOException e) {- throw new Parser.Failure("IO error:" + e.getMessage(), location);+ throw new Parser.Failure("IO error:" + e.getMessage(), location);+ } catch (final Exception e) {+ e.printStackTrace();+ throw new Parser.Failure("html parser - other error:" + e.getMessage(), location);} finally {writer.flush();//sourceStream.close(); keep open for multipe parsing (close done by caller)@@ -540,7 +539,7 @@ public class htmlParser extends AbstractParser implements Parser {try {snapshotStream = locationSnapshot.getInputStream(ClientIdentification.yacyInternetCrawlerAgent);ContentScraper scraperSnapshot = parseToScraper(location, documentCharset, defaultValency, valencySwitchTagNames, vocscraper, detectedcharsetcontainer, timezoneOffset, snapshotStream, maxAnchors, maxLinks, maxBytes);- documentSnapshot = transformScraper(location, mimeType, detectedcharsetcontainer[0].name(), scraperSnapshot);+ documentSnapshot = this.transformScraper(location, mimeType, detectedcharsetcontainer[0].name(), scraperSnapshot);} finally {if(snapshotStream != null) {try {diff --git a/source/net/yacy/htroot/ViewFile.java b/source/net/yacy/htroot/ViewFile.javaindex 09dde61b1..d94943ed6 100644--- a/source/net/yacy/htroot/ViewFile.java+++ b/source/net/yacy/htroot/ViewFile.java@@ -93,7 +93,7 @@ public class ViewFile {final Switchboard sb = (Switchboard)env;prop.put("topmenu", sb.getConfigBool("publicTopmenu", true) ? 1 : 0);prop.put("searchindocument", 0);- prop.put("viewMode", VIEW_MODE_NO_TEXT);+ prop.put("viewMode", VIEW_MODE_AS_PARSED_TEXT);prop.put("viewModeValue", "sentences");prop.putHTML("error_words", "");prop.put("error_vMode-sentences", "1");@@ -123,7 +123,7 @@ public class ViewFile {prop.put("error_vMode-iframeCitations", "0");prop.put("error_vMode-schema", "0");final boolean showSnippet = post.get("show", "").equals("Show Snippet");- final String viewMode = showSnippet ? "sentences" : post.get("viewMode", "sentences");+ final String viewMode = showSnippet ? "sentences" : post.get("viewMode", "parsed");prop.put("error_vMode-" + viewMode, "1");prop.put("viewModeValue", viewMode);@@ -484,7 +484,7 @@ public class ViewFile {*/private static void putLinks(final serverObjects prop, final String[] wordArray, final Document document, final String agentName) {prop.put("viewMode", VIEW_MODE_AS_LINKLIST);- boolean dark = true;+ boolean dark;int i = 0;i += putMediaInfo(prop, wordArray, i, document.getVideolinks(), "video", (i % 2 == 0), agentName);