Java Code Examples for org.jsoup.nodes.Attribute#getKey()

The following examples show how to use org.jsoup.nodes.Attribute#getKey() . 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: W3CDom.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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) : "";
}
 
Example 2
Source File: W3CDom.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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) : "";
}
 
Example 3
Source File: W3CDom.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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) : "";
}
 
Example 4
Source File: Html.java    From flow with Apache License 2.0 5 votes vote down vote up
private void setAttribute(Attribute attribute) {
    String name = attribute.getKey();
    String value = attribute.getValue();
    if (value == null) {
        value = "";
    }
    getElement().setAttribute(name, value);
}
 
Example 5
Source File: Whitelist.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test if the supplied attribute is allowed by this whitelist for this tag
 * @param tagName tag to consider allowing the attribute in
 * @param el element under test, to confirm protocol
 * @param attr attribute under test
 * @return true if allowed
 */
protected boolean isSafeAttribute(String tagName, Element el, Attribute attr) {
    TagName tag = TagName.valueOf(tagName);
    AttributeKey key = AttributeKey.valueOf(attr.getKey());

    Set<AttributeKey> okSet = attributes.get(tag);
    if (okSet != null && okSet.contains(key)) {
        if (protocols.containsKey(tag)) {
            Map<AttributeKey, Set<Protocol>> attrProts = protocols.get(tag);
            // ok if not defined protocol; otherwise test
            return !attrProts.containsKey(key) || testValidProtocol(el, attr, attrProts.get(key));
        } else { // attribute found, no protocols defined, so OK
            return true;
        }
    }
    // might be an enforced attribute?
    Map<AttributeKey, AttributeValue> enforcedSet = enforcedAttributes.get(tag);
    if (enforcedSet != null) {
        Attributes expect = getEnforcedAttributes(tagName);
        String attrKey = attr.getKey();
        if (expect.hasKeyIgnoreCase(attrKey)) {
            return expect.getIgnoreCase(attrKey).equals(attr.getValue());
        }
    }
    // no attributes defined for tag, try :all tag
    return !tagName.equals(":all") && isSafeAttribute(":all", el, attr);
}
 
Example 6
Source File: Whitelist.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test if the supplied attribute is allowed by this whitelist for this tag
 * @param tagName tag to consider allowing the attribute in
 * @param el element under test, to confirm protocol
 * @param attr attribute under test
 * @return true if allowed
 */
protected boolean isSafeAttribute(String tagName, Element el, Attribute attr) {
    TagName tag = TagName.valueOf(tagName);
    AttributeKey key = AttributeKey.valueOf(attr.getKey());

    Set<AttributeKey> okSet = attributes.get(tag);
    if (okSet != null && okSet.contains(key)) {
        if (protocols.containsKey(tag)) {
            Map<AttributeKey, Set<Protocol>> attrProts = protocols.get(tag);
            // ok if not defined protocol; otherwise test
            return !attrProts.containsKey(key) || testValidProtocol(el, attr, attrProts.get(key));
        } else { // attribute found, no protocols defined, so OK
            return true;
        }
    }
    // might be an enforced attribute?
    Map<AttributeKey, AttributeValue> enforcedSet = enforcedAttributes.get(tag);
    if (enforcedSet != null) {
        Attributes expect = getEnforcedAttributes(tagName);
        String attrKey = attr.getKey();
        if (expect.hasKeyIgnoreCase(attrKey)) {
            return expect.getIgnoreCase(attrKey).equals(attr.getValue());
        }
    }
    // no attributes defined for tag, try :all tag
    return !tagName.equals(":all") && isSafeAttribute(":all", el, attr);
}