@Override public List<String> selectList(Element element) { Elements elements = element.select("a"); List<String> links = new ArrayList<String>(elements.size()); for (Element element0 : elements) { if (!StringUtil.isBlank(element0.baseUri())) { links.add(element0.attr("abs:href")); } else { links.add(element0.attr("href")); } } return links; }
/** * 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; }
/** * 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; }
/** * Get a list of all URLs in a page for zip files */ public static List<String> getZipUrlsFromPage(String url) { List<String> out = new ArrayList<>(); try { Document doc = Jsoup.connect(url).get(); Elements links = doc.select("a[href]"); for (Element e : links) { String s = e.attr("href"); if (s.endsWith(".zip")) { if (s.startsWith("http")) { //Absolute link out.add(s); } else { //Relative link out.add(e.baseUri() + s); } } } } catch (IOException e) { throw new RuntimeException(e); } return out; }
/** * Create a new element by tag name, and add it as the first child. * * @param tagName the name of the tag (e.g. {@code div}). * @return the new element, to allow you to add content to it, e.g.: * {@code parent.prependElement("h1").attr("id", "header").text("Welcome");} */ public Element prependElement(String tagName) { Element child = new Element(Tag.valueOf(tagName), baseUri()); prependChild(child); return child; }
/** * Create a new element by tag name, and add it as the last child. * * @param tagName the name of the tag (e.g. {@code div}). * @return the new element, to allow you to add content to it, e.g.: * {@code parent.appendElement("h1").attr("id", "header").text("Welcome");} */ public Element appendElement(String tagName) { Element child = new Element(Tag.valueOf(tagName), baseUri()); appendChild(child); return child; }
@Override public boolean matches(Element root, Element element) { if (element instanceof PseudoTextElement) return true; List<TextNode> textNodes = element.textNodes(); for (TextNode textNode : textNodes) { PseudoTextElement pel = new PseudoTextElement( org.jsoup.parser.Tag.valueOf(element.tagName()), element.baseUri(), element.attributes()); textNode.replaceWith(pel); pel.appendChild(textNode); } return false; }
private ElementMeta createSafeElement(Element sourceEl) { String sourceTag = sourceEl.tagName(); Attributes destAttrs = new Attributes(); Element dest = new Element(Tag.valueOf(sourceTag), sourceEl.baseUri(), destAttrs); int numDiscarded = 0; Attributes sourceAttrs = sourceEl.attributes(); for (Attribute sourceAttr : sourceAttrs) { if (whitelist.isSafeAttribute(sourceTag, sourceEl, sourceAttr)) destAttrs.put(sourceAttr); else numDiscarded++; } Attributes enforcedAttrs = whitelist.getEnforcedAttributes(sourceTag); destAttrs.addAll(enforcedAttrs); return new ElementMeta(dest, numDiscarded); }
public void head(Node source, int depth) { if (elementToSkip != null) { return; } if (source instanceof Element) { Element sourceElement = (Element) source; if (isSafeTag(sourceElement)) { String sourceTag = sourceElement.tagName(); Attributes destinationAttributes = sourceElement.attributes().clone(); Element destinationChild = new Element(Tag.valueOf(sourceTag), sourceElement.baseUri(), destinationAttributes); destination.appendChild(destinationChild); destination = destinationChild; } else if (source != root) { elementToSkip = sourceElement; } } else if (source instanceof TextNode) { TextNode sourceText = (TextNode) source; TextNode destinationText = new TextNode(sourceText.getWholeText(), source.baseUri()); destination.appendChild(destinationText); } else if (source instanceof DataNode && isSafeTag(source.parent())) { DataNode sourceData = (DataNode) source; DataNode destinationData = new DataNode(sourceData.getWholeData(), source.baseUri()); destination.appendChild(destinationData); } }
new DataNode(builder.toString(), mainScriptTag.baseUri())); fragmentNodes.add(mainScriptTag);
public String baseUri() { return originElement.baseUri(); }
for (Element link : links) System.out.println(Jsoup.connect(link.baseUri()).get());
@Override public List<String> selectList(Element element) { Elements elements = element.select("a"); List<String> links = new ArrayList<String>(elements.size()); for (Element element0 : elements) { if (!StringUtil.isBlank(element0.baseUri())) { links.add(element0.attr("abs:href")); } else { links.add(element0.attr("href")); } } return links; }
@Override public List<String> selectList(Element element) { Elements elements = element.select("a"); List<String> links = new ArrayList<String>(elements.size()); for (Element element0 : elements) { if (!StringUtil.isBlank(element0.baseUri())) { links.add(element0.attr("abs:href")); } else { links.add(element0.attr("href")); } } return links; }
private static Element createInlineJavaScriptElement( String javaScriptContents) { // defer makes no sense without src: // https://developer.mozilla.org/en/docs/Web/HTML/Element/script Element wrapper = createJavaScriptElement(null, false); wrapper.appendChild( new DataNode(javaScriptContents, wrapper.baseUri())); return wrapper; }
public static TyBox transToTy(Element body) { String base = body.baseUri(); String title = body.getElementsByClass("mk-header").get(0) .getElementsByClass("title").get(0).text(); String next = body.getElementsByClass("mk-pages").get(0) .getElementsByClass("next").get(0).absUrl("href"); Element comic_sort = body.getElementsByClass("comic-sort").get(0); List<BookBean> books = new ArrayList<>(); Elements items = comic_sort.getElementsByClass("comic-item"); if (items != null || items.size() > 0) { for (Element item : items) { books.add(getBook(item)); } } TyBox box = new TyBox(base, title, next, books); return box; }
@Override public Document submit(Iterable<NameValuePair> values) throws ClientException { if (el.tagName().equalsIgnoreCase("form")) { String action = el.attr("abs:action"); if (action.length() == 0) { action = el.baseUri(); } String method = el.attr("method"); if (method.length() == 0 || method.equalsIgnoreCase("get")) { FormValues query = new FormValues(el, values); String url = action + (action.contains("?") ? "?" : "&") + query.toString(); return document.client.enter(url); } if (method.equalsIgnoreCase("post")) { String enctype = el.attr("enctype"); FormValues v = new FormValues(el, values); if (enctype.length() == 0 || enctype.equalsIgnoreCase("application/x-www-form-urlencoded")) { return document.client.post(action, v.toUrlEncodedEntity()); } else if (enctype.equalsIgnoreCase("multipart/form-data")) { return document.client.post(action, v.toMultipartEntity()); } throw new ClientException("Unsupported form enctype: " + enctype); } throw new ClientException("Unsupported form method: " + method); } throw new ClientException("The item is not a form"); }
@Override protected void loadPage() throws Exception { Document jsoup = Jsoup.parse(getInputStream(), getContentEncoding(response), getUri()); setBaseUri(jsoup.head().baseUri()); this.htmlElements = new HtmlElements(this, jsoup); }
private static Element createSafeElement(Element sourceEl) { String sourceTag = sourceEl.tagName(); org.jsoup.nodes.Attributes destAttrs = new org.jsoup.nodes.Attributes(); Element dest = new Element(Tag.valueOf(sourceTag), sourceEl.baseUri(), destAttrs); org.jsoup.nodes.Attributes sourceAttrs = sourceEl.attributes(); for (Attribute sourceAttr : sourceAttrs) { destAttrs.put(sourceAttr); } return dest; }
private static Element createDependencyElement( BootstrapUriResolver resolver, LoadMode loadMode, JsonObject dependency, Dependency.Type type) { boolean inlineElement = loadMode == LoadMode.INLINE; String url = dependency.hasKey(Dependency.KEY_URL) ? resolver.resolveVaadinUri( dependency.getString(Dependency.KEY_URL)) : null; final Element dependencyElement; switch (type) { case STYLESHEET: dependencyElement = createStylesheetElement(url); break; case JAVASCRIPT: dependencyElement = createJavaScriptElement(url, !inlineElement); break; case HTML_IMPORT: dependencyElement = createHtmlImportElement(url); break; default: throw new IllegalStateException( "Unsupported dependency type: " + type); } if (inlineElement) { dependencyElement.appendChild( new DataNode(dependency.getString(Dependency.KEY_CONTENTS), dependencyElement.baseUri())); } return dependencyElement; }