Java Code Examples for org.jsoup.nodes.Element#attributes()

The following examples show how to use org.jsoup.nodes.Element#attributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlDecoder.java    From metafacture-core with Apache License 2.0 6 votes vote down vote up
private void process(Element parent, StreamReceiver receiver) {
    for (Element element : parent.children()) {
        receiver.startEntity(element.nodeName());
        Attributes attributes = element.attributes();
        for (Attribute attribute : attributes) {
            receiver.literal(attribute.getKey(), attribute.getValue());
        }
        if (element.children().isEmpty()) {
            String text = element.text().trim();
            String value = text.isEmpty() ? element.data() : text;
            if (!value.isEmpty()) {
                receiver.literal("value", value);
            }
        }
        process(element, receiver);
        receiver.endEntity();
    }
}
 
Example 2
Source File: Cleaner.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: Cleaner.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: WebComponentBootstrapHandler.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a javascript which copies attributes from the {@code element} to
 * the created DOM element identified by {@code elementRef}. If {@code
 * element} contains a {@code src} attribute, its path is prefixed with
 * {@code basePath}.
 *
 * @param writer
 *            response writer
 * @param elementRef
 *            variable name of the element in javascript
 * @param element
 *            jsoup element from which to copy the attributes
 * @param basePath
 *            base path of {@code src} attributes (service url's path)
 * @throws IOException
 *             if {@code writer} is unable to write
 */
private void transferAttribute(Writer writer, String elementRef,
        Element element, String basePath) throws IOException {
    for (Attribute attribute : element.attributes()) {
        writer.append(elementRef).append(".setAttribute('")
                .append(attribute.getKey()).append("',");
        if (attribute.getValue() == null) {
            writer.append("''");
        } else {
            String path = attribute.getValue();
            if ("src".equals(attribute.getKey())) {
                path = modifyPath(basePath, path);
            }
            writer.append("'").append(path).append("'");
        }
        writer.append(");");
    }
}
 
Example 5
Source File: CaptchaElementSelector.java    From Asqatasun with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 *
 * @param element
 * @return wheter either one attribute of the current element, either its
 * text, either one attribute of one of its parent or the text of one of
 * its parents contains the "captcha" keyword
 */
private boolean parseAttributeToExtractCaptcha(Element element) {
    if (element.nodeName().equalsIgnoreCase(HTML_ELEMENT) || 
            element.nodeName().equalsIgnoreCase(BODY_ELEMENT)) {
        return false;
    }
    if (StringUtils.containsIgnoreCase(element.ownText(), CAPTCHA_KEY)) {
        return true;
    } else {
        for (Attribute attr : element.attributes()) {
            if (StringUtils.containsIgnoreCase(attr.getValue(), CAPTCHA_KEY)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 6
Source File: TemplateRecordDefinitionAnnotator.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
  }
}
 
Example 7
Source File: Calendar.java    From calendar-component with Apache License 2.0 5 votes vote down vote up
@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()));
    }
}
 
Example 8
Source File: AttributeParseTest.java    From jsoup-learning with MIT License 5 votes vote down vote up
@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"));
}
 
Example 9
Source File: AttributeParseTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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"));
}
 
Example 10
Source File: HtmlNavigator.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Attribute> getAttributeAxisIterator(Object contextNode, String localName, String namespacePrefix, String namespaceURI) {
    Element node = ((Element) contextNode);
    LinkedList<Attribute> attributes = new LinkedList<>();
    for (Attribute attribute : node.attributes()) {
        if (localName.equals(attribute.getKey())) {
            attributes.add(attribute);
        }
    }
    return attributes.iterator();
}
 
Example 11
Source File: AttributeParseTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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"));
}
 
Example 12
Source File: AttributeParseTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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"));
}
 
Example 13
Source File: AttributeParseTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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"));
}
 
Example 14
Source File: AttributeParseTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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"));
}
 
Example 15
Source File: TagServlet.java    From firing-range with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the request filtering out unallowed tags. Note that an empty allowedTag we allow
 * all tags.
 */
private void handleRequest(
    Elements elements, HttpServletResponse response, String allowedTag, String allowedAttr)
        throws IOException {
  if (allowedTag.equalsIgnoreCase("script")) {
    elements.empty();
  }

  StringBuilder res = new StringBuilder();
  for (Element element : elements) {
    String tag = element.tagName();

    if (!allowedTag.isEmpty() && !allowedTag.equalsIgnoreCase(tag)) {
      continue;
    }

    if (!allowedAttr.isEmpty()) {
      Attributes attributes = element.attributes();
      for (Attribute attribute : attributes) {
        if (!attribute.getKey().equalsIgnoreCase(allowedAttr)) {
          Responses.sendError(response, "Invalid input attribute", 400);
          return;
        }
      }
    }
    res.append(element.toString());
  }
  Responses.sendXssed(response, res.toString());
}
 
Example 16
Source File: AttributeParseTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test public void parsesEmptyString() {
    String html = "<a />";
    Element el = Jsoup.parse(html).getElementsByTag("a").get(0);
    Attributes attr = el.attributes();
    assertEquals(0, attr.size());
}
 
Example 17
Source File: AttributeParseTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test public void parsesEmptyString() {
    String html = "<a />";
    Element el = Jsoup.parse(html).getElementsByTag("a").get(0);
    Attributes attr = el.attributes();
    assertEquals(0, attr.size());
}
 
Example 18
Source File: AttributeParseTest.java    From jsoup-learning with MIT License 4 votes vote down vote up
@Test public void parsesEmptyString() {
    String html = "<a />";
    Element el = Jsoup.parse(html).getElementsByTag("a").get(0);
    Attributes attr = el.attributes();
    assertEquals(0, attr.size());
}
 
Example 19
Source File: AttributeParseTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test public void parsesEmptyString() {
    String html = "<a />";
    Element el = Jsoup.parse(html).getElementsByTag("a").get(0);
    Attributes attr = el.attributes();
    assertEquals(0, attr.size());
}
 
Example 20
Source File: Expression.java    From firing-range with Apache License 2.0 4 votes vote down vote up
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  if (request.getParameter("q") == null) {
    Responses.sendError(response, "Missing q parameter", 400);
    return;
  }

  String  q = request.getParameter("q");
  Document doc = Jsoup.parseBodyFragment(q);
  Element body = doc.body();
  Elements elements = body.getAllElements();
  elements.remove(body);
  if (elements.isEmpty()) {
    Responses.sendError(response, "Invalid input, no tags", 400);
    return;
  }

  StringBuilder res = new StringBuilder();
  for (Element element : elements) {
    boolean validElement = true;

    Attributes attributes = element.attributes();
    for (Attribute attribute : attributes) {
      if (attribute.getKey().toLowerCase().startsWith("on")
          || attribute.getKey().toLowerCase().equals("href")
          || attribute.getKey().toLowerCase().equals("src")) {
        validElement = false;
      }

      if (attribute.getKey().toLowerCase().equals("style")
          && attribute.getValue().toLowerCase().contains("expression")) {
        validElement = false;
      }
    }

    if (validElement) {
      res.append(element.toString());
    }
  }
  Responses.sendXssed(response, res.toString());
}