Example usage for java.util Scanner nextLine

List of usage examples for java.util Scanner nextLine

Introduction

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

Prototype

public String nextLine() 

Source Link

Document

Advances this scanner past the current line and returns the input that was skipped.

Usage

From source file:com.gitblit.HtpasswdUserService.java

/**
 * Reads the realm file and rebuilds the in-memory lookup tables.
 *//*from  w w w  . j av  a  2  s .c  om*/
protected synchronized void read() {

    // This is done in two steps in order to avoid calling GitBlit.getFileOrFolder(String, String) which will segfault for unit tests.
    String file = settings.getString(KEY_HTPASSWD_FILE, DEFAULT_HTPASSWD_FILE);
    if (!file.equals(htpasswdFilePath)) {
        // The htpasswd file setting changed. Rediscover the file.
        this.htpasswdFilePath = file;
        this.htpasswdFile = GitBlit.getFileOrFolder(file);
        this.htUsers.clear();
        this.forceReload = true;
    }

    if (htpasswdFile.exists() && (forceReload || (htpasswdFile.lastModified() != lastModified))) {
        forceReload = false;
        lastModified = htpasswdFile.lastModified();
        htUsers.clear();

        Pattern entry = Pattern.compile("^([^:]+):(.+)");

        Scanner scanner = null;
        try {
            scanner = new Scanner(new FileInputStream(htpasswdFile));
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine().trim();
                if (!line.isEmpty() && !line.startsWith("#")) {
                    Matcher m = entry.matcher(line);
                    if (m.matches()) {
                        htUsers.put(m.group(1), m.group(2));
                    }
                }
            }
        } catch (Exception e) {
            logger.error(MessageFormat.format("Failed to read {0}", htpasswdFile), e);
        } finally {
            if (scanner != null)
                scanner.close();
        }
    }
}

From source file:azkaban.jobtype.ReportalHiveRunner.java

@Override
protected void runReportal() throws Exception {
    System.out.println("Reportal Hive: Setting up Hive");
    HiveConf conf = new HiveConf(SessionState.class);

    if (System.getenv("HADOOP_TOKEN_FILE_LOCATION") != null) {
        conf.set("mapreduce.job.credentials.binary", System.getenv("HADOOP_TOKEN_FILE_LOCATION"));
    }/*ww w.  j  a va  2s .c  o  m*/

    File tempTSVFile = new File("./temp.tsv");
    OutputStream tsvTempOutputStream = new BoundedOutputStream(
            new BufferedOutputStream(new FileOutputStream(tempTSVFile)), outputCapacity);
    PrintStream logOut = System.out;

    // NOTE: It is critical to do this here so that log4j is reinitialized
    // before any of the other core hive classes are loaded
    // criccomini@linkedin.com: I disabled this because it appears to swallow
    // all future logging (even outside of hive).
    // SessionState.initHiveLog4j();

    String orig = HiveConf.getVar(conf, HiveConf.ConfVars.HIVEAUXJARS);

    CliSessionState sessionState = new CliSessionState(conf);
    sessionState.in = System.in;
    sessionState.out = new PrintStream(tsvTempOutputStream, true, "UTF-8");
    sessionState.err = new PrintStream(logOut, true, "UTF-8");

    OptionsProcessor oproc = new OptionsProcessor();

    // Feed in Hive Args
    String[] args = buildHiveArgs();
    if (!oproc.process_stage1(args)) {
        throw new Exception("unable to parse options stage 1");
    }

    if (!oproc.process_stage2(sessionState)) {
        throw new Exception("unable to parse options stage 2");
    }

    // Set all properties specified via command line
    for (Map.Entry<Object, Object> item : sessionState.cmdProperties.entrySet()) {
        conf.set((String) item.getKey(), (String) item.getValue());
    }

    SessionState.start(sessionState);

    String expanded = expandHiveAuxJarsPath(orig);
    if (orig == null || orig.equals(expanded)) {
        System.out.println("Hive aux jars variable not expanded");
    } else {
        System.out.println("Expanded aux jars variable from [" + orig + "] to [" + expanded + "]");
        HiveConf.setVar(conf, HiveConf.ConfVars.HIVEAUXJARS, expanded);
    }

    if (!ShimLoader.getHadoopShims().usesJobShell()) {
        // hadoop-20 and above - we need to augment classpath using hiveconf
        // components
        // see also: code in ExecDriver.java
        ClassLoader loader = conf.getClassLoader();
        String auxJars = HiveConf.getVar(conf, HiveConf.ConfVars.HIVEAUXJARS);

        System.out.println("Got auxJars = " + auxJars);

        if (StringUtils.isNotBlank(auxJars)) {
            loader = Utilities.addToClassPath(loader, StringUtils.split(auxJars, ","));
        }
        conf.setClassLoader(loader);
        Thread.currentThread().setContextClassLoader(loader);
    }

    CliDriver cli = new CliDriver();
    int returnValue = 0;
    String prefix = "";

    returnValue = cli.processLine("set hive.cli.print.header=true;");
    String[] queries = jobQuery.split("\n");
    for (String line : queries) {
        if (!prefix.isEmpty()) {
            prefix += '\n';
        }
        if (line.trim().endsWith(";") && !line.trim().endsWith("\\;")) {
            line = prefix + line;
            line = injectVariables(line);
            System.out.println("Reportal Hive: Running Hive Query: " + line);
            System.out.println("Reportal Hive: HiveConf HIVEAUXJARS: "
                    + HiveConf.getVar(conf, HiveConf.ConfVars.HIVEAUXJARS));
            returnValue = cli.processLine(line);
            prefix = "";
        } else {
            prefix = prefix + line;
            continue;
        }
    }

    tsvTempOutputStream.close();

    // convert tsv to csv and write it do disk
    System.out.println("Reportal Hive: Converting output");
    InputStream tsvTempInputStream = new BufferedInputStream(new FileInputStream(tempTSVFile));
    Scanner rowScanner = new Scanner(tsvTempInputStream);
    PrintStream csvOutputStream = new PrintStream(outputStream);
    while (rowScanner.hasNextLine()) {
        String tsvLine = rowScanner.nextLine();
        // strip all quotes, and then quote the columns
        csvOutputStream.println("\"" + tsvLine.replace("\"", "").replace("\t", "\",\"") + "\"");
    }
    rowScanner.close();
    csvOutputStream.close();

    // Flush the temp file out
    tempTSVFile.delete();

    if (returnValue != 0) {
        throw new Exception("Hive query finished with a non zero return code");
    }

    System.out.println("Reportal Hive: Ended successfully");
}

From source file:com.ibm.iotf.sample.devicemgmt.device.RasPiFirmwareHandlerSample.java

private InstalStatus ParseInstallLog() throws FileNotFoundException {
    try {//www . ja v  a2  s.  c om
        File file = new File(INSTALL_LOG_FILE);
        Scanner scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.contains(DEPENDENCY_ERROR_MSG)) {
                scanner.close();
                return InstalStatus.DEPENDENCY_ERROR;
            } else if (line.contains(ERROR_MSG)) {
                scanner.close();
                return InstalStatus.ERROR;
            }
        }
        scanner.close();
    } catch (FileNotFoundException e) {
        throw e;
    }
    return InstalStatus.SUCCESS;
}

From source file:com.pinterest.clusterservice.cm.AwsVmManager.java

private Map<String, String> transformUserDataToConfigMap(String clusterName, String userData) throws Exception {
    String userDataString = userData.replace(String.format(AWS_USERDATA_TEMPLATE, clusterName, clusterName),
            "");//w  ww  . j  a  v a  2 s.  c  om
    Map<String, String> resultMap = new HashMap<>();
    if (userDataString.length() == 0) {
        return resultMap;
    }

    Scanner scanner = new Scanner(userDataString);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        List<String> config = Arrays.asList(line.split(": "));
        if (config.size() == 2) {
            resultMap.put(config.get(0), config.get(1));
        }
    }
    scanner.close();
    return resultMap;
}

From source file:analyzer.code.FXMLAnalyzerController.java

@FXML
private void openProjectMenuItem() throws FileNotFoundException, IOException {
    source.clear();//w  ww .j a v  a 2 s .co  m
    DirectoryChooser chooser = new DirectoryChooser();
    chooser.setTitle("C project");
    File selectedDirectory = chooser.showDialog(null);
    if (null != selectedDirectory) {
        File[] files = selectedDirectory.listFiles();
        for (File file : files) {
            if (FilenameUtils.getExtension(file.getAbsolutePath()).equals("java")) {
                analyzer = new AnalyzerC();
                curLang = C;
                countOperatorsEnable = true;
                levelNestEnable = true;
                middleLenIdentEnable = true;
                tableMetric.setVisible(true);
                tableDescript.setVisible(true);
                labelDesc.setVisible(true);
                labelMetrics.setVisible(true);
                button.setVisible(true);
                Scanner scanner = new Scanner(file);
                nameFile.add(FilenameUtils.getName(file.getAbsolutePath()));
                String tmpSource = new String();
                while (scanner.hasNext()) {
                    tmpSource += scanner.nextLine() + '\n';
                }
                source.add(tmpSource);
            }
        }
    }
}

From source file:com.joliciel.talismane.fr.TalismaneFrench.java

@Override
public TransitionSystem getDefaultTransitionSystem() {
    TransitionSystem transitionSystem = this.getParserService().getArcEagerTransitionSystem();
    InputStream inputStream = getInputStreamFromResource("talismaneDependencyLabels.txt");
    Scanner scanner = new Scanner(inputStream, "UTF-8");
    List<String> dependencyLabels = new ArrayList<String>();
    while (scanner.hasNextLine()) {
        String dependencyLabel = scanner.nextLine();
        if (!dependencyLabel.startsWith("#"))
            dependencyLabels.add(dependencyLabel);
    }//from  w  ww .j a  v a2s .c o  m
    transitionSystem.setDependencyLabels(dependencyLabels);
    return transitionSystem;
}

From source file:com.searchcode.app.util.Helpers.java

/**
 * Reads a certain amount of lines deep into a file to save on memory
 *//* w ww .  j a  v a  2s  . co m*/
public List<String> readFileLines(String filePath, int maxFileLineDepth) throws FileNotFoundException {
    List<String> lines = new ArrayList<>();
    Scanner scanner = null;
    int counter = 0;

    try {
        scanner = new Scanner(new File(filePath));

        while (scanner.hasNextLine() && counter < maxFileLineDepth) {
            lines.add(scanner.nextLine());
            counter++;
        }
    } finally {
        IOUtils.closeQuietly(scanner);
    }

    return lines;
}

From source file:com.alibaba.dubbo.qos.textui.TTree.java

@Override
public String rendering() {

    final StringBuilder treeSB = new StringBuilder();
    recursive(0, true, "", root, new Callback() {

        @Override//  w w w  . j a va2s  .  co  m
        public void callback(int deep, boolean isLast, String prefix, Node node) {

            final boolean hasChild = !node.children.isEmpty();
            final String stepString = isLast ? STEP_FIRST_CHAR : STEP_NORMAL_CHAR;
            final int stepStringLength = StringUtils.length(stepString);
            treeSB.append(prefix).append(stepString);

            int costPrefixLength = 0;
            if (hasChild) {
                treeSB.append("+");
            }
            if (isPrintCost && !node.isRoot()) {
                final String costPrefix = String.format("[%s,%sms]", (node.endTimestamp - root.beginTimestamp),
                        (node.endTimestamp - node.beginTimestamp));
                costPrefixLength = StringUtils.length(costPrefix);
                treeSB.append(costPrefix);
            }

            final Scanner scanner = new Scanner(new StringReader(node.data.toString()));
            try {
                boolean isFirst = true;
                while (scanner.hasNextLine()) {
                    if (isFirst) {
                        treeSB.append(scanner.nextLine()).append("\n");
                        isFirst = false;
                    } else {
                        treeSB.append(prefix).append(repeat(' ', stepStringLength))
                                .append(hasChild ? "|" : EMPTY).append(repeat(' ', costPrefixLength))
                                .append(scanner.nextLine()).append("\n");
                    }
                }
            } finally {
                scanner.close();
            }

        }

    });

    return treeSB.toString();
}

From source file:org.apache.sling.testing.clients.SlingHttpResponse.java

/**
 * <p>For each regular expression, assert that at least one line of the response matches the expression</p>
 * <p>The regular expressions are automatically prefixed and suffixed with .* it order to partial-match the lines</p>
 *
 * @param regexp list of regular expressions
 * @throws AssertionError if the response content does not match one of the regexp
 *//* w ww  .  j  a v  a2s  . c o  m*/
public void checkContentRegexp(String... regexp) throws ClientException {
    for (String expr : regexp) {
        final Pattern p = Pattern.compile(".*" + expr + ".*");
        final Scanner scanner = new Scanner(this.getContent());
        boolean matched = false;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (p.matcher(line).matches()) {
                matched = true;
                break;
            }
        }

        if (!matched) {
            throw new ClientException("Pattern " + p + " didn't match any line in content");
        }
    }
}

From source file:de.tudarmstadt.ukp.wikipedia.parser.mediawiki.FrenchTemplateParser.java

public HashMap<String, String> getParseText(String path, boolean print, boolean normalize)
        throws FileNotFoundException {

    HashMap<String, String> parsing = new HashMap<String, String>();

    String filePath = path;//from w ww .  j a v a  2  s .  com
    Scanner scanner = new Scanner(new File(filePath));

    // On boucle sur chaque champ detect
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        line = line.trim();

        if (!line.startsWith("#") && line.length() > 0) {

            String name = line.replaceAll("\\[(.*)\\]", "");
            String value = line.substring(name.length());
            value = value.replaceAll("\\[", "");
            value = value.replaceAll("\\]", "");

            if (print) {
                System.out.println(name + "\t" + value);
            }
            parsing.put(name, value);

            if (normalize) {
                if (print) {
                    System.out.println(name.toUpperCase() + "\t" + value);
                    System.out.println(name.toLowerCase() + "\t" + value);
                }
                parsing.put(name.toUpperCase(), value);
                parsing.put(name.toLowerCase(), value);
            }
        }
        // faites ici votre traitement
    }

    scanner.close();

    return parsing;
}