Java Code Examples for org.jsoup.nodes.Element#appendElement()
The following examples show how to use
org.jsoup.nodes.Element#appendElement() .
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: JsoupTest.java From crawler-jsoup-maven with Apache License 2.0 | 6 votes |
public static void main(String[] args) { String d = "<span><div>test</div></span>"; Document doc = Jsoup.parse(d); Element div = doc.select("div").first(); // <div></div> div.html("<p>lorem ipsum</p>"); // <div><p>lorem ipsum</p></div> div.prepend("<p>First</p>"); div.append("<p>Last</p>"); // now: <div><p>First</p><p>lorem ipsum</p><p>Last</p></div> div.appendElement(d); Element span = doc.select("span").first(); // <span>One</span> span.wrap("<li><a href='http://example.com/'></a></li>"); // now: <li><a href="http://example.com"><span>One</span></a></li> System.out.println(doc.html()); String s = Jsoup.clean(doc.html(), "", Whitelist.relaxed(), new OutputSettings().prettyPrint(false)); System.out.println(s); }
Example 2
Source File: Html5.java From baleen with Apache License 2.0 | 6 votes |
@Override protected void writeBody(JCas jCas, Element body) { // Entities Map<Integer, String> insertPositions = getEntityInsertPositions(jCas); Element div = body.appendElement("div"); div.attr("style", "white-space: pre-line"); String text = jCas.getDocumentText(); Integer offset = 0; for (Entry<Integer, String> pos : insertPositions.entrySet()) { String insert = pos.getValue(); text = text.substring(0, pos.getKey() + offset) + insert + text.substring(pos.getKey() + offset); offset += insert.length(); } div.append(text); }
Example 3
Source File: UsageStatisticsExporterTest.java From flow with Apache License 2.0 | 6 votes |
@Test public void should_append_script_element_to_the_body(){ Document document = new Document(""); Element html = document.appendElement("html"); html.appendElement("body"); UsageStatisticsExporter.exportUsageStatisticsToDocument(document); String entries = UsageStatistics.getEntries().map(entry -> { JsonObject json = Json.createObject(); json.put("is", entry.getName()); json.put("version", entry.getVersion()); return json.toString(); }).collect(Collectors.joining(",")); String expected = StringUtil.normaliseWhitespace( "window.Vaadin = window.Vaadin || {};\n" + "window.Vaadin.registrations = window.Vaadin.registrations || [];\n" + "window.Vaadin.registrations.push(" + entries + ");"); Elements bodyInlineElements = document.body().getElementsByTag("script"); assertEquals(StringUtil.normaliseWhitespace(expected), bodyInlineElements.get(0).childNode(0).outerHtml()); }
Example 4
Source File: ComboBoxMultiselect.java From vaadin-combobox-multiselect with Apache License 2.0 | 5 votes |
@Override protected Element writeItem(final Element design, final T item, final DesignContext context) { final Element element = design.appendElement("option"); final String caption = getItemCaptionGenerator().apply(item); if (caption != null) { element.html(DesignFormatter.encodeForTextNode(caption)); } else { element.html(DesignFormatter.encodeForTextNode(item.toString())); } element.attr("item", item.toString()); final Resource icon = getItemIconGenerator().apply(item); if (icon != null) { DesignAttributeHandler.writeAttribute("icon", element.attributes(), icon, null, Resource.class, context); } final String style = getStyleGenerator().apply(item); if (style != null) { element.attr("style", style); } if (isSelected(item)) { element.attr("selected", ""); } return element; }
Example 5
Source File: ComboBoxMultiselect.java From vaadin-combobox-multiselect with Apache License 2.0 | 5 votes |
@Override protected Element writeItem(final Element design, final T item, final DesignContext context) { final Element element = design.appendElement("option"); final String caption = getItemCaptionGenerator().apply(item); if (caption != null) { element.html(DesignFormatter.encodeForTextNode(caption)); } else { element.html(DesignFormatter.encodeForTextNode(item.toString())); } element.attr("item", item.toString()); final Resource icon = getItemIconGenerator().apply(item); if (icon != null) { DesignAttributeHandler.writeAttribute("icon", element.attributes(), icon, null, Resource.class, context); } final String style = getStyleGenerator().apply(item); if (style != null) { element.attr("style", style); } if (isSelected(item)) { element.attr("selected", ""); } return element; }
Example 6
Source File: AbstractHtmlConsumer.java From baleen with Apache License 2.0 | 5 votes |
/** * Append meta tag to an element (typically a head). * * @param el the el * @param name the meta name * @param content the meta content * @return the meta element */ private Element appendMeta(final Element el, final String name, final String content) { if (Strings.isNullOrEmpty(name) || Strings.isNullOrEmpty(content)) { return null; } final Element meta = el.appendElement("meta"); meta.attr("name", name); meta.attr("content", content); return meta; }
Example 7
Source File: AbstractHtmlConsumer.java From baleen with Apache License 2.0 | 4 votes |
@Override protected void doProcess(final JCas jCas) throws AnalysisEngineProcessException { final File f = getFileName(jCas); final DocumentAnnotation da = getDocumentAnnotation(jCas); final Document doc = Jsoup.parse("<!DOCTYPE html>\n<html lang=\"" + da.getLanguage() + "\"></html>"); doc.outputSettings(new Document.OutputSettings().prettyPrint(false)); final Element head = doc.head(); if (!Strings.isNullOrEmpty(css)) { final Element cssLink = head.appendElement("link"); cssLink.attr("rel", "stylesheet"); cssLink.attr("href", css); } final Element charset = head.appendElement("meta"); charset.attr("charset", "utf-8"); appendMeta(head, "document.type", da.getDocType()); appendMeta(head, "document.sourceUri", da.getSourceUri()); appendMeta(head, "externalId", da.getHash()); appendMeta(head, "document.classification", da.getDocumentClassification()); appendMeta( head, "document.caveats", String.join(",", UimaTypesUtils.toArray(da.getDocumentCaveats()))); appendMeta( head, "document.releasability", String.join(",", UimaTypesUtils.toArray(da.getDocumentReleasability()))); String title = null; for (final Metadata md : JCasUtil.select(jCas, Metadata.class)) { appendMeta(head, md.getKey(), md.getValue()); if ("documentTitle".equalsIgnoreCase(md.getKey())) { title = md.getValue(); } } if (!Strings.isNullOrEmpty(title)) { doc.title(title); } final Element body = doc.body(); writeBody(jCas, body); try { FileUtils.writeStringToFile(f, doc.html(), Charset.defaultCharset()); } catch (final IOException e) { throw new AnalysisEngineProcessException(e); } }
Example 8
Source File: BootstrapHandler.java From flow with Apache License 2.0 | 4 votes |
/** * Returns the bootstrap page for the given context. * * @param context * Context to generate bootstrap page for. * @return A document with the corresponding HTML page. */ @Override public Document getBootstrapPage(BootstrapContext context) { DeploymentConfiguration config = context.getSession() .getConfiguration(); Document document = new Document(""); DocumentType doctype = new DocumentType("html", "", ""); document.appendChild(doctype); Element html = document.appendElement("html"); html.attr("lang", context.getUI().getLocale().getLanguage()); Element head = html.appendElement("head"); html.appendElement("body"); List<Element> dependenciesToInlineInBody = setupDocumentHead(head, context); dependenciesToInlineInBody.forEach( dependency -> document.body().appendChild(dependency)); setupDocumentBody(document); document.outputSettings().prettyPrint(false); BootstrapUtils.getInlineTargets(context) .ifPresent(targets -> handleInlineTargets(context, head, document.body(), targets)); BootstrapUtils.getInitialPageSettings(context).ifPresent( initialPageSettings -> handleInitialPageSettings(context, head, initialPageSettings)); if (!config.isProductionMode()) { UsageStatisticsExporter .exportUsageStatisticsToDocument(document); } setupPwa(document, context); if (!config.isProductionMode()) { showWebpackErrors(document); } BootstrapPageResponse response = new BootstrapPageResponse( context.getRequest(), context.getSession(), context.getResponse(), document, context.getUI(), context.getUriResolver()); context.getSession().getService().modifyBootstrapPage(response); return document; }