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: Factory.java From Skype4J with Apache License 2.0 | 6 votes |
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 #2
Source File: Whitelist.java From astor with GNU General Public License v2.0 | 6 votes |
/** 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 |
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: Whitelist.java From astor with GNU General Public License v2.0 | 6 votes |
/** 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 #5
Source File: QueryParser.java From astor with GNU General Public License v2.0 | 6 votes |
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 #6
Source File: Whitelist.java From astor with GNU General Public License v2.0 | 6 votes |
/** 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><a href="..." rel="nofollow"></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 #7
Source File: XmlTreeBuilder.java From astor with GNU General Public License v2.0 | 6 votes |
@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 #8
Source File: Tokeniser.java From jsoup-learning with MIT License | 6 votes |
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 #9
Source File: QueryParser.java From jsoup-learning with MIT License | 5 votes |
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())); }
Example #10
Source File: Element.java From astor with GNU General Public License v2.0 | 5 votes |
/** 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 #11
Source File: HtmlTreeBuilder.java From astor with GNU General Public License v2.0 | 5 votes |
void reconstructFormattingElements() { Element last = lastFormattingElement(); if (last == null || onStack(last)) return; Element entry = last; int size = formattingElements.size(); int pos = size - 1; boolean skip = false; while (true) { if (pos == 0) { // step 4. if none before, skip to 8 skip = true; break; } entry = formattingElements.get(--pos); // step 5. one earlier than entry if (entry == null || onStack(entry)) // step 6 - neither marker nor on stack break; // jump to 8, else continue back to 4 } while(true) { if (!skip) // step 7: on later than entry entry = formattingElements.get(++pos); Validate.notNull(entry); // should not occur, as we break at last element // 8. create new element from element, 9 insert into current node, onto stack skip = false; // can only skip increment from 4. Element newEl = insertStartTag(entry.nodeName()); // todo: avoid fostering here? // newEl.namespace(entry.namespace()); // todo: namespaces newEl.attributes().addAll(entry.attributes()); // 10. replace entry with new entry formattingElements.set(pos, newEl); // 11 if (pos == size-1) // if not last entry in list, jump to 7 break; } }
Example #12
Source File: Node.java From astor with GNU General Public License v2.0 | 5 votes |
protected void addChildren(int index, Node... children) { Validate.noNullElements(children); final List<Node> nodes = ensureChildNodes(); for (Node child : children) { reparentChild(child); } nodes.addAll(index, Arrays.asList(children)); reindexChildren(index); }
Example #13
Source File: Whitelist.java From astor with GNU General Public License v2.0 | 5 votes |
/** 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><a href="#anchor"></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 #14
Source File: CharacterReader.java From astor with GNU General Public License v2.0 | 5 votes |
public CharacterReader(Reader input, int sz) { Validate.notNull(input); Validate.isTrue(input.markSupported()); reader = input; charBuf = new char[sz > maxBufferLen ? maxBufferLen : sz]; bufferUp(); }
Example #15
Source File: Node.java From jsoup-learning with MIT License | 5 votes |
protected void removeChild(Node out) { Validate.isTrue(out.parentNode == this); int index = out.siblingIndex(); childNodes.remove(index); reindexChildren(); out.parentNode = null; }
Example #16
Source File: Element.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Find elements that have an attribute name starting with the supplied prefix. Use {@code data-} to find elements * that have HTML5 datasets. * @param keyPrefix name prefix of the attribute e.g. {@code data-} * @return elements that have attribute names that start with with the prefix, empty if none. */ public Elements getElementsByAttributeStarting(String keyPrefix) { Validate.notEmpty(keyPrefix); keyPrefix = keyPrefix.trim(); return Collector.collect(new Evaluator.AttributeStarting(keyPrefix), this); }
Example #17
Source File: Node.java From jsoup-learning with MIT License | 5 votes |
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 #18
Source File: Attributes.java From astor with GNU General Public License v2.0 | 5 votes |
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 #19
Source File: Element.java From astor with GNU General Public License v2.0 | 5 votes |
/** * 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 #20
Source File: Attributes.java From jsoup-learning with MIT License | 5 votes |
/** Remove an attribute by key. @param key attribute key to remove */ public void remove(String key) { Validate.notEmpty(key); if (attributes == null) return; attributes.remove(key.toLowerCase()); }
Example #21
Source File: Element.java From astor with GNU General Public License v2.0 | 5 votes |
/** * 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 #22
Source File: Node.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Insert the specified node into the DOM after this node (i.e. as a following sibling). * @param node to add after this node * @return this node, for chaining * @see #before(Node) */ public Node after(Node node) { Validate.notNull(node); Validate.notNull(parentNode); parentNode.addChildren(siblingIndex + 1, node); return this; }
Example #23
Source File: Whitelist.java From astor with GNU General Public License v2.0 | 5 votes |
/** 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 #24
Source File: Element.java From astor with GNU General Public License v2.0 | 5 votes |
/** 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 #25
Source File: FormElement.java From astor with GNU General Public License v2.0 | 5 votes |
/** * 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 #26
Source File: Node.java From astor with GNU General Public License v2.0 | 5 votes |
/** * 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 #27
Source File: HtmlTreeBuilder.java From astor with GNU General Public License v2.0 | 5 votes |
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 #28
Source File: Element.java From astor with GNU General Public License v2.0 | 5 votes |
/** * 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 #29
Source File: CharacterReader.java From astor with GNU General Public License v2.0 | 5 votes |
public CharacterReader(Reader input, int sz) { Validate.notNull(input); Validate.isTrue(input.markSupported()); reader = input; charBuf = new char[sz > maxBufferLen ? maxBufferLen : sz]; bufferUp(); }
Example #30
Source File: Tokeniser.java From astor with GNU General Public License v2.0 | 5 votes |
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"); } }