Example usage for org.jsoup.nodes Document select

List of usage examples for org.jsoup.nodes Document select

Introduction

In this page you can find the example usage for org.jsoup.nodes Document select.

Prototype

public Elements select(String cssQuery) 

Source Link

Document

Find elements that match the Selector CSS query, with this element as the starting context.

Usage

From source file:com.entertailion.android.slideshow.utils.Utils.java

/**
 * Get the RSS feed for a web site./*from   w  w w  .  j a va  2 s. com*/
 * 
 * @param url
 * @param context
 * @param refresh
 * @return
 */
public static String getRssFeed(String url, Context context, boolean refresh) {
    String rss = Utils.getCachedData(context, url, refresh);
    if (rss != null) {
        rss = rss.trim();
        if (rss.startsWith(XML_PREFIX)) {
            return rss;
        } else {
            try {
                Document doc = Jsoup.parse(rss);
                Element link = doc.select("link[type=application/rss+xml]").first();
                if (link != null && link.attr("rel").equalsIgnoreCase("alternate")) {
                    String href = link.attr("href");
                    if (href != null) {
                        rss = Utils.getCachedData(context, href, refresh);
                        return rss;
                    }
                }
            } catch (Exception e) {
                Log.e(LOG_TAG, "Jsoup exception", e);
            }
        }
    }
    return rss;
}

From source file:io.andyc.papercut.api.PrintApi.java

/**
 * Checks to see if the number of copies form was submitted successfully by
 * parsing the resulting HTML//from w w w. ja  va  2 s  .  c o  m
 *
 * @param doc {Document} - the next page in the print job submission (the
 *            upload form)
 *
 * @return {boolean} - true if the number of copies was set successfully
 */
static boolean checkNumberOfCopiesSet(Document doc) {
    return !doc.select("form#upload-form").isEmpty();
}

From source file:io.andyc.papercut.api.PrintApi.java

/**
 * Checks to see if the printer type was set
 *
 * @param checkHtml {Document} - the resulting HTML to check after the
 *                  setPrinterType for was submitted
 *
 * @return {boolean} - true if the printer type form was submitted and the
 * printer type was successfully set//from  ww  w  .  j  ava 2 s. c  om
 */
static boolean checkIsPrinterTypeSet(Document checkHtml) {
    return !checkHtml.select("input[name=copies]").isEmpty();
}

From source file:com.hp.test.framework.htmparse.HtmlParse.java

public static String getCountsSuiteswise(String path) {
    Document htmlFile = null;
    try {// w w w .  j a va  2  s .c om
        htmlFile = Jsoup.parse(new File(path), "UTF-8");
    } catch (IOException e) {
        System.out.println("Exception in parse Current Run html file" + e.getMessage());
    }

    Map<String, Map<String, Integer>> Suites_list = new HashMap<>();
    for (Element table : htmlFile.select("table[id=tableStyle]")) {
        Elements row1 = table.select("tr");
        for (int j = 0; j < row1.size(); j++) {
            Element tds1 = row1.get(j);
            Elements tds = tds1.select("td");
            String SuiteName = "";
            String Method_type = "";
            String TestCaseStatus = "";
            Map<String, Integer> test_status_list = new HashMap<String, Integer>();
            for (int i = 0; i < tds.size(); i++) {
                Element link = tds.get(i);
                String link_temp = link.toString();
                Elements href = link.select("a");

                if (i == 0) {
                    if (href.size() > 0) {
                        SuiteName = href.get(0).text();
                    }
                }

                if (i == 3) {
                    if (href.size() > 0) {
                        Method_type = href.get(0).text();
                    }

                }
                if (i == 7 && Method_type.equals("Test Method")) {
                    if (link_temp.contains("pass.png") || link_temp.contains("fail.png")
                            || link_temp.contains("skip.png")) {
                        //          img style=\"border: none;width: 25px
                        //   ing str="img  style=\"border: none;width: 25px";

                        if (link_temp.contains("pass.png")) {
                            TestCaseStatus = "pass";
                        } else if (link_temp.contains("fail.png")) {
                            TestCaseStatus = "fail";
                        } else {
                            TestCaseStatus = "skip";
                        }
                        // System.out.println("SuiteName::" + SuiteName);
                        //  System.out.println("Method_type::" + Method_type);
                        // System.out.println("TestCaseStatus::" + TestCaseStatus);
                        //  System.out.println("*****************************");

                        if (Suites_list.get(SuiteName) == null) {
                            if (TestCaseStatus.equals("pass")) {
                                test_status_list.put("pass", 1);
                                test_status_list.put("fail", 0);
                                test_status_list.put("skip", 0);
                            }

                            if (TestCaseStatus.equals("fail")) {
                                test_status_list.put("pass", 0);
                                test_status_list.put("fail", 1);
                                test_status_list.put("skip", 0);
                            }

                            if (TestCaseStatus.equals("skip")) {
                                test_status_list.put("pass", 0);
                                test_status_list.put("fail", 0);
                                test_status_list.put("skip", 1);
                            }

                            Suites_list.put(SuiteName, test_status_list);
                        } else {
                            Map<String, Integer> temp_list = Suites_list.get(SuiteName);

                            for (String status : temp_list.keySet()) {
                                if (status.equals(TestCaseStatus)) {
                                    int count = temp_list.get(status);
                                    count = count + 1;
                                    temp_list.put(status, count);
                                }
                            }
                            Suites_list.put(SuiteName, temp_list);

                        }

                    }
                }
            }
        }

    }
    String variable = "var chartData = [";
    int NoofSuites = Suites_list.size();
    int i = 1;
    for (String FeatureName : Suites_list.keySet()) {
        String feature_data = " { \n \"feature\":\"" + FeatureName + "\",\n";

        Map<String, Integer> temp_list = Suites_list.get(FeatureName);

        for (String status : temp_list.keySet()) {
            feature_data = feature_data + "\"" + status + "\":" + temp_list.get(status) + ",\n";
        }
        if (!(NoofSuites == i)) {
            feature_data = feature_data + "},\n";
        } else {
            feature_data = feature_data + "}\n";
        }
        variable = variable + feature_data;
        i = i + 1;
    }
    variable = variable + "];";
    System.out.println("Getting the Counts Functionality Wise is Completed");
    return variable;

}

From source file:io.jari.geenstijl.API.API.java

/**
 * Downloads & parses the articles and images
 *
 * @param force Force download and bypass cache
 * @return Artikel[]/*from w  w  w.  j  a  v a 2 s.  c o m*/
 * @throws IOException
 * @throws ParseException
 * @throws URISyntaxException
 */
public static Artikel[] getArticles(boolean force, boolean page2, Context context)
        throws IOException, ParseException, URISyntaxException {
    if (!force && !page2) {
        Artikel[] cache = getCache(context);
        if (cache != null)
            return cache;
    }

    domain = context.getSharedPreferences("geenstijl", 0).getString("gsdomain", "www.geenstijl.nl");

    ensureCookies();

    //we halen onze data van de html versie van geenstijl, omdat de RSS versie pure poep is, en omdat jsoup awesome is
    Document document;
    if (page2)
        document = Jsoup.connect("http://" + domain + "/index2.html").get();
    else
        document = Jsoup.connect("http://" + domain + "/").get();

    Elements artikelen = document.select("#content>article");
    ArrayList<Artikel> resultaat = new ArrayList<Artikel>();
    for (Element artikel_el : artikelen) {
        Artikel artikel = parseArtikel(artikel_el, context);

        resultaat.add(artikel);
    }
    Artikel[] arr_res = new Artikel[resultaat.size()];
    resultaat.toArray(arr_res);
    if (!page2)
        setCache(arr_res, context);
    return arr_res;
}

From source file:com.nineash.hutsync.client.NetworkUtilities.java

/**
 * Connects to the SampleSync test server, authenticates the provided
 * username and password./*from   w  w w.  j  a  va  2  s. c o m*/
 *
 * @param username The server account username
 * @param password The server account password
 * @return String The authentication token returned by the server (or null)
 */
public static String authenticate(String username, String password, Context context) {
    try {
        final HttpResponse resp;
        final HttpResponse init_resp;
        final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        DefaultHttpClient hClient = getHttpClient(context);
        final HttpPost init_post = new HttpPost(BASE_URL);
        final int first_cookies;
        final ArrayList<SerializableCookie> saveCooks = new ArrayList<SerializableCookie>();
        BasicCookieStore bcs = new BasicCookieStore();

        params.add(new BasicNameValuePair(PARAM_USERNAME, username));
        params.add(new BasicNameValuePair(PARAM_PASSWORD, password));
        params.add(new BasicNameValuePair(PARAM_REMEMBER, "yes"));

        init_resp = hClient.execute(init_post);
        String respString = EntityUtils.toString(init_resp.getEntity());

        List<Cookie> cookies = hClient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            Log.e(TAG, "No cookies gathered first time round");
        }
        first_cookies = cookies.size();
        if (first_cookies != 2) {
            Log.e(TAG, "Should be two cookie to start off with");
        }

        Document doc = Jsoup.parse(respString);
        Elements hiddens = doc.select("div.homepage input[type=hidden]");

        for (Element hidden : hiddens) {
            params.add(new BasicNameValuePair(hidden.attr("name"), hidden.attr("value")));
        }

        final HttpEntity entity;
        try {
            entity = new UrlEncodedFormEntity(params);
        } catch (final UnsupportedEncodingException e) {
            // this should never happen.
            throw new IllegalStateException(e);
        }
        Log.i(TAG, "Authenticating to: " + AUTH_URI);
        final HttpPost post = new HttpPost(AUTH_URI);
        post.addHeader(entity.getContentType());
        post.setEntity(entity);
        resp = hClient.execute(post);
        String authToken = null;
        if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            //set authtoken here

            cookies = hClient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
                Log.e(TAG, "No cookies gathered");
            } else {
                if (cookies.size() == first_cookies + 2) { //we get two new cookies when we log in
                    for (int i = 0; i < cookies.size(); i++) {
                        Cookie cur_cookie = cookies.get(i);
                        if (cur_cookie.isPersistent()) {
                            saveCooks.add(new SerializableCookie(cur_cookie));
                        }
                    }
                    authToken = toString(saveCooks);
                }
            }

        }
        if ((authToken != null) && (authToken.length() > 0)) {
            Log.v(TAG, "Successful authentication");
            return authToken;
        } else {
            Log.e(TAG, "Error authenticating" + resp.getStatusLine());
            return null;
        }
    } catch (final IOException e) {
        Log.e(TAG, "IOException when getting authtoken", e);
        return null;
    } finally {
        Log.v(TAG, "getAuthtoken completing");
    }
}

From source file:com.cbmapi.CbmAPI.java

public static String searchCpuByName(String cpuName) {
    String encodedName = encodeToUrl(cpuName);
    Document html = null;
    String url = null;/*  w w w  .j  a  va2 s . c o m*/
    try {
        //Connects to zoom's search engine and looks for given cpu from benhmarks section.
        html = Jsoup.connect("https://www.passmark.com/search/zoomsearch.php?zoom_sort=0&zoom_query="
                + encodedName + "&zoom_cat%5B%5D=5").get();
    } catch (IOException e) {
        System.out.println("Connection throws an exception: " + e);
    }

    //Regex check is used to validate correct search result.
    if (html != null) {
        Elements links = html.select("div.results");
        links = links.select("a[href~=^(https?:\\/\\/www.cpubenchmark.net/cpu.php\\?)]");
        url = links.attr("href");
        if (url.isEmpty()) {
            return "No results found for: " + cpuName;
        }
    } //message for connection issues.
    else {
        return "Connection to the search engine failed.";
    }
    return url;
}

From source file:net.slkdev.swagger.confluence.service.impl.XHtmlToConfluenceServiceImpl.java

private static void reformatXHtmlBreakAfterElements(final Document document, final String elements) {
    document.select(elements).after("<br />");
}

From source file:com.amazonaws.eclipse.core.diagnostic.utils.AwsPortalFeedbackFormUtils.java

/**
 * Returns the value of 'authenticity_token' by requesting the form page
 * from 'aws.amazon.com/forms'.//from  w  w w.  j a  v a  2s.c  om
 *
 * @param httpClient
 *            The http-client instance to use when sending the GET request.
 */
private static String getFreshAuthenticityToken(final HttpClient httpClient) {
    Document freshForm;
    try {
        HttpGet getForm = new HttpGet(FORM_URL);

        HttpResponse response = httpClient.execute(getForm);
        String formPageContent = IOUtils.toString(response.getEntity().getContent());

        freshForm = Jsoup.parse(formPageContent);
    } catch (IOException ioe) {
        throw new AmazonClientException("Cannot get the form page from " + FORM_URL, ioe);
    }

    for (Element formInput : freshForm.select(AWS_FORM_INPUT_SELECTOR)) {
        if (formInput.attr("name").equals(AUTHENTICITY_TOKEN)) {
            return formInput.attr("value");
        }
    }

    throw new AmazonClientException("Failed to extract " + AUTHENTICITY_TOKEN + " from " + FORM_URL);
}

From source file:io.andyc.papercut.api.PrintApi.java

/**
 * Extracts the form data required to submit to the print service in order
 * to set the printer to print to/*from   w w w  .  ja  va 2  s .c  o m*/
 *
 * @param printerType {Document} - the Document to parse and extract the
 *                    printer type form
 * @param printJob    {PrintJob} - the print job to be worked on
 *
 * @return {Map<String, String>} - the resulting form data to submit to the
 * print service
 */
static Map<String, String> buildPrinterTypeData(Document printerType, PrintJob printJob) {
    Elements printerTypeElements = printerType.select("form#form").select("input");
    Map<String, String> result = new HashMap<>();
    for (Element element : printerTypeElements) {
        String key = element.attr("name");
        String value = element.attr("value");
        if (Objects.equals(key, "$Submit$0")) {
            continue;
        }
        if (key.equals("$RadioGroup")) {
            value = printJob.getPrinterOption().getValue();

        }
        result.put(key, value);
    }

    return result;
}