Example usage for org.jsoup.nodes Element getElementsByTag

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

Introduction

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

Prototype

public Elements getElementsByTag(String tagName) 

Source Link

Document

Finds elements, including and recursively under this element, with the specified tag name.

Usage

From source file:mx.itdurango.rober.siitdocentes.ActivityAlumnos.java

/**
 * Permite descomponer el cdigo html que se enva con una estructura especifica para llenar los datos de la vista
 *
 * @param html cdigo html que se recibi de una peticin HttpGet, debe tener una estructura similar a la siguiente para que el proceso funcione correctamente
 *             <p/>//from   w  w w  . ja  v a2 s  .c o m
 *             <input name="periodo" type="hidden" value="20141" />
 *             <input name="materia" type="hidden" value="SD2424" />
 *             <input name="grupo" type="hidden" value="5VR" />
 *             <input name="docente" type="hidden" value="LOQR841213822" />
 *             <input name="fecha_captura" type="hidden" value="2014/06/12" />
 *             <table>
 *             <tr>
 *             <td>No</td>
 *             <td>Noctrl</td>
 *             <td>Nombre</td>
 *             <td>Unidad 1</td>
 *             <td>Unidad 1</td>
 *             <td>Unidad 3</td>
 *             <td>...</td>
 *             <td>Unidad N</td>
 *             </tr>
 *             <tr>
 *             <td>1</td>
 *             <td>9999999</td>
 *             <td>XXXXXXXXXXXXXXXXXXXXX</td>
 *             <td><input type="text" name="calif[1][1]" value="999"/></td>
 *             <td><input type="text" name="calif[1][2]" value="999"/></td>
 *             <td><input type="text" name="calif[1][3]" value="999"/></td>
 *             <td>...</td>
 *             <td><input type="text" name="calif[1][N]" value="999"/></td>
 *             </tr>
 *             <tr>
 *             <td>2</td>
 *             <td>888888888</td>
 *             <td>YYYYYYYYYYYYYYYYYYYYY</td>
 *             <td><input type="text" name="calif[2][1]" value="999"/></td>
 *             <td><input type="text" name="calif[2][2]" value="999"/></td>
 *             <td><input type="text" name="calif[2][3]" value="999"/></td>
 *             <td>...</td>
 *             <td><input type="text" name="calif[2][N]" value="999"/></td>
 *             </tr>
 *             <tr>
 *             <td>M</td>
 *             <td>000000000</td>
 *             <td>ZZZZZZZZZZZZZZZZZZZZZZ</td>
 *             <td><input type="text" name="calif[M][1]" value="999"/></td>
 *             <td><input type="text" name="calif[M][2]" value="999"/></td>
 *             <td><input type="text" name="calif[M][3]" value="999"/></td>
 *             <td>...</td>
 *             <td><input type="text" name="calif[M][N]" value="999"/></td>
 *             </tr>
 *             </table>
 */
void llenaAlumnos(String html) {
    //Generar un archivo de documento para almacenar los datos del html de forma que se pueda
    //manipular facilmente usando la librera Jsoup
    Document doc = Jsoup.parse(html);

    try {
        //extraer los valores de los elementos del formulario y almacenarlos en los atributos correspondientes de la clase
        Elements e = doc.getElementsByAttributeValue("name", "periodo");
        periodo = e.get(0).attr("value");
        e = doc.getElementsByAttributeValue("name", "materia");
        materia = e.get(0).attr("value");
        e = doc.getElementsByAttributeValue("name", "grupo");
        grupo = e.get(0).attr("value");
        e = doc.getElementsByAttributeValue("name", "docente");
        docente = e.get(0).attr("value");
        e = doc.getElementsByAttributeValue("name", "fecha_captura");
        fecha_captura = e.get(0).attr("value");

        //extraer la tabla correspondiente al listado de alumnos en el caso del siit.itdurango.edu.mx,
        // corresponde a la tabla numero 2 y ya que la numeracin comienza en 0, la tabla que necesitamos est en el indice 1
        Element tabla = doc.getElementsByTag("table").get(1);
        //Extraer todos los elementos de tipo tr que pertenecen a la tabla y almacenarlos en una coleccion de tipo Elements.
        Elements renglones = tabla.getElementsByTag("tr");
        //Recorrer la coleccin de renglones y almacenar cada uno en un objeto
        for (Element tr : renglones) {
            //para cada objeto tr, extraer sus elementos td y almacenarlos en una coleccion
            Elements tds = tr.getElementsByTag("td");
            //permite llevar el control de la columna que se est leyendo, ya que las columnas no tienen un id o clase, se realiza el proceso a mano.
            int col = 1;
            //contenedor de tipo AlumosParciales para almacenar la informacin de cada alumno (tr)
            AlumnosParciales c = new AlumnosParciales();
            for (Element td : tds) {
                if (col == 1) {// la columna 1 corresponde al nmero consecutivo de la tabla
                    c.setNum(td.html());
                } else if (col == 2) {// la columna 2 corresponde al nmero de control del alumno
                    c.setControl(td.html());
                } else if (col == 3) {// la columna 3 corresponde al nombre del alumno
                    c.setNombre(Estaticos.sanitize(td.html()));
                } else { //el resto de las columnas pertenecen a las calificaciones parciales
                    //se extrae el elemento <input> de la columna y se obtiene el atributo valor para recuperar la calificacin en caso de que ya hubiera sido asignada
                    String cal = td.getElementsByTag("input").get(0).attr("value");

                    ArrayList<String> calif = c.getCalificaciones();
                    calif.add(cal);
                    //se agrega la nueva calificacin al conjunto de calificaciones del alumno
                    c.setCalificaciones(calif);
                }
                col++; //incrementa el numero de columa
            }
            if (c.getCalificaciones().size() > 0) { //para evitar agregar al listado de alumnos el encabezado de la tabla, validamos que existan calificaciones.
                gcs.add(c);
            }
        }

        //Llenamos el spinner de unidades a partir del numero de calificaciones que existen en el arreglo
        List<String> spinnerArray = new ArrayList<String>();
        for (int i = 1; i <= gcs.get(1).getCalificaciones().size() - 1; i++) {
            spinnerArray.add("Unidad " + i);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
                spinnerArray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spn_unidad.setAdapter(adapter);

        //llenamos el listado de alumnos con la informacin que se obtuvo del proceso anterior
        alumnosParcialesAdapter = new AlumnosParcialesAdapter(this, gcs, unidad);
        lvAlumnos.setAdapter(alumnosParcialesAdapter);

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this, getString(R.string.error_parser), Toast.LENGTH_SHORT).show();
        finish(); //finaliza el intent actual para desplegar el anterior
    }
}

From source file:info.smartkit.hairy_batman.query.SogouSearchQuery.java

public void parseWxOpenId() {
    Document doc;//from  w  w  w.j a  va2  s. c o m
    try {

        // need http protocol
        // doc = Jsoup.connect(GlobalConsts.SOGOU_SEARCH_URL_BASE+ wxFoo.getSubscribeId()).get();
        doc = Jsoup.connect("http://weixin.sogou.com/weixin?type=1&query=" + wxFoo.getSubscribeId()
                + "&fr=sgsearch&ie=utf8&_ast=1423915648&_asf=null&w=01019900&cid=null&sut=19381").get();

        LOG.debug("openID html INFO:" + doc.html());

        // get page title
        String title = doc.title();
        LOG.debug("title : " + title);
        // get all "?:" value of html <span>
        //Elements openIdLink = doc.select(GlobalConsts.SOGOU_SEARCH_WX_OPEN_ID_HTML_ELEMENTS).select(GlobalConsts.SOGOU_SEARCH_WX_OPEN_ID_HTML_ELE_IDENTITY);

        Elements openIdLink = doc.getElementsByClass("wx-rb");
        Element a = null;
        String openIdLinkHref = "";
        if (openIdLink != null && openIdLink.size() > 0) {
            Iterator<Element> itea = openIdLink.iterator();
            while (itea.hasNext()) {
                a = itea.next();
                LOG.debug("openID html INFO:" + a.html());
                if (a.getElementsByTag("em").html().indexOf(wxFoo.getSubscribeId()) != -1) {
                    break;
                }
            }
        }
        if (a != null) {
            openIdLinkHref = a.attr("href");
        }
        LOG.debug("openIdLinkHref:" + openIdLinkHref);
        // FIXME:????
        if (this.wxFoo.getOpenId() == null && openIdLinkHref.length() > 0) {

            this.wxFoo.setOpenId(openIdLinkHref.split(GlobalConsts.SOGOU_SEARCH_WX_OPEN_ID_KEYWORDS)[1]);
            LOG.info("saved wxOpenId value: " + this.wxFoo.getOpenId());
            GlobalVariables.wxFooListWithOpenId.add(this.wxFoo);
            // File reporting
            new FileReporter(GlobalConsts.REPORT_FILE_OUTPUT_OPENID, GlobalVariables.wxFooListWithOpenId,
                    FileReporter.REPORTER_TYPE.R_T_OPENID, FileReporter.REPORTER_FILE_TYPE.EXCEL).write();
            // Then,OpenID JSON site parse
            if (this.wxFoo.getOpenId() != null) {
                // Save openId to DB.
                try {
                    GlobalVariables.jdbcTempate.update("insert into " + GlobalConsts.QUERY_TABLE_NAME_BASIC
                            + "(id,store,agency,unit,subscribeId,onSubscribe,code,openId) values(?,?,?,?,?,?,?,?)",
                            new Object[] { this.wxFoo.getId(), this.wxFoo.getStore(), this.wxFoo.getAgency(),
                                    this.wxFoo.getUnit(), this.wxFoo.getSubscribeId(),
                                    this.wxFoo.getOnSubscribe(), this.wxFoo.getCode(), this.wxFoo.getOpenId() },
                            new int[] { java.sql.Types.INTEGER, java.sql.Types.VARCHAR, java.sql.Types.VARCHAR,
                                    java.sql.Types.VARCHAR, java.sql.Types.VARCHAR, java.sql.Types.VARCHAR,
                                    java.sql.Types.VARCHAR, java.sql.Types.VARCHAR });
                    this.parseSogouJsonSite(this.wxFoo.getOpenId());
                } catch (DataAccessException e) {
                    e.printStackTrace();
                }
            } else {
                LOG.warn("SogouSearchQuery getOpenId Failure! site info:" + wxFoo.getCode());
                // TODO write those info to File or DB for collect which
                // agency not open weixin service
                // Save openId to DB.
                try {
                    GlobalVariables.jdbcTempate.update("insert into " + GlobalConsts.QUERY_TABLE_NAME_BASIC
                            + "(id,store,agency,unit,subscribeId,onSubscribe,code,openId) values(?,?,?,?,?,?,?,?)",
                            new Object[] { this.wxFoo.getId(), this.wxFoo.getStore(), this.wxFoo.getAgency(),
                                    this.wxFoo.getUnit(), this.wxFoo.getSubscribeId(),
                                    this.wxFoo.getOnSubscribe(), this.wxFoo.getCode(), "" },
                            new int[] { java.sql.Types.INTEGER, java.sql.Types.VARCHAR, java.sql.Types.VARCHAR,
                                    java.sql.Types.VARCHAR, java.sql.Types.VARCHAR, java.sql.Types.VARCHAR,
                                    java.sql.Types.VARCHAR, java.sql.Types.VARCHAR });
                    LOG.warn("Can not get subsriber info: " + this.wxFoo.getCode());

                    this.parseSogouJsonSite(this.wxFoo.getOpenId());
                } catch (DataAccessException e) {
                    e.printStackTrace();
                }
            }
        }

    } catch (IOException e) {
        // e.printStackTrace();
        LOG.error(e.toString());
    }
}

From source file:tkbautobooking.BookingSystem.java

private void praseBookingPage() throws Exception {

    Document doc = Jsoup.parse(BookingPageHTML);
    Element class_selector = doc.getElementById("class_selector");

    if (class_selector == null)
        throw new Exception("Prase Booking Page fail !");

    classMap = new TreeMap<>();
    for (Element option : class_selector.getElementsByTag("option")) {
        if (option.attr("value").equals(""))
            continue;

        classMap.put(option.attr("value"), option.text().replace("", " "));
    }// w  ww  .  ja  v  a  2s  .  c o  m
}

From source file:prince.app.ccm.tools.Task.java

public String getFormParams(String html, String username, String password) throws UnsupportedEncodingException {

    System.out.println("Extracting form's data...");

    Document doc = Jsoup.parse(html);

    // Google form id
    Element loginform = doc.getElementById("contenido_right");
    Elements loginaction = doc.getElementsByTag("form");
    Element form = loginaction.first();
    log = MAIN_PAGE + form.attr("action");
    Log.e(TAG, "Action: " + log);
    Elements inputElements = loginform.getElementsByTag("input");
    List<String> paramList = new ArrayList<String>();
    for (Element inputElement : inputElements) {
        String key = inputElement.attr("name");
        String value = inputElement.attr("value");

        if (key.equals("usuario")) {
            value = username;//w w w  .j  ava  2 s.  co  m
            paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
        } else if (key.equals("contrasena")) {
            value = password;
            paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
        }
    }

    // build parameters list
    StringBuilder result = new StringBuilder();
    for (String param : paramList) {
        if (result.length() == 0) {
            result.append(param);
        } else {
            result.append("&" + param);
        }
    }

    Log.d(TAG, "Done in getFormParams: " + result.toString());
    return result.toString();
}

From source file:ie.nuim.cs.dri.metadata.WebSearch.java

/**
 *
 * @param title the title of the ROS// www. j av  a  2s  . com
 */
public void searchGoogle(String title) {
    String searchTitle = buildGoogleSearchTitle(title);
    boolean found = false;
    String publication = "";
    String publicationType = "";
    int citationCount = -1;
    String url = "http://scholar.google.com/scholar?" + searchTitle;
    Document doc = Jsoup.parse(getGS());
    Elements aElement = doc.getElementsByTag("h3");
    System.out.println("=====searching google=======");
    for (Element e : aElement) {
        Elements bElement = e.getElementsByTag("a");
        for (Element f : bElement) {
            System.out.println(f.text() + "\t" + title);

            if (title.equalsIgnoreCase(f.text())) {
                found = true;
                break;
            }
        }
        // System.out.println(e);

    }
    if (found == true) {
        Elements pElement = doc.getElementsByTag("div");
        for (Element p : pElement) {
            Elements pubElement = p.getElementsByClass("gs_a");
            for (Element pub : pubElement) {
                System.out.println(pub);
            }

        }
        for (Element p : pElement) {
            Elements pubElement = p.getElementsByClass("gs_fl");
            for (Element pub : pubElement) {
                System.out.println(pub);
            }

        }

    }
}

From source file:net.GoTicketing.GoTicketing.java

/**
 * ???//from ww  w .j  a va 2s .  c om
 * @throws Exception 
 */
private void praseFormActionSrc() throws Exception {
    Document doc = Jsoup.parse(TicketingPageHTML);
    Element form = doc.getElementsByTag("form").first();
    if (form == null)
        throw new Exception("Can't get form action source !");

    String src = form.attr("action");
    FormActionSrc = host + src.replace("/", "%2F");

    FormInputData = new TreeMap<>();
    for (Element elm : form.getElementsByTag("input")) {
        if (elm.attr("type").equals("hidden"))
            FormInputData.put(elm.attr("name"), elm.attr("value"));
    }
}

From source file:net.kevxu.purdueassist.course.ScheduleDetail.java

private ScheduleDetailEntry parseDocument(Document document)
        throws HtmlParseException, CourseNotFoundException, ResultNotMatchException {
    ScheduleDetailEntry entry = new ScheduleDetailEntry(term, crn);
    Elements tableElements = document.getElementsByAttributeValue("summary",
            "This table is used to present the detailed class information.");

    if (!tableElements.isEmpty()) {
        for (Element tableElement : tableElements) {
            // get basic info for selected course
            Element tableBasicInfoElement = tableElement.getElementsByClass("ddlabel").first();
            if (tableBasicInfoElement != null) {
                setBasicInfo(entry, tableBasicInfoElement.text());
            } else {
                throw new HtmlParseException("Basic info element empty.");
            }/*from   w w  w  . j  a v a2s .co m*/

            // get detailed course info
            Element tableDetailedInfoElement = tableElement.getElementsByClass("dddefault").first();

            if (tableDetailedInfoElement != null) {
                // process seat info
                Elements tableSeatDetailElements = tableDetailedInfoElement.getElementsByAttributeValue(
                        "summary", "This layout table is used to present the seating numbers.");
                if (tableSeatDetailElements.size() == 1) {
                    Element tableSeatDetailElement = tableSeatDetailElements.first();
                    Elements tableSeatDetailEntryElements = tableSeatDetailElement.getElementsByTag("tbody")
                            .first().children();
                    if (tableSeatDetailEntryElements.size() == 3 || tableSeatDetailEntryElements.size() == 4) {
                        setSeats(entry, tableSeatDetailEntryElements.get(1).text());
                        setWaitlistSeats(entry, tableSeatDetailEntryElements.get(2).text());
                        if (tableSeatDetailEntryElements.size() == 4) {
                            setCrosslistSeats(entry, tableSeatDetailEntryElements.get(3).text());
                        }
                    } else {
                        throw new HtmlParseException("Seat detail entry elements size not 3. We have "
                                + tableSeatDetailEntryElements.size() + ".");
                    }
                } else {
                    throw new HtmlParseException(
                            "Seat detail elements size not 1. We have " + tableSeatDetailElements.size() + ".");
                }
                // remove the seat info from detailed info
                tableSeatDetailElements.remove();

                // remaining information
                setRemainingInfo(entry, tableDetailedInfoElement.html());

            } else {
                throw new HtmlParseException("Detailed info element empty.");
            }

        }
    } else {
        // test empty
        Elements informationElements = document.getElementsByAttributeValue("summary",
                "This layout table holds message information");
        if (!informationElements.isEmpty()
                && informationElements.text().contains("No detailed class information found")) {
            throw new CourseNotFoundException(informationElements.text());
        } else {
            throw new HtmlParseException(
                    "Course table not found, but page does not contain message stating no course found.");
        }
    }

    return entry;
}

From source file:biz.shadowservices.DegreesToolbox.DataFetcher.java

public FetchResult updateData(Context context, boolean force) {
    //Open database
    DBOpenHelper dbhelper = new DBOpenHelper(context);
    SQLiteDatabase db = dbhelper.getWritableDatabase();

    // check for internet connectivity
    try {//from  w ww .j a  v a  2 s.c o  m
        if (!isOnline(context)) {
            Log.d(TAG, "We do not seem to be online. Skipping Update.");
            return FetchResult.NOTONLINE;
        }
    } catch (Exception e) {
        exceptionReporter.reportException(Thread.currentThread(), e, "Exception during isOnline()");
    }
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    if (!force) {
        try {
            if (sp.getBoolean("loginFailed", false) == true) {
                Log.d(TAG, "Previous login failed. Skipping Update.");
                DBLog.insertMessage(context, "i", TAG, "Previous login failed. Skipping Update.");
                return FetchResult.LOGINFAILED;
            }
            if (sp.getBoolean("autoupdates", true) == false) {
                Log.d(TAG, "Automatic updates not enabled. Skipping Update.");
                DBLog.insertMessage(context, "i", TAG, "Automatic updates not enabled. Skipping Update.");
                return FetchResult.NOTALLOWED;
            }
            if (!isBackgroundDataEnabled(context) && sp.getBoolean("obeyBackgroundData", true)) {
                Log.d(TAG, "Background data not enabled. Skipping Update.");
                DBLog.insertMessage(context, "i", TAG, "Background data not enabled. Skipping Update.");
                return FetchResult.NOTALLOWED;
            }
            if (!isAutoSyncEnabled() && sp.getBoolean("obeyAutoSync", true)
                    && sp.getBoolean("obeyBackgroundData", true)) {
                Log.d(TAG, "Auto sync not enabled. Skipping Update.");
                DBLog.insertMessage(context, "i", TAG, "Auto sync not enabled. Skipping Update.");
                return FetchResult.NOTALLOWED;
            }
            if (isWifi(context) && !sp.getBoolean("wifiUpdates", true)) {
                Log.d(TAG, "On wifi, and wifi auto updates not allowed. Skipping Update");
                DBLog.insertMessage(context, "i", TAG,
                        "On wifi, and wifi auto updates not allowed. Skipping Update");
                return FetchResult.NOTALLOWED;
            } else if (!isWifi(context)) {
                Log.d(TAG, "We are not on wifi.");
                if (!isRoaming(context) && !sp.getBoolean("2DData", true)) {
                    Log.d(TAG, "Automatic updates on 2Degrees data not enabled. Skipping Update.");
                    DBLog.insertMessage(context, "i", TAG,
                            "Automatic updates on 2Degrees data not enabled. Skipping Update.");
                    return FetchResult.NOTALLOWED;
                } else if (isRoaming(context) && !sp.getBoolean("roamingData", false)) {
                    Log.d(TAG, "Automatic updates on roaming mobile data not enabled. Skipping Update.");
                    DBLog.insertMessage(context, "i", TAG,
                            "Automatic updates on roaming mobile data not enabled. Skipping Update.");
                    return FetchResult.NOTALLOWED;
                }

            }
        } catch (Exception e) {
            exceptionReporter.reportException(Thread.currentThread(), e,
                    "Exception while finding if to update.");
        }

    } else {
        Log.d(TAG, "Update Forced");
    }

    try {
        String username = sp.getString("username", null);
        String password = sp.getString("password", null);
        if (username == null || password == null) {
            DBLog.insertMessage(context, "i", TAG, "Username or password not set.");
            return FetchResult.USERNAMEPASSWORDNOTSET;
        }

        // Find the URL of the page to send login data to.
        Log.d(TAG, "Finding Action. ");
        HttpGetter loginPageGet = new HttpGetter("https://secure.2degreesmobile.co.nz/web/ip/login");
        String loginPageString = loginPageGet.execute();
        if (loginPageString != null) {
            Document loginPage = Jsoup.parse(loginPageString,
                    "https://secure.2degreesmobile.co.nz/web/ip/login");
            Element loginForm = loginPage.getElementsByAttributeValue("name", "loginFrm").first();
            String loginAction = loginForm.attr("action");
            // Send login form
            List<NameValuePair> loginValues = new ArrayList<NameValuePair>();
            loginValues.add(new BasicNameValuePair("externalURLRedirect", ""));
            loginValues.add(new BasicNameValuePair("hdnAction", "login_userlogin"));
            loginValues.add(new BasicNameValuePair("hdnAuthenticationType", "M"));
            loginValues.add(new BasicNameValuePair("hdnlocale", ""));

            loginValues.add(new BasicNameValuePair("userid", username));
            loginValues.add(new BasicNameValuePair("password", password));
            Log.d(TAG, "Sending Login ");
            HttpPoster sendLoginPoster = new HttpPoster(loginAction, loginValues);
            // Parse result

            String loginResponse = sendLoginPoster.execute();
            Document loginResponseParsed = Jsoup.parse(loginResponse);
            // Determine if this is a pre-pay or post-paid account.
            boolean postPaid;
            if (loginResponseParsed
                    .getElementById("p_CustomerPortalPostPaidHomePage_WAR_customerportalhomepage") == null) {
                Log.d(TAG, "Pre-pay account or no account.");
                postPaid = false;
            } else {
                Log.d(TAG, "Post-paid account.");
                postPaid = true;
            }

            String homepageUrl = "https://secure.2degreesmobile.co.nz/group/ip/home";
            if (postPaid) {
                homepageUrl = "https://secure.2degreesmobile.co.nz/group/ip/postpaid";
            }
            HttpGetter homepageGetter = new HttpGetter(homepageUrl);
            String homepageHTML = homepageGetter.execute();
            Document homePage = Jsoup.parse(homepageHTML);

            Element accountSummary = homePage.getElementById("accountSummary");
            if (accountSummary == null) {
                Log.d(TAG, "Login failed.");
                return FetchResult.LOGINFAILED;
            }
            db.delete("cache", "", null);
            /* This code fetched some extra details for postpaid users, but on reflection they aren't that useful.
             * Might reconsider this.
             *
             if (postPaid) {
                     
               Element accountBalanceSummaryTable = accountSummary.getElementsByClass("tableBillSummary").first();
               Elements rows = accountBalanceSummaryTable.getElementsByTag("tr");
               int rowno = 0;
               for (Element row : rows) {
                  if (rowno > 1) {
             break;
                  }
                  //Log.d(TAG, "Starting row");
                  //Log.d(TAG, row.html());
                  Double value;
                  try {
             Element amount = row.getElementsByClass("tableBillamount").first();
             String amountHTML = amount.html();
             Log.d(TAG, amountHTML.substring(1));
             value = Double.parseDouble(amountHTML.substring(1));
                  } catch (Exception e) {
             Log.d(TAG, "Failed to parse amount from row.");
             value = null;
                  }
                  String expiresDetails = "";
                  String expiresDate = null;
                  String name = null;
                  try {
             Element details = row.getElementsByClass("tableBilldetail").first();
             name = details.ownText();
             Element expires = details.getElementsByTag("em").first();
             if (expires != null) {
                 expiresDetails = expires.text();
             } 
             Log.d(TAG, expiresDetails);
             Pattern pattern;
             pattern = Pattern.compile("\\(payment is due (.*)\\)");
             Matcher matcher = pattern.matcher(expiresDetails);
             if (matcher.find()) {
                /*Log.d(TAG, "matched expires");
                Log.d(TAG, "group 0:" + matcher.group(0));
                Log.d(TAG, "group 1:" + matcher.group(1));
                Log.d(TAG, "group 2:" + matcher.group(2)); *
                String expiresDateString = matcher.group(1);
                Date expiresDateObj;
                if (expiresDateString != null) {
                   if (expiresDateString.length() > 0) {
                      try {
                         expiresDateObj = DateFormatters.EXPIRESDATE.parse(expiresDateString);
                         expiresDate = DateFormatters.ISO8601DATEONLYFORMAT.format(expiresDateObj);
                      } catch (java.text.ParseException e) {
                         Log.d(TAG, "Could not parse date: " + expiresDateString);
                      }
                   }   
                }
             }
                  } catch (Exception e) {
             Log.d(TAG, "Failed to parse details from row.");
                  }
                  String expirev = null;
                  ContentValues values = new ContentValues();
                  values.put("name", name);
                  values.put("value", value);
                  values.put("units", "$NZ");
                  values.put("expires_value", expirev );
                  values.put("expires_date", expiresDate);
                  db.insert("cache", "value", values );
                  rowno++;
               }
            } */
            Element accountSummaryTable = accountSummary.getElementsByClass("tableAccountSummary").first();
            Elements rows = accountSummaryTable.getElementsByTag("tr");
            for (Element row : rows) {
                // We are now looking at each of the rows in the data table.
                //Log.d(TAG, "Starting row");
                //Log.d(TAG, row.html());
                Double value;
                String units;
                try {
                    Element amount = row.getElementsByClass("tableBillamount").first();
                    String amountHTML = amount.html();
                    //Log.d(TAG, amountHTML);
                    String[] amountParts = amountHTML.split("&nbsp;", 2);
                    //Log.d(TAG, amountParts[0]);
                    //Log.d(TAG, amountParts[1]);
                    if (amountParts[0].contains("Included") || amountParts[0].equals("All You Need")
                            || amountParts[0].equals("Unlimited Text*")) {
                        value = Values.INCLUDED;
                    } else {
                        try {
                            value = Double.parseDouble(amountParts[0]);
                        } catch (NumberFormatException e) {
                            exceptionReporter.reportException(Thread.currentThread(), e, "Decoding value.");
                            value = 0.0;
                        }
                    }
                    units = amountParts[1];
                } catch (NullPointerException e) {
                    //Log.d(TAG, "Failed to parse amount from row.");
                    value = null;
                    units = null;
                }
                Element details = row.getElementsByClass("tableBilldetail").first();
                String name = details.getElementsByTag("strong").first().text();
                Element expires = details.getElementsByTag("em").first();
                String expiresDetails = "";
                if (expires != null) {
                    expiresDetails = expires.text();
                }
                Log.d(TAG, expiresDetails);
                Pattern pattern;
                if (postPaid == false) {
                    pattern = Pattern.compile("\\(([\\d\\.]*) ?\\w*? ?expiring on (.*)\\)");
                } else {
                    pattern = Pattern.compile("\\(([\\d\\.]*) ?\\w*? ?will expire on (.*)\\)");
                }
                Matcher matcher = pattern.matcher(expiresDetails);
                Double expiresValue = null;
                String expiresDate = null;
                if (matcher.find()) {
                    /*Log.d(TAG, "matched expires");
                    Log.d(TAG, "group 0:" + matcher.group(0));
                    Log.d(TAG, "group 1:" + matcher.group(1));
                    Log.d(TAG, "group 2:" + matcher.group(2)); */
                    try {
                        expiresValue = Double.parseDouble(matcher.group(1));
                    } catch (NumberFormatException e) {
                        expiresValue = null;
                    }
                    String expiresDateString = matcher.group(2);
                    Date expiresDateObj;
                    if (expiresDateString != null) {
                        if (expiresDateString.length() > 0) {
                            try {
                                expiresDateObj = DateFormatters.EXPIRESDATE.parse(expiresDateString);
                                expiresDate = DateFormatters.ISO8601DATEONLYFORMAT.format(expiresDateObj);
                            } catch (java.text.ParseException e) {
                                Log.d(TAG, "Could not parse date: " + expiresDateString);
                            }
                        }
                    }
                }
                ContentValues values = new ContentValues();
                values.put("name", name);
                values.put("value", value);
                values.put("units", units);
                values.put("expires_value", expiresValue);
                values.put("expires_date", expiresDate);
                db.insert("cache", "value", values);
            }

            if (postPaid == false) {
                Log.d(TAG, "Getting Value packs...");
                // Find value packs
                HttpGetter valuePacksPageGet = new HttpGetter(
                        "https://secure.2degreesmobile.co.nz/group/ip/prevaluepack");
                String valuePacksPageString = valuePacksPageGet.execute();
                //DBLog.insertMessage(context, "d", "",  valuePacksPageString);
                if (valuePacksPageString != null) {
                    Document valuePacksPage = Jsoup.parse(valuePacksPageString);
                    Elements enabledPacks = valuePacksPage.getElementsByClass("yellow");
                    for (Element enabledPack : enabledPacks) {
                        Element offerNameElemt = enabledPack
                                .getElementsByAttributeValueStarting("name", "offername").first();
                        if (offerNameElemt != null) {
                            String offerName = offerNameElemt.val();
                            DBLog.insertMessage(context, "d", "", "Got element: " + offerName);
                            ValuePack[] packs = Values.valuePacks.get(offerName);
                            if (packs == null) {
                                DBLog.insertMessage(context, "d", "",
                                        "Offer name: " + offerName + " not matched.");
                            } else {
                                for (ValuePack pack : packs) {
                                    ContentValues values = new ContentValues();
                                    values.put("plan_startamount", pack.value);
                                    values.put("plan_name", offerName);
                                    DBLog.insertMessage(context, "d", "",
                                            "Pack " + pack.type.id + " start value set to " + pack.value);
                                    db.update("cache", values, "name = '" + pack.type.id + "'", null);
                                }
                            }
                        }
                    }
                }
            }

            SharedPreferences.Editor prefedit = sp.edit();
            Date now = new Date();
            prefedit.putString("updateDate", DateFormatters.ISO8601FORMAT.format(now));
            prefedit.putBoolean("loginFailed", false);
            prefedit.putBoolean("networkError", false);
            prefedit.commit();
            DBLog.insertMessage(context, "i", TAG, "Update Successful");
            return FetchResult.SUCCESS;

        }
    } catch (ClientProtocolException e) {
        DBLog.insertMessage(context, "w", TAG, "Network error: " + e.getMessage());
        return FetchResult.NETWORKERROR;
    } catch (IOException e) {
        DBLog.insertMessage(context, "w", TAG, "Network error: " + e.getMessage());
        return FetchResult.NETWORKERROR;
    } finally {
        db.close();
    }
    return null;
}

From source file:org.dataconservancy.ui.it.support.ListMetadataFormatRequest.java

/**
 * Turns a row from the {@link #METADATA_FORMAT_TABLE} into a
 * {@link org.dataconservancy.ui.stripes.UiConfigurationActionBean.MetaDataFormatTransport} object.  The discipline
 * titles are resolved to discipline identifiers using the {@link #disciplineDao}.
 *
 * @param tableRow a row from the {@link #METADATA_FORMAT_TABLE} that represents a MetaDataFormatTransport object
 * @return the MetaDataFormatTransport object that represents the {@code tableRow}
 *///  w w w.j a v a 2  s  . co  m
private UiConfigurationActionBean.MetaDataFormatTransport toMetadataTransport(Element tableRow) {
    final UiConfigurationActionBean.MetaDataFormatTransport mdft = new UiConfigurationActionBean()
            .getNewMetadataFormatTransport();

    // Table columns are:
    //   0) Name (text)
    //   1) Version (text)
    //   2) Schema URL (an anchor)
    //   3) Project (text)
    //   4) Collection (text)
    //   5) Item (text)
    //   6) TODO: Verify: Disciplines (text?)
    //   7) Validates (text)
    //   8) Actions (anchors, separated by a '|' character)

    final Elements columns = tableRow.getElementsByTag("td");
    assertNotNull("Unable to parse rows from the Metadata Format Table (div id " + METADATA_FORMAT_TABLE + "): "
            + "No columns were found.", columns);
    assertEquals("Unable to parse rows from the Metadata Format Table (div id " + METADATA_FORMAT_TABLE + "): "
            + "Expected 9 columns, but found " + columns.size(), 9, columns.size());

    mdft.setName(columns.get(MDF_NAME_INDEX).text().trim());
    mdft.setVersion(columns.get(MDF_VERSION_INDEX).text().trim());
    mdft.setSchemaURL(columns.get(MDF_SCHEMA_URL_INDEX).select("a").text().trim());
    mdft.setSchemaSource(columns.get(MDF_SCHEMA_URL_INDEX).select("a").attr("href").trim());

    String applies = columns.get(MDF_APPLIES_TO_PROJECT_INDEX).text().trim();

    if (YES.equalsIgnoreCase(applies)) {
        mdft.setAppliesToProject(YES);
    } else if (NO.equalsIgnoreCase(applies)) {
        mdft.setAppliesToProject(NO);
    } else if (NOT_SPECIFIED.equalsIgnoreCase(applies)) {
        mdft.setAppliesToProject(NOT_SPECIFIED);
    }

    applies = columns.get(MDF_APPLIES_TO_COLLECTION_INDEX).text().trim();

    if (YES.equalsIgnoreCase(applies)) {
        mdft.setAppliesToCollection(YES);
    } else if (NO.equalsIgnoreCase(applies)) {
        mdft.setAppliesToCollection(NO);
    } else if (NOT_SPECIFIED.equalsIgnoreCase(applies)) {
        mdft.setAppliesToCollection(NOT_SPECIFIED);
    }

    applies = columns.get(MDF_APPLIES_TO_ITEM_INDEX).text().trim();

    if (YES.equalsIgnoreCase(applies)) {
        mdft.setAppliesToItem(YES);
    } else if (NO.equalsIgnoreCase(applies)) {
        mdft.setAppliesToItem(NO);
    } else if (NOT_SPECIFIED.equalsIgnoreCase(applies)) {
        mdft.setAppliesToItem(NOT_SPECIFIED);
    }

    applies = columns.get(MDF_VALIDATES_INDEX).text().trim();

    if (YES.equalsIgnoreCase(applies)) {
        mdft.setValidates(YES);
    } else if (NO.equalsIgnoreCase(applies)) {
        mdft.setValidates(NO);
    } else if (NOT_SPECIFIED.equalsIgnoreCase(applies)) {
        mdft.setValidates(NOT_SPECIFIED);
    }

    // Get the text of the disciplines.  For example:
    //  "Biology, Social Science"
    String disciplineText = columns.get(MDF_DISCIPLINE_TITLE_INDEX).text().trim();
    String[] disciplineTitles = new String[] {};
    if (disciplineText.length() > 0) {
        disciplineTitles = Pattern.compile(",").split(disciplineText);
    }

    if (disciplineTitles.length > 0) {
        NEXT_TITLE: for (String disciplineTitle : disciplineTitles) {
            for (Discipline d : disciplineDao.list()) {
                if (disciplineTitle.equals(d.getTitle())) {
                    mdft.getDisciplineIds().add(d.getId());
                    continue NEXT_TITLE;
                }
            }
        }
    }

    return mdft;
}

From source file:com.screenslicer.core.util.Util.java

public static WebElement toElement(RemoteWebDriver driver, HtmlNode htmlNode, Element body)
        throws ActionFailed {
    if (body == null) {
        body = Util.openElement(driver, null, null, null);
    }// w  ww.  java2 s. c om
    if (!CommonUtil.isEmpty(htmlNode.id)) {
        WebElement element = toElement(driver, body.getElementById(htmlNode.id));
        if (element != null) {
            return element;
        }
    }
    List<Elements> selected = new ArrayList<Elements>();
    if (!CommonUtil.isEmpty(htmlNode.tagName)) {
        selected.add(body.getElementsByTag(htmlNode.tagName));
    } else if (!CommonUtil.isEmpty(htmlNode.href)) {
        selected.add(body.getElementsByTag("a"));
    }
    if (!CommonUtil.isEmpty(htmlNode.name)) {
        selected.add(body.getElementsByAttributeValue("name", htmlNode.name));
    }
    if (!CommonUtil.isEmpty(htmlNode.type)) {
        selected.add(body.getElementsByAttributeValue("type", htmlNode.type));
    }
    if (!CommonUtil.isEmpty(htmlNode.value)) {
        selected.add(body.getElementsByAttributeValue("value", htmlNode.value));
    }
    if (!CommonUtil.isEmpty(htmlNode.title)) {
        selected.add(body.getElementsByAttributeValue("title", htmlNode.title));
    }
    if (htmlNode.classes != null && htmlNode.classes.length > 0) {
        Map<Element, Integer> found = new HashMap<Element, Integer>();
        for (int i = 0; i < htmlNode.classes.length; i++) {
            Elements elements = body.getElementsByClass(htmlNode.classes[i]);
            for (Element element : elements) {
                if (!found.containsKey(element)) {
                    found.put(element, 0);
                }
                found.put(element, found.get(element) + 1);
            }
        }
        Elements elements = new Elements();
        for (int i = htmlNode.classes.length; i > 0; i--) {
            for (Map.Entry<Element, Integer> entry : found.entrySet()) {
                if (entry.getValue() == i) {
                    elements.add(entry.getKey());
                }
            }
            if (!elements.isEmpty()) {
                break;
            }
        }
        selected.add(elements);
    }
    if (!CommonUtil.isEmpty(htmlNode.href)) {
        Elements hrefs = body.getElementsByAttribute("href");
        Elements toAdd = new Elements();
        String currentUrl = driver.getCurrentUrl();
        String hrefGiven = htmlNode.href;
        for (Element href : hrefs) {
            String hrefFound = href.attr("href");
            if (hrefGiven.equalsIgnoreCase(hrefFound)) {
                toAdd.add(href);
            } else {
                String uriGiven = Util.toCanonicalUri(currentUrl, hrefGiven);
                String uriFound = Util.toCanonicalUri(currentUrl, hrefFound);
                if (uriGiven.equalsIgnoreCase(uriFound)) {
                    toAdd.add(href);
                }
            }
        }
        selected.add(toAdd);
    }
    if (!CommonUtil.isEmpty(htmlNode.innerText)) {
        selected.add(body.getElementsMatchingText(Pattern.quote(htmlNode.innerText)));
    }
    if (htmlNode.multiple != null) {
        selected.add(body.getElementsByAttribute("multiple"));
    }
    Map<Element, Integer> votes = new HashMap<Element, Integer>();
    for (Elements elements : selected) {
        for (Element element : elements) {
            if (!Util.isHidden(element)) {
                if (!votes.containsKey(element)) {
                    votes.put(element, 0);
                }
                votes.put(element, votes.get(element) + 1);
            }
        }
    }
    int maxVote = 0;
    Element maxElement = null;
    for (Map.Entry<Element, Integer> entry : votes.entrySet()) {
        if (entry.getValue() > maxVote) {
            maxVote = entry.getValue();
            maxElement = entry.getKey();
        }
    }
    return toElement(driver, maxElement);
}