org.jsoup.nodes.DataNode Java Examples

The following examples show how to use org.jsoup.nodes.DataNode. 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: Cleaner.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void head(Node source, int depth) {
    if (source instanceof Element) {
        Element sourceEl = (Element) source;

        if (whitelist.isSafeTag(sourceEl.tagName())) { // safe, clone and copy safe attrs
            ElementMeta meta = createSafeElement(sourceEl);
            Element destChild = meta.el;
            destination.appendChild(destChild);

            numDiscarded += meta.numAttribsDiscarded;
            destination = destChild;
        } else if (source != root) { // not a safe tag, so don't add. don't count root against discarded.
            numDiscarded++;
        }
    } else if (source instanceof TextNode) {
        TextNode sourceText = (TextNode) source;
        TextNode destText = new TextNode(sourceText.getWholeText());
        destination.appendChild(destText);
    } else if (source instanceof DataNode && whitelist.isSafeTag(source.parent().nodeName())) {
      DataNode sourceData = (DataNode) source;
      DataNode destData = new DataNode(sourceData.getWholeData());
      destination.appendChild(destData);
    } else { // else, we don't care about comments, xml proc instructions, etc
        numDiscarded++;
    }
}
 
Example #2
Source File: Cleaner.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void head(Node source, int depth) {
    if (source instanceof Element) {
        Element sourceEl = (Element) source;

        if (whitelist.isSafeTag(sourceEl.tagName())) { // safe, clone and copy safe attrs
            ElementMeta meta = createSafeElement(sourceEl);
            Element destChild = meta.el;
            destination.appendChild(destChild);

            numDiscarded += meta.numAttribsDiscarded;
            destination = destChild;
        } else if (source != root) { // not a safe tag, so don't add. don't count root against discarded.
            numDiscarded++;
        }
    } else if (source instanceof TextNode) {
        TextNode sourceText = (TextNode) source;
        TextNode destText = new TextNode(sourceText.getWholeText());
        destination.appendChild(destText);
    } else if (source instanceof DataNode && whitelist.isSafeTag(source.parent().nodeName())) {
      DataNode sourceData = (DataNode) source;
      DataNode destData = new DataNode(sourceData.getWholeData());
      destination.appendChild(destData);
    } else { // else, we don't care about comments, xml proc instructions, etc
        numDiscarded++;
    }
}
 
Example #3
Source File: JSInterface.java    From NoVIP with Apache License 2.0 5 votes vote down vote up
@JavascriptInterface
public void showSource(String html) {
    Log.d("HTML", html);
    Element body = Jsoup.parse(html).body();
    Elements elements = body.getElementsByAttribute("src");
    for(Element e: elements){
        if(e.tagName().equals("video")){
            for(DataNode node : e.dataNodes()){
                if(node.nodeName().equals("src")){

                    Log.d("JSInterface",node.getWholeData());
                }
            }
        }
        Log.d("JSInterface",e.toString());
    }
    /*Elements elements = body.getElementsByTag("body");
    if(elements != null && !elements.isEmpty()){
        elements = elements.get(0).getElementsByTag("video");
        if(elements != null && !elements.isEmpty()){
            Log.d("JSInterface",elements.get(0).toString());
            elements = elements.get(0).getElementsByAttribute("src");
            if(elements != null && !elements.isEmpty()){
                Log.d("JSInterface",elements.get(0).toString());
            }
        }
    }else {
        Log.d("JSInterface","没有Video");
    }*/
    FileUtils.saveFile("video.html",html);
}
 
Example #4
Source File: IndexHtmlRequestHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
private void addInitialFlow(JsonObject initialJson, Document indexDocument,
                            VaadinSession session) {
    String csrfToken = session.getCsrfToken();
    if (csrfToken != null) {
        initialJson.put(CSRF_TOKEN, csrfToken);
    }

    Element elm = new Element("script");
    elm.attr("initial", "");
    elm.appendChild(new DataNode(
            "window.Vaadin = {TypeScript: " + JsonUtil.stringify(initialJson) + "};"
    ));
    indexDocument.head().insertChildren(0, elm);
}
 
Example #5
Source File: AppShellSettings.java    From flow with Apache License 2.0 5 votes vote down vote up
private static Element createElement(String tag, String content,
        String... attrs) {
    Element elm = new Element(Tag.valueOf(tag), "");
    if (content != null && !content.isEmpty()) {
        elm.appendChild(new DataNode(content));
    }
    for (int i = 0; i < attrs.length - 1; i += 2) {
        elm.attr(attrs[i], attrs[i + 1]);
    }
    return elm;
}
 
Example #6
Source File: BootstrapHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
private 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));
    return wrapper;
}
 
Example #7
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 #8
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);
			}
		}
	}
}