Example usage for java.util Scanner hasNextLine

List of usage examples for java.util Scanner hasNextLine

Introduction

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

Prototype

public boolean hasNextLine() 

Source Link

Document

Returns true if there is another line in the input of this scanner.

Usage

From source file:com.mapr.db.utils.ImportCSV.java

public void readAndImportCSV(String path, String delimiter) {

    String dataLine = "";
    String[] dataList;/* w  w w  .j  a v a2 s  .  com*/
    String data = "";

    ArrayList<Object> values = new ArrayList<>();
    int countColumnsInData = 0;

    try {
        Scanner scan = new Scanner(new FileReader(path));

        while (scan.hasNextLine()) {
            countColumnsInData = 0;
            dataLine = scan.nextLine().trim();
            //System.out.println(dataLine);
            if (dataLine.endsWith(delimiter)) {
                dataLine = dataLine.substring(0, dataLine.length() - 1);
            }
            dataList = StringUtils.splitPreserveAllTokens(dataLine, delimiter);

            for (int i = 0; i < dataList.length; i++) {

                data = dataList[i];
                if (data.isEmpty()) {
                    if (valueTypesInSchema.get(i) == "String") {
                        data = "null";
                    } else {
                        data = "0";
                    }
                }

                switch (valueTypesInSchema.get(i).toLowerCase()) {
                case "int":
                case "integer":
                case "bigint":
                case "long":
                    values.add(countColumnsInData, Long.valueOf(data));
                    break;
                case "float":
                case "double":
                    values.add(countColumnsInData, Double.valueOf(data));
                    break;
                case "date":
                    values.add(countColumnsInData, Date.valueOf(data));
                    break;
                default:
                    values.add(countColumnsInData, String.valueOf(data));
                    break;
                }

                //System.out.println("Inserting " + values.get(countColumnsInData)
                //        + " into column " + columnNamesInSchema.get(countColumnsInData));

                countColumnsInData++;
            }
            insertDocument(values, countColumnsInData, maprdbTable, maprdbTablePath);
        }
        scan.close();
    } catch (Exception e) {
        System.out.println("Error importing text:\n\t" + dataLine + "\ninto\n\t" + maprdbTablePath);
        e.printStackTrace();
        System.exit(-1);
    }
}

From source file:com.joliciel.talismane.machineLearning.ModelFactory.java

public MachineLearningModel getMachineLearningModel(ZipInputStream zis) {
    try {//from   w  ww.ja  va 2  s  .co m
        MachineLearningModel machineLearningModel = null;
        ZipEntry ze = zis.getNextEntry();
        if (!ze.getName().equals("algorithm.txt")) {
            throw new JolicielException("Expected algorithm.txt as first entry in zip. Was: " + ze.getName());
        }

        // note: assuming the model type will always be the first entry
        Scanner typeScanner = new Scanner(zis, "UTF-8");
        MachineLearningAlgorithm algorithm = MachineLearningAlgorithm.MaxEnt;
        if (typeScanner.hasNextLine()) {
            String algorithmString = typeScanner.nextLine();
            try {
                algorithm = MachineLearningAlgorithm.valueOf(algorithmString);
            } catch (IllegalArgumentException iae) {
                LogUtils.logError(LOG, iae);
                throw new JolicielException("Unknown algorithm: " + algorithmString);
            }
        } else {
            throw new JolicielException("Cannot find algorithm in zip file");
        }
        switch (algorithm) {
        case MaxEnt:
            machineLearningModel = maxentService.getMaxentModel();
            break;
        case LinearSVM:
            machineLearningModel = linearSVMService.getLinearSVMModel();
            break;
        case Perceptron:
            machineLearningModel = perceptronService.getPerceptronModel();
            break;
        case PerceptronRanking:
            machineLearningModel = perceptronService.getPerceptronRankingModel();
            break;
        case OpenNLPPerceptron:
            machineLearningModel = maxentService.getPerceptronModel();
            break;
        default:
            throw new JolicielException("Machine learning algorithm not yet supported: " + algorithm);
        }

        while ((ze = zis.getNextEntry()) != null) {
            LOG.debug(ze.getName());
            machineLearningModel.loadZipEntry(zis, ze);
        } // next zip entry

        machineLearningModel.onLoadComplete();

        return machineLearningModel;
    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    } finally {
        try {
            zis.close();
        } catch (IOException ioe) {
            LogUtils.logError(LOG, ioe);
        }
    }
}

From source file:com.mgmtp.perfload.perfalyzer.binning.ErrorCountBinningStragegy.java

@Override
public void binData(final Scanner scanner, final WritableByteChannel destChannel) throws IOException {
    BinManager binManager = new BinManager(startOfFirstBin, PerfAlyzerConstants.BIN_SIZE_MILLIS_30_SECONDS);

    while (scanner.hasNextLine()) {
        tokenizer.reset(scanner.nextLine());
        String[] tokens = tokenizer.getTokenArray();

        long timestampMillis = Long.parseLong(tokens[0]);

        boolean isError = "ERROR".equals(tokens[MEASURING_NORMALIZED_COL_RESULT]);
        if (isError) {
            String errorMsg = tokens[MEASURING_NORMALIZED_COL_ERROR_MSG];
            MutableInt errorsByTypeCounter = errorsByType.get(errorMsg);
            if (errorsByTypeCounter == null) {
                errorsByTypeCounter = new MutableInt();
                errorsByType.put(errorMsg, errorsByTypeCounter);
            }/*from w w w .j a  v a2s  . co m*/
            errorsByTypeCounter.increment();

            binManager.addValue(timestampMillis);
        }
    }

    binManager.toCsv(destChannel, "seconds", "count", intNumberFormat);
}

From source file:org.sasabus.export2Freegis.network.SubscriptionManager.java

public boolean subscribe() throws IOException {
    for (int i = 0; i < SUBFILEARRAY.length; ++i) {
        Scanner sc = new Scanner(new File(SUBFILEARRAY[i]));
        String subscriptionstring = "";
        while (sc.hasNextLine()) {
            subscriptionstring += sc.nextLine();
        }//w  w w . j ava 2 s . co m
        sc.close();
        SimpleDateFormat date_date = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat date_time = new SimpleDateFormat("HH:mm:ssZ");

        Date d = new Date();
        String timestamp = date_date.format(d) + "T" + date_time.format(d);
        timestamp = timestamp.substring(0, timestamp.length() - 2) + ":"
                + timestamp.substring(timestamp.length() - 2);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.add(Calendar.DATE, 1);
        d = c.getTime();
        String valid_until = date_date.format(d) + "T" + date_time.format(d);
        valid_until = valid_until.substring(0, valid_until.length() - 2) + ":"
                + valid_until.substring(valid_until.length() - 2);
        subscriptionstring = subscriptionstring.replaceAll(":timestamp_valid", valid_until);
        subscriptionstring = subscriptionstring.replaceAll(":timestamp", timestamp);

        String requestString = "http://" + this.address + ":" + this.portnumber_sender
                + "/TmEvNotificationService/gms/subscription.xml";

        HttpPost subrequest = new HttpPost(requestString);

        StringEntity requestEntity = new StringEntity(subscriptionstring,
                ContentType.create("text/xml", "ISO-8859-1"));

        CloseableHttpClient httpClient = HttpClients.createDefault();

        subrequest.setEntity(requestEntity);

        CloseableHttpResponse response = httpClient.execute(subrequest);

        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            try {
                System.out.println("Stauts Response: " + response.getStatusLine().getStatusCode());
                System.out.println("Status Phrase: " + response.getStatusLine().getReasonPhrase());
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    String responsebody = EntityUtils.toString(responseEntity);
                    System.out.println(responsebody);
                }
            } finally {
                response.close();
                httpClient.close();
            }
            return false;
        }
        System.out.println("Subscription of " + SUBFILEARRAY[i]);
    }
    return true;

}

From source file:eu.planets_project.pp.plato.services.action.planets.PlanetsMigrationService.java

private List<Parameter> getParameters(String configSettings) {

    List<Parameter> serviceParams = new ArrayList<Parameter>();

    if (configSettings == null) {
        return serviceParams;
    }//  w  w  w  . j  a  v  a2s. co m

    Scanner scanner = new Scanner(configSettings);

    int index;
    while (scanner.hasNextLine()) {

        String line = scanner.nextLine();

        if ((index = line.indexOf('=')) > 0) {
            String name = line.substring(0, index);
            String value = line.substring(index + 1);

            if (name.length() > 0 && value.length() > 0) {
                serviceParams.add((new Parameter.Builder(name.trim(), value.trim()).build()));
            }
        }
    }

    return serviceParams;
}

From source file:org.curriki.xwiki.plugin.asset.attachment.SankoreAssetManager.java

public void updateSubAssetClass(XWikiDocument assetDoc, String filetype, String category,
        XWikiAttachment attachment, XWikiContext context) {
    if (filetype.equals("ubz") || filetype.equals("ubw")) {
        String contents = new String();
        ZipEntry ze = null;//from w  ww.j  a  v  a 2s. co  m
        try {
            ZipInputStream zin = new ZipInputStream(attachment.getContentInputStream(context));
            while ((ze = zin.getNextEntry()) != null) {
                if (ze.getName().equals("metadata.rdf")) {
                    byte[] buffer = new byte[8192];
                    int len;
                    while ((len = zin.read(buffer)) != -1) {
                        String buffersz = new String(buffer, 0, len, "UTF-8");
                        contents += buffersz;
                    }
                    break;
                }
            }
            zin.close();
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document dDoc = builder.parse(new ByteArrayInputStream(contents.getBytes("UTF8")));
            XPath xPath = XPathFactory.newInstance().newXPath();
            xPath.setNamespaceContext(new UniversalNamespaceResolver(dDoc));
            assetDoc.setTitle(xPath.evaluate("RDF/Description/sessionTitle", dDoc));
            assetDoc.setTags(xPath.evaluate("RDF/Description/sessionKeywords", dDoc), context);
            BaseObject assetObject = assetDoc.getObject(Constants.ASSET_CLASS);
            BaseObject licenceObject = assetDoc.getObject(Constants.ASSET_LICENCE_CLASS);
            assetObject.setLargeStringValue("description",
                    xPath.evaluate("RDF/Description/sessionObjectives", dDoc));
            assetObject.setStringValue("keywords", xPath.evaluate("RDF/Description/sessionKeywords", dDoc));
            assetObject.setStringValue("language", "fr");
            assetObject.setStringValue("education_system", "AssetMetadata.FranceEducation");
            String xvalue = xPath.evaluate("RDF/Description/sessionGradeLevel", dDoc);
            if (!xvalue.isEmpty() && context.getWiki().exists("SankoreCode.GradeLevelMapping", context)) {
                String mappingString = context.getWiki().getDocument("SankoreCode.GradeLevelMapping", context)
                        .getContent();
                Scanner scanner = new Scanner(mappingString);
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    String[] pair = line.split("=");
                    String key = pair[0];
                    if (key.equals(xvalue)) {
                        xvalue = pair[1];
                        assetObject.setDBStringListValue("educational_level", Arrays.asList(xvalue.split(",")));
                        break;
                    }
                }
            }
            xvalue = xPath.evaluate("RDF/Description/sessionSubjects", dDoc);
            if (!xvalue.isEmpty() && context.getWiki().exists("SankoreCode.SubjectsMapping", context)) {
                String mappingString = context.getWiki().getDocument("SankoreCode.SubjectsMapping", context)
                        .getContent();
                Scanner scanner = new Scanner(mappingString);
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    String[] pair = line.split("=");
                    String key = pair[0];
                    if (key.equals(xvalue)) {
                        xvalue = pair[1];
                        assetObject.setDBStringListValue("fw_items", Arrays.asList(xvalue.split(",")));
                        break;
                    }
                }
            }
            xvalue = xPath.evaluate("RDF/Description/sessionType", dDoc);
            if (!xvalue.isEmpty() && context.getWiki().exists("SankoreCode.TypeMapping", context)) {
                String mappingString = context.getWiki().getDocument("SankoreCode.TypeMapping", context)
                        .getContent();
                Scanner scanner = new Scanner(mappingString);
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    String[] pair = line.split("=");
                    String key = pair[0];
                    if (key.equals(xvalue)) {
                        xvalue = pair[1];
                        assetObject.setDBStringListValue("instructional_component",
                                Arrays.asList(xvalue.split(",")));
                        break;
                    }
                }
            }
            xvalue = xPath.evaluate("RDF/Description/sessionLicence", dDoc);
            if (!xvalue.isEmpty() && context.getWiki().exists("SankoreCode.LicenseMapping", context)) {
                String mappingString = context.getWiki().getDocument("SankoreCode.LicenseMapping", context)
                        .getContent();
                Scanner scanner = new Scanner(mappingString);
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    String[] pair = line.split("=");
                    String key = pair[0];
                    if (key.equals(xvalue)) {
                        xvalue = pair[1];
                        licenceObject.setStringValue("licenseType", xvalue);
                        break;
                    }
                }
            }
            licenceObject.setStringValue("rightsHolder",
                    xPath.evaluate("RDF/Description/sessionAuthors", dDoc));
        } catch (IOException ioe) {
            LOG.error("Unexpected exception ", ioe);
        } catch (ParserConfigurationException pce) {
            LOG.error("Unexpected exception ", pce);
        } catch (SAXException saxe) {
            LOG.error("Unexpected exception ", saxe);
        } catch (XPathException xpe) {
            LOG.error("Unexpected exception ", xpe);
        } catch (XWikiException xe) {
            LOG.error("Unexpected exception ", xe);
        }
    }
}

From source file:ProgressMonitorInputStreamTest.java

/**
 * Prompts the user to select a file, loads the file into a text area, and sets it as the content
 * pane of the frame./*w w  w. j av  a  2s . co m*/
 */
public void openFile() throws IOException {
    int r = chooser.showOpenDialog(this);
    if (r != JFileChooser.APPROVE_OPTION)
        return;
    final File f = chooser.getSelectedFile();

    // set up stream and reader filter sequence

    FileInputStream fileIn = new FileInputStream(f);
    ProgressMonitorInputStream progressIn = new ProgressMonitorInputStream(this, "Reading " + f.getName(),
            fileIn);
    final Scanner in = new Scanner(progressIn);

    textArea.setText("");

    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        protected Void doInBackground() throws Exception {
            while (in.hasNextLine()) {
                String line = in.nextLine();
                textArea.append(line);
                textArea.append("\n");
            }
            in.close();
            return null;
        }
    };
    worker.execute();
}

From source file:nl.b3p.viewer.admin.ViewerAdminLockoutIntegrationTest.java

/**
 * This test work; but will be ignored because we need to catalina.out file
 * to check for the message and that does not seem te be generated.
 *
 * @throws IOException if any//from  ww  w.  j  a v  a2s.  c om
 * @throws URISyntaxException if any
 */
@Test
public void testCLockout() throws IOException, URISyntaxException {
    response = client.execute(new HttpGet(BASE_TEST_URL));
    EntityUtils.consume(response.getEntity());

    HttpUriRequest login = RequestBuilder.post().setUri(new URI(BASE_TEST_URL + "j_security_check"))
            .addParameter("j_username", "admin").addParameter("j_password", "fout").build();
    response = client.execute(login);
    EntityUtils.consume(response.getEntity());
    assertThat("Response status is OK.", response.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_OK));

    // the default lockout is 5 attempt in 5 minutes
    for (int c = 1; c < 6; c++) {
        response = client.execute(login);
        EntityUtils.consume(response.getEntity());
        assertThat("Response status is OK.", response.getStatusLine().getStatusCode(),
                equalTo(HttpStatus.SC_OK));
    }
    // user 'fout' is now locked out, but we have no way to detect apart from looking at the cataline logfile,
    //   the status for a form-based login page is (and should be) 200

    LOG.info("trying one laste time with locked-out user, but correct password");
    login = RequestBuilder.post().setUri(new URI(BASE_TEST_URL + "j_security_check"))
            .addParameter("j_username", "admin").addParameter("j_password", "flamingo").build();
    response = client.execute(login);

    String body = EntityUtils.toString(response.getEntity());
    assertThat("Response status is OK.", response.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_OK));

    assertNotNull("Response body mag niet null zijn.", body);
    assertTrue("Response moet 'Ongeldige logingegevens.' text hebben.",
            body.contains("Ongeldige logingegevens."));

    // there will be a message in catalina.out similar to: `WARNING: An attempt was made to authenticate the locked user "admin"`
    // problem is this is output to the console so logging is broken in tomcat plugin, so below assumption will fail and mark this test as ignored
    InputStream is = ViewerAdminLockoutIntegrationTest.class.getClassLoader()
            .getResourceAsStream("catalina.log");
    assumeNotNull("The catalina.out should privide a valid inputstream.", is);

    Scanner s = new Scanner(is);
    boolean lokkedOut = false;
    while (s.hasNextLine()) {
        final String lineFromFile = s.nextLine();
        if (lineFromFile.contains("An attempt was made to authenticate the locked user \"admin\"")) {
            lokkedOut = true;
            break;
        }
    }
    assertTrue("gebruiker 'admin' is buitengesloten", lokkedOut);
}

From source file:uk.ac.ebi.phenotype.web.util.BioMartBot.java

public String getQueryFromFile(String filename) {

    StringBuilder text = new StringBuilder();
    String NL = System.getProperty("line.separator");
    Scanner scanner = null;

    try {//from www .j av a2 s  .  com
        scanner = new Scanner(new FileInputStream(filename));

        while (scanner.hasNextLine()) {
            text.append(scanner.nextLine() + NL);
        }
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } finally {
        scanner.close();
    }
    return text.toString();
}

From source file:org.apache.marmotta.ucuenca.wk.commons.function.TranslateForSemanticDistance.java

private synchronized String http2(String s, Map<String, String> mp) throws SQLException, IOException {
    String md = s + mp.toString();
    Statement stmt = conn.createStatement();
    String sql;/*from  w w w  . j a  v a  2 s  . c  o  m*/
    sql = "SELECT * FROM cache where cache.key='" + commonservices.getMD5(md) + "'";
    java.sql.ResultSet rs = stmt.executeQuery(sql);
    String resp = "";
    if (rs.next()) {
        resp = rs.getString("value");
        rs.close();
        stmt.close();
    } else {
        rs.close();
        stmt.close();

        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(s);

        //Add any parameter if u want to send it with Post req.
        for (Map.Entry<String, String> mcc : mp.entrySet()) {
            method.addParameter(mcc.getKey(), mcc.getValue());
        }
        int statusCode = client.executeMethod(method);

        if (statusCode != -1) {
            InputStream in = method.getResponseBodyAsStream();
            final Scanner reader = new Scanner(in, "UTF-8");
            while (reader.hasNextLine()) {
                final String line = reader.nextLine();
                resp += line + "\n";
            }
            reader.close();
            try {
                JsonParser parser = new JsonParser();
                parser.parse(resp);
                PreparedStatement stmt2 = conn.prepareStatement("INSERT INTO cache (key, value) values (?, ?)");
                stmt2.setString(1, commonservices.getMD5(md));
                stmt2.setString(2, resp);
                stmt2.executeUpdate();
                stmt2.close();
            } catch (Exception e) {
            }
        }
    }

    return resp;
}