Example usage for org.jsoup.select Elements select

List of usage examples for org.jsoup.select Elements select

Introduction

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

Prototype

public Elements select(String query) 

Source Link

Document

Find matching elements within this element list.

Usage

From source file:at.ac.tuwien.dsg.quelle.cloudDescriptionParsers.impl.AmazonCloudJSONDescriptionParser.java

public CloudProvider getCloudProviderDescription() {

    //used to fast add cost properties
    //key is ServiceUnit name = its ID, such as m1.large
    Map<String, CloudOfferedService> units = new HashMap<>();

    //key is ServiceUnit name = its ID, such as m1.large
    Map<String, List<ElasticityCapability.Dependency>> costDependencies = new HashMap<>();

    CloudProvider cloudProvider = new CloudProvider("Amazon EC2", CloudProvider.Type.IAAS);

    cloudProvider.withUuid(UUID.randomUUID());

    //other misc Amazon Services 
    {/*from w  w  w.  j  a v a  2 s. com*/
        //create EBS instance
        CloudOfferedService ebsStorageUtility = new CloudOfferedService("IaaS", "Storage", "EBS");
        ebsStorageUtility.withUuid(UUID.randomUUID());

        cloudProvider.addCloudOfferedService(ebsStorageUtility);
        {
            List<ElasticityCapability.Dependency> qualityCapabilityTargets = new ArrayList<>();

            // utility quality
            Quality stdQuality = new Quality("Standard I/O Performance");
            Metric storageIOPS = new Metric("Storage", "IOPS");
            storageIOPS.setType(Metric.MetricType.QUALITY);
            stdQuality.addProperty(storageIOPS, new MetricValue("100"));
            qualityCapabilityTargets.add(new ElasticityCapability.Dependency(stdQuality,
                    ElasticityCapability.Type.OPTIONAL_ASSOCIATION).withVolatility(new Volatility(1, 1)));
            // utility.addQualityProperty(stdQuality);

            // utility quality
            Quality highQuality = new Quality("High I/O Performance");
            highQuality.addProperty(new Metric("Storage", "IOPS"), new MetricValue("4000"));
            qualityCapabilityTargets.add(new ElasticityCapability.Dependency(highQuality,
                    ElasticityCapability.Type.OPTIONAL_ASSOCIATION).withVolatility(new Volatility(1, 1)));
            // utility.addQualityProperty(highQuality);

            {
                ElasticityCapability characteristic = new ElasticityCapability("Quality");

                characteristic.setPhase(ElasticityCapability.Phase.INSTANTIATION_TIME);

                for (ElasticityCapability.Dependency d : qualityCapabilityTargets) {

                    characteristic.addCapabilityDependency(d);
                }

                ebsStorageUtility.addElasticityCapability(characteristic);
            }

            List<ElasticityCapability.Dependency> costCapabilityTargets = new ArrayList<>();

            CostFunction costFunctionForStdPerformance = new CostFunction("StandardIOPerformanceCost");
            costCapabilityTargets.add(new ElasticityCapability.Dependency(costFunctionForStdPerformance,
                    ElasticityCapability.Type.OPTIONAL_ASSOCIATION).withVolatility(new Volatility(1, 1)));
            {
                // currently Cost is cost unit agnostic?
                CostElement costPerGB = new CostElement("StorageCost",
                        new Metric("diskSize", "GB", Metric.MetricType.COST), CostElement.Type.USAGE)
                                .withBillingCycle(CostElement.BillingCycle.HOUR);
                //convert from month to hour
                costPerGB.addBillingInterval(new MetricValue(1), 0.1 / 30 / 24);
                costFunctionForStdPerformance.addCostElement(costPerGB);
            }

            {
                CostElement costPerIO = new CostElement("I/OCost",
                        new Metric("diskIOCount", "#", Metric.MetricType.COST), CostElement.Type.USAGE);
                costPerIO.addBillingInterval(new MetricValue(1), 0.1);
                costFunctionForStdPerformance.addCostElement(costPerIO);
            }
            costFunctionForStdPerformance.addAppliedIfServiceInstanceUses(stdQuality);
            // utility.addCostFunction(costFunctionForStdPerformance);

            CostFunction costFunctionForMaxPerformance = new CostFunction("HighIOPerformanceCost");
            costCapabilityTargets.add(new ElasticityCapability.Dependency(costFunctionForMaxPerformance,
                    ElasticityCapability.Type.OPTIONAL_ASSOCIATION).withVolatility(new Volatility(1, 1)));
            {
                // currently Cost is cost unit agnostic?
                CostElement costPerGB = new CostElement("StorageCost",
                        new Metric("diskSize", "GB", Metric.MetricType.COST), CostElement.Type.USAGE)
                                .withBillingCycle(CostElement.BillingCycle.HOUR);
                costPerGB.addBillingInterval(new MetricValue(1), 0.125 / 30 / 24);
                costFunctionForMaxPerformance.addCostElement(costPerGB);
            }

            {
                CostElement costPerIO = new CostElement("I/OCost",
                        new Metric("diskIOCount", "#", Metric.MetricType.COST), CostElement.Type.USAGE);
                costPerIO.addBillingInterval(new MetricValue(1), 0.1);
                costFunctionForMaxPerformance.addCostElement(costPerIO);
            }
            costFunctionForMaxPerformance.addAppliedIfServiceInstanceUses(highQuality);
            // utility.addCostFunction(costFunctionForMaxPerformance);

            {

                ElasticityCapability characteristic = new ElasticityCapability("PerformanceCost");
                characteristic.setPhase(ElasticityCapability.Phase.INSTANTIATION_TIME);
                for (ElasticityCapability.Dependency d : costCapabilityTargets) {
                    characteristic.addCapabilityDependency(d);
                }

                ebsStorageUtility.addElasticityCapability(characteristic);
            }
        }
    }

    {
        //Monitoring
        {
            CloudOfferedService utility = new CloudOfferedService("MaaS", "Monitoring", "Monitoring");
            utility.withUuid(UUID.randomUUID());
            cloudProvider.addCloudOfferedService(utility);

            List<ElasticityCapability.Dependency> qualityCapabilityTargets = new ArrayList<>();
            List<ElasticityCapability.Dependency> costCapabilityTargets = new ArrayList<>();

            //utility quality
            Quality stdQuality = new Quality("StdMonitoringFreq");
            stdQuality.addProperty(new Metric("monitoredFreq", "min"), new MetricValue(5));

            qualityCapabilityTargets.add(new ElasticityCapability.Dependency(stdQuality,
                    ElasticityCapability.Type.OPTIONAL_ASSOCIATION).withVolatility(new Volatility(0, 0)));

            //utility quality
            Quality higherQuality = new Quality("HighMonitoringFreq");
            higherQuality.addProperty(new Metric("monitoredFreq", "min"), new MetricValue(1));

            qualityCapabilityTargets.add(new ElasticityCapability.Dependency(higherQuality,
                    ElasticityCapability.Type.OPTIONAL_ASSOCIATION).withVolatility(new Volatility(0, 0)));

            //  quality elasticity
            {
                ElasticityCapability characteristic = new ElasticityCapability("MonitoringQuality");
                characteristic.setPhase(ElasticityCapability.Phase.INSTANTIATION_TIME);

                for (ElasticityCapability.Dependency d : qualityCapabilityTargets) {
                    characteristic.addCapabilityDependency(d);
                }
                utility.addElasticityCapability(characteristic);
            }

            CostFunction costFunctionForStdMonitoring = new CostFunction("StdMonitoringFreqCost");
            {
                //currently Cost is cost unit agnostic?
                CostElement monCost = new CostElement("MonitoringCost",
                        new Metric("monitoringCost", "$/hour", Metric.MetricType.COST), CostElement.Type.USAGE);
                monCost.addBillingInterval(new MetricValue(1), 0.0);
                costFunctionForStdMonitoring.addCostElement(monCost);
                costFunctionForStdMonitoring.addAppliedIfServiceInstanceUses(stdQuality);
                costCapabilityTargets.add(new ElasticityCapability.Dependency(costFunctionForStdMonitoring,
                        ElasticityCapability.Type.OPTIONAL_ASSOCIATION).withVolatility(new Volatility(0, 0)));
            }

            CostFunction costFunctionForCustomMonitoring = new CostFunction("HighMonitoringFreqCost");
            {
                CostElement monCost = new CostElement("MonitoringCost",
                        new Metric("monitoringCost", "$/month", Metric.MetricType.COST),
                        CostElement.Type.USAGE);
                monCost.addBillingInterval(new MetricValue(1), 3.5);
                costFunctionForCustomMonitoring.addCostElement(monCost);
                costFunctionForCustomMonitoring.addAppliedIfServiceInstanceUses(higherQuality);
                costCapabilityTargets.add(new ElasticityCapability.Dependency(costFunctionForCustomMonitoring,
                        ElasticityCapability.Type.OPTIONAL_ASSOCIATION).withVolatility(new Volatility(0, 0)));
            }

            //cost   elasticity
            {
                ElasticityCapability characteristic = new ElasticityCapability("MonitoringCost");
                characteristic.setPhase(ElasticityCapability.Phase.INSTANTIATION_TIME);

                for (ElasticityCapability.Dependency d : costCapabilityTargets) {
                    characteristic.addCapabilityDependency(d);
                }
                utility.addElasticityCapability(characteristic);
            }

        }
    }

    {
        //Amazon SQS 
        {
            CloudOfferedService sqs = new CloudOfferedService("PaaS", "CommunicationServices", "SimpleQueue");
            sqs.withUuid(UUID.randomUUID());
            cloudProvider.addCloudOfferedService(sqs);

            //utility quality
            Resource resource = new Resource("MessagingService");
            resource.addProperty(new Metric("message", "queue"), new MetricValue(""));

            sqs.addResourceProperty(resource);

            CostFunction messagingCost = new CostFunction("MessagingCostFct");
            {
                //currently Cost is cost unit agnostic?
                {
                    CostElement d = new CostElement("MessagingCost",
                            new Metric("messages", "#", Metric.MetricType.COST), CostElement.Type.USAGE);
                    d.addBillingInterval(new MetricValue(1), 0.5);
                    messagingCost.addCostElement(d);
                }
            }

            sqs.addCostFunction(messagingCost);
        }
    }

    //put old instance types
    try {
        Document doc = Jsoup.connect(amazonPreviousInstanceTypesURL).get();
        Elements tableElements = doc.select("div.aws-table*").get(5).getElementsByTag("table");

        Elements tableHeaderEles = tableElements.select("thead tr th");
        System.out.println("headers");
        for (int i = 0; i < tableHeaderEles.size(); i++) {
            System.out.println(tableHeaderEles.get(i).text());
        }
        System.out.println();

        Elements tableRowElements = tableElements.select(":not(thead) tr");
        Elements headers = tableRowElements.get(0).select("td");

        //at i = 0 is the HEADER of the table
        for (int i = 1; i < tableRowElements.size(); i++) {

            //for each row we create another ServiceUnit
            ServiceUnitBuilder builder = new ServiceUnitBuilder("IaaS", "VM");

            Element row = tableRowElements.get(i);
            System.out.println("row");

            Elements rowItems = row.select("td");
            for (int j = 0; j < rowItems.size(); j++) {
                //* marks notes, such as 1 *1 (note 1)
                String value = rowItems.get(j).text().split("\\*")[0];

                //do not know why, for large VMs amazon says 24 x 2,048 GB
                value = value.replaceAll(",", "");
                String propertyName = headers.get(j).text();
                if (builder.getPropertyNames().contains(propertyName)) {
                    builder.addProperty(propertyName, value);
                } else {
                    log.error("Property {} not found in property builder", propertyName);
                }
            }
            CloudOfferedService unit = builder.getUnit();
            unit.withUuid(UUID.randomUUID());
            cloudProvider.addCloudOfferedService(unit);
            units.put(unit.getName(), unit);

            System.out.println();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    //put new instance types
    try {
        Document doc = Jsoup.connect(amazonInstanceTypesURL).get();
        Elements tableElements = doc.select("div.aws-table*").get(8).getElementsByTag("table");

        Elements tableHeaderEles = tableElements.select("thead tr th");
        System.out.println("headers");
        for (int i = 0; i < tableHeaderEles.size(); i++) {
            System.out.println(tableHeaderEles.get(i).text());
        }
        System.out.println();

        Elements tableRowElements = tableElements.select(":not(thead) tr");
        Elements headers = tableRowElements.get(0).select("td");

        //at i = 0 is the HEADER of the table
        for (int i = 1; i < tableRowElements.size(); i++) {

            //for each row we create another ServiceUnit
            ServiceUnitBuilder builder = new ServiceUnitBuilder("IaaS", "VM");

            Element row = tableRowElements.get(i);
            System.out.println("row");

            Elements rowItems = row.select("td");
            for (int j = 0; j < rowItems.size(); j++) {
                //* marks notes, such as 1 *1 (note 1)
                String value = rowItems.get(j).text().split("\\*")[0];

                //do not know why, for large VMs amazon says 24 x 2,048 GB
                value = value.replaceAll(",", "");
                String propertyName = headers.get(j).text();
                if (builder.getPropertyNames().contains(propertyName)) {
                    builder.addProperty(propertyName, value);
                } else {
                    log.error("Property {} not found in property builder", propertyName);
                }
            }
            CloudOfferedService unit = builder.getUnit();
            unit.withUuid(UUID.randomUUID());
            cloudProvider.addCloudOfferedService(unit);
            units.put(unit.getName(), unit);

            System.out.println();
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    //spot price http://spot-price.s3.amazonaws.com/spot.js
    //on demand http://a0.awsstatic.com/pricing/1/ec2/linux-od.min.js
    //Reserved light a0.awsstatic.com/pricing/1/ec2/linux-ri-light.min.js 
    //Reserved medium a0.awsstatic.com/pricing/1/ec2/linux-ri-medium.min.js
    //Reserved heavy a0.awsstatic.com/pricing/1/ec2/linux-ri-heavy.min.js .
    //get on demand price
    //spot price http://a0.awsstatic.com/pricing/1/ec2/linux-od.min.js
    try {
        //get reserved light utilization
        addReservedCostOptions("LightUtilization", amazonInstancesReservedLightUtilizationCostURL,
                cloudProvider, units, costDependencies);
    } catch (IOException | ParseException ex) {
        Logger.getLogger(AmazonCloudJSONDescriptionParser.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        addReservedCostOptions("MediumUtilization", amazonInstancesReservedMediumUtilizationCostURL,
                cloudProvider, units, costDependencies);
        //            addReservedCostOptions("MediumUtilization", "http://s3.amazonaws.com/aws-assets-pricing-prod/pricing/ec2/SF-Summit-2014/medium_linux.js", cloudProvider, units, costDependencies);
    } catch (IOException | ParseException ex) {
        Logger.getLogger(AmazonCloudJSONDescriptionParser.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        addReservedCostOptions("HeavyUtilization", amazonInstancesReservedHeavyUtilizationCostURL,
                cloudProvider, units, costDependencies);
        //            addReservedCostOptions("HeavyUtilization", "http://s3.amazonaws.com/aws-assets-pricing-prod/pricing/ec2/SF-Summit-2014/heavy_linux.js", cloudProvider, units, costDependencies);
    } catch (IOException | ParseException ex) {
        Logger.getLogger(AmazonCloudJSONDescriptionParser.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        addOndemandCostOptions(amazonInstancesOndemandCostURL, cloudProvider, units, costDependencies);
        //            addReservedCostOptions("HeavyUtilization", "http://s3.amazonaws.com/aws-assets-pricing-prod/pricing/ec2/SF-Summit-2014/heavy_linux.js", cloudProvider, units, costDependencies);
    } catch (IOException | ParseException ex) {
        Logger.getLogger(AmazonCloudJSONDescriptionParser.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        addSpotCostOptions(amazonInstancesSpotCostURL, cloudProvider, units, costDependencies);
    } catch (IOException | ParseException ex) {
        Logger.getLogger(AmazonCloudJSONDescriptionParser.class.getName()).log(Level.SEVERE, null, ex);
    }
    //add for each unit its cost elasticity dependencies
    {
        for (String suName : costDependencies.keySet()) {

            //currently due to Neo4J accesss tyle implemented by me, i need unique names for Cost elasticity dependencies.
            ElasticityCapability characteristic = new ElasticityCapability("Cost_" + suName);
            characteristic.setPhase(ElasticityCapability.Phase.INSTANTIATION_TIME);

            for (ElasticityCapability.Dependency d : costDependencies.get(suName)) {
                characteristic.addCapabilityDependency(d);
            }
            CloudOfferedService unit = units.get(suName);
            unit.addElasticityCapability(characteristic);
        }
    }

    //remove units to see outcome
    //        cloudProvider.setServiceUnits(cloudProvider.getServiceUnits().subList(4, 6));
    return cloudProvider;

}

From source file:cn.wanghaomiao.xpath.core.XpathEvaluator.java

/**
 * ?xpath//from w ww  .ja  va2s.c  om
 *
 * @param xpath
 * @param root
 * @return
 */
public List<JXNode> evaluate(String xpath, Elements root) throws NoSuchAxisException, NoSuchFunctionException {
    List<JXNode> res = new LinkedList<JXNode>();
    Elements context = root;
    List<Node> xpathNodes = getXpathNodeTree(xpath);
    for (int i = 0; i < xpathNodes.size(); i++) {
        Node n = xpathNodes.get(i);
        LinkedList<Element> contextTmp = new LinkedList<Element>();
        if (n.getScopeEm() == ScopeEm.RECURSIVE || n.getScopeEm() == ScopeEm.CURREC) {
            if (n.getTagName().startsWith("@")) {
                for (Element e : context) {
                    //?
                    String key = n.getTagName().substring(1);
                    if (key.equals("*")) {
                        res.add(JXNode.t(e.attributes().toString()));
                    } else {
                        String value = e.attr(key);
                        if (StringUtils.isNotBlank(value)) {
                            res.add(JXNode.t(value));
                        }
                    }
                    //??
                    for (Element dep : e.getAllElements()) {
                        if (key.equals("*")) {
                            res.add(JXNode.t(dep.attributes().toString()));
                        } else {
                            String value = dep.attr(key);
                            if (StringUtils.isNotBlank(value)) {
                                res.add(JXNode.t(value));
                            }
                        }
                    }
                }
            } else if (n.getTagName().endsWith("()")) {
                //??text()
                res.add(JXNode.t(context.text()));
            } else {
                Elements searchRes = context.select(n.getTagName());
                for (Element e : searchRes) {
                    Element filterR = filter(e, n);
                    if (filterR != null) {
                        contextTmp.add(filterR);
                    }
                }
                context = new Elements(contextTmp);
                if (i == xpathNodes.size() - 1) {
                    for (Element e : contextTmp) {
                        res.add(JXNode.e(e));
                    }
                }
            }

        } else {
            if (n.getTagName().startsWith("@")) {
                for (Element e : context) {
                    String key = n.getTagName().substring(1);
                    if (key.equals("*")) {
                        res.add(JXNode.t(e.attributes().toString()));
                    } else {
                        String value = e.attr(key);
                        if (StringUtils.isNotBlank(value)) {
                            res.add(JXNode.t(value));
                        }
                    }
                }
            } else if (n.getTagName().endsWith("()")) {
                res = (List<JXNode>) callFunc(n.getTagName().substring(0, n.getTagName().length() - 2),
                        context);
            } else {
                for (Element e : context) {
                    Elements filterScope = e.children();
                    if (StringUtils.isNotBlank(n.getAxis())) {
                        filterScope = getAxisScopeEls(n.getAxis(), e);
                    }
                    for (Element chi : filterScope) {
                        Element fchi = filter(chi, n);
                        if (fchi != null) {
                            contextTmp.add(fchi);
                        }
                    }
                }
                context = new Elements(contextTmp);
                if (i == xpathNodes.size() - 1) {
                    for (Element e : contextTmp) {
                        res.add(JXNode.e(e));
                    }
                }
            }
        }
    }
    return res;
}

From source file:com.jp.miaulavirtual.DisplayMessageActivity.java

public void asigsToArray(Elements melem, Boolean isHome, Boolean comun) throws IndexOutOfBoundsException {
    int i = 1;/*from w  w  w  .  j a  v  a 2s  . c o m*/
    Elements elem;
    if (isHome) {
        elem = melem.select("td[headers=contents_name] a, td[headers=folders_name] a").not("[href*=/clubs/]"); //Nombre Asignaturas String !"Comunuidades"
        names = new String[(elem.size()) + 1]; //todo-comunidades + 1(carpeta comunidades)
        names[0] = "Comunidades y otros";
        for (Element el : elem) {
            names[i] = el.text();
            i++;
        }
    } else if (comun) {
        elem = melem.select(
                "td[headers=contents_name] a[href*=/clubs/], td[headers=folders_name] a[href*=/clubs/]"); //Nombre Asignaturas String "Comunuidades"
        names = new String[elem.size() + 1]; //comunidades + 1(atrs)
        names[0] = "Atrs " + onData.get(onData.size() - 2)[1];
        for (Element el : elem) {
            names[i] = el.text();
            i++;
        }
    } else {
        elem = melem.select("td[headers=contents_name] a[href], td[headers=folders_name] a[href]"); //Nombre Asignaturas String 
        names = new String[elem.size() + 1]; //todo + 1 (atrs)
        names[0] = "Atrs " + onData.get(onData.size() - 2)[1];
        for (Element el : elem) {
            names[i] = el.text();
            i++;
        }
    }
    Log.d("asigseToArray", String.valueOf(elem.size()));
}

From source file:com.jp.miaulavirtual.DisplayMessageActivity.java

public void urlsToArray(Elements melem, Boolean isHome, Boolean comun) {
    Elements elem;/*w  w w. j a  va2s. co m*/
    int i = 1;
    if (isHome) {
        elem = melem.select("td[headers=contents_name] a, td[headers=folders_name] a").not("[href*=/clubs/]"); //Nombre Asignaturas String !"Comunuidades"
        urls = new String[(elem.size()) + 1];
        urls[0] = "/dotlrn/?page_num=" + panel;
        for (Element el : elem) {
            urls[i] = el.select("a").attr("href");
            i++;
        }
    } else if (comun) {
        elem = melem.select(
                "td[headers=contents_name] a[href*=/clubs/], td[headers=folders_name] a[href*=/clubs/]"); //Nombre Asignaturas String "Comunuidades"
        urls = new String[elem.size() + 1];
        urls[0] = onData.get(onData.size() - 2)[0];
        for (Element el : elem) {
            urls[i] = el.select("a").attr("href");
            i++;
        }
    } else {
        elem = melem.select("td[headers=contents_name] a[href], td[headers=folders_name] a[href]"); //Nombre Asignaturas String 
        urls = new String[elem.size() + 1];
        urls[0] = onData.get(onData.size() - 2)[0];
        for (Element el : elem) {
            urls[i] = el.select("a").attr("href");
            i++;
        }
    }
    Log.d("urlsToArray", String.valueOf(urls.length));
}

From source file:com.jp.miaulavirtual.DisplayMessageActivity.java

public void typeToArray(Elements melem, Boolean isHome, Boolean comun, int size) {
    Elements elem = melem.select("td[headers=folders_type], td[headers=contents_type]"); //Nombre Asignaturas String
    String[] mtypes = { "carpeta", "Carpeta", "PDF", "Microsoft Excel", "Microsoft PowerPoint",
            "Microsoft Word" };
    int i = 1;// www.j  av  a  2  s  . c o  m
    if (isHome) {
        types = new String[size];
        types[0] = "6";
        while (i < size) {
            types[i] = "1";
            i++;
        }
    } else if (comun && onData.size() < 3) {
        types = new String[size];
        types[0] = "0";
        while (i < size) {
            types[i] = "6";
            i++;
        }
    } else {
        types = new String[elem.size() + 1];
        types[0] = "0";
        for (Element el : elem) {
            String the_types = el.text().trim();
            types[i] = "7"; // Defecto al menos que...:
            if (mtypes[0].equals(the_types.toString()) || mtypes[1].equals(the_types.toString()))
                types[i] = "1"; // 1 = Carpeta
            if (mtypes[2].equals(the_types.toString()))
                types[i] = "2"; // 2 = PDF 
            if (mtypes[3].equals(the_types.toString()))
                types[i] = "3"; // 3 = Excel
            if (mtypes[4].equals(the_types.toString()))
                types[i] = "4"; // 4 = Power Point
            if (mtypes[5].equals(the_types.toString()))
                types[i] = "5"; // 5 = Word
            i++;
        }
    }
}

From source file:com.lloydtorres.stately.issues.IssueDecisionActivity.java

/**
 * Process the received page into the Issue and its IssueOptions
 * @param v Activity view/*from   w w  w .  j  a  v a2  s. co m*/
 * @param d Document received from NationStates
 */
private void processIssueInfo(View v, Document d) {
    // First check if the issue is still available
    if (d.text().contains(NOT_AVAILABLE)) {
        mSwipeRefreshLayout.setRefreshing(false);
        SparkleHelper.makeSnackbar(v,
                String.format(Locale.US, getString(R.string.issue_unavailable), mNation.name));
        return;
    }

    Element issueInfoContainer = d.select("div#dilemma").first();

    if (issueInfoContainer == null) {
        // safety check
        mSwipeRefreshLayout.setRefreshing(false);
        SparkleHelper.makeSnackbar(v, getString(R.string.login_error_parsing));
        return;
    }

    Elements issueInfoRaw = issueInfoContainer.children();

    String issueText = issueInfoRaw.select("p").first().text();
    // If this is an issue chain, grab the second paragraph instead
    if (d.select("div.dilemmachain").first() != null) {
        issueText = issueInfoRaw.select("p").get(1).text();
        if (d.text().contains(STORY_SO_FAR)) {
            issueText = issueText + "<br><br>" + issueInfoRaw.select("p").get(2).text();
        }
    }
    issue.content = issueText;

    issue.options = new ArrayList<IssueOption>();

    Element optionHolderMain = issueInfoRaw.select("ol.diloptions").first();
    if (optionHolderMain != null) {
        Elements optionsHolder = optionHolderMain.select("li");

        int i = 0;
        for (Element option : optionsHolder) {
            IssueOption issueOption = new IssueOption();
            issueOption.index = i++;

            Element button = option.select("button").first();
            if (button != null) {
                issueOption.header = button.attr("name");
            } else {
                issueOption.header = IssueOption.SELECTED_HEADER;
            }

            Element optionContentHolder = option.select("p").first();
            if (optionContentHolder == null) {
                // safety check
                mSwipeRefreshLayout.setRefreshing(false);
                SparkleHelper.makeSnackbar(v, getString(R.string.login_error_parsing));
                return;
            }

            issueOption.content = optionContentHolder.text();
            issue.options.add(issueOption);
        }
    }

    IssueOption dismissOption = new IssueOption();
    dismissOption.index = -1;
    dismissOption.header = IssueOption.DISMISS_HEADER;
    dismissOption.content = "";
    issue.options.add(dismissOption);

    setRecyclerAdapter(issue);
    mSwipeRefreshLayout.setRefreshing(false);
    mSwipeRefreshLayout.setEnabled(false);
}

From source file:com.lloydtorres.stately.issues.IssuesFragment.java

/**
 * Process the HTML contents of the issues into actual Issue objects
 * @param d//from   w  w w. j  a  v a 2 s.c om
 */
private void processIssues(View v, Document d) {
    issues = new ArrayList<Object>();

    Element issuesContainer = d.select("ul.dilemmalist").first();

    if (issuesContainer == null) {
        // safety check
        mSwipeRefreshLayout.setRefreshing(false);
        SparkleHelper.makeSnackbar(v, getString(R.string.login_error_parsing));
        return;
    }

    Elements issuesRaw = issuesContainer.children();

    for (Element i : issuesRaw) {
        Issue issueCore = new Issue();

        Elements issueContents = i.children();

        // Get issue ID and name
        Element issueMain = issueContents.select("a").first();

        if (issueMain == null) {
            continue;
        }

        String issueLink = issueMain.attr("href");
        issueCore.id = Integer.valueOf(issueLink.replace("page=show_dilemma/dilemma=", ""));
        Matcher chainMatcher = CHAIN_ISSUE_REGEX.matcher(issueMain.text());
        if (chainMatcher.find()) {
            issueCore.chain = chainMatcher.group(1);
            issueCore.title = chainMatcher.group(2);
        } else {
            issueCore.title = issueMain.text();
        }

        issues.add(issueCore);
    }

    Element nextIssueUpdate = d.select("p.dilemmanextupdate").first();
    if (nextIssueUpdate != null) {
        String nextUpdate = nextIssueUpdate.text();
        issues.add(nextUpdate);
    }

    if (issuesRaw.size() <= 0) {
        String nextUpdate = getString(R.string.no_issues);

        Matcher m = NEXT_ISSUE_REGEX.matcher(d.html());
        if (m.find()) {
            long nextUpdateTime = Long.valueOf(m.group(1)) / 1000L;
            nextUpdate = String.format(Locale.US, getString(R.string.next_issue),
                    SparkleHelper.getReadableDateFromUTC(getContext(), nextUpdateTime));
        }

        issues.add(nextUpdate);
    }

    if (mRecyclerAdapter == null) {
        mRecyclerAdapter = new IssuesRecyclerAdapter(getContext(), issues, mNation);
        mRecyclerView.setAdapter(mRecyclerAdapter);
    } else {
        ((IssuesRecyclerAdapter) mRecyclerAdapter).setIssueCards(issues);
    }
    mSwipeRefreshLayout.setRefreshing(false);
}

From source file:com.normalexception.app.rx8club.fragment.category.CategoryFragment.java

/**
 * Grab contents from the forum that the user clicked on
 * @param doc      The document parsed from the link
 * @param id      The id number of the link
 * @param isMarket    True if the link is from a marketplace category
 *//*from  w w  w.j  av a 2  s  .com*/
public void getCategoryContents(Document doc, String id, boolean isMarket) {

    // Update pagination
    try {
        Elements pageNumbers = doc.select("div[class=pagenav]");
        Elements pageLinks = pageNumbers.first().select("td[class^=vbmenu_control]");
        thisPage = pageLinks.text().split(" ")[1];
        finalPage = pageLinks.text().split(" ")[3];
    } catch (Exception e) {
    }

    // Make sure id contains only numbers
    if (!isNewTopicActivity)
        id = Utils.parseInts(id);

    // Grab each thread
    Elements threadListing = doc.select("table[id=threadslist] > tbody > tr");

    for (Element thread : threadListing) {
        try {
            boolean isSticky = false, isLocked = false, hasAttachment = false, isAnnounce = false,
                    isPoll = false;
            String formattedTitle = "", postCount = "0", views = "0", forum = "", threadUser = "",
                    lastUser = "", threadLink = "", lastPage = "", totalPosts = "0", threadDate = "";

            Elements announcementContainer = thread.select("td[colspan=5]");
            Elements threadTitleContainer = thread.select("a[id^=thread_title]");

            // We could have two different types of threads.  Announcement threads are 
            // completely different than the other types of threads (sticky, locked, etc)
            // so we need to play some games here
            if (announcementContainer != null && !announcementContainer.isEmpty()) {
                Log.d(TAG, "Announcement Thread Found");

                Elements annThread = announcementContainer.select("div > a");
                Elements annUser = announcementContainer.select("div > span[class=smallfont]");
                formattedTitle = "Announcement: " + annThread.first().text();
                threadUser = annUser.last().text();
                threadLink = annThread.attr("href");
                isAnnounce = true;
            } else if (threadTitleContainer != null && !threadTitleContainer.isEmpty()) {
                Element threadLinkEl = thread.select("a[id^=thread_title]").first();
                Element repliesText = thread.select("td[title^=Replies]").first();
                Element threaduser = thread.select("td[id^=td_threadtitle_] div.smallfont").first();
                Element threadicon = thread.select("img[id^=thread_statusicon_]").first();
                Element threadDiv = thread.select("td[id^=td_threadtitle_] > div").first();
                Element threadDateFull = thread.select("td[title^=Replies:] > div").first();

                try {
                    isSticky = threadDiv.text().contains("Sticky:");
                } catch (Exception e) {
                }

                try {
                    isPoll = threadDiv.text().contains("Poll:");
                } catch (Exception e) {
                }

                try {
                    String icSt = threadicon.attr("src");
                    isLocked = (icSt.contains("lock") && icSt.endsWith(".gif"));
                } catch (Exception e) {
                }

                String preString = "";
                try {
                    preString = threadDiv.select("span > b").text();
                } catch (Exception e) {
                }

                try {
                    hasAttachment = !threadDiv.select("a[onclick^=attachments]").isEmpty();
                } catch (Exception e) {
                }

                // Find the last page if it exists
                try {
                    lastPage = threadDiv.select("span").last().select("a").last().attr("href");
                } catch (Exception e) {
                }

                threadDate = threadDateFull.text();
                int findAMPM = threadDate.indexOf("M") + 1;
                threadDate = threadDate.substring(0, findAMPM);

                String totalPostsInThreadTitle = threadicon.attr("alt");

                if (totalPostsInThreadTitle != null && totalPostsInThreadTitle.length() > 0)
                    totalPosts = totalPostsInThreadTitle.split(" ")[2];

                // Remove page from the link
                String realLink = Utils.removePageFromLink(link);

                if (threadLinkEl.attr("href").contains(realLink) || (isNewTopicActivity || isMarket)) {

                    String txt = repliesText.getElementsByClass("alt2").attr("title");
                    String splitter[] = txt.split(" ", 4);

                    postCount = splitter[1].substring(0, splitter[1].length() - 1);
                    views = splitter[3];

                    try {
                        if (this.isNewTopicActivity)
                            forum = thread.select("td[class=alt1]").last().text();
                    } catch (Exception e) {
                    }

                    formattedTitle = String.format("%s%s%s", isSticky ? "Sticky: " : isPoll ? "Poll: " : "",
                            preString.length() == 0 ? "" : preString + " ", threadLinkEl.text());
                }

                threadUser = threaduser.text();
                lastUser = repliesText.select("a[href*=members]").text();
                threadLink = threadLinkEl.attr("href");
            }

            // Add our thread to our list as long as the thread
            // contains a title
            if (!formattedTitle.equals("")) {
                ThreadModel tv = new ThreadModel();
                tv.setTitle(formattedTitle);
                tv.setStartUser(threadUser);
                tv.setLastUser(lastUser);
                tv.setLink(threadLink);
                tv.setLastLink(lastPage);
                tv.setPostCount(postCount);
                tv.setMyPosts(totalPosts);
                tv.setViewCount(views);
                tv.setLocked(isLocked);
                tv.setSticky(isSticky);
                tv.setAnnouncement(isAnnounce);
                tv.setPoll(isPoll);
                tv.setHasAttachment(hasAttachment);
                tv.setForum(forum);
                tv.setLastPostTime(threadDate);
                threadlist.add(tv);
            } else if (thread.text()
                    .contains(MainApplication.getAppContext().getString(R.string.constantNoUpdate))) {
                Log.d(TAG, String.format("Found End of New Threads after %d threads...", threadlist.size()));
                if (threadlist.size() > 0) {
                    ThreadModel ltv = threadlist.get(threadlist.size() - 1);
                    Log.d(TAG, String.format("Last New Thread '%s'", ltv.getTitle()));
                }

                if (!PreferenceHelper.hideOldPosts(MainApplication.getAppContext()))
                    threadlist.add(new ThreadModel(true));
                else {
                    Log.d(TAG, "User Chose To Hide Old Threads");
                    break;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Error Parsing That Thread...", e);
            Log.d(TAG, "Thread may have moved");
        }
    }
}

From source file:com.normalexception.app.rx8club.fragment.HomeFragment.java

/**
 * Get the forum contents as a Map that links the category names
 * to a list of the forums within each category
 * @param root   The full forum document
 * @return      A map of the categories to the forums
 *///w  w  w  . ja  v a2  s. c om
private void getCategories(Document root) {
    // Grab each category
    Elements categories = root.select("td[class=tcat][colspan=5]");
    Log.d(TAG, "Category Size: " + categories.size());

    // Now grab each section within a category
    Elements categorySections = root.select("tbody[id^=collapseobj_forumbit_]");

    // These should match in size
    if (categories.size() != categorySections.size()) {
        Log.w(TAG, String.format("Size of Categories (%d) doesn't match Category Sections (%d)",
                categories.size(), categorySections.size()));
        return;
    }

    // Iterate over each category
    int catIndex = 0;
    for (Element category : categorySections) {

        CategoryModel cv = new CategoryModel();
        cv.setTitle(categories.get(catIndex++).text());
        mainList.add(cv);

        Elements forums = category.select("tr[align=center]");
        for (Element forum : forums) {
            cv = new CategoryModel();
            List<SubCategoryModel> scvList = cv.getSubCategories();

            // Each forum object should have 5 columns
            Elements columns = forum.select("tr[align=center] > td");
            try {
                if (columns.size() != 5)
                    continue;

                String forum_name = columns.get(HomeFragment.FORUM_NAME).select("strong").text();
                String forum_href = columns.get(HomeFragment.FORUM_NAME).select("a").attr("href");
                String forum_desc = "";
                try {
                    forum_desc = columns.get(HomeFragment.FORUM_NAME).select("div[class=smallfont]").first()
                            .text();
                } catch (NullPointerException npe) {
                    /* Some might not have a desc */ }
                String threads = columns.get(HomeFragment.THREADS_CNT).text();
                String posts = columns.get(HomeFragment.POSTS_CNT).text();

                // Lets grab each subcategory
                Elements subCats = columns.select("tbody a");
                for (Element subCat : subCats) {
                    SubCategoryModel scv = new SubCategoryModel();
                    scv.setLink(subCat.attr("href"));
                    scv.setTitle(subCat.text().toString());
                    scvList.add(scv);
                }

                cv.setTitle(forum_name);
                cv.setThreadCount(threads);
                cv.setPostCount(posts);
                cv.setLink(forum_href);
                cv.setDescription(forum_desc);
                cv.setSubCategories(scvList);
                mainList.add(cv);
            } catch (Exception e) {
                Log.e(TAG, "Error Parsing Forum", e);
            }
        }
    }

    return;
}

From source file:com.normalexception.app.rx8club.fragment.pm.PrivateMessageViewFragment.java

/**
 * Construct the view elements/*  w w w .  j av a  2  s .  com*/
 */
private void constructView() {
    AsyncTask<Void, String, Void> updaterTask = new AsyncTask<Void, String, Void>() {
        @Override
        protected void onPreExecute() {

            loadingDialog = ProgressDialog.show(getActivity(), getString(R.string.loading),
                    getString(R.string.pleaseWait), true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            String link = getArguments().getString("link");
            Document doc = VBForumFactory.getInstance().get(getActivity(),
                    VBForumFactory.getRootAddress() + "/" + link);

            if (doc != null) {
                securityToken = HtmlFormUtils.getInputElementValueByName(doc, "securitytoken");

                pmid = HtmlFormUtils.getInputElementValueByName(doc, "pmid");

                title = HtmlFormUtils.getInputElementValueByName(doc, "title");

                Elements userPm = doc.select("table[id^=post]");
                publishProgress(getString(R.string.asyncDialogLoadingPM));

                // User Control Panel
                Elements userCp = userPm.select("td[class=alt2]");
                Elements userDetail = userCp.select("div[class=smallfont]");
                Elements userSubDetail = userDetail.last().select("div");
                Elements userAvatar = userDetail.select("img[alt$=Avatar]");
                Elements postMessage = doc.select("div[id=post_message_]");

                PMPostModel pv = new PMPostModel();
                pv.setUserName(userCp.select("div[id^=postmenu]").text());
                pv.setIsLoggedInUser(LoginFactory.getInstance().isLoggedIn()
                        ? UserProfile.getInstance().getUsername().equals(pv.getUserName())
                        : false);
                pv.setUserTitle(userDetail.first().text());
                pv.setUserImageUrl(Utils.resolveUrl(userAvatar.attr("src")));
                pv.setPostDate(userPm.select("td[class=thead]").first().text());

                // userSubDetail
                // 0 - full container , full container
                // 1 - Trader Score   , Trader Score
                // 2 - Join Date      , Join Date
                // 3 - Post Count     , Location
                // 4 - Blank          , Post Count
                // 5 -                , Blank || Social
                //
                Iterator<Element> itr = userSubDetail.listIterator();
                while (itr.hasNext()) {
                    String txt = itr.next().text();
                    if (txt.contains("Location:"))
                        pv.setUserLocation(txt);
                    else if (txt.contains("Posts:"))
                        pv.setUserPostCount(txt);
                    else if (txt.contains("Join Date:"))
                        pv.setJoinDate(txt);
                }

                // User Post Content
                pv.setUserPost(formatUserPost(postMessage));

                pmlist.add(pv);

                TextView comment = (TextView) getView().findViewById(R.id.pmitem_comment);
                Elements textarea = doc.select("textarea[id=vB_Editor_QR_textarea]");
                if (textarea != null) {
                    comment.setText(textarea.first().text());
                }

                updateList();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(String... progress) {
            if (loadingDialog != null)
                loadingDialog.setMessage(progress[0]);
        }

        @Override
        protected void onPostExecute(Void result) {
            try {
                loadingDialog.dismiss();
                loadingDialog = null;
            } catch (Exception e) {
                Log.w(TAG, e.getMessage());
            }
        }
    };
    updaterTask.execute();
}