Example usage for org.apache.commons.lang3 StringUtils equalsIgnoreCase

List of usage examples for org.apache.commons.lang3 StringUtils equalsIgnoreCase

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils equalsIgnoreCase.

Prototype

public static boolean equalsIgnoreCase(final CharSequence str1, final CharSequence str2) 

Source Link

Document

Compares two CharSequences, returning true if they represent equal sequences of characters, ignoring case.

null s are handled without exceptions.

Usage

From source file:com.moviejukebox.model.Codec.java

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }// www  . j  a v a2  s .  c o  m

    if (getClass() != obj.getClass()) {
        return false;
    }

    final Codec other = (Codec) obj;
    if (this.codecType != other.codecType) {
        return false;
    }

    if (!StringUtils.equals(this.codec, other.codec)) {
        return false;
    }

    // Are both languages the same? or is either one "UNKNOWN" which we will treat as a match
    if (!(StringUtils.equalsIgnoreCase(this.codecLanguage, other.codecLanguage)
            && StringTools.isValidString(this.codecLanguage)
            && StringTools.isValidString(other.codecLanguage))) {
        return false;
    }

    return this.codecChannels == other.codecChannels;
}

From source file:com.glaf.core.util.HttpQueryUtils.java

public static BaseQuery prepareQuery(HttpServletRequest request, HttpServletResponse response,
        String serviceKey, Map<String, Object> paramMap) {
    BaseQuery query = new BaseQuery();
    Map<String, Object> params = new java.util.HashMap<String, Object>();
    List<QueryCondition> conditions = new java.util.ArrayList<QueryCondition>();
    JSONObject rootJson = new JSONObject();
    JSONObject paramJson = new JSONObject();
    JSONObject queryJson = new JSONObject();

    String qt = getStringValue(request, "qt");
    String qid = getStringValue(request, "qid");
    String field = getStringValue(request, "field");
    boolean removeLast = getBooleanValue(request, "removeLast");

    queryJson.put("removeLast", removeLast);

    if (serviceKey != null) {
        queryJson.put("serviceKey", serviceKey);
    }/*from w w  w.  j a  va  2 s  .co m*/
    if (qt != null) {
        queryJson.put("qt", qt);
    }
    if (qid != null) {
        queryJson.put("qid", qid);
    }
    if (field != null) {
        queryJson.put("field", field);
    }

    /**
     * IP?Cookie
     */
    String ip = RequestUtils.getIPAddress(request);
    String cookieKey = ip + "_mx_query_" + serviceKey;
    cookieKey = Hex.bytesToHex(cookieKey.getBytes());

    String content = null;

    Cookie[] cookies = request.getCookies();
    if (cookies != null && cookies.length > 0) {
        for (Cookie cookie : cookies) {
            /**
             * Cookie???
             */
            if (StringUtils.equals(cookie.getName(), cookieKey)) {
                content = cookie.getValue();
            }
        }
    }

    JSONObject oldJson = null;

    if (StringUtils.isNotEmpty(content)) {
        String str = new String(Hex.hexToBytes(content));
        if (str != null) {
            oldJson = JSON.parseObject(str);
        }
    }

    Object value = null;
    String fieldValue = null;
    QueryCondition currentCondition = null;

    if (oldJson != null) {
        logger.debug("@@previous query json:\n" + oldJson.toJSONString());
        JSONObject paramJx = oldJson.getJSONObject("params");
        if (paramJx != null && !paramJx.isEmpty()) {
            Set<String> keySet = paramJx.keySet();
            Iterator<String> iterator = keySet.iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                if (paramJx.getString(key) != null) {
                    params.put(key, paramJx.getString(key));
                    paramJson.put(key, paramJx.getString(key));
                }
            }
        }

        JSONObject conjx = oldJson.getJSONObject("currentCondition");
        if (conjx != null && conjx.get("value") != null) {
            currentCondition = new QueryCondition();
            currentCondition.setAlias(conjx.getString("alias"));
            currentCondition.setName(conjx.getString("name"));
            currentCondition.setColumn(conjx.getString("column"));
            currentCondition.setType(conjx.getString("type"));
            currentCondition.setFilter(conjx.getString("filter"));
            currentCondition.setStringValue(conjx.getString("stringValue"));
            currentCondition.setValue(conjx.get("value"));
        }
    }

    Enumeration<?> enumeration = request.getParameterNames();
    while (enumeration.hasMoreElements()) {
        String paramName = (String) enumeration.nextElement();
        String paramValue = getStringValue(request, paramName);
        if (paramName != null) {
            if (StringUtils.isNotEmpty(paramValue)) {
                params.put(paramName, paramValue);
                paramJson.put(paramName, paramValue);
            }
        }
    }

    if (paramMap != null && !paramMap.isEmpty()) {
        if (paramMap != null && paramMap.size() > 0) {
            Set<Entry<String, Object>> entrySet = paramMap.entrySet();
            for (Entry<String, Object> entry : entrySet) {
                String name = entry.getKey();
                Object v = entry.getValue();
                if (name != null && v != null) {
                    params.put(name, v);
                    paramJson.put(name, v);
                }
            }
        }
    }

    if (qt == null) {
        qt = (String) params.get("qt");
    }
    if (qid == null) {
        qid = (String) params.get("qid");
    }
    if (field == null) {
        field = (String) params.get("field");
    }

    /**
     * ??
     */
    if (StringUtils.isNotEmpty(qid)) {
        EntityService entityService = ContextFactory.getBean("entityService");
        /**
         * ?????
         */
        List<Object> rows = entityService.getList(qid, params);
        if (rows != null && rows.size() > 0) {
            for (Object object : rows) {
                if (object instanceof ColumnDefinition) {
                    ColumnDefinition column = (ColumnDefinition) object;
                    query.addColumn(column.getName(), column.getColumnName());
                    if (StringUtils.isNotEmpty(field) && StringUtils.equals(field, column.getName())) {
                        fieldValue = request.getParameter(field);
                        if (StringUtils.isNotEmpty(fieldValue)) {
                            String alias = getStringValue(request, "alias");
                            String filter = getStringValue(request, "filter");
                            if (StringUtils.isEmpty(alias)) {
                                alias = getStringValue(request, field + "_alias");
                            }
                            if (StringUtils.isEmpty(filter)) {
                                filter = getStringValue(request, field + "_filter");
                            }
                            String type = column.getJavaType();
                            if (StringUtils.equalsIgnoreCase(type, "datetime")
                                    || StringUtils.equalsIgnoreCase(type, "Date")) {
                                type = "Date";
                                value = fieldValue;
                                if (StringUtils.isEmpty(filter)) {
                                    filter = SearchFilter.GREATER_THAN_OR_EQUAL;
                                }
                            } else if (StringUtils.equalsIgnoreCase(type, "i4")
                                    || StringUtils.equalsIgnoreCase(type, "Integer")) {
                                type = "Integer";
                                value = Integer.parseInt(fieldValue);
                                if (StringUtils.isEmpty(filter)) {
                                    filter = SearchFilter.GREATER_THAN_OR_EQUAL;
                                }
                            } else if (StringUtils.equalsIgnoreCase(type, "i8")
                                    || StringUtils.equalsIgnoreCase(type, "Long")) {
                                type = "Long";
                                value = Long.parseLong(fieldValue);
                                if (StringUtils.isEmpty(filter)) {
                                    filter = SearchFilter.GREATER_THAN_OR_EQUAL;
                                }
                            } else if (StringUtils.equalsIgnoreCase(type, "r8")
                                    || StringUtils.equalsIgnoreCase(type, "Double")) {
                                type = "Double";
                                value = Double.parseDouble(fieldValue);
                                if (StringUtils.isEmpty(filter)) {
                                    filter = SearchFilter.GREATER_THAN_OR_EQUAL;
                                }
                            } else if (StringUtils.equalsIgnoreCase(type, "String")) {
                                type = "String";
                                value = fieldValue;
                                if (StringUtils.isEmpty(filter)) {
                                    filter = SearchFilter.LIKE;
                                }
                                if (StringUtils.equals(filter, SearchFilter.LIKE)) {
                                    value = "%" + fieldValue + "%";
                                }
                            }
                            if (value != null && filter != null) {
                                currentCondition = new QueryCondition();
                                currentCondition.setType(type);
                                currentCondition.setAlias(alias);
                                currentCondition.setName(field);
                                currentCondition.setColumn(column.getColumnName());
                                currentCondition.setFilter(filter);
                                currentCondition.setStringValue(fieldValue);
                                currentCondition.setValue(value);
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * CookieSession??Ta???
     */
    if (oldJson != null) {
        JSONArray array = oldJson.getJSONArray("conditions");
        if (array != null) {
            // logger.debug("previous conditions:" + array.toJSONString());
            int size = array.size();
            for (int i = 0; i < size; i++) {
                JSONObject json = array.getJSONObject(i);
                QueryCondition c = new QueryCondition();
                c.setAlias(json.getString("alias"));
                c.setName(json.getString("name"));
                c.setColumn(json.getString("column"));
                c.setType(json.getString("type"));
                c.setFilter(json.getString("filter"));
                String val = json.getString("stringValue");

                if (StringUtils.equals(c.getType(), "Date")) {
                    c.setValue(DateUtils.toDate(val));
                    c.setStringValue(val);
                } else if (StringUtils.equals(c.getType(), "Integer")) {
                    c.setValue(Integer.parseInt(val));
                    c.setStringValue(val);
                } else if (StringUtils.equals(c.getType(), "Long")) {
                    c.setValue(Long.parseLong(val));
                    c.setStringValue(val);
                } else if (StringUtils.equals(c.getType(), "Double")) {
                    c.setValue(Double.parseDouble(val));
                    c.setStringValue(val);
                } else if (StringUtils.equals(c.getType(), "Boolean")) {
                    c.setValue(Boolean.valueOf(val));
                    c.setStringValue(val);
                } else {
                    c.setValue(json.get("value"));
                    c.setStringValue(val);
                }

                if (!conditions.contains(c)) {
                    conditions.add(c);
                }
            }
        }
        /**
         * ?
         */
        if (removeLast && conditions.size() > 0) {
            conditions.remove(conditions.size() - 1);
        }
    }

    /**
     * ????
     */
    if (StringUtils.equals("R", qt)) {
        logger.debug("#### clear conditions");
        conditions.clear();
    }

    if (currentCondition != null && currentCondition.getValue() != null) {
        query.setCurrentQueryCondition(currentCondition);
        if (!conditions.contains(currentCondition)) {
            conditions.add(currentCondition);
        }
        JSONObject json = new JSONObject();
        if (currentCondition.getAlias() != null) {
            json.put("alias", currentCondition.getAlias());
        }
        json.put("name", currentCondition.getName());
        json.put("column", currentCondition.getColumn());
        json.put("type", currentCondition.getType());
        json.put("filter", currentCondition.getFilter());
        json.put("value", currentCondition.getValue());
        json.put("stringValue", currentCondition.getStringValue());
        json.put("index", 0);
        rootJson.put("currentCondition", json);
    }

    if (conditions.size() > 0) {
        JSONArray jsonArray = new JSONArray();
        int index = 0;
        for (QueryCondition c : conditions) {
            if (c.getValue() != null) {
                JSONObject json = new JSONObject();
                if (c.getAlias() != null) {
                    json.put("alias", c.getAlias());
                }
                json.put("name", c.getName());
                json.put("column", c.getColumn());
                json.put("type", c.getType());
                json.put("filter", c.getFilter());
                json.put("value", c.getValue());
                json.put("stringValue", c.getStringValue());
                json.put("index", index++);
                jsonArray.add(json);
            }
        }
        rootJson.put("conditions", jsonArray);
    }

    rootJson.put("query", queryJson);
    rootJson.put("params", paramJson);

    String jsonText = rootJson.toJSONString();
    logger.debug("prepare query json:\n" + jsonText);

    jsonText = Hex.bytesToHex(jsonText.getBytes());

    if (response != null) {
        Cookie cookie = new Cookie(cookieKey, jsonText);
        response.addCookie(cookie);
    }

    query.setParameter(params);
    query.getParameters().putAll(params);

    logger.debug("#conditions:" + conditions);

    for (QueryCondition condition : conditions) {
        query.addCondition(condition);
    }

    return query;
}

From source file:com.nridge.core.base.field.Field.java

/**
 * Returns the field operator matching the string representation.
 *
 * @param anOperator String representation of a field operator.
 *
 * @return Field operator./*w ww .  j  a v a 2 s  .co m*/
 */
public static Operator stringToOperator(String anOperator) {
    if (StringUtils.equalsIgnoreCase(anOperator, "Equal"))
        return Operator.EQUAL;
    else if (StringUtils.equalsIgnoreCase(anOperator, "NotEqual"))
        return Operator.NOT_EQUAL;
    else if (StringUtils.equalsIgnoreCase(anOperator, "GreaterThan"))
        return Operator.GREATER_THAN;
    else if (StringUtils.equalsIgnoreCase(anOperator, "GreaterThanEqual"))
        return Operator.GREATER_THAN_EQUAL;
    else if (StringUtils.equalsIgnoreCase(anOperator, "LessThan"))
        return Operator.LESS_THAN;
    else if (StringUtils.equalsIgnoreCase(anOperator, "LessThanEqual"))
        return Operator.LESS_THAN_EQUAL;
    else if (StringUtils.equalsIgnoreCase(anOperator, "Contains"))
        return Operator.CONTAINS;
    else if (StringUtils.equalsIgnoreCase(anOperator, "NotContains"))
        return Operator.NOT_CONTAINS;
    else if (StringUtils.equalsIgnoreCase(anOperator, "StartsWith"))
        return Operator.STARTS_WITH;
    else if (StringUtils.equalsIgnoreCase(anOperator, "NotStartsWith"))
        return Operator.NOT_STARTS_WITH;
    else if (StringUtils.equalsIgnoreCase(anOperator, "EndsWith"))
        return Operator.ENDS_WITH;
    else if (StringUtils.equalsIgnoreCase(anOperator, "NotEndsWith"))
        return Operator.NOT_ENDS_WITH;
    else if (StringUtils.equalsIgnoreCase(anOperator, "Between"))
        return Operator.BETWEEN;
    else if (StringUtils.equalsIgnoreCase(anOperator, "NotBetween"))
        return Operator.NOT_BETWEEN;
    else if (StringUtils.equalsIgnoreCase(anOperator, "BetweenInclusive"))
        return Operator.BETWEEN_INCLUSIVE;
    else if (StringUtils.equalsIgnoreCase(anOperator, "RegEx"))
        return Operator.REGEX;
    else if (StringUtils.equalsIgnoreCase(anOperator, "Empty"))
        return Operator.EMPTY;
    else if (StringUtils.equalsIgnoreCase(anOperator, "NotEmpty"))
        return Operator.NOT_EMPTY;
    else if (StringUtils.equalsIgnoreCase(anOperator, "And"))
        return Operator.AND;
    else if (StringUtils.equalsIgnoreCase(anOperator, "Or"))
        return Operator.OR;
    else if (StringUtils.equalsIgnoreCase(anOperator, "In"))
        return Operator.IN;
    else if (StringUtils.equalsIgnoreCase(anOperator, "NotIn"))
        return Operator.NOT_IN;
    else if (StringUtils.equalsIgnoreCase(anOperator, "Sort"))
        return Operator.SORT;
    else
        return Operator.UNDEFINED;
}

From source file:com.hybris.mobile.data.WebService.java

@Override
public String updateProductInCartAtEntry(int quantity, int carEntryNumber) throws Exception {
    Bundle parameters = new Bundle();
    parameters.putString("qty", quantity + "");
    try {/*w  w  w .  ja v  a2  s  .c o  m*/
        String response = WebServiceDataProvider.getResponse(mContext, urlForCartEntry() + "/" + carEntryNumber,
                false, "PUT", parameters);

        ProductStockLevelStatus productStockLevelStatus = JsonUtils.fromJson(response,
                ProductStockLevelStatus.class);

        if (productStockLevelStatus != null
                && StringUtils.equalsIgnoreCase(productStockLevelStatus.getStatusCode(), "success")) {
            return response;
        } else {
            throw new Exception(response);
        }

    } catch (Exception e) {
        LoggingUtils.e(LOG_CAT, e.getLocalizedMessage(), null);
        throw new Exception(e);
    }
}

From source file:com.xpn.xwiki.XWikiContext.java

/**
 * @param wikiName the name of the wiki.
 * @return true it's main wiki's context, false otherwise.
 *///from w w w .j  ava 2s . c  o  m
public boolean isMainWiki(String wikiName) {
    return (getWiki() != null && !getWiki().isVirtualMode())
            || StringUtils.equalsIgnoreCase(wikiName, getMainXWiki());
}

From source file:com.nridge.core.base.io.xml.DocumentXML.java

private void loadACL(Document aDocument, Element anElement) throws IOException {
    Node nodeItem;/*w  ww.j  av  a2 s  .co m*/
    Element nodeElement;
    String nodeName, aceName, aceValue;

    HashMap<String, String> docACL = aDocument.getACL();
    NodeList nodeList = anElement.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        nodeItem = nodeList.item(i);

        if (nodeItem.getNodeType() == Node.ELEMENT_NODE) {
            nodeName = nodeItem.getNodeName();
            if (StringUtils.equalsIgnoreCase(nodeName, IO.XML_ACE_NODE_NAME)) {
                nodeElement = (Element) nodeItem;
                aceName = nodeElement.getAttribute("name");
                aceValue = XMLUtl.getNodeStrValue(nodeItem);
                docACL.put(aceName, aceValue);
            }
        }
    }
}

From source file:com.quinsoft.zeidon.standardoe.ActivateOisFromJsonStream.java

private boolean readOi() throws Exception {
    JsonToken token = jp.nextToken();//from  ww w.  jav  a2 s.c om

    // If we find the end of the OI array then that's the end of OIs.
    if (token == JsonToken.END_ARRAY)
        return false; // No more OIs in the stream.

    if (token != JsonToken.START_OBJECT)
        throw new ZeidonException("OI JSON stream doesn't start with object.");

    token = jp.nextToken();

    String fieldName = jp.getCurrentName();
    if (StringUtils.equals(fieldName, ".oimeta"))
        token = readOiMeta();
    else
        throw new ZeidonException(".oimeta object not specified in JSON stream");

    // If the token after reading the .oimeta is END_OBJECT then the OI is empty.
    if (token != JsonToken.END_OBJECT) {
        fieldName = jp.getCurrentName();
        if (!StringUtils.equalsIgnoreCase(fieldName, lodDef.getRoot().getName()))
            throw new ZeidonException("First entity specified in OI (%s) is not the root (%s)", fieldName,
                    lodDef.getRoot().getName());

        readEntity(fieldName);
        token = jp.nextToken();
    }

    if (selectedInstances.size() > 0)
        setCursors();
    else
        view.reset();

    if (token != JsonToken.END_OBJECT)
        throw new ZeidonException("OI JSON stream doesn't end with object.");

    if (readOnlyOi)
        ((InternalView) view).getViewImpl().getObjectInstance().setReadOnly(true);

    if (readOnly)
        view.setReadOnly(true);

    if (totalRootCount != null)
        view.setTotalRootCount(totalRootCount);

    return true; // Keep looking for OIs in the stream.
}

From source file:crawler.HackerEarthCrawler.java

@Override
public void crawl() {

    int flag = 0;

    //set of urls which should be crawled
    TreeSet<String> linksset = new TreeSet<String>();
    TreeSet<String> tempset = new TreeSet<String>();
    TreeSet<String> tutorialset = new TreeSet<String>();
    //final set of problem urls
    TreeSet<String> problemset = new TreeSet<String>();
    //visited for maintaing status of if url is already crawled or not
    TreeMap<String, Integer> visited = new TreeMap<String, Integer>();

    //add base url
    linksset.add(baseUrl);/*  w w w  .j a  v a2  s  .com*/
    //mark base url as not crawled
    visited.put(baseUrl, 0);

    try {
        while (true) {
            flag = 0;
            tempset.clear();

            for (String str : linksset) {
                //check if url is already crawled or not and it has valid domain name
                if ((visited.get(str) == 0) && (str.startsWith("https://www.hackerearth.com/"))) {
                    System.out.println("crawling  " + str);

                    //retriving response of current url as document
                    Document doc = Jsoup.connect(str).timeout(0).userAgent(
                            "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
                            .referrer("http://www.google.com").ignoreHttpErrors(true).get();
                    //retriving all urls from current page
                    Elements links = doc.select("a[href]");

                    //mark url as crawled
                    visited.put(str, 1);

                    //mark flag as url is crawled
                    flag = 1;
                    //retrive all urls
                    for (Element link : links) {
                        if (link.absUrl("href").endsWith("/tutorial/")) {
                            tutorialset.add(link.absUrl("href"));
                        }
                        //check if url is problem url then add it in problemurlset
                        if (link.absUrl("href").startsWith("https://www.hackerearth.com/")
                                && isProblemUrl(link.absUrl("href"))) {
                            problemset.add(link.absUrl("href"));
                        }
                        //check if url has valid domain and it has problem urls or not
                        if (link.absUrl("href").contains(("https://www.hackerearth.com/"))
                                && isCrawlable(link.absUrl("href"))) {
                            //if link is not visited then mark it as uncrawled
                            if (!visited.containsKey(link.absUrl("href"))) {
                                visited.put(link.absUrl("href"), 0);
                            }
                            //add it in tempsetorary set
                            tempset.add(link.absUrl("href"));
                            //System.out.println("\n  base: "+str+" ::: link  : " + link.absUrl("href"));
                        }
                    }
                }
            }
            //if nothing is left to crawl break the loop
            if (flag == 0) {
                break;
            }
            //add all retrieved links to linksset
            linksset.addAll(tempset);
        }

        System.out.println("\n\ntotal problem urls " + problemset.size());

        int i = 0;
        for (String str : problemset) {
            System.out.println("link " + i + " : " + str);
            i++;
        }

    } catch (IOException ex) {
        Logger.getLogger(HackerEarthCrawler.class.getName()).log(Level.SEVERE, null, ex);
    }

    //scrap and store into database
    //for every problem url scrap problem page
    for (String problemUrl : problemset) {

        System.out.println("problemUrl :" + problemUrl);
        try {
            //create problem class to store in database
            Problem problem = new Problem();
            String problemSIOC = "", problemIOC = "";
            String problemTitle = "", problemStatement = "", problemInput = "", problemOutput = "",
                    problemConstraints = "";
            String sampleInput = "", sampleOutput = "";
            String problemExplanation = "";
            //set default timelimit to 1 second
            double problemTimeLimit = 1.0;
            ArrayList<String> tags = new ArrayList<String>();

            //get response for given problem url
            Response response = Jsoup.connect(problemUrl).execute();
            Document doc = response.parse();

            //retrieve problem title from page
            Element elementTitle = doc.getElementsByTag("title").first();
            StringTokenizer stTitle = new StringTokenizer(elementTitle.text(), "|");
            problemTitle = stTitle.nextToken().trim();

            Element content = doc.getElementsByClass("starwars-lab").first();
            problemSIOC = content.text();
            Elements e = content.children();

            //to find problem statement
            String breakloop[] = { "input", "input:", "input :", "input format:", "input format :",
                    "input format", "Input and output", "constraints :", "constraints:", "constraints",
                    "$$Input :$$" };
            flag = 0;
            for (Element p : e) {
                String tempStatement = "";
                for (Element pp : p.getAllElements()) {

                    for (String strbreak : breakloop) {
                        if (StringUtils.equalsIgnoreCase(pp.ownText(), strbreak)) {
                            //System.out.println("strbreak :"+strbreak);

                            tempStatement = p.text().substring(0,
                                    p.text().toLowerCase().indexOf(strbreak.toLowerCase()));
                            // System.out.println("temp "+tempStatement);
                            flag = 1;
                            break;
                        }
                    }
                }

                if (flag == 1) {
                    problemStatement += tempStatement;
                    //remove extra space at end
                    if (tempStatement.length() == 0) {
                        problemStatement = problemStatement.substring(0, problemStatement.length() - 1);
                    }
                    break;
                }
                problemStatement += p.text() + " ";
            }

            System.out.println("problemSIOC :" + problemSIOC);
            System.out.println("problemStatement :" + problemStatement);

            if (problemStatement.length() <= problemSIOC.length()) {
                //remove problem statement from whole text and remove extra spaces at the beginning and the end
                problemIOC = problemSIOC.substring(problemStatement.length()).trim();
            } else {
                problemIOC = "";
            }

            System.out.println("problemIOC :" + problemIOC);

            //keywords for identifying input
            String decideInput[] = { "Input format :", "Input format:", "Input format", "inputformat:",
                    "inputformat :", "inputformat", "input and output", "input :", "input:", "input" };
            //keywords for identifying output
            String decideOutput[] = { "output format :", "output format:", "Output format", "outputformat:",
                    "outputformat :", "outputformat", "output :", "output:", "output" };
            //keywords for identifying constraint
            String decideConstraint[] = { "constraints:", "constraints :", "constraints", "Constraints :",
                    "constraint:", "constraint :", "constraint", "Contraints :" };

            int posin = 0, posoutput = 0, poscon = 0, idxin, idxout, idxcon, flaginput = 0, flagoutput = 0,
                    flagcon = 0, inlen = 0, outlen = 0, conlen = 0;

            //find inputformat position,length of keyword
            for (idxin = 0; idxin < decideInput.length; idxin++) {
                if (StringUtils.containsIgnoreCase(problemIOC, decideInput[idxin])) {

                    posin = problemIOC.toLowerCase().indexOf(decideInput[idxin].toLowerCase());
                    flaginput = 1;
                    inlen = decideInput[idxin].length();

                    //decide it is keyowrd for actucal input or it is "sample input"
                    if (StringUtils.containsIgnoreCase(problemIOC, "sample input")) {
                        if (posin > problemIOC.toLowerCase().indexOf("sample input")) {
                            flaginput = 0;
                            inlen = 0;
                        } else {
                            break;
                        }
                    } else {
                        break;
                    }
                }
            }

            //find outputformat position,length of keyword
            for (idxout = 0; idxout < decideOutput.length; idxout++) {
                if (StringUtils.containsIgnoreCase(problemIOC, decideOutput[idxout])) {
                    posoutput = problemIOC.toLowerCase().indexOf(decideOutput[idxout].toLowerCase());
                    flagoutput = 1;
                    outlen = decideOutput[idxout].length();
                    break;
                }
            }

            //find constraint position,length of keyword
            for (idxcon = 0; idxcon < decideConstraint.length; idxcon++) {
                if (StringUtils.containsIgnoreCase(problemIOC, decideConstraint[idxcon])) {
                    poscon = problemIOC.toLowerCase().indexOf(decideConstraint[idxcon].toLowerCase());
                    flagcon = 1;
                    conlen = decideConstraint[idxcon].length();
                    break;
                }
            }

            System.out.println("input " + flaginput + " " + inlen + " " + posin);
            System.out.println("output " + flagoutput + " " + outlen + " " + posoutput);
            System.out.println("constraint " + flagcon + " " + conlen + " " + poscon);
            //retrieve problem input and output if present in problem page

            //if input format is present
            if (flaginput == 1) {
                //if input keyword is "input and output" and contraint is present in problem page
                if (idxin == 6 && flagcon == 1) {
                    problemInput = problemIOC.substring(inlen, poscon);
                }
                //if input keyword is "input and output" and contraint is not present in problem page
                else if (idxin == 6 && flagcon == 0) {
                    problemInput = problemIOC.substring(inlen);
                }
                //if output format and constraint is present
                else if (flagoutput == 1 && flagcon == 1) {
                    //if constraint is present before input format
                    if (poscon < posin) {
                        problemInput = problemIOC.substring(posin + inlen, posoutput);
                        problemOutput = problemIOC.substring(posoutput + outlen);
                    }
                    //if constraint is present before sample
                    else if (poscon < posoutput) {
                        problemInput = problemIOC.substring(inlen, poscon);
                        problemOutput = problemIOC.substring(posoutput + outlen);
                    } else {
                        problemInput = problemIOC.substring(inlen, posoutput);
                        problemOutput = problemIOC.substring(posoutput + outlen, poscon);
                    }
                }
                //if constraint is not present
                else if (flagoutput == 1 && flagcon == 0) {
                    problemInput = problemIOC.substring(inlen, posoutput);
                    problemOutput = problemIOC.substring(posoutput + outlen);
                } else if (flagoutput == 0 && flagcon == 1) {
                    if (poscon < posin) {
                        problemInput = problemIOC.substring(posin + inlen);
                    } else {
                        problemInput = problemIOC.substring(poscon + conlen, posin);
                    }
                    problemOutput = "";
                } else {
                    problemInput = problemIOC.substring(inlen);
                    problemOutput = "";
                }
            }
            //if input format and output format is not present
            else {
                problemInput = "";
                problemOutput = "";
            }

            //if constraint is present
            if (flagcon == 1) {
                //if constraint is present before input format
                if (poscon < posin) {
                    problemConstraints = problemIOC.substring(0, posin);
                }
                //if constraint is present before output format
                else if (poscon < posoutput) {
                    problemConstraints = problemIOC.substring(poscon + conlen, posoutput);
                } else {
                    problemConstraints = problemIOC.substring(poscon + conlen);
                }
            }

            System.out.println("problemInput :" + problemInput);
            System.out.println("problemOutput :" + problemOutput);
            System.out.println("problemConstraints :" + problemConstraints);

            //retrieve problem tags from problem page
            Element elementtag = doc.getElementsByClass("problem-tags").first().child(1);
            StringTokenizer st = new StringTokenizer(elementtag.text(), ",");
            while (st.hasMoreTokens()) {
                tags.add(st.nextToken().trim());
            }

            //retrieve sample input sample output if present
            Element elementSIO = doc.getElementsByClass("input-output-container").first();
            //if sample input output is present
            if (elementSIO != null) {
                //find position of sample output
                int soutpos = elementSIO.text().indexOf("SAMPLE OUTPUT");
                sampleInput = elementSIO.text().substring(12, soutpos);
                sampleOutput = elementSIO.text().substring(soutpos + 13);
                System.out.println("Sample input :\n" + sampleInput + "\n\n\n");
                System.out.println("Sample Output :\n" + sampleOutput);
            } else {
                sampleInput = "";
                sampleOutput = "";
            }

            //retrieve problem explanation from problem page if present
            Element elementExplanation = doc.getElementsByClass("standard-margin").first().child(0);
            if (elementExplanation.text().toLowerCase().contains("explanation")) {
                problemExplanation = elementExplanation.nextElementSibling().text();
            }
            System.out.println("Explanation :" + problemExplanation);

            //retrieve timelimit
            Element elementTL = doc.getElementsByClass("problem-guidelines").first().child(0).child(1);
            StringTokenizer stTL = new StringTokenizer(elementTL.ownText(), " ");
            problemTimeLimit = Double.parseDouble(stTL.nextToken());

            //System.out.println("problemTimeLimit :"+problemTimeLimit);
            //set all retrieved information to problem class
            problem.setProblemUrl(problemUrl);
            if (problemTitle.length() == 0) {
                problemTitle = null;
            }
            if (problemStatement.length() == 0) {
                problemStatement = null;
            }
            if (problemInput.length() == 0) {
                problemInput = null;
            }
            if (problemOutput.length() == 0) {
                problemOutput = null;
            }
            if (problemExplanation.length() == 0) {
                problemExplanation = null;
            }
            if (problemConstraints.length() == 0) {
                problemConstraints = null;
            }
            problem.setTitle(problemTitle);
            problem.setProblemUrl(problemUrl);
            problem.setProblemStatement(problemStatement);
            problem.setInputFormat(problemInput);
            problem.setOutputFormat(problemOutput);
            problem.setTimeLimit(problemTimeLimit);
            problem.setExplanation(problemExplanation);
            problem.setConstraints(problemConstraints);

            //set sample input output to problem class
            SampleInputOutput sampleInputOutput = new SampleInputOutput(problem, sampleInput, sampleOutput);
            problem.getSampleInputOutputs().add(sampleInputOutput);
            //set platform as hackerearth
            problem.setPlatform(Platform.HackerEarth);
            for (String strtag : tags) {
                problem.getTags().add(strtag);
            }

            //store in database
            Session session = null;
            Transaction transaction = null;
            try {
                //start session
                session = HibernateUtil.getSessionFactory().openSession();
                transaction = session.beginTransaction();

                //check if problem is already stored in database
                String hql = "FROM Problem p where p.problemUrl = :problem_url";
                Problem oldProblem = (Problem) session.createQuery(hql).setString("problem_url", problemUrl)
                        .uniqueResult();
                String task;

                //if problem is present in database
                if (oldProblem != null) {
                    //update the old problem
                    task = "updated";
                    //retrieve id of old problem
                    problem.setId(oldProblem.getId());
                    session.delete(oldProblem);
                    session.flush();
                    session.save(problem);
                } else {
                    task = "saved";
                    session.save(problem);
                }

                transaction.commit();
                //log the info to console
                Logger.getLogger(CodeForcesCrawler.class.getName()).log(Level.INFO, "{0} {1}",
                        new Object[] { task, problem.getProblemUrl() });
            } catch (HibernateException ee) {
                if (transaction != null) {
                    transaction.rollback();
                }
                Logger.getLogger(CodeForcesCrawler.class.getName()).log(Level.SEVERE,
                        "Cannot Insert/Update problem into databse: " + problemUrl, e);
            } finally {
                //close the session
                if (session != null) {
                    session.close();
                }
            }
        } catch (Exception ee) {
            System.out.println(ee.toString());
        }
    }

    System.out.println("\n\n\n\ntutorial urls\n\n");
    try {

        for (String tutorialurl : tutorialset) {
            //System.out.println(tutorialurl+"\n\n");
            Response tutorialres = Jsoup.connect(tutorialurl).execute();
            Document doc = tutorialres.parse();

            Tutorial tutorial = new Tutorial();
            tutorial.setContent(doc.getElementsByClass("tutorial").first().text());

            tutorial.setName(baseUrl);
            tutorialurl = tutorialurl.substring(0, tutorialurl.length() - 10);
            StringTokenizer tutorialtok = new StringTokenizer(tutorialurl, "/");

            String tempstr = "";
            while (tutorialtok.hasMoreTokens()) {
                tempstr = tutorialtok.nextToken();
            }

            Session session = null;
            Transaction transaction = null;
            try {
                //start session
                session = HibernateUtil.getSessionFactory().openSession();
                transaction = session.beginTransaction();

                //check if problem is already stored in database
                String hql = "FROM Tutorial p where p.name = :name";
                Tutorial oldProblem = (Tutorial) session.createQuery(hql).setString("name", tempstr)
                        .uniqueResult();
                String task;

                //if problem is present in database
                if (oldProblem != null) {
                    //update the old problem
                    task = "updated";
                    //retrieve id of old problem
                    tutorial.setName(oldProblem.getName());
                    session.delete(oldProblem);
                    session.flush();
                    session.save(tutorial);
                } else {
                    task = "saved";
                    tutorial.setName(tempstr);
                    session.save(tutorial);
                }

                transaction.commit();
                //log the info to console
                Logger.getLogger(CodeForcesCrawler.class.getName()).log(Level.INFO, "{0} {1}",
                        new Object[] { task, tutorial.getName() });
            } catch (HibernateException ee) {
                if (transaction != null) {
                    transaction.rollback();
                }
                Logger.getLogger(CodeForcesCrawler.class.getName()).log(Level.SEVERE,
                        "Cannot Insert/Update problem into databse: " + tempstr, ee);
            } finally {
                //close the session
                if (session != null) {
                    session.close();
                }
            }

        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:com.moviejukebox.scanner.WatchedScanner.java

private static boolean isMatchingMovie(TrackedMovie tracked, Movie movie) {
    final Ids ids = tracked.getMovie().getIds();
    final String traktId = StringUtils.trimToNull(movie.getId(TraktTV.SCANNER_ID));
    final String tmdbId = StringUtils.trimToNull(movie.getId(TheMovieDbPlugin.TMDB_PLUGIN_ID));
    final String imdbId = StringUtils.trimToNull(movie.getId(ImdbPlugin.IMDB_PLUGIN_ID));

    if (ids.trakt() != null && StringUtils.equals(traktId, ids.trakt().toString())) {
        return true;
    }//from  ww w  .j  a v a  2  s . c o  m
    if (ids.slug() != null && StringUtils.equalsIgnoreCase(traktId, ids.slug())) {
        setTraktId(ids, traktId, movie);
        return true;
    }
    if (ids.tmdb() != null && StringUtils.equals(tmdbId, ids.tmdb().toString())) {
        setTraktId(ids, traktId, movie);
        return true;
    }
    if (ids.imdb() != null && StringUtils.equalsIgnoreCase(imdbId, ids.imdb())) {
        setTraktId(ids, traktId, movie);
        return true;
    }
    return false;
}

From source file:com.andrada.sitracker.reader.Samlib.java

@Override
public boolean updateAuthor(@NotNull Author author) throws SQLException {
    boolean authorUpdated = false;
    HttpRequest request;//from  ww w.j a va2  s  .  c o m
    AuthorPageReader reader;
    try {
        URL authorURL = new URL(author.getUrl());
        request = HttpRequest.get(authorURL);
        if (request.code() == 404) {
            //skip this author
            //Not available atm
            return false;
        }
        if (!authorURL.getHost().equals(request.url().getHost())) {
            //We are being redirected hell knows where.
            //Skip
            return false;
        }
        //TODO OutOfMemory thrown here
        reader = new SamlibAuthorPageReader(request.body());
        //We go a blank response but no exception, skip author
        if (reader.isPageBlank()) {
            return false;
        }
    } catch (MalformedURLException e) {
        //Just swallow exception, as this is unlikely to happen
        //Skip author
        trackException(e.getMessage());
        return false;
    } catch (HttpRequest.HttpRequestException e) {
        //Author currently inaccessible or no internet
        //Skip author
        trackException(e.getMessage());
        return false;
    }
    AuthorDao authorDao = null;
    PublicationDao publicationsDao = null;

    try {
        publicationsDao = helper.getDao(Publication.class);
    } catch (SQLException e) {
        Log.e("Samlib", "Could not create DAO publicationsDao", e);
    }
    try {
        authorDao = helper.getDao(Author.class);
    } catch (SQLException e) {
        Log.e("Samlib", "Could not create DAO authorDao", e);
    }

    assert authorDao != null;
    assert publicationsDao != null;

    String authImgUrl = reader.getAuthorImageUrl(author.getUrl());
    String authDescription = reader.getAuthorDescription();
    if (authImgUrl != null) {
        author.setAuthorImageUrl(authImgUrl);
    }
    if (authDescription != null) {
        author.setAuthorDescription(authDescription);
    }

    authorDao.update(author);

    ForeignCollection<Publication> oldItems = author.getPublications();
    List<Publication> newItems = reader.getPublications(author);

    HashMap<String, Publication> oldItemsMap = new HashMap<String, Publication>();
    for (Publication oldPub : oldItems) {
        oldItemsMap.put(oldPub.getUrl(), oldPub);
    }

    if (newItems.size() == 0 && oldItemsMap.size() > 1) {
        LogUtils.LOGW(Constants.APP_TAG,
                "Something went wrong. No publications found for author that already exists");
        //Just skip for now to be on the safe side.
        return false;
    }

    for (Publication pub : newItems) {
        //Find pub in oldItems
        if (oldItemsMap.containsKey(pub.getUrl())) {
            Publication old = oldItemsMap.get(pub.getUrl());
            if (old.getUpdatesIgnored()) {
                //Do not check anything
                continue;
            }
            //Check size/name/description
            if (pub.getSize() != old.getSize() || !pub.getName().equals(old.getName())) {
                //if something differs
                //Store the old size
                if (old.getOldSize() != 0) {
                    pub.setOldSize(old.getOldSize());
                } else {
                    pub.setOldSize(old.getSize());
                }
                //Swap the ids, do an update in DB
                pub.setId(old.getId());
                //Copy over custom properties that do not relate to samlib
                pub.setMyVote(old.getMyVote());
                pub.setVoteCookie(old.getVoteCookie());
                pub.setVoteDate(old.getVoteDate());
                pub.setUpdatesIgnored(old.getUpdatesIgnored());
                pub.setNew(true);
                authorUpdated = true;
                publicationsDao.update(pub);
                //Mark author new, update in DB
                author.setUpdateDate(new Date());
                author.setNew(true);
                authorDao.update(author);
            } else if (!StringUtils.equalsIgnoreCase(old.getImagePageUrl(), pub.getImagePageUrl())) {
                pub.setId(old.getId());
                //Update silently
                publicationsDao.update(pub);
            }
        } else {
            //Mark author new, update in DB
            author.setUpdateDate(new Date());
            author.setNew(true);
            authorDao.update(author);
            //Mark publication new, create in DB
            pub.setNew(true);
            authorUpdated = true;
            publicationsDao.create(pub);
        }
    }

    return authorUpdated;
}