Example usage for org.jsoup.nodes Element className

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

Introduction

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

Prototype

public String className() 

Source Link

Document

Gets the literal value of this element's "class" attribute, which may include multiple class names, space separated.

Usage

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;
        }//from   ww  w.  j a  v a2  s . c  om
        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 Element getSingleElementByQuery(Element root, String query) {
    Elements elementsByQuery = root.select(query);
    if (elementsByQuery.size() > 1) {
        String error = "Found " + elementsByQuery.size() + " elements matching query \"" + query
                + "\" inside of " + root.tagName() + "-" + root.className();
        throw new RuntimeException(error + root.html());
    }//from   w  w  w .  ja v a  2  s .  co m
    return elementsByQuery.first();
}

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

private static Element getSingleElementByClass(Element root, String className) {
    Elements elementsByClass = root.getElementsByClass(className);
    if (elementsByClass.size() != 1) {
        String error = "Found " + elementsByClass.size() + " elements with class " + className + " inside of "
                + root.tagName() + "-" + root.className();
        throw new RuntimeException(error + root.html());
    }//from   www.  j  a va 2  s .  com
    return elementsByClass.first();
}

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 ava 2  s  .c  o  m*/
        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.kantenkugel.discordbot.jdocparser.JDocParser.java

private static List<DocBlock> getDocBlock(String jdocBase, Element elem, ClassDocumentation reference) {
    if (elem != null) {
        String baseLink = JDocUtil.getLink(jdocBase, reference);
        List<DocBlock> blocks = new ArrayList<>(10);
        String hashLink = null;//from w w  w  .  ja v  a 2  s.c  o m
        for (elem = elem.nextElementSibling(); elem != null; elem = elem.nextElementSibling()) {
            if (elem.tagName().equals("a")) {
                hashLink = '#' + elem.attr("name");
            } else if (elem.tagName().equals("ul")) {
                Element tmp = elem.getElementsByTag("h4").first();
                String title = JDocUtil.fixSpaces(tmp.text().trim());
                String description = "", signature = "";
                OrderedMap<String, List<String>> fields = new ListOrderedMap<>();
                for (; tmp != null; tmp = tmp.nextElementSibling()) {
                    if (tmp.tagName().equals("pre")) {
                        //contains full signature
                        signature = JDocUtil.fixSpaces(tmp.text().trim());
                    } else if (tmp.tagName().equals("div") && tmp.className().equals("block")) {
                        //main block of content (description or deprecation)
                        Element deprecationElem = tmp.getElementsByClass("deprecationComment").first();
                        if (deprecationElem != null) {
                            //deprecation block
                            fields.put("Deprecated:", Collections
                                    .singletonList(JDocUtil.formatText(deprecationElem.html(), baseLink)));
                        } else {
                            //description block
                            description = JDocUtil.formatText(tmp.html(), baseLink);
                        }
                    } else if (tmp.tagName().equals("dl")) {
                        //a field
                        String fieldName = null;
                        List<String> fieldValues = new ArrayList<>();
                        for (Element element : tmp.children()) {
                            if (element.tagName().equals("dt")) {
                                if (fieldName != null) {
                                    fields.put(fieldName, fieldValues);
                                    fieldValues = new ArrayList<>();
                                }
                                fieldName = JDocUtil.fixSpaces(element.text().trim());
                            } else if (element.tagName().equals("dd")) {
                                fieldValues.add(JDocUtil.formatText(element.html(), baseLink));
                            }
                        }
                        if (fieldName != null) {
                            fields.put(fieldName, fieldValues);
                        }
                    }
                }
                blocks.add(new DocBlock(title, hashLink, signature, description, fields));
            }
        }
        return blocks;
    }
    return null;
}

From source file:com.obnsoft.ptcm3.MyApplication.java

private void parseCommandHtml() {
    mCommands = new ArrayList<Command>();
    mCategories = new ArrayList<String>();
    int categoryId = -1;
    try {/*from   w w  w  .  ja v a  2  s  . c om*/
        InputStream in = openFileInput(FNAME_CMD_HTML);
        Document document = Jsoup.parse(in, "UTF-8", URL_CMD_HTML);
        in.close();
        Element divContentArea = document.getElementById(ID_CONTENTAREA);
        for (Element e : divContentArea.children()) {
            if (e.tagName().equals(TAG_TABLE)) {
                if (e.className().equals("")) {
                    mCommands.add(new Command(e, categoryId));
                }
            } else if (e.tagName().equals(TAG_H3)) {
                mCategories.add(e.text());
                categoryId++;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        mCommands = null;
        mCategories = null;
    }
}

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());//w  ww .  j a va 2s . c  om
        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:me.vertretungsplan.parser.SVPlanParser.java

private void parseSvPlanDay(SubstitutionSchedule v, Element svp, Document doc) throws IOException {
    SubstitutionScheduleDay day = new SubstitutionScheduleDay();
    if ((svp.select(".svp-plandatum-heute, .svp-plandatum-morgen, .Titel").size() > 0
            || doc.title().startsWith("Vertretungsplan fr "))) {
        setDate(svp, doc, day);/*from w w w .j  a v a  2 s  . c om*/
        if (svp.select(".svp-tabelle, table:has(.Klasse)").size() > 0) {

            Elements rows = svp.select(".svp-tabelle tr, table:has(.Klasse) tr");
            String lastLesson = "";
            String lastClass = "";
            for (Element row : rows) {
                if ((doc.select(".svp-header").size() > 0 && row.hasClass("svp-header"))
                        || row.select("th").size() > 0 || row.text().trim().equals("")) {
                    continue;
                }

                Substitution substitution = new Substitution();

                for (Element column : row.select("td")) {
                    String type = column.className();
                    if (!hasData(column.text())) {
                        if ((type.startsWith("svp-stunde") || type.startsWith("Stunde"))
                                && hasData(lastLesson)) {
                            substitution.setLesson(lastLesson);
                        } else if ((type.startsWith("svp-klasse") || type.startsWith("Klasse"))
                                && hasData(lastClass)) {
                            substitution.getClasses().addAll(Arrays
                                    .asList(lastClass.split(data.optString(PARAM_CLASS_SEPARATOR, ", "))));
                        }
                        continue;
                    }
                    if (type.startsWith("svp-stunde") || type.startsWith("Stunde")) {
                        substitution.setLesson(column.text());
                        lastLesson = column.text();
                    } else if (type.startsWith("svp-klasse") || type.startsWith("Klasse")) {
                        substitution.getClasses().addAll(Arrays
                                .asList(column.text().split(data.optString(PARAM_CLASS_SEPARATOR, ", "))));
                        lastClass = column.text();
                    } else if (type.startsWith("svp-esfehlt") || type.startsWith("Lehrer")) {
                        if (!data.optBoolean(PARAM_EXCLUDE_TEACHERS)) {
                            substitution.setPreviousTeacher(column.text());
                        }
                    } else if (type.startsWith("svp-esvertritt") || type.startsWith("Vertretung")) {
                        if (!data.optBoolean(PARAM_EXCLUDE_TEACHERS)) {
                            substitution.setTeacher(column.text().replaceAll(" \\+$", ""));
                        }
                    } else if (type.startsWith("svp-fach") || type.startsWith("Fach")) {
                        substitution.setSubject(column.text());
                    } else if (type.startsWith("svp-bemerkung") || type.startsWith("Anmerkung")) {
                        substitution.setDesc(column.text());
                        String recognizedType = recognizeType(column.text());
                        substitution.setType(recognizedType);
                        substitution.setColor(colorProvider.getColor(recognizedType));
                    } else if (type.startsWith("svp-raum") || type.startsWith("Raum")) {
                        substitution.setRoom(column.text());
                    }
                }

                if (substitution.getType() == null) {
                    substitution.setType("Vertretung");
                    substitution.setColor(colorProvider.getColor("Vertretung"));
                }

                day.addSubstitution(substitution);
            }
        }
        if (svp.select(".LehrerVerplant").size() > 0) {
            day.addMessage("<b>Verplante Lehrer:</b> " + svp.select(".LehrerVerplant").text());
        }
        if (svp.select(".Abwesenheiten").size() > 0) {
            day.addMessage("<b>Abwesenheiten:</b> " + svp.select(".Abwesenheiten").text());
        }

        if (svp.select("h2:contains(Mitteilungen)").size() > 0) {
            Element h2 = svp.select("h2:contains(Mitteilungen)").first();
            Element sibling = h2.nextElementSibling();
            while (sibling != null && sibling.tagName().equals("p")) {
                for (String nachricht : TextNode.createFromEncoded(sibling.html(), null).getWholeText()
                        .split("<br />\\s*<br />")) {
                    if (hasData(nachricht))
                        day.addMessage(nachricht);
                }
                sibling = sibling.nextElementSibling();
            }
        } else if (svp.select(".Mitteilungen").size() > 0) {
            for (Element p : svp.select(".Mitteilungen")) {
                for (String nachricht : TextNode.createFromEncoded(p.html(), null).getWholeText()
                        .split("<br />\\s*<br />")) {
                    if (hasData(nachricht))
                        day.addMessage(nachricht);
                }
            }
        }
        v.addDay(day);
    } else {
        throw new IOException("keine SVPlan-Tabelle gefunden");
    }
}

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

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

    JSONArray urls = schule.getData().getJSONArray("urls");
    String encoding = schule.getData().getString("encoding");
    List<Document> docs = new ArrayList<Document>();

    for (int i = 0; i < urls.length(); i++) {
        JSONObject url = urls.getJSONObject(i);
        loadUrl(url.getString("url"), encoding, docs);
    }/*www  .  j  av  a2s . co m*/

    LinkedHashMap<String, VertretungsplanTag> tage = new LinkedHashMap<String, VertretungsplanTag>();
    for (Document doc : docs) {
        if (doc.select(".svp-tabelle").size() > 0) {
            VertretungsplanTag tag = new VertretungsplanTag();
            String date = "Unbekanntes Datum";
            if (doc.select(".svp-plandatum-heute, .svp-plandatum-morgen").size() > 0)
                date = doc.select(".svp-plandatum-heute, .svp-plandatum-morgen").text();
            else if (doc.title().startsWith("Vertretungsplan fr "))
                date = doc.title().substring("Vertretungsplan fr ".length());
            tag.setDatum(date);
            if (doc.select(".svp-uploaddatum").size() > 0)
                tag.setStand(doc.select(".svp-uploaddatum").text().replace("Aktualisierung: ", ""));

            Elements rows = doc.select(".svp-tabelle tr");
            String lastLesson = "";
            for (Element row : rows) {
                if (row.hasClass("svp-header"))
                    continue;

                Vertretung vertretung = new Vertretung();
                List<String> affectedClasses = new ArrayList<String>();

                for (Element column : row.select("td")) {
                    if (!hasData(column.text())) {
                        continue;
                    }
                    String type = column.className();
                    if (type.startsWith("svp-stunde")) {
                        vertretung.setLesson(column.text());
                        lastLesson = column.text();
                    } else if (type.startsWith("svp-klasse"))
                        affectedClasses = Arrays.asList(column.text().split(", "));
                    else if (type.startsWith("svp-esfehlt"))
                        vertretung.setPreviousTeacher(column.text());
                    else if (type.startsWith("svp-esvertritt"))
                        vertretung.setTeacher(column.text());
                    else if (type.startsWith("svp-fach"))
                        vertretung.setSubject(column.text());
                    else if (type.startsWith("svp-bemerkung")) {
                        vertretung.setDesc(column.text());
                        vertretung.setType(recognizeType(column.text()));
                    } else if (type.startsWith("svp-raum"))
                        vertretung.setRoom(column.text());

                    if (vertretung.getLesson() == null)
                        vertretung.setLesson(lastLesson);
                }

                if (vertretung.getType() == null) {
                    vertretung.setType("Vertretung");
                }

                for (String klasse : affectedClasses) {
                    KlassenVertretungsplan kv = tag.getKlassen().get(klasse);
                    if (kv == null)
                        kv = new KlassenVertretungsplan(klasse);
                    kv.add(vertretung);
                    tag.getKlassen().put(klasse, kv);
                }
            }

            List<String> nachrichten = new ArrayList<String>();
            if (doc.select("h2:contains(Mitteilungen)").size() > 0) {
                Element h2 = doc.select("h2:contains(Mitteilungen)").first();
                Element sibling = h2.nextElementSibling();
                while (sibling != null && sibling.tagName().equals("p")) {
                    for (String nachricht : TextNode.createFromEncoded(sibling.html(), null).getWholeText()
                            .split("<br />\\s*<br />")) {
                        if (hasData(nachricht))
                            nachrichten.add(nachricht);
                    }
                    sibling = sibling.nextElementSibling();
                }
            }
            tag.setNachrichten(nachrichten);

            tage.put(date, tag);
        } else {
            throw new IOException("keine SVPlan-Tabelle gefunden");
        }
    }
    Vertretungsplan v = new Vertretungsplan();
    v.setTage(new ArrayList<VertretungsplanTag>(tage.values()));

    return v;
}

From source file:com.aestasit.markdown.slidery.converters.TextTemplateConverter.java

private void renderSyntaxHighlightingHtml(final Document slidesDocument, final Configuration config) {
    for (Element code : slidesDocument.select("code")) {
        Charset encoding = config.getInputEncoding();
        ByteArrayInputStream input = new ByteArrayInputStream(code.text().getBytes(encoding));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        String className = code.className();
        if (StringUtils.isBlank(className)) {
            className = "java";
        }/*w  ww . j av  a 2 s  .com*/
        Renderer renderer = XhtmlRendererFactory.getRenderer(className);
        if (renderer != null) {
            try {
                renderer.highlight("slidery", input, out, encoding.name(), true);
                code.html(new String(out.toByteArray(), encoding));
                code.select("br").remove();
                removeComments(code);
                code.html(code.html().trim());
                Element parent = code.parent();
                if (parent.tagName() == "pre") {
                    parent.addClass("code");
                }
            } catch (IOException e) {
                // TODO: Handle exception
            }
        }
    }
}