Example usage for org.jsoup.nodes Element parent

List of usage examples for org.jsoup.nodes Element parent

Introduction

In this page you can find the example usage for org.jsoup.nodes Element parent.

Prototype

@Override
    public final Element parent() 

Source Link

Usage

From source file:Main.java

private static boolean preserveWhitespace(Node node) {
    // looks only at this element and one level up, to prevent recursion & needless stack searches
    if (node != null && node instanceof Element) {
        Element element = (Element) node;
        return element.tag().preserveWhitespace()
                || element.parent() != null && element.parent().tag().preserveWhitespace();
    }//from w ww  . ja v  a 2 s.  co m
    return false;
}

From source file:io.github.carlomicieli.footballdb.starter.pages.TableBuilder.java

private static boolean isHeaderRow(Element r) {
    return r.parent().tagName().equals("thead");
}

From source file:com.megatome.j2d.support.JavadocSupport.java

private static List<SearchIndexValue> indexFile(File f) throws BuilderException {
    final List<SearchIndexValue> values = new ArrayList<>();
    final Elements elements = loadAndFindLinks(f);
    for (final Element e : elements) {
        Element parent = e.parent();
        if (!parent.child(0).equals(e)) {
            continue;
        }//from   www.j  a v  a2 s .  c om
        final String parentTagName = parent.tagName();
        if (parentPattern.matcher(parentTagName).matches()) {
            parent = parent.parent();
            if (!parent.child(0).equals(e.parent())) {
                continue;
            }
        }
        if (!containsIgnoreCase(parentTagName, "dt")) {
            continue;
        }
        final String text = parent.text();
        final String name = e.text();
        final String className = parent.className();

        final MatchType type = getMatchingType(text, className);

        if (null == type) {
            System.err.println(String.format(
                    "Unknown type found. Please submit a bug report. (Text: %s, Name: %s, className: %s)", text,
                    name, className));
            continue;
        }
        try {
            final String linkPath = URLDecoder.decode(e.attr("href"), "UTF-8");

            values.add(new SearchIndexValue(name, type, linkPath));
        } catch (UnsupportedEncodingException ex) {
            throw new BuilderException("Error decoding a link", ex);
        }
    }
    return values;
}

From source file:com.megatome.j2d.support.JavadocSupport.java

private static List<SearchIndexValue> indexClassFile(File f) throws BuilderException {
    final List<SearchIndexValue> values = new ArrayList<>();
    final Elements elements = loadAndFindLinks(f);
    String lastContext = "";
    for (final Element e : elements) {
        Element parent = e.parent();
        if (!parent.child(0).equals(e)) {
            continue;
        }//  w  ww  .  ja va 2 s . c  o m
        if (e.hasAttr("name")) {
            lastContext = e.attr("name");
        }
        final String parentTagName = parent.tagName();
        final String parentClassName = parent.className();
        if (parentPattern.matcher(parentTagName).matches()) {
            parent = parent.parent();
            if (!parent.child(0).equals(e.parent())) {
                continue;
            }
        }

        if (!containsIgnoreCase(parentTagName, "span") || !containsIgnoreCase(parentClassName, "memberNameLink")
                || equalsIgnoreCase("nested.class.summary", lastContext)
                || equalsIgnoreCase("enum.constant.summary", lastContext)) {
            continue;
        }
        final String text = parent.text();

        final MatchType type = getMatchingType(lastContext, null);

        if (null == type) {
            System.err.println(
                    String.format("Unknown type found. Please submit a bug report. (Text: %s, Context: %s)",
                            text, lastContext));
            continue;
        }
        try {
            final String linkPath = URLDecoder.decode(e.attr("href"), "UTF-8").replaceAll("\\.\\.\\/", "");

            values.add(new SearchIndexValue(text, type, linkPath));
        } catch (UnsupportedEncodingException ex) {
            throw new BuilderException("Error decoding a link", ex);
        }
    }
    return values;
}

From source file:com.kantenkugel.discordbot.jdocparser.JDocParser.java

private static Map<String, String> getInheritedMethods(Element summaryAnchor) {
    Map<String, String> inherited = new HashMap<>();
    if (summaryAnchor == null)
        return inherited;
    summaryAnchor = summaryAnchor.parent();
    Elements inheritAnchors = summaryAnchor.select("a[name^=\"methods.inherited.from.class\"]");
    for (Element inheritAnchor : inheritAnchors) {
        if (inheritAnchor.siblingElements().size() != 2)
            throw new RuntimeException("Got unexpected html while parsing inherited methods from class "
                    + inheritAnchor.attr("name"));
        Element next = inheritAnchor.nextElementSibling();
        if (!next.tagName().equals("h3"))
            throw new RuntimeException("Got unexpected html while parsing inherited methods from class "
                    + inheritAnchor.attr("name"));
        Element sub = next.children().last();
        if (sub == null || !sub.tagName().equals("a"))
            continue;
        String parent = sub.text().toLowerCase();
        next = next.nextElementSibling();
        if (!next.tagName().equals("code"))
            throw new RuntimeException("Got unexpected html while parsing inherited methods from class "
                    + inheritAnchor.attr("name"));
        for (sub = next.children().first(); sub != null; sub = sub.nextElementSibling()) {
            if (sub.tagName().equals("a")) {
                inherited.putIfAbsent(sub.text().toLowerCase(), parent);
            }/*from   www.  j  a v a2s .  c  o m*/
        }
    }
    return inherited;
}

From source file:com.mycompany.grabberrasskazov.threads.ThreadForPageSave.java

public void indexStory(String pageUrl) {
    try {//from   www  .  j a v  a  2s .co m
        String oldId = pageUrl.replace(GlobalVars.mainSite, "");
        if (!mainBean.storyExists(oldId)) {
            Stories r = new Stories();

            Document doc = Jsoup.connect(pageUrl)
                    .userAgent("Opera/9.80 (X11; Linux x86_64) " + "Presto/2.12.388 Version/12.16").get();

            Elements nameBlockElements = doc.select("b:containsOwn(?)");
            Element nameBlock = nameBlockElements.get(0);
            nameBlock = nameBlock.parent().parent();
            nameBlockElements = nameBlock.select("td:eq(1)");
            nameBlock = nameBlockElements.get(0);
            String storyName = nameBlock.text();
            r.setStoryName(storyName);

            // Start of processing writer
            Elements writerBlockElements = doc.select("b:containsOwn(?:)");
            Element writerBlock = writerBlockElements.get(0);
            writerBlock = writerBlock.parent().parent();
            writerBlockElements = writerBlock.select("td:eq(1)");
            writerBlock = writerBlockElements.get(0);

            String writersUrl = writerBlock.select("a:eq(0)").attr("href");
            String writersName = writerBlock.select("a:eq(0)").text();
            String writersContacts = writerBlock.select("a:eq(1)").attr("href");

            StoryWriters storyWriter = new StoryWriters();
            storyWriter.setOldId(writersUrl);
            storyWriter.setWriterEmail(writersContacts);
            storyWriter.setWriterName(writersName);
            storyWriter = mainBean.saveWriter(storyWriter);

            Set<StoriesToWritersRelations> storiesToWritersRelationses = new HashSet<StoriesToWritersRelations>();
            StoriesToWritersRelations storiesToWritersRelations = new StoriesToWritersRelations();
            storiesToWritersRelations.setStories(r);
            storiesToWritersRelations.setStoryWriters(storyWriter);
            r.setStoriesToWritersRelationses(storiesToWritersRelationses);

            // End of processing writer
            Set<StoriesToCategoriessRelations> catsRelationses = new HashSet<>();
            Elements katsInfo = doc.select("a[href*=ras.shtml?kat]");
            for (Element kat : katsInfo) {
                String katId = kat.attr("href");
                StoryCategories cat = mainBean.getCat(katId);

                StoriesToCategoriessRelations catsRelations = new StoriesToCategoriessRelations();
                catsRelations.setStoryCategories(cat);
                catsRelations.setStories(r);

                catsRelationses.add(catsRelations);

            }
            r.setStoriesToCategoriessRelationses(catsRelationses);

            Elements textBlocks = doc.select("p[align=justify]");
            Element textBlock = textBlocks.get(0);
            String textStr = textBlock.html();
            r.setStoryText(textStr.replace("\"", "'"));

            r.setOldId(oldId);

            mainBean.saveStory(r);
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:com.johan.vertretungsplan.parser.UntisInfoHeadlessParser.java

@Override
public Vertretungsplan getVertretungsplan() throws IOException, JSONException {
    new LoginHandler(schule).handleLogin(executor, cookieStore, username, password);

    Vertretungsplan v = new Vertretungsplan();
    List<VertretungsplanTag> tage = new ArrayList<VertretungsplanTag>();

    Document doc = Jsoup.parse(httpGet(url, schule.getData().getString("encoding")));
    Elements days = doc.select("#vertretung > p > b, #vertretung > b");
    for (Element day : days) {
        VertretungsplanTag tag = new VertretungsplanTag();
        tag.setStand("");
        tag.setDatum(day.text());/* ww  w.  jav a  2 s. c  o m*/
        Element next = null;
        if (day.parent().tagName().equals("p")) {
            next = day.parent().nextElementSibling().nextElementSibling();
        } else
            next = day.parent().select("p").first().nextElementSibling();
        if (next.className().equals("subst")) {
            //Vertretungstabelle
            if (next.text().contains("Vertretungen sind nicht freigegeben"))
                continue;
            parseVertretungsplanTable(next, data, tag);
        } else {
            //Nachrichten
            parseNachrichten(next, data, tag);
            next = next.nextElementSibling().nextElementSibling();
            parseVertretungsplanTable(next, data, tag);
        }
        tage.add(tag);
    }
    v.setTage(tage);
    return v;
}

From source file:com.geecko.QuickLyric.tasks.IdDecoder.java

@Override
protected Lyrics doInBackground(String... strings) {
    String url = strings[0];//from   w w  w.  j  a  va 2  s  .  c om
    String artist;
    String track;
    if (url.contains("//www.soundhound.com/")) {
        try { // todo switch to Jsoup
            String html = getUrlAsString(url);
            int preceding = html.indexOf("root.App.trackDa") + 19;
            int following = html.substring(preceding).indexOf(";");
            String data = html.substring(preceding, preceding + following);
            JSONObject jsonData = new JSONObject(data);
            artist = jsonData.getString("artist_display_name");
            track = jsonData.getString("track_name");
        } catch (IOException | JSONException e) {
            e.printStackTrace();
            return new Lyrics(ERROR);
        }

    } else if (url.contains("//shz.am/")) {
        try {
            Document doc = Jsoup.connect(url.trim()).get();
            track = doc.getElementsByAttribute("data-track-title").text();
            artist = doc.getElementsByAttribute("data-track-artist").text();
        } catch (IOException e) {
            e.printStackTrace();
            return new Lyrics(ERROR);
        }
    } else if (url.contains("//play.google.com/store/music/")) {
        String docID = url.substring(url.indexOf("&tid=") + 5);
        try {
            Document doc = Jsoup.connect(url).get();
            Element playCell = doc.getElementsByAttributeValue("data-track-docid", docID).get(0);
            artist = doc.getElementsByClass("primary").text();
            track = playCell.parent().parent().child(1).getElementsByClass("title").text();
        } catch (IOException e) {
            e.printStackTrace();
            return new Lyrics(ERROR);
        }
    } else
        return new Lyrics(ERROR);
    Lyrics res = new Lyrics(Lyrics.SEARCH_ITEM);
    res.setArtist(artist);
    res.setTitle(track);
    return res;
}

From source file:net.devietti.ArchConfMapServlet.java

/**
 * Returns the URL of the external conference website (not the WikiCFP page) for the given
 * eventid.//from   w ww.  j  av a2  s  . c o  m
 */
private void getConfLink(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String eids = req.getParameter("eventid");
    if (eids == null) {
        error("missing required URL parameter: eventid");
        return;
    }
    Integer eid;
    try {
        eid = Integer.valueOf(eids);
    } catch (NumberFormatException e) {
        error(e.getMessage());
        return;
    }
    if (eid == null || eid == 0) {
        error("error parsing eventid");
        return;
    }

    // pull down the CFP
    Document cfp = getURL("http://www.wikicfp.com/cfp/servlet/event.showcfp?eventid=" + eids);

    for (Element a : cfp.select("tr td[align=center] a")) {
        Element td = a.parent();
        if (td.text().contains("Link:") && a.hasAttr("href") && a.attr("href").contains("http://")) {
            // got the link!
            resp.setContentType("application/json");
            resp.getWriter().println(GSON.toJson(a.attr("href")));
            return;
        }
    }

    error("no matching link");
}