org.jsoup.helper.Validate Java Examples

The following examples show how to use org.jsoup.helper.Validate. 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: Tokeniser.java    From jsoup-learning with MIT License 6 votes vote down vote up
void emit(Token token) {
    Validate.isFalse(isEmitPending, "There is an unread token pending!");

    emitPending = token;
    isEmitPending = true;

    if (token.type == Token.TokenType.StartTag) {
        Token.StartTag startTag = (Token.StartTag) token;
        lastStartTag = startTag;
        if (startTag.selfClosing)
            selfClosingFlagAcknowledged = false;
    } else if (token.type == Token.TokenType.EndTag) {
        Token.EndTag endTag = (Token.EndTag) token;
        if (endTag.attributes != null)
            error("Attributes incorrectly present on end tag");
    }
}
 
Example #2
Source File: Whitelist.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 Remove a previously configured enforced attribute from a tag.

 @param tag   The tag the enforced attribute is for.
 @param attribute   The attribute name
 @return this (for chaining)
 */
public Whitelist removeEnforcedAttribute(String tag, String attribute) {
    Validate.notEmpty(tag);
    Validate.notEmpty(attribute);

    TagName tagName = TagName.valueOf(tag);
    if(tagNames.contains(tagName) && enforcedAttributes.containsKey(tagName)) {
        AttributeKey attrKey = AttributeKey.valueOf(attribute);
        Map<AttributeKey, AttributeValue> attrMap = enforcedAttributes.get(tagName);
        attrMap.remove(attrKey);

        if(attrMap.isEmpty()) // Remove tag from enforced attribute map if no enforced attributes are present
            enforcedAttributes.remove(tagName);
    }
    return this;
}
 
Example #3
Source File: XPathParser.java    From xsoup with MIT License 6 votes vote down vote up
public XPathEvaluator parse() {

        while (!tq.isEmpty()) {
            Validate.isFalse(noEvalAllow, "XPath error! No operator allowed after attribute or function!" + tq);
            if (tq.matchChomp(OR_COMBINATOR)) {
                tq.consumeWhitespace();
                return combineXPathEvaluator(tq.remainder());
            } else if (tq.matchesAny(HIERARCHY_COMBINATORS)) {
                combinator(tq.consumeAny(HIERARCHY_COMBINATORS));
            } else {
                findElements();
            }
            tq.consumeWhitespace();
        }
        return collectXPathEvaluator();
    }
 
Example #4
Source File: Factory.java    From Skype4J with Apache License 2.0 6 votes vote down vote up
public static ChatImpl createChat(SkypeImpl client, String identity) throws ConnectionException, ChatNotFoundException {
    Validate.notNull(client, "Client must not be null");
    Validate.notEmpty(identity, "Identity must not be null/empty");

    ChatImpl result = null;

    if (identity.startsWith("19:")) {
        if (identity.endsWith("@thread.skype")) {
            result = new ChatGroup(client, identity);
        } else if (identity.endsWith("@p2p.thread.skype")) {
            result = new ChatP2P(client, identity);
        }
    } else if (identity.startsWith("8:")) {
        result = new ChatIndividual(client, identity);
    } else if (identity.startsWith("28:")) {
        result = new ChatBot(client, identity);
    }

    if (result != null) {
        result.load();
        return result;
    }

    throw new IllegalArgumentException(String.format("Unknown chat type with identity %s", identity));
}
 
Example #5
Source File: Whitelist.java    From astor with GNU General Public License v2.0 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 with this value.
 <p>
 E.g.: <code>addEnforcedAttribute("a", "rel", "nofollow")</code> will make all <code>a</code> tags output as
 <code>&lt;a href="..." rel="nofollow"&gt;</code>
 </p>

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

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

    if (enforcedAttributes.containsKey(tagName)) {
        enforcedAttributes.get(tagName).put(attrKey, attrVal);
    } else {
        Map<AttributeKey, AttributeValue> attrMap = new HashMap<>();
        attrMap.put(attrKey, attrVal);
        enforcedAttributes.put(tagName, attrMap);
    }
    return this;
}
 
Example #6
Source File: XmlTreeBuilder.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected boolean process(Token token) {
    // start tag, end tag, doctype, comment, character, eof
    switch (token.type) {
        case StartTag:
            insert(token.asStartTag());
            break;
        case EndTag:
            popStackToClose(token.asEndTag());
            break;
        case Comment:
            insert(token.asComment());
            break;
        case Character:
            insert(token.asCharacter());
            break;
        case Doctype:
            insert(token.asDoctype());
            break;
        case EOF: // could put some normalisation here if desired
            break;
        default:
            Validate.fail("Unexpected token type: " + token.type);
    }
    return true;
}
 
Example #7
Source File: Whitelist.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 Remove a previously configured enforced attribute from a tag.

 @param tag   The tag the enforced attribute is for.
 @param key   The attribute key
 @return this (for chaining)
 */
public Whitelist removeEnforcedAttribute(String tag, String key) {
    Validate.notEmpty(tag);
    Validate.notEmpty(key);

    TagName tagName = TagName.valueOf(tag);
    if(tagNames.contains(tagName) && enforcedAttributes.containsKey(tagName)) {
        AttributeKey attrKey = AttributeKey.valueOf(key);
        Map<AttributeKey, AttributeValue> attrMap = enforcedAttributes.get(tagName);
        attrMap.remove(attrKey);

        if(attrMap.isEmpty()) // Remove tag from enforced attribute map if no enforced attributes are present
            enforcedAttributes.remove(tagName);
    }
    return this;
}
 
Example #8
Source File: QueryParser.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void byTag() {
    String tagName = tq.consumeElementSelector();

    Validate.notEmpty(tagName);

    // namespaces: wildcard match equals(tagName) or ending in ":"+tagName
    if (tagName.startsWith("*|")) {
        evals.add(new CombiningEvaluator.Or(new Evaluator.Tag(normalize(tagName)), new Evaluator.TagEndsWith(normalize(tagName.replace("*|", ":")))));
    } else {
        // namespaces: if element name is "abc:def", selector must be "abc|def", so flip:
        if (tagName.contains("|"))
            tagName = tagName.replace("|", ":");

        evals.add(new Evaluator.Tag(tagName.trim()));
    }
}
 
Example #9
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 #10
Source File: QueryParser.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void byAttribute() {
    TokenQueue cq = new TokenQueue(tq.chompBalanced('[', ']')); // content queue
    String key = cq.consumeToAny(AttributeEvals); // eq, not, start, end, contain, match, (no val)
    Validate.notEmpty(key);
    cq.consumeWhitespace();

    if (cq.isEmpty()) {
        if (key.startsWith("^"))
            evals.add(new Evaluator.AttributeStarting(key.substring(1)));
        else
            evals.add(new Evaluator.Attribute(key));
    } else {
        if (cq.matchChomp("="))
            evals.add(new Evaluator.AttributeWithValue(key, cq.remainder()));

        else if (cq.matchChomp("!="))
            evals.add(new Evaluator.AttributeWithValueNot(key, cq.remainder()));

        else if (cq.matchChomp("^="))
            evals.add(new Evaluator.AttributeWithValueStarting(key, cq.remainder()));

        else if (cq.matchChomp("$="))
            evals.add(new Evaluator.AttributeWithValueEnding(key, cq.remainder()));

        else if (cq.matchChomp("*="))
            evals.add(new Evaluator.AttributeWithValueContaining(key, cq.remainder()));

        else if (cq.matchChomp("~="))
            evals.add(new Evaluator.AttributeWithValueMatching(key, Pattern.compile(cq.remainder())));
        else
            throw new Selector.SelectorParseException("Could not parse attribute query '%s': unexpected token at '%s'", query, cq.remainder());
    }
}
 
Example #11
Source File: Element.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static <E extends Element> Integer indexInList(Element search, List<E> elements) {
    Validate.notNull(search);
    Validate.notNull(elements);

    for (int i = 0; i < elements.size(); i++) {
        E element = elements.get(i);
        if (element == search)
            return i;
    }
    return null;
}
 
Example #12
Source File: TextNode.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Split this text node into two nodes at the specified string offset. After splitting, this node will contain the
 * original text up to the offset, and will have a new text node sibling containing the text after the offset.
 * @param offset string offset point to split node at.
 * @return the newly created text node containing the text after the offset.
 */
public TextNode splitText(int offset) {
    Validate.isTrue(offset >= 0, "Split offset must be not be negative");
    Validate.isTrue(offset < text.length(), "Split offset must not be greater than current text length");

    String head = getWholeText().substring(0, offset);
    String tail = getWholeText().substring(offset);
    text(head);
    TextNode tailNode = new TextNode(tail, this.baseUri());
    if (parent() != null)
        parent().addChildren(siblingIndex()+1, tailNode);

    return tailNode;
}
 
Example #13
Source File: HtmlToPlainText.java    From jsoup-learning with MIT License 5 votes vote down vote up
public static void main(String... args) throws IOException {
    Validate.isTrue(args.length == 1, "usage: supply url to fetch");
    String url = args[0];

    // fetch the specified URL and parse to a HTML DOM
    Document doc = Jsoup.connect(url).get();

    HtmlToPlainText formatter = new HtmlToPlainText();
    String plainText = formatter.getPlainText(doc);
    System.out.println(plainText);
}
 
Example #14
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 #15
Source File: Element.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 Remove a class name from this element's {@code class} attribute.
 @param className class name to remove
 @return this element
 */
public Element removeClass(String className) {
    Validate.notNull(className);

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

    return this;
}
 
Example #16
Source File: Attributes.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
int indexOfKey(String key) {
    Validate.notNull(key);
    for (int i = 0; i < size; i++) {
        if (key.equals(keys[i]))
            return i;
    }
    return NotFound;
}
 
Example #17
Source File: Element.java    From jsoup-learning with MIT License 5 votes vote down vote up
/**
 * Gets the next sibling element of this element. E.g., if a {@code div} contains two {@code p}s, 
 * the {@code nextElementSibling} of the first {@code p} is the second {@code p}.
 * <p/>
 * This is similar to {@link #nextSibling()}, but specifically finds only Elements
 * @return the next element, or null if there is no next element
 * @see #previousElementSibling()
 */
public Element nextElementSibling() {
    if (parentNode == null) return null;
    List<Element> siblings = parent().children();
    Integer index = indexInList(this, siblings);
    Validate.notNull(index);
    if (siblings.size() > index+1)
        return siblings.get(index+1);
    else
        return null;
}
 
Example #18
Source File: Document.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 Create a valid, empty shell of a document, suitable for adding more elements to.
 @param baseUri baseUri of document
 @return document with html, head, and body elements.
 */
public static Document createShell(String baseUri) {
    Validate.notNull(baseUri);

    Document doc = new Document(baseUri);
    Element html = doc.appendElement("html");
    html.appendElement("head");
    html.appendElement("body");

    return doc;
}
 
Example #19
Source File: Whitelist.java    From jsoup-learning with MIT License 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 #20
Source File: Node.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void replaceChild(Node out, Node in) {
    Validate.isTrue(out.parentNode == this);
    Validate.notNull(in);
    if (in.parentNode != null)
        in.parentNode.removeChild(in);

    final int index = out.siblingIndex;
    ensureChildNodes().set(index, in);
    in.parentNode = this;
    in.setSiblingIndex(index);
    out.parentNode = null;
}
 
Example #21
Source File: Attributes.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void checkCapacity(int minNewSize) {
    Validate.isTrue(minNewSize >= size);
    int curSize = keys.length;
    if (curSize >= minNewSize)
        return;

    int newSize = curSize >= InitialCapacity ? size * GrowthFactor : InitialCapacity;
    if (minNewSize > newSize)
        newSize = minNewSize;

    keys = copyOf(keys, newSize);
    vals = copyOf(vals, newSize);
}
 
Example #22
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 #23
Source File: Node.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Perform a depth-first traversal through this node and its descendants.
 * @param nodeVisitor the visitor callbacks to perform on each node
 * @return this node, for chaining
 */
public Node traverse(NodeVisitor nodeVisitor) {
    Validate.notNull(nodeVisitor);
    NodeTraversor traversor = new NodeTraversor(nodeVisitor);
    traversor.traverse(this);
    return this;
}
 
Example #24
Source File: Element.java    From jsoup-learning with MIT License 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 #25
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
boolean inSelectScope(String targetName) {
    for (int pos = stack.size() -1; pos >= 0; pos--) {
        Element el = stack.get(pos);
        String elName = el.nodeName();
        if (elName.equals(targetName))
            return true;
        if (!StringUtil.in(elName, TagSearchSelectScope)) // all elements except
            return false;
    }
    Validate.fail("Should not be reachable");
    return false;
}
 
Example #26
Source File: Element.java    From astor with GNU General Public License v2.0 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().childElementsList();
    Integer index = indexInList(this, siblings);
    Validate.notNull(index);
    if (index > 0)
        return siblings.get(index-1);
    else
        return null;
}
 
Example #27
Source File: NodeTraversor.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Start a depth-first traverse of all elements.
 * @param visitor Node visitor.
 * @param elements Elements to filter.
 */
public static void traverse(NodeVisitor visitor, Elements elements) {
    Validate.notNull(visitor);
    Validate.notNull(elements);
    for (Element el : elements)
        traverse(visitor, el);
}
 
Example #28
Source File: QueryParser.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void matches(boolean own) {
    tq.consume(own ? ":matchesOwn" : ":matches");
    String regex = tq.chompBalanced('(', ')'); // don't unescape, as regex bits will be escaped
    Validate.notEmpty(regex, ":matches(regex) query must not be empty");

    if (own)
        evals.add(new Evaluator.MatchesOwn(Pattern.compile(regex)));
    else
        evals.add(new Evaluator.Matches(Pattern.compile(regex)));
}
 
Example #29
Source File: Tokeniser.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
void emit(Token token) {
    Validate.isFalse(isEmitPending, "There is an unread token pending!");

    emitPending = token;
    isEmitPending = true;

    if (token.type == Token.TokenType.StartTag) {
        Token.StartTag startTag = (Token.StartTag) token;
        lastStartTag = startTag.tagName;
    } else if (token.type == Token.TokenType.EndTag) {
        Token.EndTag endTag = (Token.EndTag) token;
        if (endTag.attributes != null)
            error("Attributes incorrectly present on end tag");
    }
}
 
Example #30
Source File: QueryParser.java    From jsoup-learning with MIT License 5 votes vote down vote up
private void byTag() {
    String tagName = tq.consumeElementSelector();
    Validate.notEmpty(tagName);

    // namespaces: if element name is "abc:def", selector must be "abc|def", so flip:
    if (tagName.contains("|"))
        tagName = tagName.replace("|", ":");

    evals.add(new Evaluator.Tag(tagName.trim().toLowerCase()));
}