private boolean isIllegalStringInTag(final Element tag){ final String[] illegalWords = {"advert", "werbung", "anzeige", "adsense"}; if (tag == null){ return false; } for (final String word : illegalWords) { final Attributes tagAttrs = tag.attributes(); if (tagAttrs != null){ for(final Attribute attr : tagAttrs){ if(attr.toString().toLowerCase().contains(word.toLowerCase())){ return true; } } }else{ return false; } } return false; }
@Override public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); for (Element child : design.children()) { Component childComponent = designContext.readDesign(child); if (!(childComponent instanceof Step)) { throw new IllegalArgumentException("Only implementations of " + Step.class.getName() + " are allowed as children of " + getClass().getName()); } stepIterator.add(((Step) childComponent)); } boolean linear = false; Attributes attributes = design.attributes(); if (attributes.hasKey(DESIGN_ATTRIBUTE_LINEAR)) { linear = DesignAttributeHandler.getFormatter() .parse(design.attr(DESIGN_ATTRIBUTE_LINEAR), Boolean.class); } stepIterator.setLinear(linear); }
@Test public void replacePlaceholderWithNode() { Map<String, Node> nodeIdMap = new HashMap<>(); String html = "<div>"; for (int i = 0; i < 5; i++) { Attributes attrs = new Attributes(); String id = "id" + i; attrs.put("id", id); Element ele = new Element(Tag.valueOf("span"), "", attrs); ele.append("The original node"); nodeIdMap.put(id, ele); Element placeholder = ArticleUtil.generatePlaceholderNode(id); html += placeholder.outerHtml(); } html += "</div>"; String results = ArticleUtil.replacePlaceholderWithNode(nodeIdMap, html); for (Node originalNode: nodeIdMap.values()) { assertThat(results).contains(originalNode.outerHtml()); } }
/** * Finds any namespaces defined in this element. Returns any tag prefix. */ private String updateNamespaces(org.jsoup.nodes.Element el) { // scan the element for namespace declarations // like: xmlns="blah" or xmlns:prefix="blah" Attributes attributes = el.attributes(); for (Attribute attr : attributes) { String key = attr.getKey(); String prefix; if (key.equals(xmlnsKey)) { prefix = ""; } else if (key.startsWith(xmlnsPrefix)) { prefix = key.substring(xmlnsPrefix.length()); } else { continue; } namespaces.put(prefix, attr.getValue()); } // get the element prefix if any int pos = el.tagName().indexOf(":"); return pos > 0 ? el.tagName().substring(0, pos) : ""; }
private static void renameAllAttributeKeys( ImmutableMap<String, String> renameMap, Element element) { Attributes attributes = element.attributes(); for (Attribute attribute : attributes) { String key = attribute.getKey(); // Polymer events are referenced as strings. As a result they do not participate in renaming. // Additionally, it is not valid to have a Polymer property start with "on". if (!key.startsWith("on-")) { String renamedProperty = renameMap.get( CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, key)); if (renamedProperty != null) { attribute.setKey(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, renamedProperty)); } } } }
private ElementMeta createSafeElement(Element sourceEl) { String sourceTag = sourceEl.tagName(); Attributes destAttrs = new Attributes(); Element dest = new Element(Tag.valueOf(sourceTag), sourceEl.baseUri(), destAttrs); int numDiscarded = 0; Attributes sourceAttrs = sourceEl.attributes(); for (Attribute sourceAttr : sourceAttrs) { if (whitelist.isSafeAttribute(sourceTag, sourceEl, sourceAttr)) destAttrs.put(sourceAttr); else numDiscarded++; } Attributes enforcedAttrs = whitelist.getEnforcedAttributes(sourceTag); destAttrs.addAll(enforcedAttrs); return new ElementMeta(dest, numDiscarded); }
public ArrayList<LTCRoute> loadRoutes() throws ScrapeException, IOException { ArrayList<LTCRoute> routes = new ArrayList<LTCRoute>(); Document doc = parseDocFromUri(ROUTE_PATH, ROUTE_PATH); Elements routeLinks = doc.select("a[href]"); for (Element routeLink : routeLinks) { String name = routeLink.text(); Attributes attrs = routeLink.attributes(); String href = attrs.get("href"); Matcher m = ROUTE_NUM_PATTERN.matcher(href); if (m.find()) { String number = m.group(1); LTCRoute route = new LTCRoute(number, name/*, href*/); routes.add(route); } } return routes; }
ArrayList<LTCDirection> loadDirections(String routeNum) throws ScrapeException, IOException { ArrayList<LTCDirection> directions = new ArrayList<LTCDirection>(2); // probably 2 String path = String.format(DIRECTION_PATH, routeNum); Document doc = parseDocFromUri(path, path); Elements dirLinks = doc.select("a[href]"); for (Element dirLink : dirLinks) { String name = dirLink.text(); Attributes attrs = dirLink.attributes(); String href = attrs.get("href"); Matcher m = DIRECTION_NUM_PATTERN.matcher(href); if (m.find()) { Integer number = Integer.valueOf(m.group(1)); LTCDirection dir = new LTCDirection(number, name); directions.add(dir); } } return directions; }
HashMap<Integer, LTCStop> loadStops(String routeNum, int direction) throws ScrapeException, IOException { HashMap<Integer, LTCStop> stops = new HashMap<Integer, LTCStop>(); String path = String.format(STOPS_PATH, routeNum, direction); Document doc = parseDocFromUri(path, path); Elements stopLinks = doc.select("a[href]"); for (Element stopLink : stopLinks) { String name = stopLink.text(); Attributes attrs = stopLink.attributes(); String href = attrs.get("href"); Matcher m = STOP_NUM_PATTERN.matcher(href); if (m.find()) { Integer number = Integer.valueOf(m.group(1)); LTCStop stop = new LTCStop(number, name); stops.put(stop.number, stop); } } return stops; }
/** * this method gets the parent node of the node in param * with attribute Class not null * @param n * @return */ public Node searchDirectParentWithAttribute(Node n){ if (n!=null) { Attributes attributes =n.attributes(); List <Attribute> list_attributes= attributes.asList(); if (list_attributes.size()>0){ for (int i=0; i<list_attributes.size(); i++){ String attributeHtml =list_attributes.get(i).html(); if(attributeHtml.toLowerCase().contains("class=")) { if(list_attributes.get(i).getValue().length()>0) { return n; } } } return searchDirectParentWithAttribute( n.parent()); } else { return searchDirectParentWithAttribute( n.parent()); } } else { return n; } }
@Override public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); Attributes attr = design.attributes(); if (attr.hasKey("mime-type")) { MimeType mimeType = null; String mimeTypeString = DesignAttributeHandler.getFormatter().parse( attr.get("mime-type"), String.class); try { mimeType = MimeType.valueOfMimeType(mimeTypeString); } catch (IllegalArgumentException e) { Logger.getLogger(SignatureField.class.getName()).info( "Unsupported MIME-Type found when reading from design : " .concat(mimeTypeString)); } setMimeType(mimeType); } }
@SuppressWarnings("unused") private void addMeta(Element head, String... attributes) { Attributes attr = new Attributes(); for (String attribute : attributes) { String[] keyValue = attribute.split("="); String key = keyValue[0]; String value = keyValue[1]; attr.put(key, value); } Element linkElement = new Element(Tag.valueOf("meta"), "", attr); head.appendChild(linkElement); }
public void head(Node source, int depth) { if (skipChildren) { return; } if (source instanceof Element) { Element sourceElement = (Element) source; if (isSafeTag(sourceElement)) { String sourceTag = sourceElement.tagName(); Attributes destinationAttributes = sourceElement.attributes().clone(); Element destinationChild = new Element(Tag.valueOf(sourceTag), sourceElement.baseUri(), destinationAttributes); destination.appendChild(destinationChild); destination = destinationChild; } else if (source != root) { skipChildren = true; } } else if (source instanceof TextNode) { TextNode sourceText = (TextNode) source; TextNode destinationText = new TextNode(sourceText.getWholeText(), source.baseUri()); destination.appendChild(destinationText); } else if (source instanceof DataNode && isSafeTag(source.parent())) { DataNode sourceData = (DataNode) source; DataNode destinationData = new DataNode(sourceData.getWholeData(), source.baseUri()); destination.appendChild(destinationData); } }
@Override public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); Attributes attr = design.attributes(); if (design.hasAttr("time-zone")) { setZoneId(ZoneId.of(DesignAttributeHandler.readAttribute("end-date", attr, String.class))); } if (design.hasAttr("time-format")) { setTimeFormat(TimeFormat.valueOf( "Format" + design.attr("time-format").toUpperCase())); } if (design.hasAttr("start-date")) { setStartDate( ZonedDateTime.ofInstant(DesignAttributeHandler.readAttribute("start-date", attr, Date.class) .toInstant(), getZoneId())); } if (design.hasAttr("end-date")) { setEndDate( ZonedDateTime.ofInstant(DesignAttributeHandler.readAttribute("end-date", attr, Date.class) .toInstant(), getZoneId())); } }
private void addHiddenInputTag(Element form, String formIdAttrName, String formIdAttrValue) { Attributes attributes = Stream.of( new Attribute("type", "hidden"), new Attribute("name", formIdAttrName), new Attribute("value", formIdAttrValue)) .collect(Attributes::new, Attributes::put, Attributes::addAll); form.prependChild(new Element(Tag.valueOf("input"), "/", attributes)); }
/** * Generate a non-translatable element */ public static Element generatePlaceholderNode(@NotNull String id) { Attributes attrs = new Attributes(); attrs.put("id", id); attrs.put("translate", "no"); return new Element(Tag.valueOf("var"), "", attrs); }
@Test public void testConstructor() { Map<String, Node> map = new HashMap<>(); Element doc = new Element(Tag.valueOf("span"), "", new Attributes()); TranslatableHTMLNode node = new TranslatableHTMLNode( Lists.newArrayList(doc), map); assertThat(node.getPlaceholderIdMap()).isEqualTo(map); assertThat(node.getHtml()).isEqualTo(doc.outerHtml()); }
Attributes getEnforcedAttributes(String tagName) { Attributes attrs = new Attributes(); TagName tag = TagName.valueOf(tagName); if (enforcedAttributes.containsKey(tag)) { Map<AttributeKey, AttributeValue> keyVals = enforcedAttributes.get(tag); for (Map.Entry<AttributeKey, AttributeValue> entry : keyVals.entrySet()) { attrs.put(entry.getKey().toString(), entry.getValue().toString()); } } return attrs; }
public boolean processStartTag(String name, Attributes attrs) { if (currentToken == start) { // don't recycle an in-use token return process(new Token.StartTag().nameAttr(name, attrs)); } start.reset(); start.nameAttr(name, attrs); return process(start); }
@Override Tag reset() { super.reset(); attributes = new Attributes(); // todo - would prefer these to be null, but need to check Element assertions return this; }
@Test public void parsesRoughAttributeString() { String html = "<a id=\"123\" class=\"baz = 'bar'\" style = 'border: 2px'qux zim foo = 12 mux=18 />"; // should be: <id=123>, <class=baz = 'bar'>, <qux=>, <zim=>, <foo=12>, <mux.=18> Element el = Jsoup.parse(html).getElementsByTag("a").get(0); Attributes attr = el.attributes(); assertEquals(7, attr.size()); assertEquals("123", attr.get("id")); assertEquals("baz = 'bar'", attr.get("class")); assertEquals("border: 2px", attr.get("style")); assertEquals("", attr.get("qux")); assertEquals("", attr.get("zim")); assertEquals("12", attr.get("foo")); assertEquals("18", attr.get("mux")); }
@Test public void canStartWithEq() { String html = "<a =empty />"; Element el = Jsoup.parse(html).getElementsByTag("a").get(0); Attributes attr = el.attributes(); assertEquals(1, attr.size()); assertTrue(attr.hasKey("=empty")); assertEquals("", attr.get("=empty")); }
/** * 按原Element重建一个新的Element * @param sourceEl * @return */ private static Element createSafeElement(Element sourceEl) { String sourceTag = sourceEl.tagName(); Attributes destAttrs = new Attributes(); Element dest = new Element(Tag.valueOf(sourceTag), sourceEl.baseUri(), destAttrs); Attributes sourceAttrs = sourceEl.attributes(); for (Attribute sourceAttr : sourceAttrs) { destAttrs.put(sourceAttr); } return dest; }
public AttributesAdaptor(Attributes attributes, Element element) { this.attributes = attributes; this.element = element; attrList = new ArrayList<Attr>(); for (Attribute attribute : attributes) { attrList.add(new AttributeAdaptor(attribute,element)); } }
public static Map<String,String> getAttributesAsMap(Element el) { Attributes attrs = el.attributes(); if(attrs.size() == 0) return Collections.emptyMap(); HashMap<String,String> unorderedAttrs = new HashMap<>(); for (Attribute attr : attrs.asList()) { unorderedAttrs.put(attr.getKey(), attr.getValue()); } return unorderedAttrs; }
/** * Add the attributes to the given record definition * <p> * Uses Jsoup to parse the tag as if html * * @param recordDefinition * the record definition * @param beginText * the begin tag of the record definition */ private void addAttributes(TemplateRecordDefinition recordDefinition, String beginText) { Document doc = Jsoup.parseBodyFragment(beginText); Element fieldElement = doc.body().child(0); Attributes attributes = fieldElement.attributes(); if (attributes.hasKey(REPEAT_ATTRIBUTE)) { String required = attributes.get(REPEAT_ATTRIBUTE); recordDefinition.setRepeat(Strings.isNullOrEmpty(required) ? true : Boolean.valueOf(required)); } }
private void appendAttributes(final TableHtmlElement element, final DataTable<?, ?> dataTable) { final Attributes attributes = element.getElement().attributes(); for (final Attribute attr : attributes) { if (Strings.startsWithIgnoreCase(attr.getKey(), "wicket:")) { continue; } dataTable.add(AttributeAppender.append(attr.getKey(), attr.getValue())); } }
private void renameAllAttributeValues(Element element) { Attributes attributes = element.attributes(); if (attributes != null) { for (Attribute attribute : attributes) { attribute.setValue(renameStringWithDatabindingDirectives(attribute.getValue())); } } }
private void renameAllAnnotatedEventAttributes(Element element) { Attributes attributes = element.attributes(); if (attributes != null) { for (Attribute attribute : attributes) { String key = attribute.getKey(); if (key.startsWith("on-")) { String renamedEventHandler = renameMap.get(attribute.getValue()); if (renamedEventHandler != null) { attribute.setValue(renamedEventHandler); } } } } }
/** * Handles the initial tag of every element * * @param element */ private void handleStartTag(Element element) { final String tag = element.tagName(); switch (tag.toLowerCase()) { case "br": mBuilder.append('\n'); break; case "hr": handleHR(); break; case "p": case "div": handleP(); break; case "blockquote": handleP(); startSpan(); break; case "b": case "strong": case "em": case "cite": case "dfn": case "i": case "big": case "small": case "tt": case "u": case "sub": case "sup": case "a": startSpan(); break; } final Attributes attributes = element.attributes(); if (attributes.hasKey("style")) handleCssStart(attributes.get("style")); }
protected Element createMenuElement(MenuItem item, DesignContext designContext) { Element menuElement = new Element(Tag.valueOf("menu"), ""); // Defaults MenuItem def = new MenuItemImpl("", null, null); Attributes attr = menuElement.attributes(); DesignAttributeHandler.writeAttribute("icon", attr, item.getIcon(), def.getIcon(), Resource.class, designContext); DesignAttributeHandler.writeAttribute("disabled", attr, !item.isEnabled(), !def.isEnabled(), boolean.class, designContext); DesignAttributeHandler.writeAttribute("visible", attr, item.isVisible(), def.isVisible(), boolean.class, designContext); DesignAttributeHandler.writeAttribute("separator", attr, item.isSeparator(), def.isSeparator(), boolean.class, designContext); DesignAttributeHandler.writeAttribute("checkable", attr, item.isCheckable(), def.isCheckable(), boolean.class, designContext); DesignAttributeHandler.writeAttribute("checked", attr, item.isChecked(), def.isChecked(), boolean.class, designContext); DesignAttributeHandler.writeAttribute("description", attr, item.getDescription(), def.getDescription(), String.class, designContext); DesignAttributeHandler.writeAttribute("style-name", attr, item.getStyleName(), def.getStyleName(), String.class, designContext); menuElement.append(item.getText()); if (item.hasChildren()) { for (MenuItem subMenu : item.getChildren()) { menuElement .appendChild(createMenuElement(subMenu, designContext)); } } return menuElement; }