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:org.apache.camel.dataformat.bindy.kvp.BindyKeyValuePairDataFormat.java

public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
    BindyKeyValuePairFactory factory = (BindyKeyValuePairFactory) getFactory(
            exchange.getContext().getPackageScanClassResolver());

    // List of Pojos
    List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();

    // Pojos of the model
    Map<String, Object> model;

    InputStreamReader in = new InputStreamReader(inputStream);

    // Scanner is used to read big file
    Scanner scanner = new Scanner(in);

    // Retrieve the pair separator defined to split the record
    ObjectHelper.notNull(factory.getPairSeparator(), "The pair separator property of the annotation @Message");
    String separator = factory.getPairSeparator();

    int count = 0;
    try {//from w ww  .j  a v a 2s  . com
        while (scanner.hasNextLine()) {
            // Read the line
            String line = scanner.nextLine().trim();

            if (ObjectHelper.isEmpty(line)) {
                // skip if line is empty
                continue;
            }

            // Increment counter
            count++;

            // Create POJO
            model = factory.factory();

            // Split the message according to the pair separator defined in
            // annotated class @Message
            List<String> result = Arrays.asList(line.split(separator));

            if (result.size() == 0 || result.isEmpty()) {
                throw new java.lang.IllegalArgumentException("No records have been defined in the KVP !");
            }

            if (result.size() > 0) {

                // Bind data from message with model classes
                // Counter is used to detect line where error occurs
                factory.bind(result, model, count);

                // Link objects together
                factory.link(model);

                // Add objects graph to the list
                models.add(model);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Graph of objects created : " + model);
                }
            }

        }

        // Test if models list is empty or not
        // If this is the case (correspond to an empty stream, ...)
        if (models.size() == 0) {
            throw new java.lang.IllegalArgumentException("No records have been defined in the KVP !");
        } else {
            return models;
        }

    } finally {
        scanner.close();
        IOHelper.close(in, "in", LOG);
    }
}

From source file:de.iteratec.iteraplan.general.PropertiesTest.java

/**
 * @see {@link #testJspKeys()}/* w w w  .  ja v  a2 s .  c o m*/
 * @param collectedBundleKeys
 *          Bundle keys collected so far. Acts as in-out parameter.
 * @param p
 *          The pattern to find.
 * @param fileToProcess
 *          The file to be looked at.
 * @return true if a bundle key was missing.
 * @throws FileNotFoundException
 */
private boolean parseJspForBundleKeys(Set<String> collectedBundleKeys, Pattern p, File fileToProcess)
        throws FileNotFoundException {
    boolean wasSuccessful = true;

    if (fileToProcess.isDirectory()) {
        for (File f : fileToProcess.listFiles()) {
            boolean containedSuccess = parseJspForBundleKeys(collectedBundleKeys, p, f);
            if (!containedSuccess) {
                wasSuccessful = false;
            }
        }
    } else if (fileToProcess.getName().toLowerCase().endsWith("jsp")) {
        Scanner sc = new Scanner(fileToProcess);
        StringBuffer jspAsStringBuffer = new StringBuffer();
        while (sc.hasNextLine()) {
            jspAsStringBuffer.append(sc.nextLine());
        }
        String jspAsString = jspAsStringBuffer.toString();
        MatchResult result = null;
        sc = new Scanner(jspAsString);
        while (true) {
            if (sc.findInLine(p) == null) {
                break;
            }
            result = sc.match();
            String bundleKey = result.group(POSITION_OF_PROPERTY_IN_JSP_TAG); // refers to regexp which is passed into this method
            collectedBundleKeys.add(bundleKey);

            // omit registered keys and keys which contain variables
            if (!ACCEPTABLE_MISSES_LIST.contains(bundleKey) && !(bundleKey.contains("$"))) {
                for (LanguageFile l : languageFiles) {
                    if (!l.keySet().contains(bundleKey)) {
                        wasSuccessful = false;
                        LOGGER.info(
                                "Bundle key {0} defined in JSP {1} was not found in {2} ApplicationResources.properties!",
                                bundleKey, fileToProcess, l.language);
                    }
                }
            }
        }
    }

    return wasSuccessful;
}

From source file:br.univali.ps.fuzzy.portugolFuzzyCorretor.core.PortugolFuzzyCorretor.java

private int contarNumeroLinhas() {
    int linhas = 0;
    Scanner scanner = new Scanner(this.codigoPortugol); // Para possibilitar a edio do texto dentro do programa
                                                        // Caso no seja necessrio, contabilizar dentro do FileController
    while (scanner.hasNextLine()) {
        linhas++;//w  ww  .  j a  va  2 s. c  o m
        scanner.nextLine();
    }
    scanner.close();
    return linhas;
}

From source file:configurator.Configurator.java

/**
 * Method for configuring device via telnet. Connect to device, authorizes,
 * and send to cli one by one line from command list, analyse every answer
 * and make report/*from  w  w  w . j  av  a  2s.c  om*/
 *
 * @param login username for authorization
 * @param password password for authorization
 * @param ip ip address of device
 * @param commandList command list for input on device
 * @return report of telnet configuring this device
 */
String telnetConfigure(String login, String password, String ip, String commandList) {
    StringBuilder report = new StringBuilder("Configure: ");
    report.append(ip);
    try {
        Telnet telnet = new Telnet(login, password, ip);
        telnet.init(usernamePrompt, passwordPrompt, commandPrompt);
        Scanner commandReader = new Scanner(commandList);
        while (commandReader.hasNextLine()) {
            final String command = commandReader.nextLine();
            final String answer = telnet.sendCommand(command, commandPrompt);
            if (!(analysisMode == AnalysisMode.OFF)) {
                if (!answerAnalysis(answer)) {
                    report.append("\n").append(command).append(":\n").append(answer);
                }
            }
        }
        report.append("\n").append("Complete");
    } catch (IOException ex) {
        Logger.getLogger(Device.class.getName()).log(Level.SEVERE, null, ex);
        report.append("\nConnection problem");
    } catch (LoginFailedException ex) {
        report.append("\nAutentification failed: ").append(ip).append(" login: ").append(login);
    }
    return report.toString();
}

From source file:com.all.login.services.LoginModelDao.java

private List<City> getCities() {
    if (cities == null) {
        synchronized (this) {
            if (cities == null) {
                List<City> cities = new ArrayList<City>();
                Scanner scanner = null;
                try {
                    scanner = new Scanner(getClass().getResourceAsStream("/scripts/cities.txt"));
                    while (scanner.hasNextLine()) {
                        String text = scanner.nextLine();
                        if (text.startsWith("('")) {
                            try {
                                City city = new City();
                                String[] data = getSmartData(text);
                                city.setCityId(data[0]);
                                city.setCityName(data[1]);
                                city.setCountryId(data[2]);
                                city.setCountryName(data[3]);
                                city.setStateId(data[4]);
                                city.setStateName(data[5]);
                                city.setPopIndex(data[6]);
                                cities.add(city);
                            } catch (Exception e) {
                                LOG.error(e, e);
                            }/*  www.  j  av a  2s. c  o m*/
                        }
                    }
                } catch (Exception e) {
                    LOG.error(e, e);
                } finally {
                    try {
                        scanner.close();
                    } catch (Exception e) {
                        LOG.error(e, e);
                    }
                }

                this.cities = cities;
            }
        }
    }
    return cities;
}

From source file:org.apache.stratos.cartridge.agent.config.CartridgeAgentConfiguration.java

private Map<String, String> loadParametersFile() {
    Map<String, String> parameters = new HashMap<String, String>();
    try {//from   www . j  a  va 2 s  .  c o  m

        // read launch params
        File file = new File(System.getProperty(CartridgeAgentConstants.PARAM_FILE_PATH));
        if (!file.exists()) {
            log.warn(String.format("File not found: %s", CartridgeAgentConstants.PARAM_FILE_PATH));
            return parameters;
        }
        Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            String[] params = line.split(",");
            for (String string : params) {
                if (string != null) {
                    String[] var = string.split("=");
                    if (var.length >= 2) {
                        parameters.put(var[0], var[1]);
                    }
                }
            }
        }
        scanner.close();
    } catch (Exception e) {
        String message = "Could not read launch parameter file, hence trying to read from System properties.";
        log.warn(message, e);
    }

    return parameters;
}

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

@Override
public void binData(final Scanner scanner, final WritableByteChannel destChannel) throws IOException {
    BinManager binSecondManager = new BinManager(startOfFirstBin, PerfAlyzerConstants.BIN_SIZE_MILLIS_1_SECOND);
    BinManager binMinuteManager = new BinManager(startOfFirstBin, PerfAlyzerConstants.BIN_SIZE_MILLIS_1_MINUTE);

    int requestCounter = 0;
    int errorCounter = 0;

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

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

        if (!"AGENT".equals(tokens[MEASURING_NORMALIZED_COL_REQUEST_TYPE])) {
            requestCounter++;//from w  ww.  j  a  va2 s .  c o  m
            binSecondManager.addValue(timestampMillis);
            binMinuteManager.addValue(timestampMillis);
        }
        if ("ERROR".equals(tokens[MEASURING_NORMALIZED_COL_RESULT])) {
            errorCounter++;
        }
    }

    //      binSecondManager.completeLastBin();
    //      binMinuteManager.completeLastBin();

    double[] requestsPerSecond = binSecondManager.countStream().asDoubleStream().toArray();//   Doubles.toArray(binSecondManager.getBins().values().stream().map(longToDouble()).collect(toList()));
    double minRequestsPerSecond = StatUtils.min(requestsPerSecond);
    double medianRequestsPerSecond = StatUtils.percentile(requestsPerSecond, 50d);
    double maxRequestsPerSecond = StatUtils.max(requestsPerSecond);

    double[] requestsPerMinute = binMinuteManager.countStream().asDoubleStream().toArray(); // Doubles.toArray(binMinuteManager.getBins().values().stream().map(longToDouble()).collect(toList()));
    double minRequestsPerMinute = StatUtils.min(requestsPerMinute);
    double medianRequestsPerMinute = StatUtils.percentile(requestsPerMinute, 50d);
    double maxRequestsPerMinute = StatUtils.max(requestsPerMinute);

    StrBuilder sb = new StrBuilder();
    appendEscapedAndQuoted(sb, DELIMITER, "numRequests");
    appendEscapedAndQuoted(sb, DELIMITER, "numErrors");

    appendEscapedAndQuoted(sb, DELIMITER, "minReqPerSec");
    appendEscapedAndQuoted(sb, DELIMITER, "medianReqPerSec");
    appendEscapedAndQuoted(sb, DELIMITER, "maxReqPerSec");

    appendEscapedAndQuoted(sb, DELIMITER, "minReqPerMin");
    appendEscapedAndQuoted(sb, DELIMITER, "medianReqPerMin");
    appendEscapedAndQuoted(sb, DELIMITER, "maxReqPerMin");
    writeLineToChannel(destChannel, sb.toString(), Charsets.UTF_8);

    sb = new StrBuilder();
    appendEscapedAndQuoted(sb, DELIMITER, intNumberFormat.format(requestCounter));
    appendEscapedAndQuoted(sb, DELIMITER, intNumberFormat.format(errorCounter));

    appendEscapedAndQuoted(sb, DELIMITER, intNumberFormat.format(minRequestsPerSecond));
    appendEscapedAndQuoted(sb, DELIMITER, intNumberFormat.format(medianRequestsPerSecond));
    appendEscapedAndQuoted(sb, DELIMITER, intNumberFormat.format(maxRequestsPerSecond));

    appendEscapedAndQuoted(sb, DELIMITER, intNumberFormat.format(minRequestsPerMinute));
    appendEscapedAndQuoted(sb, DELIMITER, intNumberFormat.format(medianRequestsPerMinute));
    appendEscapedAndQuoted(sb, DELIMITER, intNumberFormat.format(maxRequestsPerMinute));
    writeLineToChannel(destChannel, sb.toString(), Charsets.UTF_8);
}

From source file:azkaban.viewer.reportal.ReportalMailCreator.java

private boolean createMessage(Project project, ExecutableFlow flow, EmailMessage message, String urlPrefix,
        boolean printData) throws Exception {
    message.println("<html>");
    message.println("<head></head>");
    message.println(//from  ww  w. j a  v a  2  s. co  m
            "<body style='font-family: verdana; color: #000000; background-color: #cccccc; padding: 20px;'>");
    message.println(
            "<div style='background-color: #ffffff; border: 1px solid #aaaaaa; padding: 20px;-webkit-border-radius: 15px; -moz-border-radius: 15px; border-radius: 15px;'>");
    // Title
    message.println("<b>" + project.getMetadata().get("title") + "</b>");
    message.println("<div style='font-size: .8em; margin-top: .5em; margin-bottom: .5em;'>");
    // Status
    message.println(flow.getStatus().name());
    // Link to logs
    message.println("(<a href='" + urlPrefix + "?view&logs&id=" + flow.getProjectId() + "&execid="
            + flow.getExecutionId() + "'>Logs</a>)");
    // Link to Data
    message.println("(<a href='" + urlPrefix + "?view&id=" + flow.getProjectId() + "&execid="
            + flow.getExecutionId() + "'>Result data</a>)");
    // Link to Edit
    message.println("(<a href='" + urlPrefix + "?edit&id=" + flow.getProjectId() + "'>Edit</a>)");
    message.println("</div>");
    message.println("<div style='margin-top: .5em; margin-bottom: .5em;'>");
    // Description
    message.println(project.getDescription());
    message.println("</div>");

    // Print variable values, if any
    Map<String, String> flowParameters = flow.getExecutionOptions().getFlowParameters();
    int i = 0;
    while (flowParameters.containsKey("reportal.variable." + i + ".from")) {
        if (i == 0) {
            message.println(
                    "<div style='margin-top: 10px; margin-bottom: 10px; border-bottom: 1px solid #ccc; padding-bottom: 5px; font-weight: bold;'>");
            message.println("Variables");
            message.println("</div>");
            message.println("<table border='1' cellspacing='0' cellpadding='2' style='font-size: 14px;'>");
            message.println("<thead><tr><th><b>Name</b></th><th><b>Value</b></th></tr></thead>");
            message.println("<tbody>");
        }

        message.println("<tr>");
        message.println("<td>" + flowParameters.get("reportal.variable." + i + ".from") + "</td>");
        message.println("<td>" + flowParameters.get("reportal.variable." + i + ".to") + "</td>");
        message.println("</tr>");

        i++;
    }

    if (i > 0) { // at least one variable
        message.println("</tbody>");
        message.println("</table>");
    }

    if (printData) {
        String locationFull = (outputLocation + "/" + flow.getExecutionId()).replace("//", "/");

        IStreamProvider streamProvider = ReportalUtil.getStreamProvider(outputFileSystem);

        if (streamProvider instanceof StreamProviderHDFS) {
            StreamProviderHDFS hdfsStreamProvider = (StreamProviderHDFS) streamProvider;
            hdfsStreamProvider.setHadoopSecurityManager(hadoopSecurityManager);
            hdfsStreamProvider.setUser(reportalStorageUser);
        }

        // Get file list
        String[] fileList = ReportalHelper.filterCSVFile(streamProvider.getFileList(locationFull));

        // Sort files in execution order.
        // File names are in the format {EXECUTION_ORDER}-{QUERY_TITLE}.csv
        // E.g.: 1-queryTitle.csv
        Arrays.sort(fileList, new Comparator<String>() {
            public int compare(String a, String b) {
                Integer aExecutionOrder = Integer.parseInt(a.substring(0, a.indexOf('-')));
                Integer bExecutionOrder = Integer.parseInt(b.substring(0, b.indexOf('-')));
                return aExecutionOrder.compareTo(bExecutionOrder);
            }

            public boolean equals(Object obj) {
                return this.equals(obj);
            }
        });

        // Get jobs in execution order
        List<ExecutableNode> jobs = ReportalUtil.sortExecutableNodes(flow);

        File tempFolder = new File(reportalMailTempDirectory + "/" + flow.getExecutionId());
        tempFolder.mkdirs();

        // Copy output files from HDFS to local disk, so you can send them as email attachments
        for (String file : fileList) {
            String filePath = locationFull + "/" + file;
            InputStream csvInputStream = null;
            OutputStream tempOutputStream = null;
            File tempOutputFile = new File(tempFolder, file);
            tempOutputFile.createNewFile();
            try {
                csvInputStream = streamProvider.getFileInputStream(filePath);
                tempOutputStream = new BufferedOutputStream(new FileOutputStream(tempOutputFile));

                IOUtils.copy(csvInputStream, tempOutputStream);
            } finally {
                IOUtils.closeQuietly(tempOutputStream);
                IOUtils.closeQuietly(csvInputStream);
            }
        }

        try {
            streamProvider.cleanUp();
        } catch (IOException e) {
            e.printStackTrace();
        }

        boolean emptyResults = true;

        for (i = 0; i < fileList.length; i++) {
            String file = fileList[i];
            ExecutableNode job = jobs.get(i);
            job.getAttempt();

            message.println(
                    "<div style='margin-top: 10px; margin-bottom: 10px; border-bottom: 1px solid #ccc; padding-bottom: 5px; font-weight: bold;'>");
            message.println(file);
            message.println("</div>");
            message.println("<div>");
            message.println("<table border='1' cellspacing='0' cellpadding='2' style='font-size: 14px;'>");
            File tempOutputFile = new File(tempFolder, file);
            InputStream csvInputStream = null;
            try {
                csvInputStream = new BufferedInputStream(new FileInputStream(tempOutputFile));
                Scanner rowScanner = new Scanner(csvInputStream);
                int lineNumber = 0;
                while (rowScanner.hasNextLine() && lineNumber <= NUM_PREVIEW_ROWS) {
                    // For Hive jobs, the first line is the column names, so we ignore it
                    // when deciding whether the output is empty or not
                    if (!job.getType().equals(ReportalType.HiveJob.getJobTypeName()) || lineNumber > 0) {
                        emptyResults = false;
                    }

                    String csvLine = rowScanner.nextLine();
                    String[] data = csvLine.split("\",\"");
                    message.println("<tr>");
                    for (String item : data) {
                        String column = StringEscapeUtils.escapeHtml(item.replace("\"", ""));
                        message.println("<td>" + column + "</td>");
                    }
                    message.println("</tr>");
                    if (lineNumber == NUM_PREVIEW_ROWS && rowScanner.hasNextLine()) {
                        message.println("<tr>");
                        message.println("<td colspan=\"" + data.length + "\">...</td>");
                        message.println("</tr>");
                    }
                    lineNumber++;
                }
                rowScanner.close();
                message.println("</table>");
                message.println("</div>");
            } finally {
                IOUtils.closeQuietly(csvInputStream);
            }
            message.addAttachment(file, tempOutputFile);
        }

        // Don't send an email if there are no results, unless this is an unscheduled run.
        String unscheduledRun = flowParameters.get("reportal.unscheduled.run");
        boolean isUnscheduledRun = unscheduledRun != null && unscheduledRun.trim().equalsIgnoreCase("true");
        if (emptyResults && !isUnscheduledRun) {
            return false;
        }
    }

    message.println("</div>").println("</body>").println("</html>");

    return true;
}

From source file:com.mirth.connect.server.migration.Migrator.java

protected List<String> readStatements(String scriptResourceName, Map<String, Object> replacements)
        throws IOException {
    List<String> script = new ArrayList<String>();
    Scanner scanner = null;

    if (scriptResourceName.charAt(0) != '/' && defaultScriptPath != null) {
        scriptResourceName = defaultScriptPath + "/" + scriptResourceName;
    }/*from w ww  .  j  a  va 2 s . c  o m*/

    try {
        scanner = new Scanner(IOUtils.toString(ResourceUtil.getResourceStream(getClass(), scriptResourceName)));

        while (scanner.hasNextLine()) {
            StringBuilder stringBuilder = new StringBuilder();
            boolean blankLine = false;

            while (scanner.hasNextLine() && !blankLine) {
                String temp = scanner.nextLine();

                if (temp.trim().length() > 0) {
                    stringBuilder.append(temp + " ");
                } else {
                    blankLine = true;
                }
            }

            // Trim ending semicolons so Oracle doesn't throw
            // "java.sql.SQLException: ORA-00911: invalid character"
            String statementString = StringUtils.removeEnd(stringBuilder.toString().trim(), ";");

            if (statementString.length() > 0) {
                if (replacements != null && !replacements.isEmpty()) {
                    for (String key : replacements.keySet()) {
                        statementString = StringUtils.replace(statementString, "${" + key + "}",
                                replacements.get(key).toString());
                    }
                }

                script.add(statementString);
            }
        }

        return script;
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
}

From source file:com.sonyericsson.hudson.plugins.gerrit.trigger.playback.GerritMissedEventsPlaybackManager.java

/**
 * Takes a string of json events and creates a collection.
 * @param eventsString Events in json in a string.
 * @return collection of events.//from w w w .  j a  va 2 s.  c om
 */
private List<GerritTriggeredEvent> createEventsFromString(String eventsString) {
    List<GerritTriggeredEvent> events = Collections.synchronizedList(new ArrayList<GerritTriggeredEvent>());
    Scanner scanner = new Scanner(eventsString);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        logger.debug("found line: {}", line);
        JSONObject jsonObject = null;
        try {
            jsonObject = GerritJsonEventFactory.getJsonObjectIfInterestingAndUsable(line);
            if (jsonObject == null) {
                continue;
            }
        } catch (Exception ex) {
            logger.warn("Unanticipated error when creating DTO representation of JSON string.", ex);
            continue;
        }
        GerritEvent evt = GerritJsonEventFactory.getEvent(jsonObject);
        if (evt instanceof GerritTriggeredEvent) {
            Provider provider = new Provider();
            provider.setName(serverName);
            ((GerritTriggeredEvent) evt).setProvider(provider);
            events.add((GerritTriggeredEvent) evt);
        }
    }
    scanner.close();
    return events;
}