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

The following examples show how to use org.jsoup.helper.Validate#notNull() . 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 5 votes vote down vote up
/**
 Add a list of allowed elements to a whitelist. (If a tag is not allowed, it will be removed from the HTML.)

 @param tags tag names to allow
 @return this (for chaining)
 */
public Whitelist addTags(String... tags) {
    Validate.notNull(tags);

    for (String tagName : tags) {
        Validate.notEmpty(tagName);
        tagNames.add(TagName.valueOf(tagName));
    }
    return this;
}
 
Example 2
Source File: Element.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create and append a new TextNode to this element.
 * 
 * @param text the unencoded text to add
 * @return this element
 */
public Element appendText(String text) {
    Validate.notNull(text);
    TextNode node = new TextNode(text);
    appendChild(node);
    return this;
}
 
Example 3
Source File: Attribute.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 Set the attribute key; case is preserved.
 @param key the new key; must not be null
 */
public void setKey(String key) {
    Validate.notNull(key);
    key = key.trim();
    Validate.notEmpty(key); // trimming could potentially make empty, so validate here
    if (parent != null) {
        int i = parent.indexOfKey(this.key);
        if (i != Attributes.NotFound)
            parent.keys[i] = key;
    }
    this.key = key;
}
 
Example 4
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 5
Source File: Element.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add inner HTML into this element. The supplied HTML will be parsed, and each node prepended to the start of the element's children.
 * @param html HTML to add inside this element, before the existing HTML
 * @return this element
 * @see #html(String)
 */
public Element prepend(String html) {
    Validate.notNull(html);
    
    List<Node> nodes = Parser.parseFragment(html, this, baseUri());
    addChildren(0, nodes.toArray(new Node[nodes.size()]));
    return this;
}
 
Example 6
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 7
Source File: Attributes.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 Set a new attribute, or replace an existing one by key.
 @param attribute attribute
 */
public void put(Attribute attribute) {
    Validate.notNull(attribute);
    if (attributes == null)
         attributes = new LinkedHashMap<String, Attribute>(2);
    attributes.put(attribute.getKey(), attribute);
}
 
Example 8
Source File: Node.java    From jsoup-learning with MIT License 5 votes vote down vote up
private void addSiblingHtml(int index, String html) {
    Validate.notNull(html);
    Validate.notNull(parentNode);

    Element context = parent() instanceof Element ? (Element) parent() : null;        
    List<Node> nodes = Parser.parseFragment(html, context, baseUri());
    parentNode.addChildren(index, nodes.toArray(new Node[nodes.size()]));
}
 
Example 9
Source File: Element.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add inner HTML to this element. The supplied HTML will be parsed, and each node appended to the end of the children.
 * @param html HTML to add inside this element, after the existing HTML
 * @return this element
 * @see #html(String)
 */
public Element append(String html) {
    Validate.notNull(html);

    List<Node> nodes = Parser.parseFragment(html, this, baseUri());
    addChildren(nodes.toArray(new Node[nodes.size()]));
    return this;
}
 
Example 10
Source File: Element.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 Add a class name to this element's {@code class} attribute.
 @param className class name to add
 @return this element
 */
public Element addClass(String className) {
    Validate.notNull(className);

    Set<String> classes = classNames();
    classes.add(className);
    classNames(classes);

    return this;
}
 
Example 11
Source File: Element.java    From jsoup-learning with MIT License 5 votes vote down vote up
/**
 * Gets the previous element sibling of this element.
 * @return the previous element, or null if there is no previous element
 * @see #nextElementSibling()
 */
public Element previousElementSibling() {
    if (parentNode == null) return null;
    List<Element> siblings = parent().children();
    Integer index = indexInList(this, siblings);
    Validate.notNull(index);
    if (index > 0)
        return siblings.get(index-1);
    else
        return null;
}
 
Example 12
Source File: Node.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void addSiblingHtml(int index, String html) {
    Validate.notNull(html);
    Validate.notNull(parentNode);

    Element context = parent() instanceof Element ? (Element) parent() : null;
    List<Node> nodes = Parser.parseFragment(html, context, baseUri());
    parentNode.addChildren(index, nodes.toArray(new Node[nodes.size()]));
}
 
Example 13
Source File: Node.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void addSiblingHtml(int index, String html) {
    Validate.notNull(html);
    Validate.notNull(parentNode);

    Element context = parent() instanceof Element ? (Element) parent() : null;
    List<Node> nodes = Parser.parseFragment(html, context, baseUri());
    parentNode.addChildren(index, nodes.toArray(new Node[nodes.size()]));
}
 
Example 14
Source File: Attributes.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 Set a new attribute, or replace an existing one by key.
 @param attribute attribute with case sensitive key
 @return these attributes, for chaining
 */
public Attributes put(Attribute attribute) {
    Validate.notNull(attribute);
    put(attribute.getKey(), attribute.getValue());
    attribute.parent = this;
    return this;
}
 
Example 15
Source File: CharacterReader.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public CharacterReader(Reader input, int sz) {
    Validate.notNull(input);
    Validate.isTrue(input.markSupported());
    reader = input;
    charBuf = new char[sz > maxBufferLen ? maxBufferLen : sz];
    bufferUp();
}
 
Example 16
Source File: Node.java    From jsoup-learning with MIT License 4 votes vote down vote up
/**
 * Remove an attribute from this element.
 * @param attributeKey The attribute to remove.
 * @return this (for chaining)
 */
public Node removeAttr(String attributeKey) {
    Validate.notNull(attributeKey);
    attributes.remove(attributeKey);
    return this;
}
 
Example 17
Source File: Node.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Replace this node in the DOM with the supplied node.
 * @param in the node that will will replace the existing node.
 */
public void replaceWith(Node in) {
    Validate.notNull(in);
    Validate.notNull(parentNode);
    parentNode.replaceChild(this, in);
}
 
Example 18
Source File: Whitelist.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
TypedValue(String value) {
    Validate.notNull(value);
    this.value = value;
}
 
Example 19
Source File: CharacterReader.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
CharacterReader(String input) {
    Validate.notNull(input);
    this.input = input.toCharArray();
    this.length = this.input.length;
}
 
Example 20
Source File: Node.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Removes this node from the DOM, and moves its children up into the node's parent. This has the effect of dropping
 * the node but keeping its children.
 * <p>
 * For example, with the input html:
 * </p>
 * <p>{@code <div>One <span>Two <b>Three</b></span></div>}</p>
 * Calling {@code element.unwrap()} on the {@code span} element will result in the html:
 * <p>{@code <div>One Two <b>Three</b></div>}</p>
 * and the {@code "Two "} {@link TextNode} being returned.
 *
 * @return the first child of this node, after the node has been unwrapped. Null if the node had no children.
 * @see #remove()
 * @see #wrap(String)
 */
public Node unwrap() {
    Validate.notNull(parentNode);
    final List<Node> childNodes = ensureChildNodes();
    Node firstChild = childNodes.size() > 0 ? childNodes.get(0) : null;
    parentNode.addChildren(siblingIndex, this.childNodesAsArray());
    this.remove();

    return firstChild;
}