Example usage for org.jsoup.nodes Element attr

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

Introduction

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

Prototype

public String attr(String attributeKey) 

Source Link

Document

Get an attribute's value by its key.

Usage

From source file:net.intelliant.util.UtilCommon.java

public static String getModifiedHtmlWithAbsoluteImagePath(String html) {
    if (UtilValidate.isEmpty(html)) {
        return html;
    }/*  ww  w  . jav  a  2 s. c o m*/
    org.jsoup.nodes.Document doc = Jsoup.parse(html);
    Elements images = doc.select("img[src~=(?i)\\.(jpg|jpeg|png|gif)]");

    if (images != null && images.size() > 0) {
        String srcAttributeValue = "";
        StringBuilder finalLocation = new StringBuilder();
        Set<String> imageSrc = new HashSet<String>();

        for (Element image : images) {
            srcAttributeValue = image.attr("src");

            if (!imageSrc.contains(srcAttributeValue)) {
                int separatorIndex = srcAttributeValue.lastIndexOf("/");
                if (separatorIndex == -1) {
                    separatorIndex = srcAttributeValue
                            .lastIndexOf("\\"); /** just in case some one plays with html source. */
                }
                String outputFileName = null;
                if (separatorIndex != -1) {
                    String originalFileName = srcAttributeValue.substring(separatorIndex + 1);
                    outputFileName = originalFileName;
                }
                finalLocation = new StringBuilder(imageUploadLocation);
                finalLocation = finalLocation.append(outputFileName);

                imageSrc.add(srcAttributeValue);
                html = StringUtil.replaceString(html, srcAttributeValue, finalLocation.toString());
            }
        }
    }
    return html;
}

From source file:com.itcs.commons.email.EmailAutoconfigClient.java

private static boolean existsIncommingType(String emailAddress, String type) {
    if (existsAutoconfigSettings(emailAddress)) {
        try {/*  ww w . j av  a2  s  .c  o m*/
            String domain = "gmail.com";
            if (!isGmailAddress(emailAddress)) {
                domain = extractDomain(emailAddress);
            }
            Document doc = settingsCache.get(domain);
            for (Element element : doc.select("incomingServer")) {
                if (element.attr("type").equals(type)) {
                    return true;
                }
            }

        } catch (Exception ex) {
            //                ex.printStackTrace();
        }
    }
    return false;
}

From source file:net.intelliant.util.UtilCommon.java

/**
 * 1. Compressed JPEG images.//from  w w w  .ja v a 2 s.com
 * 2. Prefixes (if required) image server URL to image src locations. 
 * 
 * @return a <code>String</code> value
 */
public static String parseHtmlAndGenerateCompressedImages(String html) throws IOException {
    if (UtilValidate.isEmpty(html)) {
        return html;
    }
    org.jsoup.nodes.Document doc = Jsoup.parse(html);
    Elements images = doc.select("img[src~=(?i)\\.(jpg|jpeg|png|gif)]");
    if (images != null && images.size() > 0) {
        Set<String> imageLocations = new HashSet<String>();
        for (Element image : images) {
            String srcAttributeValue = image.attr("src");
            if (!(imageLocations.contains(srcAttributeValue))) {
                if (Debug.infoOn()) {
                    Debug.logInfo(
                            "[parseHtmlAndGenerateCompressedImages] originalSource >> " + srcAttributeValue,
                            module);
                }
                if (!UtilValidate.isUrl(srcAttributeValue)) {
                    int separatorIndex = srcAttributeValue.lastIndexOf("/");
                    if (separatorIndex == -1) {
                        separatorIndex = srcAttributeValue
                                .lastIndexOf("\\"); /** just in case some one plays with html source. */
                    }
                    if (separatorIndex != -1) {
                        String originalFileName = srcAttributeValue.substring(separatorIndex + 1);

                        /* Handling spaces in file-name to make url friendly. */
                        String outputFileName = StringEscapeUtils.escapeHtml(originalFileName);
                        /** Compression works for jpeg's only. 
                        if (originalFileName.endsWith("jpg") || originalFileName.endsWith("jpeg")) {
                           try {
                              outputFileName = generateCompressedImageForInputFile(imageUploadLocation, originalFileName);
                           } catch (NoSuchAlgorithmException e) {
                              Debug.logError(e, module);
                              return html;
                           }
                        }
                        */
                        StringBuilder finalLocation = new StringBuilder(campaignBaseURL);
                        finalLocation.append(imageUploadWebApp).append(outputFileName);
                        html = StringUtil.replaceString(html, srcAttributeValue, finalLocation.toString());
                        imageLocations.add(srcAttributeValue);
                    }
                } else {
                    Debug.logWarning("[parseHtmlAndGenerateCompressedImages] ignoring encountered HTML URL..",
                            module);
                }
            }
        }
    } else {
        if (Debug.infoOn()) {
            Debug.logInfo("[parseHtmlAndGenerateCompressedImages] No jpeg images, doing nothing..", module);
        }
    }
    if (Debug.infoOn()) {
        Debug.logInfo("[parseHtmlAndGenerateCompressedImages] returning html >> " + html, module);
    }
    return html;
}

From source file:me.vertretungsplan.parser.DaVinciParser.java

@NotNull
static List<String> getDayUrls(String url, Document doc) throws IOException {
    List<String> dayUrls = new ArrayList<>();
    if (doc.select("ul.classes").size() > 0) {
        // List of classes
        Elements classes = doc.select("ul.classes li a");
        for (Element klasse : classes) {
            dayUrls.add(new URL(new URL(url), klasse.attr("href")).toString());
        }//from w  w  w.  j  av  a2 s  .com
    } else if (doc.select("ul.month").size() > 0) {
        // List of days in calendar view
        Elements days = doc.select("ul.month li input[onclick]");
        for (Element day : days) {
            String urlFromOnclick = urlFromOnclick(day.attr("onclick"));
            if (urlFromOnclick == null)
                continue;
            dayUrls.add(new URL(new URL(url), urlFromOnclick).toString());
        }
    } else if (doc.select("ul.day-index").size() > 0) {
        // List of days in list view
        Elements days = doc.select("ul.day-index li a");
        for (Element day : days) {
            dayUrls.add(new URL(new URL(url), day.attr("href")).toString());
        }
    } else if (doc.select("table td[align=left] a").size() > 0) {
        // Table of classes (DaVinci 5)
        Elements classes = doc.select("table td[align=left] a");
        for (Element klasse : classes) {
            dayUrls.add(new URL(new URL(url), klasse.attr("href")).toString());
        }
    } else {
        // Single day
        dayUrls.add(url);
    }
    return dayUrls;
}

From source file:mailbox.CreationViaEmail.java

private static String replaceCidWithAttachments(String html, Map<String, Attachment> attachments) {
    Document doc = Jsoup.parse(html);
    String[] attrNames = { "src", "href" };

    for (String attrName : attrNames) {
        Elements tags = doc.select("*[" + attrName + "]");
        for (Element tag : tags) {
            String uriString = tag.attr(attrName).trim();

            if (!uriString.toLowerCase().startsWith("cid:")) {
                continue;
            }/*  w  ww . j  av  a 2 s .  c  o  m*/

            String cid = uriString.substring("cid:".length());

            if (!attachments.containsKey(cid)) {
                continue;
            }

            Long id = attachments.get(cid).id;
            tag.attr(attrName, controllers.routes.AttachmentApp.getFile(id).url());
        }
    }

    Elements bodies = doc.getElementsByTag("body");

    if (bodies.size() > 0) {
        return bodies.get(0).html();
    } else {
        return doc.html();
    }
}

From source file:mobi.jenkinsci.ci.client.JenkinsFormAuthHttpClient.java

public static HttpPost getPostForm(final String requestBaseUrl, final Element form,
        final HashMap<String, String> formMapping) throws MalformedURLException {
    final List<NameValuePair> formNvps = new ArrayList<NameValuePair>();
    final String formAction = form.attr("action");
    final HttpPost formPost = new HttpPost(getUrl(requestBaseUrl, formAction));
    final Elements formFields = form.select("input");
    for (final Element element : formFields) {
        final String fieldName = element.attr("name");
        String fieldValue = element.attr("value");
        final String fieldId = element.attr("id");

        if (formMapping != null) {
            final String mappedValue = formMapping.get(fieldId);
            if (mappedValue != null) {
                fieldValue = mappedValue;
            }/* ww  w.ja v  a 2 s  .  c om*/
        }

        log.debug(String.format("Processing form field: name='%s' value='%s' id='%s'", fieldName, fieldValue,
                fieldId));
        formNvps.add(new BasicNameValuePair(fieldName, fieldValue));
    }
    try {
        formPost.setEntity(new UrlEncodedFormEntity(formNvps, "UTF-8"));
    } catch (final UnsupportedEncodingException e) {
        // This would never happen
        throw new IllegalArgumentException("UTF-8 not recognised");
    }

    return formPost;
}

From source file:de.luhmer.owncloudnewsreader.reader.GoogleReaderApi.GoogleReaderMethods.java

@SuppressWarnings("unused")
public static ArrayList<FolderSubscribtionItem> getSubList(String _USERNAME, String _PASSWORD)
        throws UnsupportedEncodingException, IOException {
    ArrayList<FolderSubscribtionItem> _SUBTITLE_ARRAYLIST = new ArrayList<FolderSubscribtionItem>();

    Document doc = Jsoup.connect(GoogleReaderConstants._SUBSCRIPTION_LIST_URL)
            .header("Authorization",
                    GoogleReaderConstants._AUTHPARAMS
                            + AuthenticationManager.getGoogleAuthKey(_USERNAME, _PASSWORD))
            .userAgent(GoogleReaderConstants.APP_NAME).timeout(5000).get();

    Elements objects = doc.select("object");
    Element element = objects.get(0);
    Node childTemp = element.childNodes().get(0);
    List<Node> childs = childTemp.childNodes();

    for (Node node : childs) {
        Elements links = ((Element) node).select("string");
        String idFeed = null;//from   www  . j a  va 2s  . c  o  m
        String feedName;
        String parentSubscriptionName;

        for (Element link : links) {
            String tagAttrib = link.attr("name");
            String tagText = link.text();
            if (tagAttrib.equals("id") && idFeed == null)
                idFeed = tagText;
            else if (tagAttrib.equals("title"))
                feedName = tagText;
            else if (tagAttrib.equals("label"))
                parentSubscriptionName = tagText;
        }

        //String idFeed = node.attr("id");
        //String name = node.attr("title");

        //_SUBTITLE_ARRAYLIST.add(new FolderSubscribtionItem(feedName, -1, idFeed, parentSubscriptionName));//TODO implements this again... ? Update FolderSubscribtionItem
    }

    //String[] _SUBTITLE_ARRAY = new String[_SUBTITLE_ARRAYLIST.size()];
    //_SUBTITLE_ARRAYLIST.toArray(_SUBTITLE_ARRAY);
    return _SUBTITLE_ARRAYLIST;
}

From source file:app.sunstreak.yourpisd.net.Parser.java

/**
 * /* w w w .  j  av  a  2  s .  com*/
 * @param html the source code for ANY page in Gradebook (usually Default.aspx)
 * @return
 */
public static List<String[]> parseStudents(String html) {
    List<String[]> list = new ArrayList<String[]>();

    Element doc = Jsoup.parse(html);
    Element studentList = doc.getElementById("ctl00_ctl00_ContentPlaceHolder_uxStudentlist");

    // Only one student
    if (studentList.text().isEmpty()) {
        // {studentId, studentName}
        list.add(new String[] { doc.getElementById("ctl00_ctl00_ContentPlaceHolder_uxStudentId").attr("value"),
                doc.getElementById("ctl00_ctl00_ContentPlaceHolder_uxMultiple").text() });
        return list;
    }
    // Multiple students
    else {
        for (Element a : studentList.getElementsByTag("a")) {
            String name = a.text();
            String onClick = a.attr("onClick");
            String studentId = onClick.substring(onClick.indexOf('\'') + 1, onClick.lastIndexOf('\''));
            list.add(new String[] { studentId, name });
        }
        return list;
    }
}

From source file:com.itcs.commons.email.EmailAutoconfigClient.java

private static void extractIncommingServerSettings(Document doc, Map<String, String> settings, String type) {
    for (Element element : doc.select("incomingServer")) {
        //            System.out.println("element.attr(\"type\"):"+element.attr("type"));
        if (element.attr("type").equals(type)) {
            //                System.out.println("element.select(\"hostname\"):" + element.select("hostname").text());
            settings.put(EnumEmailSettingKeys.INBOUND_SERVER.getKey(), element.select("hostname").text());
            //                System.out.println("element.select(\"port\"):" + element.select("port").text());
            settings.put(EnumEmailSettingKeys.INBOUND_PORT.getKey(), element.select("port").text());
            //                System.out.println("element.select(\"socketType\"):" + element.select("socketType").text());
            settings.put(EnumEmailSettingKeys.INBOUND_SSL_ENABLED.getKey(),
                    element.select("socketType").text().trim().equals("SSL") ? "true" : "false");
        }//from  w  ww  .j a v  a2  s. c o  m
    }
}

From source file:com.itcs.commons.email.EmailAutoconfigClient.java

private static void extractOutgoingServerSettings(Document doc, Map<String, String> settings) {
    for (Element element : doc.select("outgoingServer")) {
        //            System.out.println("element.attr(\"type\"):"+element.attr("type"));
        if (element.attr("type").equals("smtp")) {
            //                System.out.println("element.select(\"hostname\"):" + element.select("hostname").text());
            settings.put(EnumEmailSettingKeys.SMTP_SERVER.getKey(), element.select("hostname").text());
            //                System.out.println("element.select(\"port\"):" + element.select("port").text());
            settings.put(EnumEmailSettingKeys.SMTP_PORT.getKey(), element.select("port").text());
            //                System.out.println("element.select(\"socketType\"):" + element.select("socketType").text());
            settings.put(EnumEmailSettingKeys.SMTP_SSL_ENABLED.getKey(),
                    element.select("socketType").text().equals("SSL") ? "true" : "false");
            settings.put(EnumEmailSettingKeys.TRANSPORT_TLS.getKey(),
                    element.select("socketType").text().equals("STARTTLS") ? "true" : "false");
        }//  www .  j  a  v  a  2s.c om
    }
}