Java Code Examples for org.jsoup.helper.Validate#notEmpty()

The following examples show how to use org.jsoup.helper.Validate#notEmpty() . 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: Whitelist.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 Add a list of allowed attributes to a tag. (If an attribute is not allowed on an element, it will be removed.)
 <p>
 E.g.: <code>addAttributes("a", "href", "class")</code> allows <code>href</code> and <code>class</code> attributes
 on <code>a</code> tags.
 </p>
 <p>
 To make an attribute valid for <b>all tags</b>, use the pseudo tag <code>:all</code>, e.g.
 <code>addAttributes(":all", "class")</code>.
 </p>

 @param tag  The tag the attributes are for. The tag will be added to the allowed tag list if necessary.
 @param attributes List of valid attributes for the tag
 @return this (for chaining)
 */
public Whitelist addAttributes(String tag, String... attributes) {
    Validate.notEmpty(tag);
    Validate.notNull(attributes);
    Validate.isTrue(attributes.length > 0, "No attribute names supplied.");

    TagName tagName = TagName.valueOf(tag);
    if (!tagNames.contains(tagName))
        tagNames.add(tagName);
    Set<AttributeKey> attributeSet = new HashSet<>();
    for (String key : attributes) {
        Validate.notEmpty(key);
        attributeSet.add(AttributeKey.valueOf(key));
    }
    if (this.attributes.containsKey(tagName)) {
        Set<AttributeKey> currentSet = this.attributes.get(tagName);
        currentSet.addAll(attributeSet);
    } else {
        this.attributes.put(tagName, attributeSet);
    }
    return this;
}
 
Example 2
Source File: SishuokWhitelist.java    From es with Apache License 2.0 6 votes vote down vote up
/**
 * Add a list of allowed attributes to a tag. (If an attribute is not allowed on an element, it will be removed.)
 * <p/>
 * To make an attribute valid for <b>all tags</b>, use the pseudo tag <code>:all</code>, e.g.
 * <code>addAttributes(":all", "class")</code>.
 *
 * @param tag  The tag the attributes are for
 * @param keys List of valid attributes for the tag
 * @return this (for chaining)
 */
public SishuokWhitelist addAttributes(String tag, String... keys) {
    Validate.notEmpty(tag);
    Validate.notNull(keys);

    TagName tagName = TagName.valueOf(tag);
    Set<AttributeKey> attributeSet = new HashSet<AttributeKey>();
    for (String key : keys) {
        Validate.notEmpty(key);
        attributeSet.add(AttributeKey.valueOf(key));
    }
    if (attributes.containsKey(tagName)) {
        Set<AttributeKey> currentSet = attributes.get(tagName);
        currentSet.addAll(attributeSet);
    } else {
        attributes.put(tagName, attributeSet);
    }
    return this;
}
 
Example 3
Source File: Whitelist.java    From jsoup-learning with MIT License 6 votes vote down vote up
/**
 Add an enforced attribute to a tag. An enforced attribute will always be added to the element. If the element
 already has the attribute set, it will be overridden.
 <p/>
 E.g.: <code>addEnforcedAttribute("a", "rel", "nofollow")</code> will make all <code>a</code> tags output as
 <code>&lt;a href="..." rel="nofollow"></code>

 @param tag   The tag the enforced attribute is for. The tag will be added to the allowed tag list if necessary.
 @param key   The attribute key
 @param value The enforced attribute value
 @return this (for chaining)
 */
public Whitelist addEnforcedAttribute(String tag, String key, String value) {
    Validate.notEmpty(tag);
    Validate.notEmpty(key);
    Validate.notEmpty(value);

    TagName tagName = TagName.valueOf(tag);
    if (!tagNames.contains(tagName))
        tagNames.add(tagName);
    AttributeKey attrKey = AttributeKey.valueOf(key);
    AttributeValue attrVal = AttributeValue.valueOf(value);

    if (enforcedAttributes.containsKey(tagName)) {
        enforcedAttributes.get(tagName).put(attrKey, attrVal);
    } else {
        Map<AttributeKey, AttributeValue> attrMap = new HashMap<AttributeKey, AttributeValue>();
        attrMap.put(attrKey, attrVal);
        enforcedAttributes.put(tagName, attrMap);
    }
    return this;
}
 
Example 4
Source File: Whitelist.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 Add a list of allowed attributes to a tag. (If an attribute is not allowed on an element, it will be removed.)
 <p>
 E.g.: <code>addAttributes("a", "href", "class")</code> allows <code>href</code> and <code>class</code> attributes
 on <code>a</code> tags.
 </p>
 <p>
 To make an attribute valid for <b>all tags</b>, use the pseudo tag <code>:all</code>, e.g.
 <code>addAttributes(":all", "class")</code>.
 </p>

 @param tag  The tag the attributes are for. The tag will be added to the allowed tag list if necessary.
 @param keys List of valid attributes for the tag
 @return this (for chaining)
 */
public Whitelist addAttributes(String tag, String... keys) {
    Validate.notEmpty(tag);
    Validate.notNull(keys);
    Validate.isTrue(keys.length > 0, "No attributes supplied.");

    TagName tagName = TagName.valueOf(tag);
    if (!tagNames.contains(tagName))
        tagNames.add(tagName);
    Set<AttributeKey> attributeSet = new HashSet<AttributeKey>();
    for (String key : keys) {
        Validate.notEmpty(key);
        attributeSet.add(AttributeKey.valueOf(key));
    }
    if (attributes.containsKey(tagName)) {
        Set<AttributeKey> currentSet = attributes.get(tagName);
        currentSet.addAll(attributeSet);
    } else {
        attributes.put(tagName, attributeSet);
    }
    return this;
}
 
Example 5
Source File: Element.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find an element by ID, including or under this element.
 * <p>
 * Note that this finds the first matching ID, starting with this element. If you search down from a different
 * starting point, it is possible to find a different element by ID. For unique element by ID within a Document,
 * use {@link Document#getElementById(String)}
 * @param id The ID to search for.
 * @return The first matching element by ID, starting with this element, or null if none found.
 */
public Element getElementById(String id) {
    Validate.notEmpty(id);
    
    Elements elements = Collector.collect(new Evaluator.Id(id), this);
    if (elements.size() > 0)
        return elements.get(0);
    else
        return null;
}
 
Example 6
Source File: Selector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Selector(String query, Element root) {
    Validate.notNull(query);
    query = query.trim();
    Validate.notEmpty(query);
    Validate.notNull(root);

    this.evaluator = QueryParser.parse(query);

    this.root = root;
}
 
Example 7
Source File: Whitelist.java    From jsoup-learning with MIT License 5 votes vote down vote up
/**
 Add allowed URL protocols for an element's URL attribute. This restricts the possible values of the attribute to
 URLs with the defined protocol.
 <p/>
 E.g.: <code>addProtocols("a", "href", "ftp", "http", "https")</code>

 @param tag       Tag the URL protocol is for
 @param key       Attribute key
 @param protocols List of valid protocols
 @return this, for chaining
 */
public Whitelist addProtocols(String tag, String key, String... protocols) {
    Validate.notEmpty(tag);
    Validate.notEmpty(key);
    Validate.notNull(protocols);

    TagName tagName = TagName.valueOf(tag);
    AttributeKey attrKey = AttributeKey.valueOf(key);
    Map<AttributeKey, Set<Protocol>> attrMap;
    Set<Protocol> protSet;

    if (this.protocols.containsKey(tagName)) {
        attrMap = this.protocols.get(tagName);
    } else {
        attrMap = new HashMap<AttributeKey, Set<Protocol>>();
        this.protocols.put(tagName, attrMap);
    }
    if (attrMap.containsKey(attrKey)) {
        protSet = attrMap.get(attrKey);
    } else {
        protSet = new HashSet<Protocol>();
        attrMap.put(attrKey, protSet);
    }
    for (String protocol : protocols) {
        Validate.notEmpty(protocol);
        Protocol prot = Protocol.valueOf(protocol);
        protSet.add(prot);
    }
    return this;
}
 
Example 8
Source File: Whitelist.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 Remove allowed URL protocols for an element's URL attribute.
 <p>
 E.g.: <code>removeProtocols("a", "href", "ftp")</code>
 </p>

 @param tag       Tag the URL protocol is for
 @param key       Attribute key
 @param protocols List of invalid protocols
 @return this, for chaining
 */
public Whitelist removeProtocols(String tag, String key, String... protocols) {
    Validate.notEmpty(tag);
    Validate.notEmpty(key);
    Validate.notNull(protocols);

    TagName tagName = TagName.valueOf(tag);
    AttributeKey attrKey = AttributeKey.valueOf(key);

    if(this.protocols.containsKey(tagName)) {
        Map<AttributeKey, Set<Protocol>> attrMap = this.protocols.get(tagName);
        if(attrMap.containsKey(attrKey)) {
            Set<Protocol> protSet = attrMap.get(attrKey);
            for (String protocol : protocols) {
                Validate.notEmpty(protocol);
                Protocol prot = Protocol.valueOf(protocol);
                protSet.remove(prot);
            }

            if(protSet.isEmpty()) { // Remove protocol set if empty
                attrMap.remove(attrKey);
                if(attrMap.isEmpty()) // Remove entry for tag if empty
                    this.protocols.remove(tagName);
            }
        }
    }
    return this;
}
 
Example 9
Source File: Whitelist.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 Add allowed URL protocols for an element's URL attribute. This restricts the possible values of the attribute to
 URLs with the defined protocol.
 <p>
 E.g.: <code>addProtocols("a", "href", "ftp", "http", "https")</code>
 </p>
 <p>
 To allow a link to an in-page URL anchor (i.e. <code>&lt;a href="#anchor"&gt;</code>, add a <code>#</code>:<br>
 E.g.: <code>addProtocols("a", "href", "#")</code>
 </p>

 @param tag       Tag the URL protocol is for
 @param attribute       Attribute name
 @param protocols List of valid protocols
 @return this, for chaining
 */
public Whitelist addProtocols(String tag, String attribute, String... protocols) {
    Validate.notEmpty(tag);
    Validate.notEmpty(attribute);
    Validate.notNull(protocols);

    TagName tagName = TagName.valueOf(tag);
    AttributeKey attrKey = AttributeKey.valueOf(attribute);
    Map<AttributeKey, Set<Protocol>> attrMap;
    Set<Protocol> protSet;

    if (this.protocols.containsKey(tagName)) {
        attrMap = this.protocols.get(tagName);
    } else {
        attrMap = new HashMap<>();
        this.protocols.put(tagName, attrMap);
    }
    if (attrMap.containsKey(attrKey)) {
        protSet = attrMap.get(attrKey);
    } else {
        protSet = new HashSet<>();
        attrMap.put(attrKey, protSet);
    }
    for (String protocol : protocols) {
        Validate.notEmpty(protocol);
        Protocol prot = Protocol.valueOf(protocol);
        protSet.add(prot);
    }
    return this;
}
 
Example 10
Source File: Element.java    From jsoup-learning with MIT License 5 votes vote down vote up
/**
 * Find an element by ID, including or under this element.
 * <p>
 * Note that this finds the first matching ID, starting with this element. If you search down from a different
 * starting point, it is possible to find a different element by ID. For unique element by ID within a Document,
 * use {@link Document#getElementById(String)}
 * @param id The ID to search for.
 * @return The first matching element by ID, starting with this element, or null if none found.
 */
public Element getElementById(String id) {
    Validate.notEmpty(id);
    
    Elements elements = Collector.collect(new Evaluator.Id(id), this);
    if (elements.size() > 0)
        return elements.get(0);
    else
        return null;
}
 
Example 11
Source File: Selector.java    From jsoup-learning with MIT License 5 votes vote down vote up
/**
 * Find elements matching selector.
 *
 * @param query CSS selector
 * @param roots root elements to descend into
 * @return matching elements, empty if not
 */
public static Elements select(String query, Iterable<Element> roots) {
    Validate.notEmpty(query);
    Validate.notNull(roots);
    LinkedHashSet<Element> elements = new LinkedHashSet<Element>();

    for (Element root : roots) {
        elements.addAll(select(query, root));
    }
    return new Elements(elements);
}
 
Example 12
Source File: QueryParser.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void not() {
    tq.consume(":not");
    String subQuery = tq.chompBalanced('(', ')');
    Validate.notEmpty(subQuery, ":not(selector) subselect must not be empty");

    evals.add(new StructuralEvaluator.Not(parse(subQuery)));
}
 
Example 13
Source File: FormElement.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Prepare to submit this form. A Connection object is created with the request set up from the form values. You
 * can then set up other options (like user-agent, timeout, cookies), then execute it.
 * @return a connection prepared from the values of this form.
 * @throws IllegalArgumentException if the form's absolute action URL cannot be determined. Make sure you pass the
 * document's base URI when parsing.
 */
public Connection submit() {
    String action = hasAttr("action") ? absUrl("action") : baseUri();
    Validate.notEmpty(action, "Could not determine a form action URL for submit. Ensure you set a base URI when parsing.");
    Connection.Method method = attr("method").toUpperCase().equals("POST") ?
            Connection.Method.POST : Connection.Method.GET;

    return Jsoup.connect(action)
            .data(formData())
            .method(method);
}
 
Example 14
Source File: Evaluator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public AttributeKeyPair(String key, String value) {
    Validate.notEmpty(key);
    Validate.notEmpty(value);

    this.key = normalize(key);
    if (value.startsWith("\"") && value.endsWith("\"")
            || value.startsWith("'") && value.endsWith("'")) {
        value = value.substring(1, value.length()-1);
    }
    this.value = normalize(value);
}
 
Example 15
Source File: Evaluator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public AttributeStarting(String keyPrefix) {
    Validate.notEmpty(keyPrefix);
    this.keyPrefix = lowerCase(keyPrefix);
}
 
Example 16
Source File: Evaluator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public AttributeStarting(String keyPrefix) {
    Validate.notEmpty(keyPrefix);
    this.keyPrefix = lowerCase(keyPrefix);
}
 
Example 17
Source File: QueryParser.java    From jsoup-learning with MIT License 4 votes vote down vote up
private void has() {
    tq.consume(":has");
    String subQuery = tq.chompBalanced('(', ')');
    Validate.notEmpty(subQuery, ":has(el) subselect must not be empty");
    evals.add(new StructuralEvaluator.Has(parse(subQuery)));
}
 
Example 18
Source File: Elements.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 Wrap the supplied HTML around each matched elements. For example, with HTML
 {@code <p><b>This</b> is <b>Jsoup</b></p>},
 <code>doc.select("b").wrap("&lt;i&gt;&lt;/i&gt;");</code>
 becomes {@code <p><i><b>This</b></i> is <i><b>jsoup</b></i></p>}
 @param html HTML to wrap around each element, e.g. {@code <div class="head"></div>}. Can be arbitrarily deep.
 @return this (for chaining)
 @see Element#wrap
 */
public Elements wrap(String html) {
    Validate.notEmpty(html);
    for (Element element : this) {
        element.wrap(html);
    }
    return this;
}
 
Example 19
Source File: Selector.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Find the first element that matches the query.
 * @param cssQuery CSS selector
 * @param root root element to descend into
 * @return the matching element, or <b>null</b> if none.
 */
public static Element selectFirst(String cssQuery, Element root) {
    Validate.notEmpty(cssQuery);
    return Collector.findFirst(QueryParser.parse(cssQuery), root);
}
 
Example 20
Source File: Element.java    From jsoup-learning with MIT License 2 votes vote down vote up
/**
 * Find elements that have this class, including or under this element. Case insensitive.
 * <p>
 * Elements can have multiple classes (e.g. {@code <div class="header round first">}. This method
 * checks each class, so you can find the above with {@code el.getElementsByClass("header");}.
 * 
 * @param className the name of the class to search for.
 * @return elements with the supplied class name, empty if none
 * @see #hasClass(String)
 * @see #classNames()
 */
public Elements getElementsByClass(String className) {
    Validate.notEmpty(className);

    return Collector.collect(new Evaluator.Class(className), this);
}