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

The following examples show how to use org.jsoup.nodes.Element#parent() . 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: SelectorFetcher.java    From stevia with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static String reversePath(Element e) {
	String loc = e.tagName();
	int div = 0;
	while (e.parent()!=null) {
		String tag = e.parent().tagName().toLowerCase();
		if (tag.compareTo("body")==0) {
			break;
		} else {
			loc = tag + "/" + loc;
		}
		
		if (tag.contentEquals("div")) {
			div++;
			if (div == 2) {
				break;
			}
		} else if (tag.contentEquals("form")) {
			break;
		}
		
		e = e.parent();
	}
	return loc;
}
 
Example 2
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 3
Source File: ContentExtractor.java    From WebCollector with GNU General Public License v3.0 5 votes vote down vote up
protected String getDate(Element contentElement) throws Exception {
    String regex = "([1-2][0-9]{3})[^0-9]{1,5}?([0-1]?[0-9])[^0-9]{1,5}?([0-9]{1,2})";
    Pattern pattern = Pattern.compile(regex);
    Element current = contentElement;
    for (int i = 0; i < 2; i++) {
        if (current != null && current != doc.body()) {
            Element parent = current.parent();
            if (parent != null) {
                current = parent;
            }
        }
    }
    for (int i = 0; i < 6; i++) {
        if (current == null) {
            break;
        }
        String currentHtml = current.outerHtml();
        Matcher matcher = pattern.matcher(currentHtml);
        if (matcher.find()) {
            return matcher.group(1) + "-" + matcher.group(2) + "-" + matcher.group(3);
        }
        if (current != doc.body()) {
            current = current.parent();
        }
    }
    throw new Exception("date not found");
}
 
Example 4
Source File: ContentExtractor.java    From WebCollector with GNU General Public License v3.0 5 votes vote down vote up
protected String getTime(Element contentElement) throws Exception {
    String regex = "([1-2][0-9]{3})[^0-9]{1,5}?([0-1]?[0-9])[^0-9]{1,5}?([0-9]{1,2})[^0-9]{1,5}?([0-2]?[1-9])[^0-9]{1,5}?([0-9]{1,2})[^0-9]{1,5}?([0-9]{1,2})";
    Pattern pattern = Pattern.compile(regex);
    Element current = contentElement;
    for (int i = 0; i < 2; i++) {
        if (current != null && current != doc.body()) {
            Element parent = current.parent();
            if (parent != null) {
                current = parent;
            }
        }
    }
    for (int i = 0; i < 6; i++) {
        if (current == null) {
            break;
        }
        String currentHtml = current.outerHtml();
        Matcher matcher = pattern.matcher(currentHtml);
        if (matcher.find()) {
            return matcher.group(1) + "-" + matcher.group(2) + "-" + matcher.group(3) + " " + matcher.group(4) + ":" + matcher.group(5) + ":" + matcher.group(6);
        }
        if (current != doc.body()) {
            current = current.parent();
        }
    }

    try {
        return getDate(contentElement);
    } catch (Exception ex) {
        throw new Exception("time not found");
    }

}
 
Example 5
Source File: Evaluator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean matches(Element root, Element element) {
	final Element p = element.parent();
	if (p == null || (p instanceof Document)) return false;
	
	final int pos = calculatePosition(root, element);
	if (a == 0) return pos == b;
	
	return (pos-b)*a >= 0 && (pos-b)%a==0;
}
 
Example 6
Source File: StructuralEvaluator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public boolean matches(Element root, Element element) {
    if (root == element)
        return false;

    Element parent = element.parent();
    return parent != null && evaluator.matches(root, parent);
}
 
Example 7
Source File: KinoxParser.java    From KinoCast with MIT License 5 votes vote down vote up
private List<ViewModel> parseList(Document doc){
    List<ViewModel> list = new ArrayList<>();
    Elements files = doc.select("div.MiniEntry");

    for(Element element : files){
        element = element.parent();
        try {
            ViewModel model = new ViewModel();
            String url = element.select("h1").parents().attr("href");
            model.setSlug(url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")));
            model.setTitle(element.select("h1").text());
            model.setSummary(element.select("div.Descriptor").text());

            String ln = element.select("div.Genre > div.floatleft").eq(0).select("img").attr("src");
            ln = ln.substring(ln.lastIndexOf("/") + 1);
            ln = ln.substring(0, ln.indexOf("."));
            int lnId = Integer.valueOf(ln);
            model.setLanguageResId(languageResMap.get(lnId));
            String language = languageKeyMap.get(lnId);

            String genre = element.select("div.Genre > div.floatleft").eq(1).text();
            genre = genre.substring(genre.indexOf(":") + 1).trim();
            if (genre.contains(",")) genre = genre.substring(0, genre.indexOf(","));
            model.setGenre(genre);

            String rating = element.select("div.Genre > div.floatright").text();
            rating = rating.substring(rating.indexOf(":") + 1, rating.indexOf("/") - 1);
            model.setRating(Float.valueOf(rating.trim()));

            model.setImage(getPageLink(model) + "#language=" + language);

            list.add(model);
        }catch (Exception e){
            Log.e("Kinox", "Error parsing " + element.html(), e);
        }
    }
    return list;
}
 
Example 8
Source File: StructuralEvaluator.java    From jsoup-learning with MIT License 5 votes vote down vote up
public boolean matches(Element root, Element element) {
    if (root == element)
        return false;

    Element parent = element.parent();
    while (parent != root) {
        if (evaluator.matches(root, parent))
            return true;
        parent = parent.parent();
    }
    return false;
}
 
Example 9
Source File: StructuralEvaluator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public boolean matches(Element root, Element element) {
    if (root == element)
        return false;

    Element parent = element.parent();
    return parent != null && evaluator.matches(root, parent);
}
 
Example 10
Source File: Evaluator.java    From jsoup-learning with MIT License 5 votes vote down vote up
@Override
public boolean matches(Element root, Element element) {
	final Element p = element.parent();
	if (p == null || (p instanceof Document)) return false;
	
	final int pos = calculatePosition(root, element);
	if (a == 0) return pos == b;
	
	return (pos-b)*a >= 0 && (pos-b)%a==0;
}
 
Example 11
Source File: Ch5Coz3.java    From CrawlerPack with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args){
    Document original = CrawlerPack.start().getFromXml(url);

    // 檢視 xml 內容
    //System.out.println(original.toString());

    // 要求要評價在4以上的店家
    for(Element elem : original.select("rating:matchesOwn(^4)") ){
        Element parentRoot = elem.parent();
        System.out.println( parentRoot.select("name").text() + "(" + elem.text() + ")");
    }
}
 
Example 12
Source File: StructuralEvaluator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public boolean matches(Element root, Element element) {
    if (root == element)
        return false;

    Element parent = element.parent();
    while (true) {
        if (evaluator.matches(root, parent))
            return true;
        if (parent == root)
            break;
        parent = parent.parent();
    }
    return false;
}
 
Example 13
Source File: Evaluator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean matches(Element root, Element element) {
	final Element p = element.parent();
	return p != null && !(p instanceof Document) && element.elementSiblingIndex() == 0;
}
 
Example 14
Source File: Evaluator.java    From jsoup-learning with MIT License 4 votes vote down vote up
@Override
public boolean matches(Element root, Element element) {
	final Element p = element.parent();
	return p != null && !(p instanceof Document) && element.elementSiblingIndex() == p.children().size()-1;
}
 
Example 15
Source File: StructuralEvaluator.java    From xsoup with MIT License 4 votes vote down vote up
public boolean matches(Element root, Element element) {
    Element parent = element.parent();
    return parent != null && evaluator.matches(root, parent);
}
 
Example 16
Source File: NavBuilder.java    From rebuild with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 渲染导航菜單
 *
 * @param item
 * @param activeNav
 * @return
 */
public String renderNavItem(JSONObject item, String activeNav) {
    final boolean isUrlType = "URL".equals(item.getString("type"));
    String navName = item.getString("value");
    String navUrl = item.getString("value");

    boolean isOutUrl = isUrlType && navUrl.startsWith("http");
    if (isUrlType) {
        navName = "nav_url-" + navName.hashCode();
        if (isOutUrl) {
            navUrl = ServerListener.getContextPath() + "/commons/url-safe?url=" + CodecUtils.urlEncode(navUrl);
        } else {
            navUrl = ServerListener.getContextPath() + navUrl;
        }
    } else if (NAV_FEEDS.equals(navName)) {
        navName = "nav_entity-Feeds";
        navUrl = ServerListener.getContextPath() + "/feeds/home";
    } else if (NAV_FILEMRG.equals(navName)) {
        navName = "nav_entity-Attachment";
        navUrl = ServerListener.getContextPath() + "/files/home";
    } else {
        navName = "nav_entity-" + navName;
        navUrl = ServerListener.getContextPath() + "/app/" + navUrl + "/list";
    }

    String navIcon = StringUtils.defaultIfBlank(item.getString("icon"), "texture");
    String navText = item.getString("text");

    JSONArray subNavs = null;
    if (activeNav != null) {
        subNavs = item.getJSONArray("sub");
        if (subNavs == null || subNavs.isEmpty()) {
            subNavs = null;
        }
    }

    StringBuilder navHtml = new StringBuilder()
            .append(String.format("<li class=\"%s\"><a href=\"%s\" target=\"%s\"><i class=\"icon zmdi zmdi-%s\"></i><span>%s</span></a>",
                    navName + (subNavs == null ? StringUtils.EMPTY : " parent"),
                    subNavs == null ? navUrl : "###",
                    isOutUrl ? "_blank" : "_self",
                    navIcon,
                    navText));
    if (subNavs != null) {
        StringBuilder subHtml = new StringBuilder()
                .append("<ul class=\"sub-menu\"><li class=\"title\">")
                .append(navText)
                .append("</li><li class=\"nav-items\"><div class=\"content\"><ul class=\"sub-menu-ul\">");

        for (Object o : subNavs) {
            JSONObject subNav = (JSONObject) o;
            subHtml.append(renderNavItem(subNav, null));
        }
        subHtml.append("</ul></div></li></ul>");
        navHtml.append(subHtml);
    }
    navHtml.append("</li>");

    if (activeNav != null) {
        Document navBody = Jsoup.parseBodyFragment(navHtml.toString());
        for (Element nav : navBody.select("." + activeNav)) {
            nav.addClass("active");
            if (activeNav.startsWith("nav_entity-")) {
                Element navParent = nav.parent();
                if (navParent != null && navParent.hasClass("sub-menu-ul")) {
                    navParent.parent().parent().parent().parent().addClass("open active");
                }
            }
        }
        return navBody.selectFirst("li").outerHtml();
    }
    return navHtml.toString();
}
 
Example 17
Source File: Evaluator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean matches(Element root, Element element) {
	final Element p = element.parent();
	return p != null && !(p instanceof Document) && element.elementSiblingIndex() == p.children().size()-1;
}
 
Example 18
Source File: Evaluator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean matches(Element root, Element element) {
	final Element p = element.parent();
	return p != null && !(p instanceof Document) && element.elementSiblingIndex() == 0;
}
 
Example 19
Source File: Evaluator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean matches(Element root, Element element) {
	final Element p = element.parent();
	return p!=null && !(p instanceof Document) && element.siblingElements().size() == 0;
}
 
Example 20
Source File: AxisSelector.java    From CrawlerForReader with Apache License 2.0 2 votes vote down vote up
/**
 * 父节点
 *
 * @param e
 * @return
 */
public Elements parent(Element e) {
    return new Elements(e.parent());
}