Example usage for java.util Scanner next

List of usage examples for java.util Scanner next

Introduction

In this page you can find the example usage for java.util Scanner next.

Prototype

public String next() 

Source Link

Document

Finds and returns the next complete token from this scanner.

Usage

From source file:com.act.reachables.LoadAct.java

private Set<Integer> extractLD50vals(String annotation) {
    // an example of what we want to process is: "Oral, mouse: LD50 = 338 mg/kg; Oral, rat: LD50 = 1944 mg/kg"
    // a second example of what to process is:   "Acute oral toxicity (LD<sub>50</sub>) in rats is 264 mg/kg."
    // a third example of what to process is :   "Oral mouse and rat LD<sub>50</sub> are 338 mg/kg and 425 mg/kg respectively"
    // a fourth example of what to process is:   "oral LD<sub>50</sub>s were 1,100-1,550 mg/kg; 1,450 mg/kg; and 1,490 mg/kg; respectively"

    // most of time it is specified as mg/kg but rarely we also have g/kg: "Oral LD50 in rat: >5 g/kg"
    Set<Integer> ld50s = new HashSet<Integer>();
    int idx = 0;//from w ww .  j a  va  2  s  .co m
    int len = annotation.length();
    String[] locator = { "LD50", "LD<sub>50</sub>" };
    int[] locator_len = { locator[0].length(), locator[1].length() };
    int delta = 60; // 60 chars +- the locator
    for (int l = 0; l < locator.length; l++) {
        while ((idx = annotation.indexOf(locator[l], idx)) != -1) {
            // look around a few tokens to find numbers that we can use...
            int low = idx - delta < 0 ? 0 : idx - delta;
            int high = idx + delta > len ? len : idx + delta;
            String sub = annotation.substring(low, idx) + annotation.substring(idx + locator_len[l], high);
            Scanner scan = new Scanner(sub).useDelimiter("[^0-9,]+");
            while (scan.hasNext()) {
                String scanned = scan.next().replaceAll(",", "");
                try {
                    ld50s.add(Integer.parseInt(scanned));
                } catch (NumberFormatException e) {
                }
            }

            idx++; // so that we skip the occurrence that we just added
        }
    }
    return ld50s;
}

From source file:argendata.web.controller.DatasetController.java

@RequestMapping(method = RequestMethod.GET)
public ModelAndView view(@RequestParam String qName, HttpSession session) {
    ModelAndView mav = new ModelAndView();
    mav.addObject("mainURL", properties.getMainURL());

    Dataset retrievedDataset = this.datasetService.getApprovedDatasetByQName(qName);

    if (retrievedDataset == null) {
        logger.info("El dataset no existe");
        mav.setViewName("redirect:/bin/dataset/error");
        return mav;
    }/* w  w w  .j  a  v a  2s  .  c om*/

    String format = null;
    if (retrievedDataset.getDistribution() != null && retrievedDataset.getDistribution().getFormat() != null) {
        format = retrievedDataset.getDistribution().getFormat().toLowerCase();
    }

    DatasetViewDTO dview;
    /*
     * Microsoft Office: doc, docx, xls, xlsx, ppt, pptx, pps; OpenDocument:
     * odt, ods, odp; OpenOffice:sxw, sxc, sxi; Other Formats: wpd, pdf,
     * rtf, txt, html, csv, tsv
     */
    if (format != null && (format.endsWith("doc") || format.endsWith("docx") || format.endsWith("xls")
            || format.endsWith("xlsx") || format.endsWith("ppt") || format.endsWith("pptx")
            || format.endsWith("pps") || format.endsWith("odt") || format.endsWith("ods")
            || format.endsWith("odp") || format.endsWith("swx") || format.endsWith("sxi")
            || format.endsWith("wpd") || format.endsWith("pdf") || format.endsWith("rtf")
            || format.endsWith("txt") || format.endsWith("csv") || format.endsWith("tsv"))) {
        dview = new DatasetViewDTO(retrievedDataset, true);
    } else {
        dview = new DatasetViewDTO(retrievedDataset, false);
    }

    mav.addObject("dto", dview);
    String theDate = "";
    /* Se pasa la fecha a un formato entendible por el usuario final */
    String correctDate = dview.getDataset().getModified();
    Scanner scanner = new Scanner(correctDate);
    scanner.useDelimiter("T");
    if (scanner.hasNext()) {
        theDate = scanner.next();
        Scanner scannerDate = new Scanner(theDate);
        scannerDate.useDelimiter("-");
        String year = scannerDate.next();
        String month = scannerDate.next();
        String day = scannerDate.next();
        theDate = year + "-" + month + "-" + day;
    } else {
        logger.error("No se pudo obtener la fecha de la ltima modificacin.");
    }
    mav.addObject("datasetDate", theDate);

    ArgendataUser user = (ArgendataUser) session.getAttribute("user");
    if (user != null && user.isAdmin()) {
        mav.addObject("admin", true);
    } else {
        mav.addObject("admin", false);
    }

    if (user != null) {
        mav.addObject("logged", true);
    } else {
        mav.addObject("logged", false);
    }

    return mav;
}

From source file:org.xwiki.commons.internal.DefaultMailSender.java

@Override
public int sendMailFromTemplate(String templateDocFullName, String from, String to, String cc, String bcc,
        String language, VelocityContext vContext) {
    if (!documentAccessBridge.hasProgrammingRights()) // Forbids the use of a template created by a user having no
                                                      // programming rights
    {/*from www. java  2 s .com*/
        logger.error(
                "No mail has been sent : The author of the document needs programming rights to be able to use the sendMailFromTemplate method");
        return 0;
    }
    try {
        ExecutionContext context = this.execution.getContext();
        XWikiContext xwikiContext = (XWikiContext) context.getProperty("xwikicontext");
        if (vContext == null)
            vContext = velocityManager.getVelocityContext();
        vContext.put("from.name", from);
        vContext.put("from.address", from);
        vContext.put("to.name", to);
        vContext.put("to.address", to);
        vContext.put("to.cc", cc);
        vContext.put("to.bcc", bcc);
        vContext.put("bounce", from);

        String wiki = documentAccessBridge.getCurrentDocumentReference().getWikiReference().getName();
        String templateSpace = "";
        String templatePage = "";
        Scanner scan = new Scanner(templateDocFullName);
        scan.useDelimiter("\\.");
        if (scan.hasNext())
            templateSpace = scan.next();
        if (scan.hasNext())
            templatePage = scan.next();
        else {
            logger.error("Template reference is invalid");
            return 0;
        }
        DocumentReference template = new DocumentReference(wiki, templateSpace, templatePage);
        boolean hasRight = checkAccess(template, xwikiContext);
        if (!hasRight) // If the current user is not allowed to view the page of the template, he can't use it to
                       // send mails
        {
            logger.error("You haven't the right to use this mail template !");
            return 0;
        }
        DocumentReference mailClass = new DocumentReference(wiki, "XWiki", "Mail");
        int n = -1;

        n = documentAccessBridge.getObjectNumber(template, mailClass, "language", language);

        if (n == -1) {
            n = documentAccessBridge.getObjectNumber(template, mailClass, "language", "en");
            logger.warn("No mail object found with language = " + language);
        }
        if (n == -1) {
            logger.error("No mail object found in the document " + templateDocFullName);
            return 0;
        }

        String subject = documentAccessBridge.getProperty(template, mailClass, n, "subject").toString();
        subject = XWikiVelocityRenderer.evaluate(subject, templateDocFullName, vContext, xwikiContext);
        String text = documentAccessBridge.getProperty(template, mailClass, n, "text").toString();
        text = XWikiVelocityRenderer.evaluate(text, templateDocFullName, vContext, xwikiContext);
        String html = documentAccessBridge.getProperty(template, mailClass, n, "html").toString();
        html = XWikiVelocityRenderer.evaluate(html, templateDocFullName, vContext, xwikiContext);

        Mail mail = new Mail();
        mail.setFrom(from);
        mail.setTo(to);
        mail.setCc(cc);
        mail.setBcc(bcc);
        mail.setSubject(subject);
        mail.addContent("text/plain", text);
        if (!StringUtils.isEmpty(html))
            mail.addContent("text/html", html);
        return this.send(mail);
    } catch (Exception e) {
        return 0;
    }

}

From source file:org.apache.openaz.xacml.admin.util.RESTfulPAPEngine.java

/**
 * Send a request to the PAP Servlet and get the response.
 * //from   w  w  w.  j  a  va  2 s.c  om
 * The content is either an InputStream to be copied to the Request OutputStream
 *    OR it is an object that is to be encoded into JSON and pushed into the Request OutputStream.
 * 
 * The Request parameters may be encoded in multiple "name=value" sets, or parameters may be combined by the caller.
 * 
 * @param method
 * @param content   - EITHER an InputStream OR an Object to be encoded in JSON
 * @param collectionTypeClass
 * @param responseContentClass
 * @param parameters
 * @return
 * @throws Exception
 */
private Object sendToPAP(String method, Object content, Class collectionTypeClass, Class responseContentClass,
        String... parameters) throws PAPException {
    HttpURLConnection connection = null;
    try {
        String fullURL = papServletURLString;
        if (parameters != null && parameters.length > 0) {
            String queryString = "";
            for (String p : parameters) {
                queryString += "&" + p;
            }
            fullURL += "?" + queryString.substring(1);
        }

        // special case - Status (actually the detailed status) comes from the PDP directly, not the PAP
        if (method.equals("GET") && content instanceof PDP && responseContentClass == StdPDPStatus.class) {
            // Adjust the url and properties appropriately
            fullURL = ((PDP) content).getId() + "?type=Status";
            content = null;
        }

        URL url = new URL(fullURL);

        //
        // Open up the connection
        //
        connection = (HttpURLConnection) url.openConnection();
        //
        // Setup our method and headers
        //
        connection.setRequestMethod(method);
        //            connection.setRequestProperty("Accept", "text/x-java-properties");
        //               connection.setRequestProperty("Content-Type", "text/x-java-properties");
        connection.setUseCaches(false);
        //
        // Adding this in. It seems the HttpUrlConnection class does NOT
        // properly forward our headers for POST re-direction. It does so
        // for a GET re-direction.
        //
        // So we need to handle this ourselves.
        //
        connection.setInstanceFollowRedirects(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        if (content != null) {
            if (content instanceof InputStream) {
                try {
                    //
                    // Send our current policy configuration
                    //
                    try (OutputStream os = connection.getOutputStream()) {
                        int count = IOUtils.copy((InputStream) content, os);
                        if (logger.isDebugEnabled()) {
                            logger.debug("copied to output, bytes=" + count);
                        }
                    }
                } catch (Exception e) {
                    logger.error("Failed to write content in '" + method + "'", e);
                    throw e;
                }
            } else {
                // The content is an object to be encoded in JSON
                ObjectMapper mapper = new ObjectMapper();
                mapper.writeValue(connection.getOutputStream(), content);
            }
        }
        //
        // Do the connect
        //
        connection.connect();
        if (connection.getResponseCode() == 204) {
            logger.info("Success - no content.");
            return null;
        } else if (connection.getResponseCode() == 200) {
            logger.info("Success. We have a return object.");

            // get the response content into a String
            String json = null;
            // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file)
            java.util.Scanner scanner = new java.util.Scanner(connection.getInputStream());
            scanner.useDelimiter("\\A");
            json = scanner.hasNext() ? scanner.next() : "";
            scanner.close();
            logger.info("JSON response from PAP: " + json);

            // convert Object sent as JSON into local object
            ObjectMapper mapper = new ObjectMapper();

            if (collectionTypeClass != null) {
                // collection of objects expected
                final CollectionType javaType = mapper.getTypeFactory()
                        .constructCollectionType(collectionTypeClass, responseContentClass);

                Object objectFromJSON = mapper.readValue(json, javaType);
                return objectFromJSON;
            } else {
                // single value object expected
                Object objectFromJSON = mapper.readValue(json, responseContentClass);
                return objectFromJSON;
            }

        } else if (connection.getResponseCode() >= 300 && connection.getResponseCode() <= 399) {
            // redirection
            String newURL = connection.getHeaderField("Location");
            if (newURL == null) {
                logger.error(
                        "No Location header to redirect to when response code=" + connection.getResponseCode());
                throw new IOException(
                        "No redirect Location header when response code=" + connection.getResponseCode());
            }
            int qIndex = newURL.indexOf("?");
            if (qIndex > 0) {
                newURL = newURL.substring(0, qIndex);
            }
            logger.info("Redirect seen.  Redirecting " + fullURL + " to " + newURL);
            return newURL;
        } else {
            logger.warn("Unexpected response code: " + connection.getResponseCode() + "  message: "
                    + connection.getResponseMessage());
            throw new IOException("Server Response: " + connection.getResponseCode() + ": "
                    + connection.getResponseMessage());
        }

    } catch (Exception e) {
        logger.error("HTTP Request/Response to PAP: " + e, e);
        throw new PAPException("Request/Response threw :" + e);
    } finally {
        // cleanup the connection
        if (connection != null) {
            try {
                // For some reason trying to get the inputStream from the connection
                // throws an exception rather than returning null when the InputStream does not exist.
                InputStream is = null;
                try {
                    is = connection.getInputStream();
                } catch (Exception e1) { //NOPMD
                    // ignore this
                }
                if (is != null) {
                    is.close();
                }

            } catch (IOException ex) {
                logger.error("Failed to close connection: " + ex, ex);
            }
            connection.disconnect();
        }
    }
}

From source file:org.apache.taverna.activities.rest.ui.config.RESTActivityConfigurationPanel.java

private String[] getMediaTypes() {
    if (mediaTypes != null) {
        return mediaTypes;
    }/* ww w.ja va  2s  . co m*/
    List<String> types = new ArrayList<String>();
    InputStream typesStream = getClass().getResourceAsStream("mediatypes.txt");
    try {
        // media types must be ASCII and can't have whitespace
        Scanner scanner = new Scanner(typesStream, "ascii");
        while (scanner.hasNext()) {
            types.add(scanner.next());
        }
        scanner.close();
    } finally {
        try {
            typesStream.close();
        } catch (IOException ex) {
        }
    }
    mediaTypes = types.toArray(new String[0]);

    return mediaTypes;
}

From source file:files.populate.java

private void populate_reviews(Connection connection, String string) {

    String fileName = "/Users/yash/Documents/workspace/HW 3/data/yelp_review.json";
    Path path = Paths.get(fileName);
    Scanner scanner = null;
    try {//from  ww w .  j  a  va  2 s .co  m
        scanner = new Scanner(path);

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

    //read file line by line
    scanner.useDelimiter("\n");
    int i = 0;
    while (scanner.hasNext()) {
        if (i < 20) {
            JSONParser parser = new JSONParser();
            String s = (String) scanner.next();
            s = s.replace("'", "''");

            JSONObject obj;

            try {
                obj = (JSONObject) parser.parse(s);
                String text = (String) obj.get("text");
                text = text.replace("\n", "");

                Map votes = (Map) obj.get("votes");
                String query = "insert into yelp_reviews values (" + votes.get("useful") + ","
                        + votes.get("funny") + "," + votes.get("cool") + ",'" + obj.get("user_id") + "','"
                        + obj.get("review_id") + "'," + obj.get("stars") + ",'" + obj.get("date") + "','" + text
                        + "','" + obj.get("type") + "','" + obj.get("business_id") + "')";
                System.out.println(query);
                Statement statement = connection.createStatement();
                statement.executeUpdate(query);
                statement.close();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ++i;

        }

    }
}

From source file:org.apache.openaz.xacml.std.json.JSONRequest.java

/**
 * Read characters from the given <code>InputStream</code> and parse them into an XACML
 * {@link org.apache.openaz.xacml.api.Request} object.
 *
 * @param is/*from ww  w  .  ja va2  s  .co m*/
 * @return
 * @throws JSONStructureException
 */
public static Request load(InputStream is) throws JSONStructureException {

    // TODO - ASSUME that order of members within an object does not matter (Different from XML, in JSON
    // everything is handled as Maps so order does not matter)

    // ensure shorthand map is set up
    if (shorthandMap == null) {
        initShorthandMap();
    }

    // ensure that we have an instance of the DataTypeFactory for generating AttributeValues by DataType
    if (dataTypeFactory == null) {
        try {
            dataTypeFactory = DataTypeFactory.newInstance();
            if (dataTypeFactory == null) {
                throw new NullPointerException("No DataTypeFactory found");
            }
        } catch (FactoryException e) {
            throw new JSONStructureException("Unable to find DataTypeFactory, e=" + e);
        }
    }

    // create a new Request object to be filled in
    StdMutableRequest stdMutableRequest = null;

    String json = null;
    ObjectMapper mapper = null;
    try {

        // read the inputStream into a buffer (trick found online scans entire input looking for
        // end-of-file)
        java.util.Scanner scanner = new java.util.Scanner(is);
        scanner.useDelimiter("\\A");
        json = scanner.hasNext() ? scanner.next() : "";
        scanner.close();

        mapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

        // TODO - ASSUME that any duplicated component is a bad thing (probably indicating an error in the
        // incoming JSON)
        mapper.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true);

        Map<?, ?> root = mapper.readValue(json, Map.class);

        //
        // Does the request exist?
        //
        Map<?, ?> jsonRequestMap = (Map<?, ?>) root.remove("Request");
        if (jsonRequestMap == null) {
            throw new JSONStructureException("No \"Request\" property found.");
        }

        checkUnknown("Top-level message", root);

        stdMutableRequest = new StdMutableRequest();

        //
        // Is there a Category?
        //
        Object categoryList = jsonRequestMap.remove("Category");
        if (categoryList != null && !(categoryList instanceof List)) {
            throw new JSONStructureException(
                    "Category must contain list of objects, not '" + categoryList.getClass() + "'");
        }
        if (categoryList != null) {
            //
            // Iterate each Category
            //
            Iterator<?> iter = ((List<?>) categoryList).iterator();
            while (iter.hasNext()) {
                Object category = iter.next();
                if (!(category instanceof Map)) {
                    throw new JSONStructureException(
                            "Category list must contain objects contained within curly braces ({})");
                }

                parseCategory((Map<?, ?>) category, "Category", null, stdMutableRequest);

            }
        }

        // The following may be either a single instance or an array. This allows multiple decisions to
        // work with the Default Category objects.
        // Example:
        // "AccessSubject" : [ {attributes group one},
        // {attributes group two}
        // ]

        //
        // Look for default Shorthand AccessSubject
        //
        parseDefaultCategory(jsonRequestMap, "AccessSubject",
                "urn:oasis:names:tc:xacml:1.0:subject-category:access-subject", stdMutableRequest);
        //
        // Provide backward compatibility for our PEP's
        //
        parseDefaultCategory(jsonRequestMap, "Subject",
                "urn:oasis:names:tc:xacml:1.0:subject-category:access-subject", stdMutableRequest);

        //
        // Look for default Shorthand Action
        //
        parseDefaultCategory(jsonRequestMap, "Action", "urn:oasis:names:tc:xacml:3.0:attribute-category:action",
                stdMutableRequest);

        //
        // Look for default Shorthand Resource
        //
        parseDefaultCategory(jsonRequestMap, "Resource",
                "urn:oasis:names:tc:xacml:3.0:attribute-category:resource", stdMutableRequest);

        //
        // Look for default Shorthand Environment
        //
        parseDefaultCategory(jsonRequestMap, "Environment",
                "urn:oasis:names:tc:xacml:3.0:attribute-category:environment", stdMutableRequest);

        //
        // Look for default Shorthand RecipientSubject
        //
        parseDefaultCategory(jsonRequestMap, "RecipientSubject",
                "urn:oasis:names:tc:xacml:1.0:subject-category:recipient-subject", stdMutableRequest);

        //
        // Look for default Shorthand IntermediarySubject
        //
        parseDefaultCategory(jsonRequestMap, "IntermediarySubject",
                "urn:oasis:names:tc:xacml:1.0:subject-category:intermediary-subject", stdMutableRequest);

        //
        // Look for default Shorthand Codebase
        //
        parseDefaultCategory(jsonRequestMap, "Codebase",
                "urn:oasis:names:tc:xacml:1.0:subject-category:codebase", stdMutableRequest);

        //
        // Look for default Shorthand RequestingMachine
        //
        parseDefaultCategory(jsonRequestMap, "RequestingMachine",
                "urn:oasis:names:tc:xacml:1.0:subject-category:requesting-machine", stdMutableRequest);

        //
        // MultiRequest
        //
        Map<?, ?> multiRequests = (Map<?, ?>) jsonRequestMap.remove("MultiRequests");
        if (multiRequests != null) {
            if (!(multiRequests instanceof Map)) {
                throw new JSONStructureException("MultiRequests must be object structure, not single value");
            }

            List<?> requestReferenceList = (List<?>) multiRequests.remove("RequestReference");
            if (requestReferenceList == null) {
                throw new JSONStructureException("MultiRequest must contain a RequestReference element");
            }
            if (requestReferenceList.size() < 1) {
                throw new JSONStructureException(
                        "MultiRequest must contain at least one element in the RequestReference list");
            }

            checkUnknown("MultiRequest", multiRequests);

            for (Object requestReferenceMapObject : requestReferenceList) {
                if (!(requestReferenceMapObject instanceof Map)) {
                    throw new JSONStructureException("MultiRequest RequestReference must be object");
                }
                Map<?, ?> requestReferenceMap = (Map<?, ?>) requestReferenceMapObject;

                // each object within the list must contain a ReferenceId and only a ReferenceId
                Object referenceIdListObject = requestReferenceMap.remove("ReferenceId");
                if (referenceIdListObject == null) {
                    throw new JSONStructureException(
                            "MultiRequest RequestReference list element must contain ReferenceId");
                }
                List<?> referenceIdList = (List<?>) referenceIdListObject;
                if (referenceIdList.size() == 0) {
                    // the spec does not disallow empty list RequestReference objects
                    continue;
                }

                checkUnknown("RequestReference", requestReferenceMap);

                // create reference corresponding to RequestReference list element
                StdMutableRequestReference requestReference = new StdMutableRequestReference();

                for (Object referenceId : referenceIdList) {
                    // add attributes to the reference
                    // Since the order of the JSON is not constrained, we could process this section
                    // before the section containing attribute being referenced,
                    // so we cannot do a cross-check here to verify that the attribute reference exists.
                    // That will happen later when the PDP attempts to find the attribute.
                    StdRequestAttributesReference requestAttributesReference = new StdRequestAttributesReference(
                            (String) referenceId);
                    requestReference.add(requestAttributesReference);
                }
                stdMutableRequest.add(requestReference);
            }
        }

        //
        // ReturnPolicyIdList
        //
        // If omitted this is set to a default of false by the StdMutableRequest constructor.
        //
        Object returnPolicyIdList = jsonRequestMap.remove("ReturnPolicyIdList");
        Boolean returnPolicyIdListBoolean = makeBoolean(returnPolicyIdList, "ReturnPolicyIdList");
        if (returnPolicyIdList != null) {
            stdMutableRequest.setReturnPolicyIdList(returnPolicyIdListBoolean);
        }

        //
        // CombinedDecision
        //
        // If omitted this is set to a default of false by the StdMutableRequest constructor.
        //
        Object combinedDecision = jsonRequestMap.remove("CombinedDecision");
        Boolean combinedDecisionBoolean = makeBoolean(combinedDecision, "CombinedDecision");
        if (combinedDecision != null) {
            stdMutableRequest.setCombinedDecision(combinedDecisionBoolean);
        }

        //
        // XPath
        //

        // The JSON spec says that this has a default value, implying that if it is missing in the Request
        // we should fill it in.
        // However the XML (DOM) version does not do that. If the value is missing it leaves the
        // requestDefaults object blank.
        // We are following the XML approach and ignoring the Default value for this field in the spec.

        // TODO - Assume that no value for XPathVersion means "leave as null", not "fill in the default
        // value from spec. This violates the JSON spec
        Object xPath = jsonRequestMap.remove("XPathVersion");
        if (xPath != null) {
            // XPath is given in the JSON input
            if (!(xPath instanceof String)) {
                throw new JSONStructureException("XPathVersion not a URI passed as a String");
            }
            URI xPathUri = null;
            try {
                xPathUri = new URI(xPath.toString());
            } catch (Exception e) {
                throw new JSONStructureException("XPathVersion not a valid URI: '" + xPath + "'", e);
            }

            StdRequestDefaults requestDefaults = new StdRequestDefaults(xPathUri);
            stdMutableRequest.setRequestDefaults(requestDefaults);
        }

        checkUnknown("Request", jsonRequestMap);

    } catch (JsonParseException e) {
        // try to point to problem area in JSON input, if possible
        JsonLocation location = e.getLocation();
        String locationOfError = "(unavailable)";
        if (location != null && location != JsonLocation.NA) {
            String jsonText = json;
            if (location.getLineNr() > 1) {
                String[] jsonArray = jsonText.split("\\r?\\n|\\r");
                jsonText = jsonArray[location.getLineNr()];
            }
            if (location.getCharOffset() < jsonText.length()) {
                if (location.getCharOffset() > 0) {
                    locationOfError = jsonText.substring((int) location.getCharOffset() - 1);
                }
                if (locationOfError.length() > 30) {
                    locationOfError = locationOfError.substring(0, 30);
                }
            }
        }
        throw new JSONStructureException("Unable to parse JSON starting at text'" + locationOfError
                + "', input was '" + json + "', exception: " + e, e);
    } catch (JsonMappingException e) {
        throw new JSONStructureException("Unable to map JSON '" + json + "', exception: " + e, e);
    } catch (IOException e) {
        throw new JSONStructureException("Unable to read JSON input, exception: " + e, e);
    }

    // all done
    return new StdRequest(stdMutableRequest);
}

From source file:files.populate.java

private void populate_checkin(Connection connection, String string) {
    String fileName = "/Users/yash/Documents/workspace/HW 3/data/yelp_checkin.json";
    Path path = Paths.get(fileName);
    Scanner scanner = null;
    try {/*from   w  w  w .ja va 2s  .c om*/
        scanner = new Scanner(path);

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

    //read file line by line
    scanner.useDelimiter("\n");
    int i = 0;
    while (scanner.hasNext()) {

        if (i < 20) {
            JSONParser parser = new JSONParser();
            String s = (String) scanner.next();
            s = s.replace("'", "''");

            JSONObject obj;

            try {
                obj = (JSONObject) parser.parse(s);
                Map checkin_info = (Map) obj.get("checkin_info");
                Set keys = checkin_info.keySet();
                Object[] days = keys.toArray();
                Statement statement = connection.createStatement();
                for (int j = 0; j < days.length; ++j) {
                    //                           
                    String thiskey = days[j].toString();
                    String q3 = "insert into yelp_checkin values ('" + obj.get("business_id") + "','"
                            + obj.get("type") + "','" + thiskey + "','" + checkin_info.get(thiskey) + "')";
                    //
                    statement.executeUpdate(q3);

                }
                statement.close();

            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            ++i;
        } else {
            break;
        }
    }
}

From source file:com.jsmartframework.web.manager.FilterControl.java

@SuppressWarnings("resource")
private String convertResourceToString(String resource) {
    InputStream is = FilterControl.class.getClassLoader().getResourceAsStream(resource);
    Scanner scanner = new Scanner(is).useDelimiter("\\A");
    return scanner.hasNext() ? scanner.next() : "";
}

From source file:files.populate.java

void populate_users(Connection connection, String filename) {
    String fileName = "/Users/yash/Documents/workspace/HW 3/data/yelp_user.json";
    Path path = Paths.get(fileName);
    Scanner scanner = null;
    try {//w  w w .  j a v  a2 s  . c  om
        scanner = new Scanner(path);

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

    //read file line by line
    scanner.useDelimiter("\n");
    int i = 0;
    while (scanner.hasNext()) {
        if (i < 500) {
            JSONParser parser = new JSONParser();
            String s = (String) scanner.next();
            JSONObject obj;
            try {
                obj = (JSONObject) parser.parse(s);
                Map votes = (Map) obj.get("votes");
                ArrayList friends;
                friends = (ArrayList) obj.get("friends");
                Map compliments = (Map) obj.get("compliments");
                //                    
                String query = "INSERT INTO YELP_USERS (user_id,yelping_since,name,fans,average_stars,type,review_count,VOTES_FUNNY,VOTES_COOL,VOTES_USEFUL) VALUES('"
                        + obj.get("user_id") + "','" + obj.get("yelping_since") + "','" + obj.get("name") + "',"
                        + obj.get("fans") + "," + obj.get("average_stars") + ",'" + obj.get("type") + "',"
                        + obj.get("review_count") + "," + votes.get("funny") + "," + votes.get("cool") + ","
                        + votes.get("useful") + ")";
                System.out.println(query);
                Statement statement = connection.createStatement();
                statement.executeUpdate(query);
                for (int j = 0; j < friends.size(); ++j) {
                    String q2 = "insert into users_friends values ('" + obj.get("user_id") + "','"
                            + friends.get(j) + "')";
                    System.out.println(q2);
                    statement.executeUpdate(q2);
                }
                Set keys = compliments.keySet();
                Object[] keys1 = keys.toArray();
                for (int j = 0; j < keys1.length; ++j) {
                    //                             String q2 = "insert into users_friends values ("+obj.get("user_id")+","+compliments.get(j)+")";
                    String thiskey = keys1[j].toString();
                    long val = (long) compliments.get(thiskey);
                    String q3 = "insert into users_compliments values ('" + obj.get("user_id") + "','" + thiskey
                            + "'," + val + ")";
                    System.out.println(q3);
                    statement.executeUpdate(q3);
                }
                statement.close();
            } catch (ParseException ex) {
                Logger.getLogger(populate.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(populate.class.getName()).log(Level.SEVERE, null, ex);
            }

            i++;
        } else
            break;
    }

    scanner.close();

}