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:com.ckfinder.connector.utils.FileUtils.java

/**
 * checks is file extension is on denied list or isn't on allowed list.
 * @param fileExt file extension/* w  w  w.  java 2 s.co  m*/
 * @param type file resource type
 * @return 0 if isn't on denied and is on allowed, otherwise 1
 */
private static boolean checkSingleExtension(final String fileExt, final ResourceType type) {
    Scanner scanner = new Scanner(type.getDeniedExtensions()).useDelimiter(",");
    while (scanner.hasNext()) {
        if (scanner.next().equalsIgnoreCase(fileExt)) {
            return false;
        }
    }
    Scanner scanner1 = new Scanner(type.getAllowedExtensions()).useDelimiter(",");
    while (scanner1.hasNext()) {
        if (scanner1.next().equalsIgnoreCase(fileExt)) {
            return true;
        }
    }
    return false;
}

From source file:com.tesora.dve.tools.CLIBuilder.java

protected static String scanFilePath(Scanner scanner) {
    if (scanner.hasNext()) {
        final String token = scanner.next();
        if (token.startsWith("\"") || token.startsWith("'")) {
            final String quote = String.valueOf(token.charAt(0));
            if (!token.endsWith(quote)) {
                final String remainder = scanner.findInLine(".+?" + quote);
                return token.substring(1) + remainder.substring(0, remainder.length() - 1);
            }/*from w ww .j a v  a  2 s  .  c om*/

            return token.substring(1, token.length() - 1);
        }

        return token;
    }

    return StringUtils.EMPTY;
}

From source file:ar.edu.taco.TacoMain.java

public static String editTestFileToCompile(String junitFile, String sourceClassName, String classPackage,
        String methodName) {//from   w w w  .j  av a2  s.co  m
    String tmpDir = junitFile.substring(0, junitFile.lastIndexOf(FILE_SEP));
    tmpDir = tmpDir.replaceAll("generated", "output");
    File destFile = new File(tmpDir, obtainClassNameFromFileName(junitFile) + /*"_temp" +*/ ".java");
    String packageSentence = "package " + classPackage + ";\n";
    int posLastUnderscore = methodName.lastIndexOf("_");
    methodName = methodName.substring(0, posLastUnderscore);
    try {
        destFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(destFile);
        boolean packageAlreadyWritten = false;
        Scanner scan = new Scanner(new File(junitFile));
        scan.useDelimiter("\n");
        boolean nextToTest = false;
        String str = null;
        while (scan.hasNext()) {
            str = scan.next();
            if (nextToTest) {
                str = str.replace("()",
                        "(String fileClasspath, String className, String methodName) throws IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException, MalformedURLException");
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                nextToTest = false;
                //            } else if (str.contains("public class")){
                //               int posOpeningBrace = str.indexOf("{");
                //               str = str.substring(0, posOpeningBrace-1);
                //               str = str + "_temp {";
                //               fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
            } else if (str.contains("package") && !packageAlreadyWritten) {
                fos.write(packageSentence.getBytes(Charset.forName("UTF-8")));
                str = "           import java.net.URL;";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                str = "           import java.net.URLClassLoader;";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                str = "           import java.net.MalformedURLException;";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                str = "           import java.io.File;";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                str = "           import java.lang.reflect.InvocationTargetException;";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                packageAlreadyWritten = true;
            } else if (str.contains("import") && !packageAlreadyWritten) {
                fos.write(packageSentence.getBytes(Charset.forName("UTF-8")));
                fos.write((scan.next() + "\n").getBytes(Charset.forName("UTF-8")));
                packageAlreadyWritten = true;
            } else if (str.contains("new " + sourceClassName + "(")) {
                //               str = "        try {";
                //               fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                str = "           ClassLoader cl = ClassLoader.getSystemClassLoader();";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                str = "           ClassLoader cl2 = new URLClassLoader(new URL[]{new File(fileClasspath).toURI().toURL()}, cl);";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                //               str = "           ClassLoaderTools.addFile(fileClasspath);";
                //               fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                str = "           Class<?> clazz = cl2.loadClass(className);";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                str = "           Object instance = clazz.newInstance();";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                str = "           cl2 = null;";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));

            } else if (str.contains("Class<?> clazz;")) {
            } else if (str.contains("} catch (ClassNotFoundException e) {")) {
                str = str.replace("ClassNotFoundException", "Exception");
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
            } else if (str.matches(".*(?i)[\\.a-z0-9\\_]*" + sourceClassName + "(?=[^a-z0-9\\_\\.]).*")) {
                str = str.replaceAll("(?i)[\\.a-z0-9\\_]*" + sourceClassName + "(?=[^a-z0-9\\_\\.])",
                        /*classPackage+"."+*/sourceClassName);
                str = str.replace("\"" + methodName + "\"", "methodName");
                str = str.replace("\"" + sourceClassName + "\"", "clazz");
                //               str = str.replace("(", "(fileClasspath, ");
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
            } else if (str.contains("e.printStackTrace();")) {
                //               fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                fos.write(("           throw(new java.lang.RuntimeException(e));" + "\n")
                        .getBytes(Charset.forName("UTF-8")));
                //               fos.write(("throw e;" + "\n").getBytes(Charset.forName("UTF-8")));
            } else if (str.contains("private Method getAccessibleMethod")) {
                str = str.replace("(String className, ", "(Class<?> clazz, ");
                //               str = str.replace(") {", ") throws MalformedURLException {");
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
            } else if (str.contains("method.invoke(instance,")) {
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                str = "           instance = null;";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                str = "           method = null;";
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));

            } else if (str.contains("methodToCheck = clazz.getDeclaredMethod(methodName, parameterTypes);")) {
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
            } else if (str.contains("clazz = Class.forName(className);")) {
                //               str = "           ClassLoader cl = ClassLoader.getSystemClassLoader();";
                //               fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                //               str = "           final ClassLoader cl2 = new URLClassLoader(new URL[]{new File(fileClasspath).toURI().toURL()}, cl);";
                //               fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                //               str = "           clazz = cl2.loadClass(className);";               
                //               fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
                //               str = "           System.out.println(\"actual class inside method: \"+clazz.getName());";
                //               fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
            } else {
                if (str.contains("@Test")) {
                    nextToTest = true;
                }
                //               if (!scan.hasNext()){
                //                  String s = "        } catch (ClassNotFoundException e){";
                //                  fos.write((s + "\n").getBytes(Charset.forName("UTF-8")));
                //                  s = "        } catch (InstantiationException e){}";
                //                  fos.write((s + "\n").getBytes(Charset.forName("UTF-8")));
                //               }
                fos.write((str + "\n").getBytes(Charset.forName("UTF-8")));
            }
        }
        fos.close();
        scan.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return destFile.toString();

}

From source file:org.opencms.workplace.explorer.CmsExplorer.java

/**
 * Creates a link for the OpenCms workplace that will reload the whole workplace, switch to the explorer view, the
 * site of the given explorerRootPath and show the folder given in the explorerRootPath.
 * <p>/*from   w w  w.j  a v  a 2  s  .  c  o  m*/
 * 
 * @param cms
 *            the cms object
 * 
 * @param explorerRootPath
 *            a root relative folder link (has to end with '/').
 * 
 * @return a link for the OpenCms workplace that will reload the whole workplace, switch to the explorer view, the
 *         site of the given explorerRootPath and show the folder given in the explorerRootPath.
 */
public static String getWorkplaceExplorerLink(final CmsObject cms, final String explorerRootPath) {

    // split the root site: 
    StringBuffer siteRoot = new StringBuffer();
    StringBuffer path = new StringBuffer('/');
    Scanner scanner = new Scanner(explorerRootPath);
    scanner.useDelimiter("/");
    int count = 0;
    while (scanner.hasNext()) {
        if (count < 2) {
            siteRoot.append('/').append(scanner.next());
        } else {
            if (count == 2) {
                path.append('/');
            }
            path.append(scanner.next());
            path.append('/');
        }
        count++;
    }
    String targetSiteRoot = siteRoot.toString();
    String targetVfsFolder = path.toString();
    // build the link
    StringBuilder link2Source = new StringBuilder();
    link2Source.append("/system/workplace/views/workplace.jsp?");
    link2Source.append(CmsWorkplace.PARAM_WP_EXPLORER_RESOURCE);
    link2Source.append("=");
    link2Source.append(targetVfsFolder);
    link2Source.append("&");
    link2Source.append(CmsFrameset.PARAM_WP_VIEW);
    link2Source.append("=");
    link2Source.append(OpenCms.getLinkManager().substituteLinkForUnknownTarget(cms,
            "/system/workplace/views/explorer/explorer_fs.jsp"));
    link2Source.append("&");
    link2Source.append(CmsWorkplace.PARAM_WP_SITE);
    link2Source.append("=");
    link2Source.append(targetSiteRoot);

    String result = link2Source.toString();
    result = OpenCms.getLinkManager().substituteLinkForUnknownTarget(cms, result);
    return result;
}

From source file:org.openbaton.vnfm.generic.GenericVNFM.java

private static String convertStreamToString(InputStream is) {
    Scanner s = new Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

From source file:com.wavemaker.spinup.web.VersionProvider.java

public String getVersion(ServletContext servletContext) throws IOException {
    String uploadDirName = servletContext.getRealPath(SpinupConstants.STUDIOD_UPLOAD_DIR);
    if (studioVersion != null) {
        return this.studioVersion;
    }//from   w ww. j  av  a 2s.  c o m
    ZipArchive studioZip = new ZipArchive(new LocalFolder(uploadDirName).getFile(SpinupConstants.STUDIO_FILE));
    InputStream configjs = studioZip.getFile("app/config.js").getContent().asInputStream();
    Scanner s = new Scanner(configjs);
    while (s.hasNext()) {
        String ln = s.nextLine();
        if (ln.contains("studioVersion:")) {
            this.studioVersion = ln.substring(ln.indexOf(":") + 1).replace(",", "").replace("'", "").trim();
            break;
        }
    }
    if (log.isInfoEnabled()) {
        log.info("*** Studio version is: " + this.studioVersion + "***");
    }
    configjs.close();
    return this.studioVersion;

}

From source file:br.com.sicoob.cro.cop.batch.core.FileReadTasklet.java

@Override
public void process() {
    try {/*from   ww w  .  j a v a2  s  .co m*/
        List<Operation> operacoes = new ArrayList();
        InputStream source = this.getClass()
                .getResourceAsStream(this.context.getParameters().get("nomeArquivo").toString());
        Scanner scan = new Scanner(source);
        while (scan.hasNext()) {
            String[] dados = scan.next().split(";");
            Operation operacao = new Operation(dados[0], new BigDecimal(dados[1]), dados[2],
                    new BigDecimal(dados[3]));
            operacoes.add(operacao);
        }

        // calcula o percentual de provisionamento
        for (Operation operacao : operacoes) {
            LOG.info(operacao.toString());
        }
        scan.close();
        source.close();
    } catch (IOException ex) {
        Logger.getLogger(FileReadTasklet.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:core.MusicStreaming.java

public void play() throws JavaLayerException, IOException {

    ftpClient = new FTPClient();
    ftpClient.connect(server, port);//from   w  w  w .j  ava  2s .  c o  m
    ftpClient.login(user, pass);
    ftpClient.enterLocalPassiveMode();
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    System.out.println(ftpClient.getListHiddenFiles());
    engine = ftpClient.initiateListParsing();

    files = engine.getFiles();

    for (int i = 0; i < files.length; i++) {
        System.out.println(i + "  " + files[i].getName());
    }

    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNext()) {

        int i = scanner.nextInt();
        song = "/" + files[i].getName();
        System.out.println("curently playing " + i);
        //        song = "/Bondhu Tomar Chokher Majhe.mp3";

        if (!isFirst) {
            System.out.println("trying to stop");
            mp3player.close();
            mp3player.getPosition();
            in.close();
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    isFirst = false;
                    in = new BufferedInputStream(ftpClient.retrieveFileStream(song));
                    mp3player = new Player(in);
                    mp3player.play();
                } catch (Exception ex) {
                    Logger.getLogger(MusicStreaming.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }).start();

    }

}

From source file:DataSci.main.JsonRequestResponseController.java

/**
 * Read the JSON schema from the file rrsJson.json
 * //from   w w w  .j  a  v a2  s . c  o m
 * @param filename It expects a fully qualified file name that contains input JSON file
 */
public void readJson(String filename) {
    try {
        File apiFile = new File(filename);
        Scanner sc = new Scanner(apiFile);
        jsonBody = "";
        while (sc.hasNext()) {
            jsonBody += sc.nextLine() + "\n";
        }
        sc.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.jdal.text.PeriodFormatter.java

/**
 * {@inheritDoc}/*from  ww w  .  j  a v  a2  s. c om*/
 */
public Number parse(String text, Locale locale) throws ParseException {
    long value = 0;
    Scanner scanner = new Scanner(text);

    while (scanner.hasNext())
        value += parse(scanner.nextLong(), scanner.next("[dhms]"));

    scanner.close();

    return value;

}