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:gov.medicaid.screening.dao.impl.DentistryLicenseDAOBean.java

/**
 * Performs a search for all possible results.
 *
 * @param criteria The search criteria.//from  w w w . java 2  s  .  co  m
 * @return the search result for licenses
 *
 * @throws URISyntaxException if an error occurs while building the URL.
 * @throws ClientProtocolException if client does not support protocol used.
 * @throws IOException if an error occurs while parsing response.
 * @throws ParseException if an error occurs while parsing response.
 * @throws ServiceException for any other problems encountered
 */
private SearchResult<License> getAllResults(DentistryLicenseSearchCriteria criteria)
        throws URISyntaxException, ClientProtocolException, IOException, ParseException, ServiceException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new LaxRedirectStrategy());

    HttpGet getSearch = new HttpGet(new URIBuilder(getSearchURL()).build());
    HttpResponse response = client.execute(getSearch);

    verifyAndAuditCall(getSearchURL(), response);

    Document page = Jsoup.parse(EntityUtils.toString(response.getEntity()));
    HttpPost search = new HttpPost(new URIBuilder(getSearchURL()).build());
    List<License> allLicenses = new ArrayList<License>();

    HttpEntity entity = postForm(getSearchURL(), client, search,
            new String[][] { { "vFirstName", Util.defaultString(criteria.getFirstName()) },
                    { "vMiddleName", Util.defaultString(criteria.getMiddleName()) },
                    { "vLastName", Util.defaultString(criteria.getLastName()) },
                    { "vLicenseNumber", Util.defaultString(criteria.getIdentifier()) },
                    { "vCity", Util.defaultString(criteria.getCity()) },
                    { "vLicenseType", criteria.getLicenseType().getName() }, { "ObjectTypeID", "1059" },
                    { "ObjectID", "40" },
                    { "__VIEWSTATE", page.select("input[name=__VIEWSTATE]").first().val() },
                    { "__EVENTVALIDATION", page.select("input[name=__EVENTVALIDATION]").first().val() } },
            true);

    page = Jsoup.parse(EntityUtils.toString(entity));

    Elements detailButtons = page.select("table#DataTable table input[type=button]");
    for (Element detailButton : detailButtons) {
        String url = detailButton.attr("onclick");
        url = url.replaceFirst("javascript:window.location='./", "");
        url = url.substring(0, url.length() - 2);
        url = url.replaceAll(" ", "+"); // replace spaces

        HttpGet getDetail = new HttpGet(Util.replaceLastURLPart(getSearchURL(), url));
        response = client.execute(getDetail);
        verifyAndAuditCall(getSearchURL(), response);
        Document licenseDetails = Jsoup.parse(EntityUtils.toString(response.getEntity()));
        allLicenses.add(parseLicense(licenseDetails));
    }

    SearchResult<License> searchResult = new SearchResult<License>();
    searchResult.setItems(allLicenses);
    return searchResult;
}

From source file:net.liuxuan.Tools.signup.SignupV2ex.java

public void getLoginForm() throws IOException {

    HttpGet httpget = new HttpGet("http://v2ex.com/signin");
    CloseableHttpResponse response1 = httpclient.execute(httpget);
    try {//w  w w. ja  va 2  s .  c om
        HttpEntity entity = response1.getEntity();
        //?once
        String content = EntityUtils.toString(entity);
        //                System.out.println(content);
        System.out.println("--------------");
        System.out.println("--------------");
        Document doc = Jsoup.parse(content);
        //                Elements inputs = doc.select("input[type=text]");
        Elements inputs = doc.select("input[type=hidden]");
        for (int i = 0; i < inputs.size(); i++) {
            Element element = inputs.get(i);
            params.add(new BasicNameValuePair(element.attr("name"), element.attr("value")));
            //                    params.put(element.attr("name"), element.attr("value"));
            System.out.println(element.toString());
            System.out.println(element.attr("name"));
            System.out.println(element.attr("value"));

        }

        System.out.println("--------------");
        System.out.println("--------------");

        System.out.println("--------------");
        System.out.println("--------------");
        System.out.println("Login form get: " + response1.getStatusLine());
        EntityUtils.consume(entity);

        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = cookieStore.getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
    } finally {
        response1.close();
    }

    //            HttpUriRequest login = RequestBuilder.post()
    //                    .setUri(new URI("http://v2ex.com/signin"))
    //                    .addParameter("u", "mosliu")
    //                    .addParameter("p", "mosesmoses")
    //                    .build();
    //            CloseableHttpResponse response2 = httpclient.execute(login);
    //            try {
    //                HttpEntity entity = response2.getEntity();
    //
    //                System.out.println("Login form get: " + response2.getStatusLine());
    //                
    //                EntityUtils.consume(entity);
    //
    //                System.out.println("Post logon cookies:");
    //                List<Cookie> cookies = cookieStore.getCookies();
    //                if (cookies.isEmpty()) {
    //                    System.out.println("None");
    //                } else {
    //                    for (int i = 0; i < cookies.size(); i++) {
    //                        System.out.println("- " + cookies.get(i).toString());
    //                    }
    //                }
    //                
    //                
    //                
    //            } finally {
    //                response2.close();
    //            }
    //            
    //            
    //            httpget = new HttpGet("http://v2ex.com/signin");
    //            response1 = httpclient.execute(httpget);
    //            try {
    //                HttpEntity entity = response1.getEntity();
    //                String content = EntityUtils.toString(entity);
    //                System.out.println("-----------------content---------------------");
    //                System.out.println(content);
    //                
    //                EntityUtils.consume(entity);
    //            } finally {
    //                response1.close();
    //            }
    //            
    //            
}

From source file:gov.medicaid.screening.dao.impl.OptometryLicenseDAOBean.java

/**
 * Performs a search for all possible results.
 *
 * @param identifier The value to be searched.
 * @return the search result for licenses
 * @throws URISyntaxException When an error occurs while building the URL.
 * @throws ClientProtocolException When client does not support protocol used.
 * @throws IOException When an error occurs while parsing response.
 * @throws ParseException When an error occurs while parsing response.
 * @throws PersistenceException for database related errors
 * @throws ServiceException for any other problems encountered
 *///from  w w w  . j a va 2  s .  com
private SearchResult<License> getAllResults(String identifier) throws URISyntaxException,
        ClientProtocolException, IOException, ParseException, PersistenceException, ServiceException {
    DefaultHttpClient client = new DefaultHttpClient();
    URIBuilder builder = new URIBuilder(getSearchURL()).setPath("/Default.aspx");
    String hostId = builder.build().toString();
    builder.setParameter("tabid", "799");

    HttpGet httpget = new HttpGet(builder.build());
    HttpResponse landing = client.execute(httpget);
    Document document = Jsoup.parse(EntityUtils.toString(landing.getEntity()));

    HttpPost httppost = new HttpPost(builder.build());
    HttpEntity entity = postForm(hostId, client, httppost,
            new String[][] { { "_ctl0:_ctl1:_ctl0:txtCriteria", identifier },
                    { "_ctl0:_ctl1:_ctl0:btnSubmit", "Search" }, { "__EVENTTARGET", "" },
                    { "__EVENTARGUMENT", "" },
                    { "__VIEWSTATE", document.select("#Form input[name=__VIEWSTATE]").first().val() } },
            true);

    // licenses list
    List<License> licenseList = new ArrayList<License>();
    while (entity != null) {
        String result = EntityUtils.toString(entity);
        document = Jsoup.parse(result);

        Elements trs = document.select("table.Datagrid tr");
        if (trs != null) {
            for (Element element : trs) {
                String cssClass = element.attr("class");
                if (!"DatagridHeaderStyle".equals(cssClass.trim()) && element.children().size() == 8) {
                    Elements tds = element.children();
                    licenseList.add(parseLicense(tds));
                }
            }
        }

        // done, check if there are additional results
        entity = null;
        Elements elements = document.getElementsByTag("a");
        for (Element element : elements) {
            if (element.text().equals("Next >>")) {
                entity = postForm(hostId, client, httppost,
                        new String[][] { { "_ctl0:_ctl1:_ctl0:txtCriteria", identifier },
                                { "__EVENTTARGET", "_ctl0:_ctl1:_ctl0:dgrdLicensee:_ctl29:_ctl1" },
                                { "__EVENTARGUMENT", "" },
                                { "__VIEWSTATE",
                                        document.select("#Form input[name=__VIEWSTATE]").first().val() } },
                        true);
                break;
            }
        }
    }

    SearchResult<License> result = new SearchResult<License>();
    result.setItems(licenseList);
    return result;
}

From source file:com.astamuse.asta4d.web.form.field.impl.AbstractRadioAndCheckboxRenderer.java

@Override
public Renderer renderForEdit(String editTargetSelector, Object value) {
    final List<String> valueList = convertValueToList(value);
    Renderer renderer = Renderer.create("input", "checked", Clear);
    // we have to iterate the elements because the attr selector would not work for blank values.
    renderer.add("input", new ElementSetter() {
        @Override/*  ww w . ja  va 2  s  .c o m*/
        public void set(Element elem) {
            String val = elem.attr("value");
            if (valueList.contains(val)) {
                elem.attr("checked", "");
            }
        }
    });
    return Renderer.create(editTargetSelector, renderer);
}

From source file:org.jasig.portlet.proxy.service.proxy.document.URLRewritingFilter.java

protected void updateUrls(final Document document, final IContentResponse proxyResponse,
        final Map<String, Set<String>> elementSet, final RenderRequest request, final RenderResponse response,
        boolean action) {

    // attempt to retrieve the list of rewritten URLs from the session
    final PortletSession session = request.getPortletSession();
    ConcurrentMap<String, String> rewrittenUrls;
    synchronized (PortletUtils.getSessionMutex(session)) {
        rewrittenUrls = (ConcurrentMap<String, String>) session.getAttribute(REWRITTEN_URLS_KEY);

        // if the rewritten URLs list doesn't exist yet, create it
        if (rewrittenUrls == null) {
            rewrittenUrls = new ConcurrentHashMap<String, String>();
            session.setAttribute(REWRITTEN_URLS_KEY, rewrittenUrls);
        }/*w w  w  .  ja va2 s. c o m*/
    }

    // get the list of configured whitelist regexes
    final PortletPreferences preferences = request.getPreferences();
    final String[] whitelistRegexes = preferences.getValues("whitelistRegexes", new String[] {});

    // If we're proxying a remote website (as opposed to a local file system 
    // resources, we'll need to transform any relative URLs.  To do this,
    // we first compute the base and relative URLs for the page.
    String baseUrl = null;
    String relativeUrl = null;
    try {
        baseUrl = getBaseServerUrl(proxyResponse.getProxiedLocation());
        relativeUrl = getRelativePathUrl(proxyResponse.getProxiedLocation());
        LOG.trace("Computed base url {} and relative url {} for proxied url {}", baseUrl, relativeUrl,
                proxyResponse.getProxiedLocation());
    } catch (URISyntaxException e) {
        LOG.error(e.getMessage(), e);
    }

    for (final Map.Entry<String, Set<String>> elementEntry : elementSet.entrySet()) {
        for (final String attributeName : elementEntry.getValue()) {

            // get a list of elements for this element type and iterate through
            // them, updating the relevant URL attribute
            final Elements elements = document.getElementsByTag(elementEntry.getKey());
            for (Element element : elements) {

                String attributeUrl = element.attr(attributeName);
                LOG.trace("Considering element {}  with URL attribute {} of value {}", element, attributeName,
                        attributeUrl);

                // don't adjust or filter javascript url targets
                if (StringUtils.isNotBlank(attributeUrl) && !attributeUrl.startsWith(JAVASCRIPT_PREFIX)
                        && !attributeUrl.startsWith(JAVASCRIPT_PREFIX.toLowerCase())) {

                    // if we're proxying a remote website, adjust any 
                    // relative URLs into absolute URLs
                    if (baseUrl != null) {

                        // (1) do not prefix absolute URLs
                        if (attributeUrl.contains("://") || attributeUrl.startsWith("//")) {
                            // do nothing...
                        }

                        // (2) if the URL is relative to the server base,
                        // prepend the base URL
                        else if (attributeUrl.startsWith("/")) {
                            attributeUrl = baseUrl.concat(attributeUrl);
                        }

                        // (3) otherwise use the full relative path
                        else {
                            attributeUrl = relativeUrl.concat(attributeUrl);
                        }

                    }

                    // if this URL matches our whitelist regex, rewrite it 
                    // to pass through this portlet
                    for (String regex : whitelistRegexes) {

                        if (StringUtils.isNotBlank(regex)) {
                            final Pattern pattern = Pattern.compile(regex); // TODO share compiled regexes
                            if (pattern.matcher(attributeUrl).find()) {

                                // record that we've rewritten this URL
                                rewrittenUrls.put(attributeUrl, attributeUrl);

                                // TODO: the value in the rewritten URLs map needs to 
                                // be a resource URL.  we also want to key URLs by a short
                                // string rather than the full URL

                                if (elementEntry.getKey().equals("form")) {
                                    // the form action needs to be set to POST to
                                    // properly pass through our portlet
                                    boolean isPost = "POST".equalsIgnoreCase(element.attr("method"));
                                    if (!isPost) {
                                        element.attr("method", "POST");
                                    }
                                    attributeUrl = createFormUrl(response, isPost, attributeUrl);
                                }

                                else if (action) {
                                    attributeUrl = createActionUrl(response, attributeUrl);
                                }

                                else {
                                    attributeUrl = createResourceUrl(response, attributeUrl);
                                }
                            }
                        }
                    }

                }

                element.attr(attributeName, attributeUrl.replace("&amp;", "&"));

            }

        }

    }

}

From source file:accountgen.controller.Controller.java

private void setGender(Document doc, Person p) {
    Element gen = doc.select(".bcs").first().select(".content").first().select("img").first();
    String g = gen.attr("alt");
    p.setGender(g);//from w  ww  .jav a2 s.co m
}

From source file:gov.medicaid.screening.dao.impl.BusinessLienDAOBean.java

/**
 * Performs a search for all possible results.
 *
 * @param criteria The search criteria.//www .j  a  v  a2 s .  c  o m
 * @return the search result for licenses
 *
 * @throws URISyntaxException if an error occurs while building the URL.
 * @throws ClientProtocolException if client does not support protocol used.
 * @throws IOException if an error occurs while parsing response.
 * @throws ParseException if an error occurs while parsing response.
 * @throws ServiceException for any other problems encountered
 */
private SearchResult<ProviderProfile> getAllResults(BusinessLienSearchCriteria criteria)
        throws URISyntaxException, ClientProtocolException, IOException, ParseException, ServiceException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new LaxRedirectStrategy());

    HttpGet getSearch = new HttpGet(new URIBuilder(getSearchURL()).build());
    // to get the cookie
    HttpResponse response = client.execute(getSearch);

    verifyAndAuditCall(getSearchURL(), response);

    Document page = Jsoup.parse(EntityUtils.toString(response.getEntity()));

    String fullSearchURL = getSearchURL() + "/Business/Search";
    HttpPost search = new HttpPost(new URIBuilder(fullSearchURL).build());
    List<ProviderProfile> allProfiles = new ArrayList<ProviderProfile>();

    HttpEntity entity = postForm(fullSearchURL, client, search,
            new String[][] { { "BusinessName", Util.defaultString(criteria.getBusinessName()) },
                    { "FileNumber", Util.defaultString(criteria.getFileNumber()) },
                    { "Status", criteria.getFilingStatus() == FilingStatus.INACTIVE ? "Inactive" : "Active" },
                    { "Type", criteria.getScope() == SearchScope.CONTAINS ? "Contains" : "BeginsWith" },
                    { "submit", "Search" } },
            true);

    page = Jsoup.parse(EntityUtils.toString(entity));

    Elements detailLinks = page.select("table.results tr td a");
    for (Element detailLink : detailLinks) {
        String href = detailLink.attr("href");

        String detailUrl = getSearchURL() + href;
        HttpGet getDetail = new HttpGet(detailUrl);
        response = client.execute(getDetail);
        verifyAndAuditCall(detailUrl, response);
        Document profileDetails = Jsoup.parse(EntityUtils.toString(response.getEntity()));
        allProfiles.add(parseProfile(profileDetails));
    }

    SearchResult<ProviderProfile> searchResult = new SearchResult<ProviderProfile>();
    searchResult.setItems(allProfiles);
    return searchResult;
}

From source file:edu.usu.sdl.openstorefront.report.ExternalLinkValidationReport.java

@Override
protected void gatherData() {
    ComponentResource componentResourceExample = new ComponentResource();
    componentResourceExample.setActiveStatus(ComponentResource.ACTIVE_STATUS);
    List<ComponentResource> componentResources = service.getPersistenceService()
            .queryByExample(ComponentResource.class, componentResourceExample);
    Map<String, List<ComponentResource>> resourceMap = new HashMap<>();
    componentResources.forEach(resource -> {
        if (resourceMap.containsKey(resource.getComponentId())) {
            resourceMap.get(resource.getComponentId()).add(resource);
        } else {//  ww  w .  ja  va2 s  .co  m
            List<ComponentResource> resources = new ArrayList<>();
            resources.add(resource);
            resourceMap.put(resource.getComponentId(), resources);
        }
    });

    Component componentExample = new Component();
    componentExample.setActiveStatus(Component.ACTIVE_STATUS);
    componentExample.setApprovalState(ApprovalStatus.APPROVED);
    List<Component> components = service.getPersistenceService().queryByExample(Component.class,
            componentExample);

    Map<String, Component> componentMap = new HashMap<>();
    components.forEach(component -> {
        componentMap.put(component.getComponentId(), component);
    });

    //exact all links
    long linkCountId = 1;
    for (Component component : componentMap.values()) {

        Document doc = Jsoup.parseBodyFragment(component.getDescription());
        Elements elements = doc.select("a");

        for (Element element : elements) {
            String link = element.attr("href");
            LinkCheckModel linkCheckModel = new LinkCheckModel();
            linkCheckModel.setId(component.getComponentId() + "-" + (linkCountId++));
            linkCheckModel.setComponentName(component.getName());
            linkCheckModel.setLink(link);
            linkCheckModel.setNetworkOfLink(getNetworkOfLink(link));
            linkCheckModel.setResourceType("Description Link");
            linkCheckModel.setSecurityMarking(component.getSecurityMarkingType());
            links.add(linkCheckModel);
        }

        List<ComponentResource> resources = resourceMap.get(component.getComponentId());
        if (resources != null) {
            for (ComponentResource resource : resources) {
                String link = resource.getLink();

                //Blank means it's an internal resource
                if (StringUtils.isNotBlank(link)) {
                    if (link.toLowerCase().contains("<a")) {
                        doc = Jsoup.parseBodyFragment(link);
                        elements = doc.select("a");
                        for (Element element : elements) {
                            link = element.attr("href");
                            break;
                        }
                    }

                    LinkCheckModel linkCheckModel = new LinkCheckModel();
                    linkCheckModel.setId(component.getComponentId() + "-" + resource.getResourceId());
                    linkCheckModel.setComponentName(component.getName());
                    linkCheckModel.setLink(link);
                    linkCheckModel.setNetworkOfLink(getNetworkOfLink(resource.getLink()));
                    linkCheckModel.setResourceType(
                            TranslateUtil.translate(ResourceType.class, resource.getResourceType()));
                    linkCheckModel.setSecurityMarking(resource.getSecurityMarkingType());
                    links.add(linkCheckModel);
                }
            }
        }

    }
    checkLinks();
}

From source file:com.k42b3.aletheia.response.html.Images.java

private void parseImages(String html) {
    Document doc = Jsoup.parse(html);
    Elements image = doc.getElementsByTag("img");

    for (Element img : image) {
        String src = img.attr("src");

        if (!src.isEmpty()) {
            try {
                URL url = new URL(Util.resolveHref(baseUrl, src));

                if (!images.contains(url)) {
                    images.add(url);/*from   w  w  w . jav  a 2s.c o  m*/
                }
            } catch (Exception e) {
                Aletheia.handleException(e);
            }
        }
    }
}

From source file:com.astamuse.asta4d.web.form.field.impl.AbstractRadioAndCheckboxRenderer.java

protected Renderer setDelayedHiddenFlag(final String targetSelector) {
    // hide the input element
    final List<String> duplicatorRefList = new LinkedList<>();
    final List<String> idList = new LinkedList<>();
    Renderer renderer = Renderer.create(targetSelector, new ElementSetter() {
        @Override//from  w ww .j  a  v  a 2 s . c  om
        public void set(Element elem) {
            String duplicatorRef = elem.attr(RadioPrepareRenderer.DUPLICATOR_REF_ATTR);
            if (StringUtils.isNotEmpty(duplicatorRef)) {
                duplicatorRefList.add(duplicatorRef);
            }
            idList.add(elem.id());
        }
    });
    return renderer.add(":root", new Renderable() {
        @Override
        public Renderer render() {
            Renderer render = Renderer.create().disableMissingSelectorWarning();

            for (String ref : duplicatorRefList) {
                render.add(SelectorUtil.attr(RadioPrepareRenderer.DUPLICATOR_REF_ID_ATTR, ref),
                        ToBeHiddenLaterFlagAttr, "");
            }
            for (String id : idList) {
                render.add(SelectorUtil.attr(RadioPrepareRenderer.LABEL_REF_ATTR, id), ToBeHiddenLaterFlagAttr,
                        "");
            }
            for (String id : idList) {
                render.add(SelectorUtil.attr("label", "for", id), ToBeHiddenLaterFlagAttr, "");
            }
            render.add(targetSelector, ToBeHiddenLaterFlagAttr, "");
            // render.addDebugger("after set hidden flag");
            return render.enableMissingSelectorWarning();
        }
    });
}