Example usage for org.jsoup.nodes Element toString

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Gets this node's outer HTML.

Usage

From source file:Main.java

public static void main(String[] args) {
    String html = "<!html>" + "<html><body>" + "<span id='my'>" + "<span class=''contentTitle'>Director:</span>"
            + "<span>test</span></span>" + "</body></html>";

    Document doc = Jsoup.parse(html);
    System.out.println(doc.toString());

    Elements spanWithId = doc.select("span#my");

    if (spanWithId != null) {
        System.out.printf("Found %d Elements\n", spanWithId.size());

        if (!spanWithId.isEmpty()) {
            Iterator<Element> it = spanWithId.iterator();
            Element element = null;
            while (it.hasNext()) {
                element = it.next();/*ww  w .  jav  a2s .  c om*/
                System.out.println(element.toString());
            }
        }
    }
}

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

public static void getTestDescription(String path) {
    Document htmlFile = null;/*from  w  ww. jav a  2 s  . c  o m*/
    try {
        htmlFile = Jsoup.parse(new File(basepath + path), "UTF-8");
    } catch (IOException e) {
        System.out.println("Exception in parse Current Run html file" + e.getMessage());
    }

    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");

            for (int i = 0; i < tds.size(); i++) {
                Element link = tds.get(i);
                String link_temp = link.toString();

                if (i == 1) {
                    //   System.out.println("data" + link_temp);
                    if (!TestCaseDesMap.containsKey(path)) {
                        TestCaseDesMap.put(path, Jsoup.parse(link_temp).text());
                    }
                    break;
                }
            }

        }
    }

}

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

public static void replaceDetailsTable(String path) throws IOException {

    File source = new File(path);
    Document report = null;/*from   w w  w . j a  v  a2  s  .com*/
    try {
        report = Jsoup.parse(source, "UTF-8");
    } catch (IOException e) {
        System.out.println("Unable to open [" + source.getAbsolutePath() + "] for parsing!");
    }
    Elements dom = report.children();
    Elements tds = report.select("table[id=tableStyle] td"); // select the tds from your table
    String temp_key = "";
    for (Element td : tds) { // loop through them

        String[] temp_ar = td.toString().split("\"");
        String Key = temp_ar[1];
        String Status = "";

        if (td.toString().contains("pass.png")) {
            Status = "pass";
        }
        if (td.toString().contains("fail.png")) {
            Status = "fail";
        }
        if (td.toString().contains("skip.png")) {
            Status = "skip";
        }

        if (TestCaseDesMap.containsKey(temp_key) && Status.length() > 1) {
            TestcaseStatusMap.put(temp_key, Status);
            temp_key = "";
        }

        if (td.text().contains("Test Method")) { // found the one you want
            String TestcaseDes;
            if (!TestCaseDesMap.containsKey(Key)) {
                TestcaseDes = "  ---------       ";
                TestCaseDesMap.put(Key, TestcaseDes);
                temp_key = Key;

            } else {
                TestcaseDes = TestCaseDesMap.get(Key);
                temp_key = Key;
                // TestcaseStatusMap.put(Key, Status);
            }
            td.text(TestcaseDes);
            // Replace with your text
        }
    }

    Elements ths = report.select("table[id=tableStyle] th"); // select the tds from your table
    for (Element th : ths) { // loop through them

        if (th.text().contains("Method Type")) { // found the one you want
            th.text("TestCase Description");

        }
        if (th.text().contains("Test Case Name")) { // found the one you want
            th.text("Testng Method");

        }
    }

    if (!source.canWrite()) {
        System.out.println("Can't write this file!");//Just check if the file is writable or not
    }
    BufferedWriter bw = new BufferedWriter(new FileWriter(source));
    bw.write(dom.toString()); //toString will give all the elements as a big string
    bw.close(); //Close to apply the changes
    //  genarateFailureReport(new File("C:\\Users\\yanamalp\\Desktop\\Gen_jelly\\HTML_Design_Files\\CSS\\HtmlReport.html"), "c:\\");

}

From source file:com.shareplaylearn.OauthPasswordFlow.java

public static LoginInfo googleLogin(String username, String password, String clientId, String callbackUri)
        throws URISyntaxException, IOException, AuthorizationException, UnauthorizedException {

    CloseableHttpClient httpClient = HttpClients.custom().build();
    String oAuthQuery = "client_id=" + clientId + "&";
    oAuthQuery += "response_type=code&";
    oAuthQuery += "scope=openid email&";
    oAuthQuery += "redirect_uri=" + callbackUri;
    URI oAuthUrl = new URI("https", null, "accounts.google.com", 443, "/o/oauth2/auth", oAuthQuery, null);
    Connection oauthGetCoonnection = Jsoup.connect(oAuthUrl.toString());
    Connection.Response oauthResponse = oauthGetCoonnection.method(Connection.Method.GET).execute();
    if (oauthResponse.statusCode() != 200) {
        String errorMessage = "Error contacting Google's oauth endpoint: " + oauthResponse.statusCode() + " / "
                + oauthResponse.statusMessage();
        if (oauthResponse.body() != null) {
            errorMessage += oauthResponse.body();
        }//from  ww  w . ja va2s. co m
        throw new AuthorizationException(errorMessage);
    }
    Map<String, String> oauthCookies = oauthResponse.cookies();
    Document oauthPage = oauthResponse.parse();
    Element oauthForm = oauthPage.getElementById("gaia_loginform");
    System.out.println(oauthForm.toString());
    Connection oauthPostConnection = Jsoup.connect("https://accounts.google.com/ServiceLoginAuth");
    HashMap<String, String> formParams = new HashMap<>();

    for (Element child : oauthForm.children()) {
        System.out.println("Tag name: " + child.tagName());
        System.out.println("attrs: " + Arrays.toString(child.attributes().asList().toArray()));
        if (child.tagName().equals("input") && child.hasAttr("name")) {

            String keyName = child.attr("name");
            String keyValue = null;

            if (child.hasAttr("value")) {
                keyValue = child.attr("value");
            }

            if (keyName != null && keyName.trim().length() != 0 && keyValue != null
                    && keyValue.trim().length() != 0) {
                oauthPostConnection.data(keyName, keyValue);
                formParams.put(keyName, keyValue);
            }
        }
    }
    oauthPostConnection.cookies(oauthCookies);
    formParams.put("Email", username);
    formParams.put("Passwd-hidden", password);
    //oauthPostConnection.followRedirects(false);
    System.out.println("form post params were: ");
    for (Map.Entry<String, String> kvp : formParams.entrySet()) {
        //DO NOT let passwords end up in the logs ;)
        if (kvp.getKey().equals("Passwd")) {
            continue;
        }
        System.out.println(kvp.getKey() + "," + kvp.getValue());
    }
    System.out.println("form cookies were: ");
    for (Map.Entry<String, String> cookie : oauthCookies.entrySet()) {
        System.out.println(cookie.getKey() + "," + cookie.getValue());
    }
    //System.exit(0);
    Connection.Response postResponse = null;
    try {
        postResponse = oauthPostConnection.method(Connection.Method.POST).timeout(5000).execute();
    } catch (Throwable t) {
        System.out.println("Failed to post login information to googles endpoint :/ " + t.getMessage());
        System.out.println("This usually means the connection is bad, shareplaylearn.com is down, or "
                + " google is being a punk - login manually and check.");
        assertTrue(false);
    }
    if (postResponse.statusCode() != 200) {
        String errorMessage = "Failed to validate credentials: " + oauthResponse.statusCode() + " / "
                + oauthResponse.statusMessage();
        if (oauthResponse.body() != null) {
            errorMessage += oauthResponse.body();
        }
        throw new UnauthorizedException(errorMessage);
    }
    System.out.println("Response headers (after post to google form & following redirect):");
    for (Map.Entry<String, String> header : postResponse.headers().entrySet()) {
        System.out.println(header.getKey() + "," + header.getValue());
    }
    System.out.println("Final response url was: " + postResponse.url().toString());
    String[] args = postResponse.url().toString().split("&");
    LoginInfo loginInfo = new LoginInfo();
    for (String arg : args) {
        if (arg.startsWith("access_token")) {
            loginInfo.accessToken = arg.split("=")[1].trim();
        } else if (arg.startsWith("id_token")) {
            loginInfo.idToken = arg.split("=")[1].trim();
        } else if (arg.startsWith("expires_in")) {
            loginInfo.expiry = arg.split("=")[1].trim();
        }
    }

    //Google doesn't actually throw a 401 or anything - it just doesn't redirect
    //and sends you back to it's login page to try again.
    //So this is what happens with an invalid password.
    if (loginInfo.accessToken == null || loginInfo.idToken == null) {
        //Document oauthPostResponse = postResponse.parse();
        //System.out.println("*** Oauth response from google *** ");
        //System.out.println(oauthPostResponse.toString());
        throw new UnauthorizedException(
                "Error retrieving authorization: did you use the correct username/password?");
    }
    String[] idTokenFields = loginInfo.idToken.split("\\.");
    if (idTokenFields.length < 3) {
        throw new AuthorizationException("Error parsing id token " + loginInfo.idToken + "\n" + "it only had "
                + idTokenFields.length + " field!");
    }
    String jwtBody = new String(Base64.decodeBase64(idTokenFields[1]), StandardCharsets.UTF_8);
    loginInfo.idTokenBody = new Gson().fromJson(jwtBody, OauthJwt.class);
    loginInfo.id = loginInfo.idTokenBody.sub;
    return loginInfo;
}

From source file:com.shareplaylearn.utilities.OauthPasswordFlow.java

public static LoginInfo googleLogin(String username, String password, String clientId, String callbackUri)
        throws URISyntaxException, IOException, AuthorizationException, UnauthorizedException {

    CloseableHttpClient httpClient = HttpClients.custom().build();
    String oAuthQuery = "client_id=" + clientId + "&";
    oAuthQuery += "response_type=code&";
    oAuthQuery += "scope=openid email&";
    oAuthQuery += "redirect_uri=" + callbackUri;
    URI oAuthUrl = new URI("https", null, "accounts.google.com", 443, "/o/oauth2/auth", oAuthQuery, null);
    Connection oauthGetCoonnection = Jsoup.connect(oAuthUrl.toString());
    Connection.Response oauthResponse = oauthGetCoonnection.method(Connection.Method.GET).execute();
    if (oauthResponse.statusCode() != 200) {
        String errorMessage = "Error contacting Google's oauth endpoint: " + oauthResponse.statusCode() + " / "
                + oauthResponse.statusMessage();
        if (oauthResponse.body() != null) {
            errorMessage += oauthResponse.body();
        }//from w w w  . java  2 s. com
        throw new AuthorizationException(errorMessage);
    }
    Map<String, String> oauthCookies = oauthResponse.cookies();
    Document oauthPage = oauthResponse.parse();
    Element oauthForm = oauthPage.getElementById("gaia_loginform");
    System.out.println(oauthForm.toString());
    Connection oauthPostConnection = Jsoup.connect("https://accounts.google.com/ServiceLoginAuth");
    HashMap<String, String> formParams = new HashMap<>();
    for (Element child : oauthForm.children()) {
        if (child.tagName().equals("input") && child.hasAttr("name")) {

            String keyName = child.attr("name");
            String keyValue = null;

            if (keyName.equals("Email")) {
                keyValue = username;
            } else if (keyName.equals("Passwd")) {
                keyValue = password;
            } else if (child.hasAttr("value")) {
                keyValue = child.attr("value");
            }

            if (keyValue != null) {
                oauthPostConnection.data(keyName, keyValue);
                formParams.put(keyName, keyValue);
            }
        }
    }
    oauthPostConnection.cookies(oauthCookies);
    //oauthPostConnection.followRedirects(false);
    System.out.println("form post params were: ");
    for (Map.Entry<String, String> kvp : formParams.entrySet()) {
        //DO NOT let passwords end up in the logs ;)
        if (kvp.getKey().equals("Passwd")) {
            continue;
        }
        System.out.println(kvp.getKey() + "," + kvp.getValue());
    }
    System.out.println("form cookies were: ");
    for (Map.Entry<String, String> cookie : oauthCookies.entrySet()) {
        System.out.println(cookie.getKey() + "," + cookie.getValue());
    }
    Connection.Response postResponse = null;
    try {
        postResponse = oauthPostConnection.method(Connection.Method.POST).timeout(5000).execute();
    } catch (Throwable t) {
        System.out.println("Failed to post login information to googles endpoint :/ " + t.getMessage());
        System.out.println("This usually means the connection is bad, shareplaylearn.com is down, or "
                + " google is being a punk - login manually and check.");
        assertTrue(false);
    }
    if (postResponse.statusCode() != 200) {
        String errorMessage = "Failed to validate credentials: " + oauthResponse.statusCode() + " / "
                + oauthResponse.statusMessage();
        if (oauthResponse.body() != null) {
            errorMessage += oauthResponse.body();
        }
        throw new UnauthorizedException(errorMessage);
    }
    System.out.println("Response headers (after post to google form & following redirect):");
    for (Map.Entry<String, String> header : postResponse.headers().entrySet()) {
        System.out.println(header.getKey() + "," + header.getValue());
    }
    System.out.println("Final response url was: " + postResponse.url().toString());
    String[] args = postResponse.url().toString().split("&");
    LoginInfo loginInfo = new LoginInfo();
    for (String arg : args) {
        if (arg.startsWith("access_token")) {
            loginInfo.accessToken = arg.split("=")[1].trim();
        } else if (arg.startsWith("id_token")) {
            loginInfo.idToken = arg.split("=")[1].trim();
        } else if (arg.startsWith("expires_in")) {
            loginInfo.expiry = arg.split("=")[1].trim();
        }
    }

    //Google doesn't actually throw a 401 or anything - it just doesn't redirect
    //and sends you back to it's login page to try again.
    //So this is what happens with an invalid password.
    if (loginInfo.accessToken == null || loginInfo.idToken == null) {
        //Document oauthPostResponse = postResponse.parse();
        //System.out.println("*** Oauth response from google *** ");
        //System.out.println(oauthPostResponse.toString());
        throw new UnauthorizedException(
                "Error retrieving authorization: did you use the correct username/password?");
    }
    String[] idTokenFields = loginInfo.idToken.split("\\.");
    if (idTokenFields.length < 3) {
        throw new AuthorizationException("Error parsing id token " + loginInfo.idToken + "\n" + "it only had "
                + idTokenFields.length + " field!");
    }
    String jwtBody = new String(Base64.decodeBase64(idTokenFields[1]), StandardCharsets.UTF_8);
    loginInfo.idTokenBody = new Gson().fromJson(jwtBody, OauthJwt.class);
    loginInfo.id = loginInfo.idTokenBody.sub;
    return loginInfo;
}

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

public static String getCountsSuiteswise(String path) {
    Document htmlFile = null;/*from  ww  w .  j  a  v a  2  s  .c o  m*/
    try {
        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:hello.Scraper.java

@Filter(inputChannel = "channel2", outputChannel = "channel3")
public boolean filter(Element payload) {
    Matcher m = patter.matcher(payload.toString());
    return m.find();
}

From source file:alliance.docs.DocumentationTest.java

@Test
public void testBrokenAnchorsPresent() throws IOException, URISyntaxException {
    List<Path> docs = Files.list(getPath()).filter(f -> f.toString().endsWith(HTML_DIRECTORY))
            .collect(Collectors.toList());

    Set<String> links = new HashSet<>();
    Set<String> anchors = new HashSet<>();

    for (Path path : docs) {
        Document doc = Jsoup.parse(path.toFile(), "UTF-8", EMPTY_STRING);

        String thisDoc = StringUtils.substringAfterLast(path.toString(), File.separator);
        Elements elements = doc.body().getAllElements();
        for (Element element : elements) {
            if (!element.toString().contains(":")
                    && StringUtils.substringBetween(element.toString(), HREF_ANCHOR, CLOSE) != null) {
                links.add(thisDoc + "#" + StringUtils.substringBetween(element.toString(), HREF_ANCHOR, CLOSE));
            }/*from   w w w.  jav  a2 s.  c o  m*/

            anchors.add(thisDoc + "#" + StringUtils.substringBetween(element.toString(), ID, CLOSE));
        }
    }
    links.removeAll(anchors);
    assertThat("Anchors missing section reference: " + links.toString(), links.isEmpty());
}

From source file:com.abixen.platform.core.service.impl.LayoutServiceImpl.java

@Override
public String htmlLayoutToJson(String htmlString) {

    log.debug("htmlLayoutToJson() - htmlString: " + htmlString);

    Document doc = Jsoup.parse(htmlString);
    Elements htmlRows = doc.getElementsByClass("row");
    List<LayoutRowUtil> rowUtilList = new ArrayList<>();

    for (Element row : htmlRows) {

        Document rowDoc = Jsoup.parse(row.toString());
        Elements htmlColumns = rowDoc.getElementsByClass("column");
        List<LayoutColumnUtil> columnUtilList = new ArrayList<>();

        for (Element column : htmlColumns) {
            String styleClass = column.attr("class");
            columnUtilList.add(new LayoutColumnUtil(styleClass.substring(styleClass.indexOf(" ") + 1)));
        }// w  w  w .  j ava 2 s  .co  m

        rowUtilList.add(new LayoutRowUtil(columnUtilList));
    }

    return "{\"rows\":" + new Gson().toJson(rowUtilList) + "}";
}

From source file:jobhunter.dice.Client.java

public Job execute() throws IOException, URISyntaxException {
    l.debug("Connecting to {}", url);

    update("Connecting", 1L);
    final Document doc = Jsoup.connect(url).get();

    update("Parsing HTML", 2L);
    final Job job = Job.of();
    job.setPortal(DicePlugin.portal);//  ww  w  . j  a  v  a2  s .  com
    job.setLink(url);

    StringBuilder description = new StringBuilder();

    for (Element meta : doc.getElementsByTag("meta")) {
        l.debug("Checking {}", meta.toString());
        if (meta.attr("name").equals("twitter:text:job_title"))
            job.setPosition(meta.attr("content"));

        if (meta.attr("name").equals("twitter:text:company"))
            job.getCompany().setName(meta.attr("content"));

        if (meta.attr("name").equals("twitter:text:city"))
            job.setAddress(meta.attr("content"));

        if (meta.attr("name").equals("twitter:text:salary"))
            job.setSalary(meta.attr("content"));

        if (meta.attr("name").equals("twitter:text:job_description_web")) {
            description.append(StringEscapeUtils.unescapeHtml4(meta.attr("content")));
        }

        if (meta.attr("name").equals("twitter:text:skills")) {
            description.append(StringEscapeUtils.unescapeHtml4(meta.attr("content")));
        }
    }

    job.setDescription(description.toString());

    update("Done", 3L);
    return job;
}