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.joliciel.jochre.search.JochreIndexBuilderImpl.java

private void processDocument(File documentDir, boolean forceUpdate) {
    try {/*from   ww w . j  a  v a2 s  . c  o m*/
        File instructionsFile = new File(documentDir, "instructions.txt");
        boolean updateIndex = false;
        if (instructionsFile.exists()) {
            String instructions = null;
            Scanner scanner = new Scanner(
                    new BufferedReader(new InputStreamReader(new FileInputStream(instructionsFile), "UTF-8")));
            while (scanner.hasNextLine()) {
                instructions = scanner.nextLine();
                break;
            }
            scanner.close();

            LOG.info("Instructions: " + instructions + " for " + documentDir.getName());
            if (instructions.equals("delete")) {
                this.deleteDocumentInternal(documentDir);
                File lastIndexDateFile = new File(documentDir, "indexDate.txt");
                if (lastIndexDateFile.exists())
                    lastIndexDateFile.delete();

                return;
            } else if (instructions.equals("skip")) {
                return;
            } else if (instructions.equals("update")) {
                updateIndex = true;
            } else {
                LOG.info("Unknown instructions.");
            }
        }

        File zipFile = new File(documentDir, documentDir.getName() + ".zip");
        if (!zipFile.exists()) {
            LOG.info("Nothing to index in " + documentDir.getName());
            return;
        }

        File metaDataFile = new File(documentDir, "metadata.txt");
        if (!metaDataFile.exists()) {
            LOG.info("Skipping: OCR analysis incomplete for " + documentDir.getName());
            return;
        }

        if (forceUpdate)
            updateIndex = true;

        if (!updateIndex) {

            LOG.debug("Checking last update date on " + documentDir.getName());
            long zipDate = zipFile.lastModified();

            File lastIndexDateFile = new File(documentDir, "indexDate.txt");

            long lastIndexDate = Long.MIN_VALUE;

            if (lastIndexDateFile.exists()) {
                Scanner scanner = new Scanner(new BufferedReader(
                        new InputStreamReader(new FileInputStream(lastIndexDateFile), "UTF-8")));
                while (scanner.hasNextLine()) {
                    lastIndexDate = Long.parseLong(scanner.nextLine());
                    break;
                }
                scanner.close();
            }
            if (zipDate > lastIndexDate)
                updateIndex = true;
        }

        if (updateIndex) {
            this.updateDocumentInternal(documentDir);
        } else {
            LOG.info("Index for " + documentDir.getName() + "already up-to-date.");
        } // should update index?

    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    }
}

From source file:net.pms.configuration.DownloadPlugins.java

private void doExec(String args) throws IOException, InterruptedException, ConfigurationException {
    int pos = args.indexOf(',');
    if (pos == -1) { // weird stuff
        return;/*from w ww  .  j  a  v  a  2  s .  com*/
    }

    // Everything after the "," is what we're supposed to run
    // First make note of jars we got
    File[] oldJar = new File(configuration.getPluginDirectory()).listFiles();

    // Before we start external installers better save the config
    configuration.save();
    ProcessBuilder pb = new ProcessBuilder(args.substring(pos + 1));
    pb.redirectErrorStream(true);
    Map<String, String> env = pb.environment();
    env.put("PROFILE_PATH", configuration.getProfilePath());
    env.put("UMS_VERSION", PMS.getVersion());

    LOGGER.debug("running '" + args + "'");
    Process pid = pb.start();
    // Consume and log any output
    Scanner output = new Scanner(pid.getInputStream());
    while (output.hasNextLine()) {
        LOGGER.debug("[" + args + "] " + output.nextLine());
    }
    configuration.reload();
    pid.waitFor();

    File[] newJar = new File(configuration.getPluginDirectory()).listFiles();
    for (File f : newJar) {
        if (!f.getAbsolutePath().endsWith(".jar")) {
            // skip non jar files
            continue;
        }
        for (File oldJar1 : oldJar) {
            if (f.getAbsolutePath().equals(oldJar1.getAbsolutePath())) {
                // old jar file break out, and set f to null to skip adding it
                f = null;
                break;
            }
        }
        // if f is null this is an jar that is old
        if (f != null) {
            jars.add(f.toURI().toURL());
        }
    }
}

From source file:edu.ucuenca.authorsdisambiguation.nwd.NWD.java

public synchronized String Http(String s, String prefix) throws SQLException, IOException {

    String get = Cache.getInstance().get(prefix + s);
    String resp = "";
    if (get != null) {
        //System.out.print(".");
        resp = get;/*from w w w  . jav  a  2  s.  c om*/
    } else {
        final URL url = new URL(s);
        final URLConnection connection = url.openConnection();
        connection.setConnectTimeout(60000);
        connection.setReadTimeout(60000);
        connection.addRequestProperty("User-Agent",
                "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0");
        connection.addRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        final Scanner reader = new Scanner(connection.getInputStream(), "UTF-8");
        while (reader.hasNextLine()) {
            final String line = reader.nextLine();
            resp += line + "\n";
        }
        reader.close();

        Cache.getInstance().put(prefix + s, resp);
    }

    return resp;
}

From source file:com.github.Jikoo.BookSuite.CommandHandler.java

public void asyncBookImport(final Player p, final String s) {
    new BukkitRunnable() {
        public void run() {
            BookMeta bm;/*from  w w w  .  j  a  v  a2s .  c o  m*/
            StringBuilder sb = new StringBuilder();
            try {
                URL url = new URL(s);
                Scanner urlInput = new Scanner(url.openStream());
                ;
                while (urlInput.hasNextLine()) {
                    sb.append(urlInput.nextLine()).append('\n');
                }
                urlInput.close();
            } catch (Exception e) {
                bm = (BookMeta) new ItemStack(Material.WRITTEN_BOOK);
            }
            bm = plugin.filemanager.makeBookMetaFromText(p, sb.toString(), true);
            syncBookImport(p, bm);
        }
    }.runTaskAsynchronously(BookSuite.getInstance());
}

From source file:com.joliciel.talismane.machineLearning.perceptron.PerceptronClassifactionModelTrainerImpl.java

void prepareData(ClassificationEventStream eventStream) {
    try {//from   w  w  w.  j a va 2  s  .  c  o m
        eventFile = File.createTempFile("events", "txt");
        eventFile.deleteOnExit();
        Writer eventWriter = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(eventFile), "UTF-8"));
        while (eventStream.hasNext()) {
            ClassificationEvent corpusEvent = eventStream.next();
            PerceptronEvent event = new PerceptronEvent(corpusEvent, params);
            event.write(eventWriter);
        }
        eventWriter.flush();
        eventWriter.close();

        if (cutoff > 1) {
            params.initialiseCounts();
            File originalEventFile = eventFile;
            Scanner scanner = new Scanner(
                    new BufferedReader(new InputStreamReader(new FileInputStream(eventFile), "UTF-8")));

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                PerceptronEvent event = new PerceptronEvent(line);
                for (int featureIndex : event.getFeatureIndexes()) {
                    params.getFeatureCounts()[featureIndex]++;
                }
            }
            scanner.close();

            if (LOG.isDebugEnabled()) {
                int[] cutoffCounts = new int[21];
                for (int count : params.getFeatureCounts()) {
                    for (int i = 1; i < 21; i++) {
                        if (count >= i) {
                            cutoffCounts[i]++;
                        }
                    }
                }
                LOG.debug("Feature counts:");
                for (int i = 1; i < 21; i++) {
                    LOG.debug("Cutoff " + i + ": " + cutoffCounts[i]);
                }
            }
            PerceptronModelParameters cutoffParams = new PerceptronModelParameters();
            int[] newIndexes = cutoffParams.initialise(params, cutoff);
            decisionMaker = new PerceptronDecisionMaker<T>(cutoffParams, decisionFactory);
            scanner = new Scanner(
                    new BufferedReader(new InputStreamReader(new FileInputStream(eventFile), "UTF-8")));

            eventFile = File.createTempFile("eventsCutoff", "txt");
            eventFile.deleteOnExit();
            Writer eventCutoffWriter = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(eventFile), "UTF-8"));
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                PerceptronEvent oldEvent = new PerceptronEvent(line);
                PerceptronEvent newEvent = new PerceptronEvent(oldEvent, newIndexes);
                newEvent.write(eventCutoffWriter);
            }
            eventCutoffWriter.flush();
            eventCutoffWriter.close();
            params = cutoffParams;
            originalEventFile.delete();
        }

        params.initialiseWeights();
        totalFeatureWeights = new double[params.getFeatureCount()][params.getOutcomeCount()];
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:org.atricore.josso.tooling.wrapper.InstallCommand.java

private void copyFilteredResourceTo(File outFile, String resource, HashMap<String, String> props)
        throws Exception {
    if (!outFile.exists()) {
        System.out.println(Ansi.ansi().a("Creating file: ").a(Ansi.Attribute.INTENSITY_BOLD)
                .a(outFile.getPath()).a(Ansi.Attribute.RESET).toString());
        InputStream is = InstallCommand.class.getResourceAsStream(resource);
        try {/*from   ww  w  . j  av  a2 s  . co  m*/
            // Read it line at a time so that we can use the platform line ending when we write it out.
            PrintStream out = new PrintStream(new FileOutputStream(outFile));
            try {
                Scanner scanner = new Scanner(is);
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    line = filter(line, props);
                    out.println(line);
                }
            } finally {
                safeClose(out);
            }
        } finally {
            safeClose(is);
        }
    } else {
        System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("File already exists").a(Ansi.Attribute.RESET)
                .a(". Move it out of the way if you want it re-created: ").a(outFile.getPath()).toString());
    }
}

From source file:carmen.LocationResolver.java

@SuppressWarnings("unchecked")
protected void loadLocationFile(String filename) throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();

    Scanner inputScanner = new Scanner(new FileInputStream(filename));
    while (inputScanner.hasNextLine()) {
        String line = inputScanner.nextLine();
        Map<String, Object> locationObj = mapper.readValue(line, Map.class);
        Location location = Location.parseLocationFromJsonObj(locationObj);

        List<String> aliases = (List<String>) locationObj.get("aliases");
        this.idToLocation.put(location.getId(), location);
        this.locationToId.put(location, location.getId());
        HashSet<String> justAddedAliases = new HashSet<String>();
        if (aliases != null) {
            for (String alias : aliases) {
                if (justAddedAliases.contains(alias))
                    continue;

                if (this.locationNameToLocation.containsKey(alias))
                    logger.warn("Duplicate location name: " + alias);
                else
                    this.locationNameToLocation.put(alias, location);
                justAddedAliases.add(alias);

                // Add entries without punctuation.
                String newEntry = alias.replaceAll("\\p{Punct}", " ").replaceAll("\\s+", " ");
                if (justAddedAliases.contains(newEntry))
                    continue;

                if (!newEntry.equals(alias)) {
                    if (this.locationNameToLocation.containsKey(newEntry))
                        logger.warn("Duplicate location name: " + newEntry);
                    else
                        this.locationNameToLocation.put(newEntry, location);
                }/*from   w ww  .j  a  v  a2 s  .c o  m*/

                justAddedAliases.add(newEntry);

            }
        }
    }
    inputScanner.close();
}

From source file:edu.cmu.lti.oaqa.framework.eval.gs.KeytermGoldStandardFilePersistenceProvider.java

@Override
public boolean initialize(ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams)
        throws ResourceInitializationException {
    boolean ret = super.initialize(aSpecifier, aAdditionalParams);
    String dataset = (String) getParameterValue("DataSet");
    Pattern lineSyntaxPattern = Pattern.compile((String) getParameterValue("LineSyntax"));
    try {//from w ww. ja v  a 2s. c  o m
        Resource[] resources = resolver.getResources((String) getParameterValue("PathPattern"));
        for (Resource resource : resources) {
            Scanner scanner = new Scanner(resource.getInputStream());
            while (scanner.findInLine(lineSyntaxPattern) != null) {
                MatchResult result = scanner.match();
                DatasetSequenceId id = new DatasetSequenceId(dataset, result.group(1));
                if (!id2gsSpans.containsKey(id)) {
                    id2gsSpans.put(id, new ArrayList<String>());
                }
                id2gsSpans.get(id).add(result.group(4));
                if (scanner.hasNextLine()) {
                    scanner.nextLine();
                } else {
                    break;
                }
            }
            scanner.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:com.conx.logistics.kernel.bpm.impl.jbpm.core.mock.BPMGuvnorUtil.java

private String readFile(InputStream inStream) throws IOException {
    StringBuilder fileContents = new StringBuilder();
    Scanner scanner = new Scanner(inStream);
    String lineSeparator = System.getProperty("line.separator");
    try {/*  w ww.  jav a 2s.  com*/
        while (scanner.hasNextLine()) {
            fileContents.append(scanner.nextLine() + lineSeparator);
        }
        return fileContents.toString();
    } finally {
        scanner.close();
    }
}

From source file:org.atricore.josso.tooling.wrapper.InstallCommand.java

private void copyResourceTo(File outFile, String resource, boolean text) throws Exception {
    if (!outFile.exists()) {
        System.out.println(Ansi.ansi().a("Creating file: ").a(Ansi.Attribute.INTENSITY_BOLD)
                .a(outFile.getPath()).a(Ansi.Attribute.RESET).toString());
        InputStream is = InstallCommand.class.getResourceAsStream(resource);
        try {/*w  w w .ja v  a  2s. c om*/
            if (text) {
                // Read it line at a time so that we can use the platform line ending when we write it out.
                PrintStream out = new PrintStream(new FileOutputStream(outFile));
                try {
                    Scanner scanner = new Scanner(is);
                    while (scanner.hasNextLine()) {
                        String line = scanner.nextLine();
                        System.out.println("writing: " + line);
                        out.println(line);
                    }
                } finally {
                    safeClose(out);
                }
            } else {
                // Binary so just write it out the way it came in.
                FileOutputStream out = new FileOutputStream(outFile);
                try {
                    int c = 0;
                    while ((c = is.read()) >= 0) {
                        out.write(c);
                    }
                } finally {
                    safeClose(out);
                }
            }
        } finally {
            safeClose(is);
        }
    } else {
        System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("File already exists").a(Ansi.Attribute.RESET)
                .a(". Move it out of the way if you want it re-created: ").a(outFile.getPath()).toString());
    }
}