Java Code Examples for org.jsoup.nodes.Element#appendChild()

The following examples show how to use org.jsoup.nodes.Element#appendChild() . 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: CifnewsPageHandler.java    From cetty with Apache License 2.0 6 votes vote down vote up
@Override
public Element appendBody(Elements tempBody) {
    final Element articleBody = new Element(Tag.valueOf("div"), "");
    String blockquote = tempBody.select("div.fetch-read>div.summary").text();
    buildBlockquote(blockquote, articleBody);
    Elements inner = tempBody.select("div.article-inner>*");
    for (Element pEl : inner) {
        if (pEl.select("div.fetch-present").size() != 0) {
            continue;
        }
        Element imgEl = pEl.select("p>img").first();
        if (imgEl != null) {
            Element figure = buildFigure(imgEl);
            if (imgEl.nextElementSibling() != null && imgEl.nextElementSibling().tagName().equals("p")) {
                Element figcaption = buildFigcaption(imgEl.nextElementSibling().text());
                figure.appendChild(figcaption);
                articleBody.appendChild(figure);
                continue;
            }
            articleBody.appendChild(figure);
            continue;
        }
        articleBody.appendChild(pEl);
    }
    return articleBody;
}
 
Example 2
Source File: BootstrapHandler.java    From flow with Apache License 2.0 6 votes vote down vote up
private void setupFrameworkLibraries(Element head,
        JsonObject initialUIDL, BootstrapContext context) {

    VaadinService service = context.getSession().getService();
    DeploymentConfiguration conf = service.getDeploymentConfiguration();

    conf.getPolyfills().forEach(
            polyfill -> head.appendChild(createJavaScriptElement(
                    "./" + VAADIN_MAPPING + polyfill, false)));
    try {
        appendNpmBundle(head, service, context);
    } catch (IOException e) {
        throw new BootstrapException(
                "Unable to read webpack stats file.", e);
    }

    if (context.getPushMode().isEnabled()) {
        head.appendChild(
                createJavaScriptElement(getPushScript(context)));
    }

    head.appendChild(getBootstrapScript(initialUIDL, context));
    head.appendChild(
            createJavaScriptElement(getClientEngineUrl(context)));
}
 
Example 3
Source File: BootstrapHandler.java    From flow with Apache License 2.0 6 votes vote down vote up
private List<Element> inlineDependenciesInHead(Element head,
        BootstrapUriResolver uriResolver, LoadMode loadMode,
        JsonArray dependencies) {
    List<Element> dependenciesToInlineInBody = new ArrayList<>();

    for (int i = 0; i < dependencies.length(); i++) {
        JsonObject dependencyJson = dependencies.getObject(i);
        Dependency.Type dependencyType = Dependency.Type
                .valueOf(dependencyJson.getString(Dependency.KEY_TYPE));
        Element dependencyElement = createDependencyElement(uriResolver,
                loadMode, dependencyJson, dependencyType);

        head.appendChild(dependencyElement);
    }
    return dependenciesToInlineInBody;
}
 
Example 4
Source File: GuxiaobeiPageHandler.java    From cetty with Apache License 2.0 6 votes vote down vote up
@Override
public Element appendBody(Elements tempBody) {
    final Element articleBody = new Element(Tag.valueOf("div"), "");
    for (final Element pEl : tempBody) {
        if (pEl.select("div.open-message,div.jp-relatedposts,div.article-social").size() != 0) {
            continue;
        }
        if (pEl.tagName().equals("p")) {
            Element imgEl = pEl.select("img").first();
            if (imgEl != null) {
                String src = imgEl.attr("src");
                if (src.contains("data:image")) {
                    src = imgEl.attr("data-src");
                } else if (!src.contains("www.guxiaobei.com")) {
                    src = "http://www.guxiaobei.com" + src;
                }
                imgEl.attr("src", src);

                articleBody.appendChild(buildFigure(imgEl));
                continue;
            }
        }
        articleBody.appendChild(pEl);
    }
    return articleBody;
}
 
Example 5
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
void insertInFosterParent(Node in) {
    Element fosterParent;
    Element lastTable = getFromStack("table");
    boolean isLastTableParent = false;
    if (lastTable != null) {
        if (lastTable.parent() != null) {
            fosterParent = lastTable.parent();
            isLastTableParent = true;
        } else
            fosterParent = aboveOnStack(lastTable);
    } else { // no table == frag
        fosterParent = stack.get(0);
    }

    if (isLastTableParent) {
        Validate.notNull(lastTable); // last table cannot be null by this point.
        lastTable.before(in);
    }
    else
        fosterParent.appendChild(in);
}
 
Example 6
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
void insertInFosterParent(Node in) {
    Element fosterParent;
    Element lastTable = getFromStack("table");
    boolean isLastTableParent = false;
    if (lastTable != null) {
        if (lastTable.parent() != null) {
            fosterParent = lastTable.parent();
            isLastTableParent = true;
        } else
            fosterParent = aboveOnStack(lastTable);
    } else { // no table == frag
        fosterParent = stack.get(0);
    }

    if (isLastTableParent) {
        Validate.notNull(lastTable); // last table cannot be null by this point.
        lastTable.before(in);
    }
    else
        fosterParent.appendChild(in);
}
 
Example 7
Source File: SimpleTextElementBuilderTest.java    From Asqatasun with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Test of buildTextFromElement method, of class SimpleTextElementBuilder.
 */
public void testBuildTextFromElementWithChildren() {
    LOGGER.debug("buildTextFromElementWithChildren");
    Element element = new Element(Tag.valueOf("div"), "");
    element.appendText("   text1   ");
    
    Element childElement = new Element(Tag.valueOf("div"), "");
    childElement.text("   child element text   ");

    Element childElement2 = new Element(Tag.valueOf("div"), "");
    childElement2.text("   child element text second level  ");
    childElement.appendChild(childElement2);
    
    element.appendChild(childElement);
    element.appendText("   text2   ");

    SimpleTextElementBuilder instance = new SimpleTextElementBuilder();
    String expResult = "text1 child element text child element text second level text2";
    String result = instance.buildTextFromElement(element);
    assertEquals(expResult, result);
}
 
Example 8
Source File: BootstrapHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
private 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 JS_MODULE:
        dependencyElement = createJavaScriptElement(url, false,
                "module");
        break;
    default:
        throw new IllegalStateException(
                "Unsupported dependency type: " + type);
    }

    if (inlineElement) {
        dependencyElement.appendChild(new DataNode(
                dependency.getString(Dependency.KEY_CONTENTS)));
    }

    return dependencyElement;
}
 
Example 9
Source File: WebComponentBootstrapHandlerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void writeBootstrapPage_skipMetaAndStyleHeaderElements()
        throws IOException {
    WebComponentBootstrapHandler handler = new WebComponentBootstrapHandler();

    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    Element head = new Document("").normalise().head();
    Element meta = head.ownerDocument().createElement("meta");
    head.appendChild(meta);
    meta.attr("http-equiv", "Content-Type");

    Element style = head.ownerDocument().createElement("style");
    head.appendChild(style);
    style.attr("type", "text/css");
    style.text("body {height:100vh;width:100vw;margin:0;}");

    Element script = head.ownerDocument().createElement("script");
    head.appendChild(script);
    script.text("var i=1;");

    VaadinResponse response = getMockResponse(stream);
    handler.writeBootstrapPage("", response, head, "");

    String resultingScript = stream.toString(StandardCharsets.UTF_8.name());

    Assert.assertThat(resultingScript,
            CoreMatchers.containsString("var i=1;"));
    Assert.assertThat(resultingScript, CoreMatchers.not(CoreMatchers
            .containsString("body {height:100vh;width:100vw;margin:0;}")));
    Assert.assertThat(resultingScript,
            CoreMatchers.not(CoreMatchers.containsString("http-equiv")));
}
 
Example 10
Source File: Parser.java    From jsoup-learning with MIT License 5 votes vote down vote up
/**
 * Parse a fragment of HTML into the {@code body} of a Document.
 *
 * @param bodyHtml fragment of HTML
 * @param baseUri base URI of document (i.e. original fetch location), for resolving relative URLs.
 *
 * @return Document, with empty head, and HTML parsed into body
 */
public static Document parseBodyFragment(String bodyHtml, String baseUri) {
    Document doc = Document.createShell(baseUri);
    Element body = doc.body();
    List<Node> nodeList = parseFragment(bodyHtml, body, baseUri);
    Node[] nodes = nodeList.toArray(new Node[nodeList.size()]); // the node list gets modified when re-parented
    for (Node node : nodes) {
        body.appendChild(node);
    }
    return doc;
}
 
Example 11
Source File: Parser.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parse a fragment of HTML into the {@code body} of a Document.
 *
 * @param bodyHtml fragment of HTML
 * @param baseUri base URI of document (i.e. original fetch location), for resolving relative URLs.
 *
 * @return Document, with empty head, and HTML parsed into body
 */
public static Document parseBodyFragment(String bodyHtml, String baseUri) {
    Document doc = Document.createShell(baseUri);
    Element body = doc.body();
    List<Node> nodeList = parseFragment(bodyHtml, body, baseUri);
    Node[] nodes = nodeList.toArray(new Node[nodeList.size()]); // the node list gets modified when re-parented
    for (int i = nodes.length - 1; i > 0; i--) {
        nodes[i].remove();
    }
    for (Node node : nodes) {
        body.appendChild(node);
    }
    return doc;
}
 
Example 12
Source File: HtmlWriter.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public void write(Table table, HtmlWriteOptions options) throws IOException {
  ElementCreator elements = options.elementCreator();
  Element html = elements.create("table");
  html.appendChild(header(table.columns(), elements));

  Element tbody = elements.create("tbody");
  html.appendChild(tbody);
  for (int row = 0; row < table.rowCount(); row++) {
    tbody.appendChild(row(row, table, elements, options));
  }

  try (Writer writer = options.destination().createWriter()) {
    writer.write(html.toString());
  }
}
 
Example 13
Source File: NewLineToNewParagraph.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Collect tags which are on the same line (unbroken by BRs)
 *
 * @param document the document
 * @param e the e
 * @return the list
 */
private List<Element> collectRuns(Document document, Element e) {
  List<Element> runs = new LinkedList<>();
  Element run = null;
  for (Node c : e.childNodesCopy()) {

    if (c instanceof Element && ("br".equalsIgnoreCase(((Element) c).tagName()))) {
      // If we hit a br then add the old run and start a new one
      if (run != null) {
        runs.add(run);
        run = null;
      }
    } else {
      // If not a br then add this node to the other
      if (run == null) {
        run = document.createElement("p");
      }
      run.appendChild(c);
    }
  }

  // Add the last run
  if (run != null) {
    runs.add(run);
  }

  return runs;
}
 
Example 14
Source File: Parser.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parse a fragment of HTML into the {@code body} of a Document.
 *
 * @param bodyHtml fragment of HTML
 * @param baseUri base URI of document (i.e. original fetch location), for resolving relative URLs.
 *
 * @return Document, with empty head, and HTML parsed into body
 */
public static Document parseBodyFragment(String bodyHtml, String baseUri) {
    Document doc = Document.createShell(baseUri);
    Element body = doc.body();
    List<Node> nodeList = parseFragment(bodyHtml, body, baseUri);
    Node[] nodes = nodeList.toArray(new Node[nodeList.size()]); // the node list gets modified when re-parented
    for (int i = nodes.length - 1; i > 0; i--) {
        nodes[i].remove();
    }
    for (Node node : nodes) {
        body.appendChild(node);
    }
    return doc;
}
 
Example 15
Source File: JsoupCssInliner.java    From ogham with Apache License 2.0 5 votes vote down vote up
/**
 * Replace link tags with style tags in order to keep the same inclusion
 * order
 *
 * @param doc
 *            the html document
 * @param cssContents
 *            the list of external css files with their content
 */
private static void internStyles(Document doc, List<ExternalCss> cssContents) {
	Elements els = doc.select(CSS_LINKS_SELECTOR);
	for (Element e : els) {
		if (isInlineModeAllowed(e, InlineModes.STYLE_ATTR)) {
			String path = e.attr(HREF_ATTR);
			ExternalCss css = getCss(cssContents, path);
			if (css != null) {
				Element style = new Element(Tag.valueOf(STYLE_TAG), "");
				style.appendChild(new DataNode(getCssContent(css)));
				e.replaceWith(style);
			}
		}
	}
}
 
Example 16
Source File: JsoupParserIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void examplesModifying() {
    Element firstArticle = doc.select("article")
        .first();
    Element timeElement = firstArticle.select("time")
        .first();
    Element sectionDiv = firstArticle.select("section div")
        .first();

    String dateTimeOfFirstArticle = timeElement.attr("datetime");
    timeElement.attr("datetime", "2016-12-16 15:19:54.3");
    sectionDiv.text("foo bar");
    firstArticle.select("h2")
        .html("<div><span></span></div>");

    Element link = new Element(Tag.valueOf("a"), "").text("Checkout this amazing website!")
        .attr("href", "http://baeldung.com")
        .attr("target", "_blank");
    firstArticle.appendChild(link);

    doc.select("li.navbar-link")
        .remove();
    firstArticle.select("img")
        .remove();

    assertTrue(doc.html()
        .contains("http://baeldung.com"));
}
 
Example 17
Source File: BasePageHandler.java    From cetty with Apache License 2.0 5 votes vote down vote up
/**
 * 生成我们自己的文章内容体
 *
 * @param tempBody
 */
public Element appendBody(Elements tempBody) {
    final Element articleBody = new Element(Tag.valueOf("div"), "");
    for (final Element pEl : tempBody) {
        Element imgEl = pEl.select("img").first();
        if (imgEl != null) {
            articleBody.appendChild(buildFigure(imgEl));
            continue;
        }
        articleBody.appendChild(pEl);
    }
    return articleBody;
}
 
Example 18
Source File: CubaBootstrapListener.java    From cuba with Apache License 2.0 4 votes vote down vote up
protected void includeMetaViewport(String content, BootstrapPageResponse response, Element head) {
    Element meta = response.getDocument().createElement("meta");
    meta.attr("name", "viewport");
    meta.attr("content", content);
    head.appendChild(meta);
}
 
Example 19
Source File: HtmlHelper.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
static void convertLists(Document document) {
    for (Element span : document.select("span")) {
        // Skip signature and referenced message
        boolean body = true;
        Element parent = span.parent();
        while (parent != null) {
            if ("div".equals(parent.tagName()) &&
                    !TextUtils.isEmpty(parent.attr("fairemail"))) {
                body = false;
                break;
            }
            parent = parent.parent();
        }
        if (!body)
            continue;

        Element list = null;
        for (int i = 0; i < span.childNodeSize(); i++) {
            boolean item = false;
            Node node = span.childNode(i);
            if (node instanceof TextNode) {
                String text = ((TextNode) node).text().trim();
                Node next = node.nextSibling();
                if ((text.startsWith("* ") || text.startsWith("- ")) &&
                        (next == null || "br".equals(next.nodeName()))) {
                    item = true;
                    String type = (text.startsWith("* ") ? "ul" : "ol");

                    Element li = document.createElement("li");
                    li.text(text.substring(2));

                    if (list == null || !list.tagName().equals(type)) {
                        Node before = node.previousSibling();
                        if (before != null && "br".equals(before.nodeName())) {
                            before.remove();
                            i--;
                        }

                        list = document.createElement(type);
                        list.appendChild(li);
                        node.replaceWith(list);

                    } else {
                        list.appendChild(li);
                        node.remove();
                        i--;
                    }

                    if (next != null)
                        next.remove();
                }
            } else {
                if (list != null && "br".equals(node.nodeName())) {
                    node.remove();
                    i--;
                }
            }
            if (!item)
                list = null;
        }
    }
}
 
Example 20
Source File: BasePageHandler.java    From cetty with Apache License 2.0 2 votes vote down vote up
/**
 * 解析段落块格式
 *
 * @param blockquote
 * @param articleBody
 */
protected void buildBlockquote(String blockquote, Element articleBody) {
    final Element blockquoteEl = new Element(Tag.valueOf("blockquote"), "");
    blockquoteEl.append(blockquote);
    articleBody.appendChild(blockquoteEl);
}