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

The following examples show how to use org.jsoup.nodes.Element#nodeName() . 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: ComMailingContentServiceImpl.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
private void generateTextContent(StringBuilder sb, List<Node> nodes) {
    for (Node node : nodes) {
        if (node instanceof Element) {
            Element element = (Element) node;

            switch (element.nodeName()) {
                case "a":
                    sb.append(getTextLink(element));
                    break;

                case "br":
                    sb.append('\n');
                    break;

                default:
                    generateTextContent(sb, element.childNodes());
                    break;
            }
        } else if (node instanceof TextNode) {
            sb.append(((TextNode) node).getWholeText());
        }
    }
}
 
Example 2
Source File: CSSJsoupPhlocContentAdapterImpl.java    From Asqatasun with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Retrieve css content and adapt it for each inline resource
 */
private void adaptInlineCSS() {
    Set<Long> relatedCssIdSet = new HashSet<>();

    for (Element el : inlineCssElements) {
        String attributeValue = el.attr("style");
        if (StringUtils.isNotBlank(attributeValue)) {
            Resource cssResource = new CSSResourceImpl(
                    el.nodeName()+"{"+attributeValue +"}", 
                    0, 
                    new InlineRsrc());
            StylesheetContent cssContent =
                    getStylesheetFromInlineResource(cssResource.getResource());
            adaptContent(cssContent, cssResource, getCurrentResourcePath(el.baseUri()), null);
            relatedCssIdSet.add(getContentDataService().saveOrUpdate(cssContent).getId());
        }
    }
    getContentDataService().saveContentRelationShip(getSSP(), relatedCssIdSet);
}
 
Example 3
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private boolean inSpecificScope(String[] targetNames, String[] baseTypes, String[] extraTypes) {
    for (int pos = stack.size() -1; pos >= 0; pos--) {
        Element el = stack.get(pos);
        String elName = el.nodeName();
        if (StringUtil.in(elName, targetNames))
            return true;
        if (StringUtil.in(elName, baseTypes))
            return false;
        if (extraTypes != null && StringUtil.in(elName, extraTypes))
            return false;
    }
    Validate.fail("Should not be reachable");
    return false;
}
 
Example 4
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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 5
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private boolean inSpecificScope(String[] targetNames, String[] baseTypes, String[] extraTypes) {
    for (int pos = stack.size() -1; pos >= 0; pos--) {
        Element el = stack.get(pos);
        String elName = el.nodeName();
        if (StringUtil.in(elName, targetNames))
            return true;
        if (StringUtil.in(elName, baseTypes))
            return false;
        if (extraTypes != null && StringUtil.in(elName, extraTypes))
            return false;
    }
    Validate.fail("Should not be reachable");
    return false;
}
 
Example 6
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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 7
Source File: M2DocHTMLParser.java    From M2Doc with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Starts the given {@link Element}.
 * 
 * @param parent
 *            the parent {@link MList}
 * @param context
 *            the current {@link Context}
 * @param element
 *            the {@link Element}
 * @return the new parent {@link MList} for {@link Element#children() children}
 */
private MList startElement(MList parent, Context context, Element element) {
    final MList res;

    final String nodeName = element.nodeName();
    boolean isNumbering = false;
    if ("p".equals(nodeName)) {
        res = createMParagraph(parent, element, null, null);
    } else if ("strong".equals(nodeName) || "b".equals(nodeName)) {
        setModifiers(context, MStyle.FONT_BOLD);
        res = parent;
    } else if ("em".equals(nodeName) || "i".equals(nodeName)) {
        setModifiers(context, MStyle.FONT_ITALIC);
        res = parent;
    } else if ("s".equals(nodeName) || "strike".equals(nodeName)) {
        setModifiers(context, MStyle.FONT_STRIKE_THROUGH);
        res = parent;
    } else if ("u".equals(nodeName)) {
        setModifiers(context, MStyle.FONT_UNDERLINE);
        res = parent;
    } else if ("font".equals(nodeName)) {
        if (element.hasAttr("color")) {
            context.style.setForegroundColor(htmlToColor(element.attr("color").toLowerCase()));
        }
        if (element.hasAttr("face")) {
            // TODO double check this
            context.style.setFontName(element.attr("face"));
        }
        if (element.hasAttr("size")) {
            context.style.setFontSize(fontSizeToPoint(Integer.valueOf(element.attr("size"))));
        }
        res = parent;
    } else if ("a".equals(nodeName)) {
        context.linkTargetURI = URI.createURI(element.attr("href")).resolve(context.baseURI);
        res = parent;
    } else if ("br".equals(nodeName)) {
        parent.add(MPagination.ligneBreak);
        res = parent;
    } else if ("li".equals(nodeName)) {
        res = createMParagraph(parent, element, context.numberingID.longValue(), context.numberingLevel - 1);
    } else if ("ol".equals(nodeName)) {
        setOrderedListNumbering(context, element);
        isNumbering = true;
        res = parent;
    } else if ("ul".equals(nodeName)) {
        setUnorderedListNumbering(context, element);
        isNumbering = true;
        res = parent;
    } else if ("img".equals(nodeName)) {
        final URI imageURI = URI.createURI(element.attr("src")).resolve(context.baseURI);
        final MImage mImage = new MImageImpl(uriConverter, imageURI);
        // TODO set height and width pixel to ??? conversion ?
        parent.add(mImage);
        res = parent;
    } else if ("h1".equals(nodeName)) {
        res = createMParagraph(parent, element, null, null);
        context.style.setFontSize(H1_FONT_SIZE);
        context.style.setModifiers(context.style.getFontModifiers() | MStyle.FONT_BOLD);
    } else if ("h2".equals(nodeName)) {
        res = createMParagraph(parent, element, null, null);
        context.style.setFontSize(H2_FONT_SIZE);
        context.style.setModifiers(context.style.getFontModifiers() | MStyle.FONT_BOLD);
    } else if ("h3".equals(nodeName)) {
        res = createMParagraph(parent, element, null, null);
        context.style.setFontSize(H3_FONT_SIZE);
        context.style.setModifiers(context.style.getFontModifiers() | MStyle.FONT_BOLD);
    } else if ("h4".equals(nodeName)) {
        res = createMParagraph(parent, element, null, null);
        context.style.setFontSize(H4_FONT_SIZE);
        context.style.setModifiers(context.style.getFontModifiers() | MStyle.FONT_BOLD);
    } else if ("h5".equals(nodeName)) {
        res = createMParagraph(parent, element, null, null);
        context.style.setFontSize(H5_FONT_SIZE);
        context.style.setModifiers(context.style.getFontModifiers() | MStyle.FONT_BOLD);
    } else if ("h6".equals(nodeName)) {
        res = createMParagraph(parent, element, null, null);
        context.style.setFontSize(H6_FONT_SIZE);
        context.style.setModifiers(context.style.getFontModifiers() | MStyle.FONT_BOLD);
    } else {
        res = parent;
    }

    if (!isNumbering) {
        context.numbering = null;
        context.numberingLevel = 0;
    }

    return res;
}
 
Example 8
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
void resetInsertionMode() {
    boolean last = false;
    for (int pos = stack.size() -1; pos >= 0; pos--) {
        Element node = stack.get(pos);
        if (pos == 0) {
            last = true;
            node = contextElement;
        }
        String name = node.nodeName();
        if ("select".equals(name)) {
            transition(HtmlTreeBuilderState.InSelect);
            break; // frag
        } else if (("td".equals(name) || "th".equals(name) && !last)) {
            transition(HtmlTreeBuilderState.InCell);
            break;
        } else if ("tr".equals(name)) {
            transition(HtmlTreeBuilderState.InRow);
            break;
        } else if ("tbody".equals(name) || "thead".equals(name) || "tfoot".equals(name)) {
            transition(HtmlTreeBuilderState.InTableBody);
            break;
        } else if ("caption".equals(name)) {
            transition(HtmlTreeBuilderState.InCaption);
            break;
        } else if ("colgroup".equals(name)) {
            transition(HtmlTreeBuilderState.InColumnGroup);
            break; // frag
        } else if ("table".equals(name)) {
            transition(HtmlTreeBuilderState.InTable);
            break;
        } else if ("head".equals(name)) {
            transition(HtmlTreeBuilderState.InBody);
            break; // frag
        } else if ("body".equals(name)) {
            transition(HtmlTreeBuilderState.InBody);
            break;
        } else if ("frameset".equals(name)) {
            transition(HtmlTreeBuilderState.InFrameset);
            break; // frag
        } else if ("html".equals(name)) {
            transition(HtmlTreeBuilderState.BeforeHead);
            break; // frag
        } else if (last) {
            transition(HtmlTreeBuilderState.InBody);
            break; // frag
        }
    }
}
 
Example 9
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
boolean isSpecial(Element el) {
    // todo: mathml's mi, mo, mn
    // todo: svg's foreigObject, desc, title
    String name = el.nodeName();
    return StringUtil.in(name, TagSearchSpecial);
}
 
Example 10
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
void resetInsertionMode() {
    boolean last = false;
    for (int pos = stack.size() -1; pos >= 0; pos--) {
        Element node = stack.get(pos);
        if (pos == 0) {
            last = true;
            node = contextElement;
        }
        String name = node.nodeName();
        if ("select".equals(name)) {
            transition(HtmlTreeBuilderState.InSelect);
            break; // frag
        } else if (("td".equals(name) || "th".equals(name) && !last)) {
            transition(HtmlTreeBuilderState.InCell);
            break;
        } else if ("tr".equals(name)) {
            transition(HtmlTreeBuilderState.InRow);
            break;
        } else if ("tbody".equals(name) || "thead".equals(name) || "tfoot".equals(name)) {
            transition(HtmlTreeBuilderState.InTableBody);
            break;
        } else if ("caption".equals(name)) {
            transition(HtmlTreeBuilderState.InCaption);
            break;
        } else if ("colgroup".equals(name)) {
            transition(HtmlTreeBuilderState.InColumnGroup);
            break; // frag
        } else if ("table".equals(name)) {
            transition(HtmlTreeBuilderState.InTable);
            break;
        } else if ("head".equals(name)) {
            transition(HtmlTreeBuilderState.InBody);
            break; // frag
        } else if ("body".equals(name)) {
            transition(HtmlTreeBuilderState.InBody);
            break;
        } else if ("frameset".equals(name)) {
            transition(HtmlTreeBuilderState.InFrameset);
            break; // frag
        } else if ("html".equals(name)) {
            transition(HtmlTreeBuilderState.BeforeHead);
            break; // frag
        } else if (last) {
            transition(HtmlTreeBuilderState.InBody);
            break; // frag
        }
    }
}
 
Example 11
Source File: HtmlTreeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
boolean isSpecial(Element el) {
    // todo: mathml's mi, mo, mn
    // todo: svg's foreigObject, desc, title
    String name = el.nodeName();
    return StringUtil.in(name, TagSearchSpecial);
}