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

The following examples show how to use org.jsoup.helper.Validate#isTrue() . 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: XPathParser.java    From xsoup with MIT License 6 votes vote down vote up
private void functionRegex(String remainder) {
    Validate.isTrue(remainder.endsWith(")"), "Unclosed bracket for function! " + remainder);
    List<String> params = XTokenQueue.trimQuotes(XTokenQueue.parseFuncionParams(remainder.substring("regex(".length(), remainder.length() - 1)));
    if (params.size() == 1) {
        elementOperator = new ElementOperator.Regex(params.get(0));
    } else if (params.size() == 2) {
        if (params.get(0).startsWith("@")) {
            elementOperator = new ElementOperator.Regex(params.get(1), params.get(0).substring(1));
        } else {
            elementOperator = new ElementOperator.Regex(params.get(0), null, Integer.parseInt(params.get(1)));
        }
    } else if (params.size() == 3) {
        elementOperator = new ElementOperator.Regex(params.get(1), params.get(0).substring(1), Integer.parseInt(params.get(2)));
    } else {
        throw new Selector.SelectorParseException("Unknown usage for regex()" + remainder);
    }
}
 
Example 3
Source File: XTokenQueue.java    From xsoup with MIT License 5 votes vote down vote up
public static String trimQuotes(String str) {
    Validate.isTrue(str != null && str.length() > 0);
    String quote = str.substring(0, 1);
    if (StringUtil.in(quote, "\"", "'")) {
        Validate.isTrue(str.endsWith(quote), "Quote" + " for " + str + " is incomplete!");
        str = str.substring(1, str.length() - 1);
    }
    return str;
}
 
Example 4
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 5
Source File: TextNode.java    From jsoup-learning with MIT License 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 6
Source File: Node.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void removeChild(Node out) {
    Validate.isTrue(out.parentNode == this);
    final int index = out.siblingIndex;
    ensureChildNodes().remove(index);
    reindexChildren(index);
    out.parentNode = null;
}
 
Example 7
Source File: XTokenQueue.java    From zongtui-webcrawler with GNU General Public License v2.0 5 votes vote down vote up
public String consumeToUnescaped(String str) {
    String s = consumeToAny(str);
    if (s.length() > 0 && s.charAt(s.length() - 1) == '\\') {
        s += consume();
        s += consumeToUnescaped(str);
    }
    Validate.isTrue(pos < queue.length(), "Unclosed quotes! " + queue);
    return s;
}
 
Example 8
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 9
Source File: Node.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void removeChild(Node out) {
    Validate.isTrue(out.parentNode == this);
    final int index = out.siblingIndex;
    ensureChildNodes().remove(index);
    reindexChildren(index);
    out.parentNode = null;
}
 
Example 10
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 11
Source File: Node.java    From jsoup-learning with MIT License 5 votes vote down vote up
protected void removeChild(Node out) {
    Validate.isTrue(out.parentNode == this);
    int index = out.siblingIndex();
    childNodes.remove(index);
    reindexChildren();
    out.parentNode = null;
}
 
Example 12
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 13
Source File: Element.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Inserts the given child nodes into this element at the specified index. Current nodes will be shifted to the
 * right. The inserted nodes will be moved from their current parent. To prevent moving, copy the nodes first.
 *
 * @param index 0-based index to insert children at. Specify {@code 0} to insert at the start, {@code -1} at the
 * end
 * @param children child nodes to insert
 * @return this element, for chaining.
 */
public Element insertChildren(int index, Node... children) {
    Validate.notNull(children, "Children collection to be inserted must not be null.");
    int currentSize = childNodeSize();
    if (index < 0) index += currentSize +1; // roll around
    Validate.isTrue(index >= 0 && index <= currentSize, "Insert position out of bounds.");

    addChildren(index, children);
    return this;
}
 
Example 14
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
void insertOnStackAfter(Element after, Element in) {
    int i = stack.lastIndexOf(after);
    Validate.isTrue(i != -1);
    stack.add(i+1, in);
}
 
Example 15
Source File: ConstrainableInputStream.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public ConstrainableInputStream(InputStream in, int bufferSize, int maxSize) {
    super(in, bufferSize);
    Validate.isTrue(maxSize >= 0);
    remaining = maxSize;
    capped = maxSize != 0;
}
 
Example 16
Source File: QueryParser.java    From jsoup-learning with MIT License 4 votes vote down vote up
private int consumeIndex() {
    String indexS = tq.chompTo(")").trim();
    Validate.isTrue(StringUtil.isNumeric(indexS), "Index must be numeric");
    return Integer.parseInt(indexS);
}
 
Example 17
Source File: QueryParser.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private int consumeIndex() {
    String indexS = tq.chompTo(")").trim();
    Validate.isTrue(StringUtil.isNumeric(indexS), "Index must be numeric");
    return Integer.parseInt(indexS);
}
 
Example 18
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void replaceInQueue(ArrayList<Element> queue, Element out, Element in) {
    int i = queue.lastIndexOf(out);
    Validate.isTrue(i != -1);
    queue.set(i, in);
}
 
Example 19
Source File: XTokenQueue.java    From zongtui-webcrawler with GNU General Public License v2.0 4 votes vote down vote up
public void unConsume(int length) {
    Validate.isTrue(length <= pos, "length " + length + " is larger than consumed chars " + pos);
    pos -= length;
}
 
Example 20
Source File: XTokenQueue.java    From xsoup with MIT License 4 votes vote down vote up
public void unConsume(int length) {
    Validate.isTrue(length <= pos, "length " + length + " is larger than consumed chars " + pos);
    pos -= length;
}