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:mobi.jenkinsci.alm.assembla.client.AssemblaClient.java

private Document postLoginForm(final Document pinDoc) throws IOException {
    final List<NameValuePair> formNvps = new ArrayList<NameValuePair>();
    final Element form = pinDoc.select("form[id=login-box]").first();
    final String formAction = form.attr("action");
    final HttpPost formPost = new HttpPost(getUrl(formAction).toString());
    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");
        final String fieldType = element.attr("type");

        if (fieldId.equalsIgnoreCase("user_login")) {
            fieldValue = username;/*  w  w  w .j av a2  s  .  c  o m*/
            ;
        } else if (fieldId.equalsIgnoreCase("user_password")) {
            fieldValue = password;
        }

        if (fieldType.equals("submit")) {
            if (!fieldName.equalsIgnoreCase("commit")) {
                continue;
            }
        }

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

    HttpResponse response;
    LOG.debug("Login via posting form-data to " + formPost.getURI());
    try {
        response = sendHttpPost(formPost);
        if (response.getStatusLine().getStatusCode() != HttpURLConnection.HTTP_MOVED_TEMP) {
            throw new IOException("Form-based login to Assembla failed: " + response.getStatusLine());
        }
        return Jsoup.parse(getData(response.getFirstHeader("Location").getValue(), false));
    } finally {
        formPost.releaseConnection();
    }
}

From source file:fr.isen.browser5.Util.UrlLoader.java

public void setTabTitleFavicon() {
    String title = doc.title();/*from  www .ja  v  a 2 s.  co  m*/
    title = Str.shortenString(title, 12);
    int tabIndex = tabView.getCurrentTabIndex();
    JPanel tabPanel = (JPanel) tabView.getCurrentFrame().getTabPane().getTabComponentAt(tabIndex);
    JLabel titleLabel = (JLabel) tabPanel.getComponent(0);
    titleLabel.setText(title);

    String baseUrl = pageUrl.getProtocol() + "://" + pageUrl.getHost();
    Element element = doc.head().select("link[href~=.*\\.(ico|png)]").first();
    String favicoUrlStr = "";
    if (element != null) {
        favicoUrlStr = element.attr("abs:href");
    } else {
        element = doc.head().select("meta[itemprop=image]").first();
        if (element != null) {
            favicoUrlStr = baseUrl + element.attr("content");
        }
    }

    ImageIcon icon = null;
    try {
        if (!favicoUrlStr.isEmpty()) {
            if (favicoUrlStr.endsWith(".ico")) {
                java.util.List<BufferedImage> imgs = ICODecoder.read(new URL(favicoUrlStr).openStream());
                icon = new ImageIcon(imgs.get(0));
            } else {
                icon = new ImageIcon(new URL(favicoUrlStr));
            }
            icon = new ImageIcon(icon.getImage().getScaledInstance(16, 16, Image.SCALE_DEFAULT));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    titleLabel.setIcon(icon);

    HistoryEntry historyEntry = new HistoryEntry(title, pageUrl.toString(), Instant.now().getEpochSecond(),
            icon, -1L);
    globalHistoryDAO.create(historyEntry);
}

From source file:mergedoc.core.APIDocument.java

/**
 * Javadoc ? ??????//  w  w  w . j av a2 s  .c  o  m
 * @param className ??
 * @param context 
 * @param comment 
 */
private void parseCommonTag(String className, Element element, Comment comment) {
    Elements dts = element.select("dl dt");
    for (Element dt : dts) {
        String dtText = dt.text();
        if (dtText.contains("")) {
            Elements aTags = dt.nextElementSibling().select("a:has(code)");
            for (Element a : aTags) {
                String url = a.attr("href");
                String ref;
                if (a.childNodeSize() != 1) {
                    ref = aTags.outerHtml();
                } else {
                    ref = formatClassName(className, url);
                    ref = FastStringUtils.replace(ref, "%28", "(");
                    ref = FastStringUtils.replace(ref, "%29", ")");

                    Pattern methodRefPat = PatternCache.getPattern("-(.*)-$");
                    Matcher methodRefMat = methodRefPat.matcher(ref);
                    if (methodRefMat.find()) {
                        ref = FastStringUtils.replaceAll(ref, "-(.*)-$", "($1)"); // for Java8
                        ref = FastStringUtils.replace(ref, "-", ","); // for Java8
                        ref = FastStringUtils.replace(ref, ":A", "[]"); // for Java8
                    }
                }
                comment.addSee(ref);
            }
        } else if (dtText.contains("???:")) {
            comment.addSince(dt.nextElementSibling().text());
        }
    }
}

From source file:net.parser.JobParser.java

public List<Job> getJobIdAndLink(List<Job> jobs) {
    Job job = null;/*from www.  ja va 2 s.c om*/
    Employer employer = null;
    Elements elements = doc.select(".searchlist").eq(0).select(".job .details");

    for (Element jobElement : elements) {

        Elements aElements = jobElement.select("a");
        job = new Job();
        employer = new Employer();

        Element aJob = aElements.get(0);
        String linkJob = aJob.attr("href");
        linkJob = linkJob.replaceFirst("www", "m");
        job.setLink(linkJob);

        linkJob = linkJob.substring(linkJob.indexOf("/") + 1, linkJob.length());
        linkJob = linkJob.substring(linkJob.indexOf("/") + 1, linkJob.length());
        linkJob = linkJob.substring(linkJob.indexOf("/") + 1, linkJob.length());
        linkJob = linkJob.substring(linkJob.indexOf("/") + 1, linkJob.length());
        String id = linkJob.substring(0, linkJob.indexOf("/"));

        job.setId(Integer.parseInt(id));

        Element aEmpoyer = null;
        try {
            aEmpoyer = aElements.get(1);
            String linkEmployer = aEmpoyer.attr("href");
            employer.setLink(linkEmployer);

            linkEmployer = linkEmployer.substring(linkEmployer.indexOf("/") + 1, linkEmployer.length());
            linkEmployer = linkEmployer.substring(linkEmployer.indexOf("/") + 1, linkEmployer.length());
            linkEmployer = linkEmployer.substring(linkEmployer.indexOf("/") + 1, linkEmployer.length());
            linkEmployer = linkEmployer.substring(linkEmployer.indexOf("/") + 1, linkEmployer.length());
            id = linkEmployer.substring(0, linkEmployer.indexOf("/"));

            employer.setId(Integer.parseInt(id));
        } catch (IndexOutOfBoundsException e) {
            employer.setId(0);
        }

        employer.setName(getEmployerName(jobElement, employer.getId()));

        job.setEmployer(employer);
        jobs.add(job);
    }

    return jobs;
}

From source file:com.wheelermarine.publicAccessSites.Updater.java

@Override
protected Integer doInBackground(URL... urls) {

    try {//from w w w . j  a v a2s .c  o  m
        final DatabaseHelper db = new DatabaseHelper(context);

        SQLiteDatabase database = db.getWritableDatabase();
        if (database == null)
            throw new IllegalStateException("Unable to open database!");

        database.beginTransaction();
        try {
            // Clear out the old data.
            database.delete(DatabaseHelper.PublicAccessEntry.TABLE_NAME, null, null);

            // Connect to the web server and locate the FTP download link.
            Log.v(TAG, "Finding update: " + urls[0]);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progress.setMessage("Locating update...");
                    progress.setIndeterminate(true);
                }
            });
            Document doc = Jsoup.connect(urls[0].toString()).timeout(timeout * 1000).userAgent(userAgent).get();
            URL dataURL = null;
            for (Element element : doc.select("a")) {
                if (element.hasAttr("href") && element.attr("href").endsWith(".zip")) {
                    dataURL = new URL(element.attr("href"));
                }
            }

            // Make sure the download URL was fund.
            if (dataURL == null)
                throw new FileNotFoundException("Unable to locate data URL.");

            // Connect to the FTP server and download the update.
            Log.v(TAG, "Downloading update: " + dataURL);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progress.setMessage("Downloading update...");
                    progress.setIndeterminate(true);
                }
            });
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(dataURL.toString());
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            if (entity == null)
                throw new IOException("Error downloading update.");

            Map<Integer, Location> locations = null;

            // Download the ZIP archive.
            Log.v(TAG, "Downloading: " + dataURL.getFile());
            InputStream in = entity.getContent();
            if (in == null)
                throw new FileNotFoundException(dataURL.getFile() + " was not found!");
            try {
                ZipInputStream zin = new ZipInputStream(in);
                try {
                    // Locate the .dbf entry in the ZIP archive.
                    ZipEntry entry;
                    while ((entry = zin.getNextEntry()) != null) {
                        if (entry.getName().endsWith(entryName)) {
                            readDBaseFile(zin, database);
                        } else if (entry.getName().endsWith(shapeEntryName)) {
                            locations = readShapeFile(zin);
                        }
                    }
                } finally {
                    try {
                        zin.close();
                    } catch (Exception e) {
                        // Ignore this error.
                    }
                }
            } finally {
                in.close();
            }

            if (locations != null) {
                final int recordCount = locations.size();
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progress.setIndeterminate(false);
                        progress.setMessage("Updating locations...");
                        progress.setMax(recordCount);
                    }
                });

                int progress = 0;
                for (int recordNumber : locations.keySet()) {
                    PublicAccess access = db.getPublicAccessByRecordNumber(recordNumber);
                    Location loc = locations.get(recordNumber);
                    access.setLatitude(loc.getLatitude());
                    access.setLongitude(loc.getLongitude());
                    db.updatePublicAccess(access);
                    publishProgress(++progress);
                }
            }
            database.setTransactionSuccessful();
            return db.getPublicAccessesCount();
        } finally {
            database.endTransaction();
        }
    } catch (Exception e) {
        error = e;
        Log.e(TAG, "Error loading data: " + e.getLocalizedMessage(), e);
        return -1;
    }
}

From source file:com.gmail.jiangyang5157.cardboard.net.DescriptionRequest.java

private Response<Object> doHtmlParse(NetworkResponse response) {
    String parsed = getParsedString(response);

    Document doc;/*w  w  w .j  a  va  2  s.  c o  m*/
    try {
        doc = Jsoup.parse(parsed);
    } catch (VerifyError | NoClassDefFoundError e) {
        // http://stackoverflow.com/questions/38059373/java-lang-verifyerror-when-downloading-data-with-jsoup-in-android-n
        // TODO: 10/1/2016 upgrade Jsoup
        return Response.error(new ParseError(response));
    }

    // for <title>Hello World</title>
    String content = doc.title();

    // for <meta property="og:description" content="Hello world." />
    Element mataPropertyOgDescription = doc.select("meta[property^=og:description]").first();
    if (mataPropertyOgDescription != null) {
        content += "\n\n" + mataPropertyOgDescription.attr("content");
    }
    return Response.success(content, HttpHeaderParser.parseCacheHeaders(response));
}

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

/**
 * Searches for the available specialty options matching the criteria.
 *
 * @param criteria the criteria for specialty search
 * @param document the current page/*from  w w w. j  a  v  a  2 s  .c o m*/
 * @return the matched code
 * @throws ServiceException if the code provided is not present
 */
private String matchSpecialtyCode(MedicalPracticeLicenseSearchCriteria criteria, Document document)
        throws ServiceException {
    Elements specialtyOptions = document.select("select#_ctl7_ddlbSpecialty option");
    Specialty specialty = criteria.getSpecialty();
    String code = null;
    boolean found = false;
    for (Element option : specialtyOptions) {
        code = option.attr("value");
        if (Util.isNotBlank(specialty.getName())) { // match the name
            if (specialty.getName().equalsIgnoreCase(option.text())) {
                if (specialty.getCode() > 0 && Integer.parseInt(code) != specialty.getCode()) {
                    throw new ServiceException(ErrorCode.MITA10007.getDesc());
                }
                found = true;
                break;
            }
        } else { // match only the code
            if (Integer.parseInt(code) == specialty.getCode()) {
                found = true;
                break;
            }
        }
    }
    if (!found) {
        throw new ServiceException(ErrorCode.MITA10007.getDesc());
    }
    return code;
}

From source file:com.bdx.rainbow.service.etl.analyze.SYJHttpAnalyze.java

/**
 * ??,?//w w w. ja  v  a2 s  .c  om
 */
@Override
public Collection<HttpSeed> findSeed(Collection<HttpSeed> seeds) throws Exception {

    if (CollectionUtils.isEmpty(seeds)) {
        return null;
    }

    Collection<HttpSeed> seedGroups = new ArrayList<HttpSeed>();

    // ?HTMLA
    for (HttpSeed seed : seeds) {
        Document doc = parse(seed.getHtml());

        Elements drug_elements = doc.select("a[href]");

        if (drug_elements.isEmpty()) {
            return null;
        }

        for (Element drug_e : drug_elements) {
            String href_string = drug_e.attr("href");
            String uri = href_string.substring(href_string.indexOf("'") + 1, href_string.lastIndexOf("'"));

            if (StringUtils.isBlank(uri)) {
                continue;
            }

            seedGroups.add(initDetailHttpSeed(DOMAIN + uri));
        }
    }

    return seedGroups;
}

From source file:io.seldon.importer.articles.dynamicextractors.FirstElementAttrValueDateWithFormatDynamicExtractor.java

@Override
public String extract(AttributeDetail attributeDetail, String url, Document articleDoc) throws Exception {

    String attrib_value = null;/*from w  w w.  java2 s.co  m*/
    String dateFormatString = null;

    if ((attributeDetail.extractor_args != null) && (attributeDetail.extractor_args.size() >= 3)) {
        String cssSelector = attributeDetail.extractor_args.get(0);
        dateFormatString = attributeDetail.extractor_args.get(1);
        Element element = articleDoc.select(cssSelector).first();
        if (StringUtils.isNotBlank(cssSelector)) {
            int arg_count = 0;
            for (String value_name : attributeDetail.extractor_args) {
                if (arg_count > 1) { // skip the first one, its the cssSelector, and second thats the Date format
                    if (element != null && element.attr(value_name) != null) {
                        attrib_value = element.attr(value_name);
                        if (StringUtils.isNotBlank(attrib_value)) {
                            break;
                        }
                    }
                }
                arg_count++;
            }
        }
    }

    if ((attrib_value != null) && (dateFormatString != null)) {
        String pubtext = attrib_value;
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df = new SimpleDateFormat(dateFormatString, Locale.ENGLISH);
        Date result = null;
        try {
            result = df.parse(pubtext);
        } catch (ParseException e) {
            logger.info("Failed to parse date with format [" + dateFormatString + "] " + pubtext);
        }

        if (result != null) {
            String attrib_value_orig = attrib_value;
            attrib_value = dateFormatter.format(result);
            String msg = "Extracted date [" + attrib_value_orig + "] - > [" + attrib_value + "]";
            logger.info(msg);
        } else {
            logger.error("Failed to parse date " + pubtext);
            attrib_value = null;
        }
    }

    return attrib_value;
}

From source file:ac.simons.oembed.Oembed.java

private OembedProvider autodiscoverOembedURIForUrl(final String url) {
    OembedProvider rv = null;//from   w ww.ja v a 2 s. c o  m

    try {
        final HttpGet request = new HttpGet(url);
        final HttpResponse httpResponse = this.httpClient.execute(request);
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
            logger.warn(String.format("Autodiscovery for %s failed, server returned error %d: %s", url,
                    httpResponse.getStatusLine().getStatusCode(),
                    EntityUtils.toString(httpResponse.getEntity())));
        else {
            final URI uri = request.getURI();
            final Document document = Jsoup.parse(EntityUtils.toString(httpResponse.getEntity(), "UTF-8"),
                    String.format("%s://%s:%d", uri.getScheme(), uri.getHost(), uri.getPort()));
            for (Element alternate : document.getElementsByAttributeValue("rel", "alternate")) {
                if (alternate.attr("type").equalsIgnoreCase("application/json+oembed"))
                    rv = new AutodiscoveredOembedProvider(url, new URI(alternate.absUrl("href")), "json");
                else if (alternate.attr("type").equalsIgnoreCase("text/xml+oembed"))
                    rv = new AutodiscoveredOembedProvider(url, new URI(alternate.absUrl("href")), "xml");
                if (rv != null)
                    break;
            }
        }
    } catch (Exception e) {
        logger.warn(String.format("Autodiscovery for %s failedd: %s", url, e.getMessage()), e);
    }

    return rv;
}