@Test public void basic() throws IOException { @Syntax("HTML") String html = "<html> <head> <meta charset=\"UTF-8\" /> <title>Peacock</title> " + "<LINK href=\"lib/bootstrap/bootstrap.css\" rel=\"stylesheet\" type=\"text/css\"> " + "<script type='text/javascript' src='lib/jquery.js'></script> " + "<script type='text/javascript' src='lib/underscore.js'></script> " + "<script type='text/javascript' src='lib/backbone.js'></script> " + "<script type='text/javascript' src='lib/moment.js'></script> " + "<script type='text/javascript' src='lib/bootstrap/bootstrap-dropdown.js'></script> " + "<script src='peacock.js'></script> </head> <body> <div id='peacock'></div> " + "<!--script src='demo.js'></script--> </body> </html>"; ValueFilterGrepTags grepTags = Configs.decodeObject( ValueFilterGrepTags.class, "values = [bootstrap, jquery], tagAttrs = src, tagName = script"); assertNotNull(grepTags.filterValue(ValueFactory.create(html))); ValueFilterGrepTags failGrepTags = Configs.decodeObject( ValueFilterGrepTags.class, "values = [foo, bar], tagAttrs = src, tagName = script"); assertNull(failGrepTags.filterValue(ValueFactory.create(html))); }
@Override @Nullable public ValueObject filterValue(ValueObject value) { if (value == null) { return null; } String html = value.asString().asNative(); if (html == null) { return null; } @Nonnull Multiset<String> valueCounts = HashMultiset.create(); try { Parser parser = Parser.htmlParser().setTrackErrors(0); @Nonnull Document doc = parser.parseInput(html, ""); @Nonnull Elements tags = doc.select(tagName); for (Element tag : tags) { for (String tagAttr : tagAttrs) { @Nonnull String attrValue = tag.attr(tagAttr).toLowerCase(); for (String matchValue : values) { if (attrValue.contains(matchValue)) { valueCounts.add(matchValue); } } } } } catch (Exception e) { if (parserErrors++ % logEveryN == 0) { log.error("Failed to extract tags due to : {} Total Parser Errors : {}", e.getMessage(), parserErrors); } } return valueCounts.isEmpty() ? null : multisetToValueMap(valueCounts); }
ValueFilterGrepTags extends AbstractValueFilter { @Override @Nullable public ValueObject filterValue(ValueObject value) { if (value == null) { return null; } String html = value.asString().asNative(); if (html == null) { return null; } @Nonnull Multiset<String> valueCounts = HashMultiset.create(); try { Parser parser = Parser.htmlParser().setTrackErrors(0); @Nonnull Document doc = parser.parseInput(html, ""); @Nonnull Elements tags = doc.select(tagName); for (Element tag : tags) { for (String tagAttr : tagAttrs) { @Nonnull String attrValue = tag.attr(tagAttr).toLowerCase(); for (String matchValue : values) { if (attrValue.contains(matchValue)) { valueCounts.add(matchValue); } } } } } catch (Exception e) { if (parserErrors++ % logEveryN == 0) { log.error("Failed to extract tags due to : {} Total Parser Errors : {}", e.getMessage(), parserErrors); } } return valueCounts.isEmpty() ? null : multisetToValueMap(valueCounts); } }
ValueFilterGrepTags extends AbstractValueFilter { @Override @Nullable public ValueObject filterValue(ValueObject value) { if (value == null) { return null; } String html = value.asString().asNative(); if (html == null) { return null; } @Nonnull Multiset<String> valueCounts = HashMultiset.create(); try { Parser parser = Parser.htmlParser().setTrackErrors(0); @Nonnull Document doc = parser.parseInput(html, ""); @Nonnull Elements tags = doc.select(tagName); for (Element tag : tags) { for (String tagAttr : tagAttrs) { @Nonnull String attrValue = tag.attr(tagAttr).toLowerCase(); for (String matchValue : values) { if (attrValue.contains(matchValue)) { valueCounts.add(matchValue); } } } } } catch (Exception e) { if (parserErrors++ % logEveryN == 0) { log.error("Failed to extract tags due to : {} Total Parser Errors : {}", e.getMessage(), parserErrors); } } return valueCounts.isEmpty() ? null : multisetToValueMap(valueCounts); }  }
ValueFilterGrepTags extends AbstractValueFilter { @Override @Nullable public ValueObject filterValue(ValueObject value) { if (value == null) { return null; } String html = value.asString().asNative(); if (html == null) { return null; } @Nonnull Multiset<String> valueCounts = HashMultiset.create(); try { Parser parser = Parser.htmlParser().setTrackErrors(0); @Nonnull Document doc = parser.parseInput(html, ""); @Nonnull Elements tags = doc.select(tagName); for (Element tag : tags) { for (String tagAttr : tagAttrs) { @Nonnull String attrValue = tag.attr(tagAttr).toLowerCase(); for (String matchValue : values) { if (attrValue.contains(matchValue)) { valueCounts.add(matchValue); } } } } } catch (Exception e) { if (parserErrors++ % logEveryN == 0) { log.error("Failed to extract tags due to : {} Total Parser Errors : {}", e.getMessage(), parserErrors); } } return valueCounts.isEmpty() ? null : multisetToValueMap(valueCounts); }  @Override @Nullable ValueObject filterValue(ValueObject value); }
ValueFilterGrepTags extends AbstractValueFilter { @Override @Nullable public ValueObject filterValue(ValueObject value) { if (value == null) { return null; } String html = value.asString().asNative(); if (html == null) { return null; } @Nonnull Multiset<String> valueCounts = HashMultiset.create(); try { Parser parser = Parser.htmlParser().setTrackErrors(0); @Nonnull Document doc = parser.parseInput(html, ""); @Nonnull Elements tags = doc.select(tagName); for (Element tag : tags) { for (String tagAttr : tagAttrs) { @Nonnull String attrValue = tag.attr(tagAttr).toLowerCase(); for (String matchValue : values) { if (attrValue.contains(matchValue)) { valueCounts.add(matchValue); } } } } } catch (Exception e) { if (parserErrors++ % logEveryN == 0) { log.error("Failed to extract tags due to : {} Total Parser Errors : {}", e.getMessage(), parserErrors); } } return valueCounts.isEmpty() ? null : multisetToValueMap(valueCounts); }  @Override @Nullable ValueObject filterValue(ValueObject value);  }