Example usage for java.util Scanner hasNext

List of usage examples for java.util Scanner hasNext

Introduction

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

Prototype

public boolean hasNext() 

Source Link

Document

Returns true if this scanner has another token in its input.

Usage

From source file:no.dusken.momus.service.drive.GoogleDriveService.java

/**
 * Fetches the new content from Google Drive as HTML, and sends
 * it to the googleDocsTextConverter//  w w w .  j a va2s.  c  om
 */
private void updateContentFromDrive(Article article) {
    String downloadUrl = "https://docs.google.com/feeds/download/documents/export/Export?id="
            + article.getGoogleDriveId() + "&exportFormat=html";

    try {
        // Read data, small hack to convert the stream to a string
        InputStream inputStream = drive.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl))
                .execute().getContent();
        Scanner s = new java.util.Scanner(inputStream).useDelimiter("\\A");
        String content = s.hasNext() ? s.next() : "";
        logger.debug("Got new content:\n{}", content);

        String convertedContent = googleDocsTextConverter.convert(content);
        article.setContent(convertedContent);
    } catch (IOException e) {
        logger.error("Couldn't get content for article", e);
        // Let the content remain as it was
    }
}

From source file:com.github.matthesrieke.simplebroker.SimpleBrokerServlet.java

protected String readContent(HttpServletRequest req) throws IOException {
    String enc = req.getCharacterEncoding();
    Scanner sc = new Scanner(req.getInputStream(), enc == null ? "utf-8" : enc);
    StringBuilder sb = new StringBuilder();

    while (sc.hasNext()) {
        sb.append(sc.nextLine());//from w  w  w. j  a  v a 2 s . c  o m
    }

    sc.close();
    return sb.toString();
}

From source file:edu.pucp.igc.piscosemanticsearch.Indexador.java

private String retomarTexto(String miArchivo) throws FileNotFoundException {
    //String rmRuta = "//Users//NuSs//Documents//workspaces//NetbeansWorkspace//SemanticSearch//src//main//resources//";
    //        FileInputStream fstream = new FileInputStream(rmRuta + miArchivo);
    //        DataInputStream = new DataInputStream(fstream);

    Scanner sc = new Scanner(new File(rmRuta + miArchivo));
    String strLinea = null;//from w w  w  . j a v a2s . c  om
    String texto = "";

    while (sc.hasNext()) {
        texto = texto.concat(sc.nextLine() + "\n");
    }
    return texto;
}

From source file:org.ff4j.store.RemoteHttpFeatureStore.java

/**
 * Invole GET webServices to feature./* www .ja  v  a  2  s .com*/
 * 
 * @param path
 *            target path
 * @param featureId
 *            current feature id
 * @return JSON output response
 */
private String makeGetCall(String path, String featureId) {
    try {
        // Create request
        HttpGet getRequest = new HttpGet(url + path + "/" + featureId);
        getRequest.addHeader("accept", JSON);
        HttpResponse response = httpClient.execute(getRequest);

        java.util.Scanner s = new java.util.Scanner(response.getEntity().getContent()).useDelimiter("\\A");
        String output = s.hasNext() ? s.next() : "";
        httpClient.getConnectionManager().shutdown();

        // Handle Error
        if (response.getStatusLine().getStatusCode() == 404) {
            throw new FeatureNotFoundException(output);
        }
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new FeatureAccessException(
                    "ErrorHTTP(" + response.getStatusLine().getStatusCode() + ") - " + output);
        }

        return output;

    } catch (ClientProtocolException cpe) {
        throw new FeatureAccessException("Cannot access remote HTTP Feature Store", cpe);
    } catch (IOException ioe) {
        throw new FeatureAccessException("Cannot read response from  HTTP Feature Store", ioe);
    }
}

From source file:me.figo.FigoConnection.java

/***
 * Handle the response of a request by decoding its JSON payload
 * @param stream Stream containing the JSON data
 * @param typeOfT Type of the data to be expected
 * @return Decoded data/*from   w  w  w  . j ava2 s .  c om*/
 */
private <T> T handleResponse(InputStream stream, Type typeOfT) {
    // check whether decoding is actual requested
    if (typeOfT == null)
        return null;

    // read stream body
    Scanner s = new Scanner(stream, "UTF-8");
    s.useDelimiter("\\A");
    String body = s.hasNext() ? s.next() : "";
    s.close();

    // decode JSON payload
    Gson gson = GsonAdapter.createGson();
    return gson.fromJson(body, typeOfT);
}

From source file:htsjdk.samtools.tabix.IndexingFeatureWriterNGTest.java

@Test
public void blockCompressFile() throws FileNotFoundException {
    String file = "/home/sol/Downloads/test.chain.gff";
    String out = "/home/sol/Downloads/t.bgz";
    BlockCompressedOutputStream bcos = new BlockCompressedOutputStream(new File(out));
    PrintStream ps = new PrintStream(bcos);
    Scanner scan = new Scanner(new File(file));
    while (scan.hasNext()) {
        ps.println(scan.nextLine());/*from  ww w.j a v a 2 s  . c  om*/
    }
    ps.close();
}

From source file:com.evolveum.midpoint.task.quartzimpl.work.segmentation.StringWorkSegmentationStrategy.java

private String expand(String s) {
    StringBuilder sb = new StringBuilder();
    Scanner scanner = new Scanner(s);

    while (scanner.hasNext()) {
        if (scanner.isDash()) {
            if (sb.length() == 0) {
                throw new IllegalArgumentException("Boundary specification cannot start with '-': " + s);
            } else {
                scanner.next();//from   www . jav a2  s .c o m
                if (!scanner.hasNext()) {
                    throw new IllegalArgumentException("Boundary specification cannot end with '-': " + s);
                } else {
                    appendFromTo(sb, sb.charAt(sb.length() - 1), scanner.next());
                }
            }
        } else {
            sb.append(scanner.next());
        }
    }
    String expanded = sb.toString();
    if (marking == INTERVAL) {
        checkBoundary(expanded);
    }
    return expanded;
}

From source file:org.seedstack.shell.internal.AbstractShell.java

List<Command> createCommandActions(String line) {
    if (Strings.isNullOrEmpty(line)) {
        return new ArrayList<>();
    }//from   www . j  a  v a 2s  .co  m

    List<Command> commands = new ArrayList<>();

    Scanner scanner = new Scanner(new StringReader(line));

    String qualifiedName = null;
    List<String> args = new ArrayList<>();

    while (scanner.hasNext()) {
        if (qualifiedName == null) {
            if (scanner.hasNext(COMMAND_PATTERN)) {
                qualifiedName = scanner.next(COMMAND_PATTERN);
            } else {
                throw SeedException.createNew(ShellErrorCode.COMMAND_PARSING_ERROR);
            }
        } else {
            // Find next token respecting quoted strings
            String arg = scanner.findWithinHorizon("[^\"\\s]+|\"(\\\\.|[^\\\\\"])*\"", 0);
            if (arg != null) {
                if ("|".equals(arg)) {
                    // unquoted pipe, we execute the command and store the result for the next one
                    commands.add(createCommandAction(qualifiedName, args));

                    qualifiedName = null;
                    args = new ArrayList<>();
                } else {
                    // if it's a quoted string, unquote it
                    if (arg.startsWith("\"")) {
                        arg = arg.substring(1);
                    }
                    if (arg.endsWith("\"")) {
                        arg = arg.substring(0, arg.length() - 1);
                    }

                    // replace any escaped quote by real quote
                    args.add(arg.replaceAll("\\\\\"", "\""));
                }
            } else {
                throw SeedException.createNew(ShellErrorCode.COMMAND_SYNTAX_ERROR).put("value", scanner.next());
            }
        }
    }

    commands.add(createCommandAction(qualifiedName, args));

    return commands;
}

From source file:kuona.jenkins.analyser.JenkinsClient.java

/**
 * Perform a GET request and parse the response and return a simple string of the content
 *
 * @param path path to request, can be relative or absolute
 * @return the entity text//from ww  w .  j av a2s .c  o m
 * @throws IOException, HttpResponseException
 */
public String get(String path) throws IOException {
    HttpGet getMethod = new HttpGet(api(path));
    HttpResponse response = client.execute(getMethod, localContext);
    try {
        httpResponseValidator.validateResponse(response);
        Scanner s = new Scanner(response.getEntity().getContent());
        s.useDelimiter("\\z");
        StringBuffer sb = new StringBuffer();
        while (s.hasNext()) {
            sb.append(s.next());
        }
        return sb.toString();
    } finally {
        releaseConnection(getMethod);
    }
}

From source file:kuona.client.JenkinsHttpClient.java

/**
 * Perform a GET request and parse the response and return a simple string of the content
 *
 * @param path path to request, can be relative or absolute
 * @return the entity text/*  w  w  w.  j  a  v a 2  s .c o m*/
 * @throws IOException, HttpResponseException
 */
public String get(String path, String contentType) throws IOException {
    HttpGet getMethod = new HttpGet(api(path));
    HttpResponse response = client.execute(getMethod, localContext);
    try {
        httpResponseValidator.validateResponse(response);
        Scanner s = new Scanner(response.getEntity().getContent());
        s.useDelimiter("\\z");
        StringBuffer sb = new StringBuffer();
        while (s.hasNext()) {
            sb.append(s.next());
        }
        return sb.toString();
    } finally {
        releaseConnection(getMethod);
    }
}