Example usage for java.util Scanner useDelimiter

List of usage examples for java.util Scanner useDelimiter

Introduction

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

Prototype

public Scanner useDelimiter(String pattern) 

Source Link

Document

Sets this scanner's delimiting pattern to a pattern constructed from the specified String .

Usage

From source file:org.apache.sysml.api.DMLScript.java

protected static String readDMLScript(String argname, String script) throws IOException, LanguageException {
    boolean fromFile = argname.equals("-f");
    String dmlScriptStr;//w w w.  j  a  v a 2  s . co m

    if (fromFile) {
        //read DML script from file
        if (script == null)
            throw new LanguageException("DML script path was not specified!");

        StringBuilder sb = new StringBuilder();
        BufferedReader in = null;
        try {
            //read from hdfs or gpfs file system
            if (script.startsWith("hdfs:") || script.startsWith("gpfs:")) {
                if (!LocalFileUtils.validateExternalFilename(script, true))
                    throw new LanguageException("Invalid (non-trustworthy) hdfs filename.");
                FileSystem fs = FileSystem.get(ConfigurationManager.getCachedJobConf());
                Path scriptPath = new Path(script);
                in = new BufferedReader(new InputStreamReader(fs.open(scriptPath)));
            }
            // from local file system
            else {
                if (!LocalFileUtils.validateExternalFilename(script, false))
                    throw new LanguageException("Invalid (non-trustworthy) local filename.");
                in = new BufferedReader(new FileReader(script));
            }

            //core script reading
            String tmp = null;
            while ((tmp = in.readLine()) != null) {
                sb.append(tmp);
                sb.append("\n");
            }
        } catch (IOException ex) {
            LOG.error("Failed to read the script from the file system", ex);
            throw ex;
        } finally {
            if (in != null)
                in.close();
        }

        dmlScriptStr = sb.toString();
    } else {
        //parse given script string 
        if (script == null)
            throw new LanguageException("DML script was not specified!");

        InputStream is = new ByteArrayInputStream(script.getBytes());
        Scanner scan = new Scanner(is);
        dmlScriptStr = scan.useDelimiter("\\A").next();
        scan.close();
    }

    return dmlScriptStr;
}

From source file:org.kalypso.kalypsomodel1d2d.conv.SWANDataConverterHelper.java

public static GM_Position readCoordinateShiftValues(final FileObject pFile) {
    GM_Position lPosRes = null;// w w  w. j av  a  2s.  c  o m

    Scanner scannerFile = null;
    Scanner scannerLine = null;
    try {

        FileObject swanShiftCoordFileObject = pFile.getParent()
                .getChild(ISimulation1D2DConstants.SIM_SWAN_COORD_SHIFT_FILE);
        if (swanShiftCoordFileObject == null) {
            return lPosRes;
        }
        File lFile = new File(swanShiftCoordFileObject.getURL().toURI());

        scannerFile = new Scanner(lFile);
        Double lDoubleShiftY = null;
        Double lDoubleShiftX = null;
        while (scannerFile.hasNextLine()) {
            String lStrNextLine = scannerFile.nextLine();
            if (lStrNextLine.contains("=")) { //$NON-NLS-1$
                scannerLine = new Scanner(lStrNextLine);
                scannerLine.useDelimiter("="); //$NON-NLS-1$
                String lStrValueName = scannerLine.next();
                String lStrValue = scannerLine.next();
                if (ISimulation1D2DConstants.SIM_SWAN_COORD_SHIFT_X.equalsIgnoreCase(lStrValueName)) {
                    lDoubleShiftX = Double.parseDouble(lStrValue);
                } else if (ISimulation1D2DConstants.SIM_SWAN_COORD_SHIFT_Y.equalsIgnoreCase(lStrValueName)) {
                    lDoubleShiftY = Double.parseDouble(lStrValue);
                }
                scannerLine.close();
            } else {
                // System.out.println("Empty or invalid line. Unable to process. Processing the results without shift!");
            }
        }
        if (lDoubleShiftX != null && lDoubleShiftY != null)
            lPosRes = GeometryFactory.createGM_Position(lDoubleShiftX, lDoubleShiftY);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (scannerFile != null)
            scannerFile.close();
        if (scannerLine != null)
            scannerLine.close();
    }

    return lPosRes;
}

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.  ja  v a 2s.c om*/
 * 
 * @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:com.ibm.bi.dml.api.DMLScript.java

/**
 * /*w  w w . j  a  v a  2  s .  c  o  m*/
 * @param argname
 * @param arg
 * @return
 * @throws IOException 
 * @throws LanguageException 
 */
protected static String readDMLScript(String argname, String script) throws IOException, LanguageException {
    boolean fromFile = argname.equals("-f") ? true : false;
    String dmlScriptStr = null;

    if (fromFile) {
        //read DML script from file
        if (script == null)
            throw new LanguageException("DML script path was not specified!");

        StringBuilder sb = new StringBuilder();
        BufferedReader in = null;
        try {
            //read from hdfs or gpfs file system
            if (script.startsWith("hdfs:") || script.startsWith("gpfs:")) {
                if (!LocalFileUtils.validateExternalFilename(script, true))
                    throw new LanguageException("Invalid (non-trustworthy) hdfs filename.");
                FileSystem fs = FileSystem.get(ConfigurationManager.getCachedJobConf());
                Path scriptPath = new Path(script);
                in = new BufferedReader(new InputStreamReader(fs.open(scriptPath)));
            }
            // from local file system
            else {
                if (!LocalFileUtils.validateExternalFilename(script, false))
                    throw new LanguageException("Invalid (non-trustworthy) local filename.");
                in = new BufferedReader(new FileReader(script));
            }

            //core script reading
            String tmp = null;
            while ((tmp = in.readLine()) != null) {
                sb.append(tmp);
                sb.append("\n");
            }
        } catch (IOException ex) {
            LOG.error("Failed to read the script from the file system", ex);
            throw ex;
        } finally {
            if (in != null)
                in.close();
        }

        dmlScriptStr = sb.toString();
    } else {
        //parse given script string 
        if (script == null)
            throw new LanguageException("DML script was not specified!");

        InputStream is = new ByteArrayInputStream(script.getBytes());
        Scanner scan = new Scanner(is);
        dmlScriptStr = scan.useDelimiter("\\A").next();
        scan.close();
    }

    return dmlScriptStr;
}

From source file:KnowledgeBooksNlpGenerateRdfPropertiesFromWebPages.java

public KnowledgeBooksNlpGenerateRdfPropertiesFromWebPages(String config_file_path, PrintWriter out)
        throws IOException {
    this.out = out;
    extractNames = new ExtractNames();
    autoTagger = new AutoTagger();
    List<String> lines = (List<String>) FileUtils.readLines(new File(config_file_path));
    for (String line : lines) {
        Scanner scanner = new Scanner(line);
        scanner.useDelimiter(" ");
        try {//from   w  ww. j  a  v a  2 s .co m
            String starting_url = scanner.next();
            int spider_depth = Integer.parseInt(scanner.next());
            spider(starting_url, spider_depth);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    this.out.close();
}

From source file:opennlp.tools.parse_thicket.apps.MostFrequentWordsFromPageGetter.java

public List<String> getMostFrequentWordsInText(String input) {
    int maxRes = 4;
    Scanner in = new Scanner(input);
    in.useDelimiter("\\s+");
    Map<String, Integer> words = new HashMap<String, Integer>();

    while (in.hasNext()) {
        String word = in.next();//  w w  w .java 2s . c o m
        if (!StringUtils.isAlpha(word) || word.length() < 4)
            continue;

        if (!words.containsKey(word)) {
            words.put(word, 1);
        } else {
            words.put(word, words.get(word) + 1);
        }
    }

    words = ValueSortMap.sortMapByValue(words, false);
    List<String> results = new ArrayList<String>(words.keySet());

    if (results.size() > maxRes)
        results = results.subList(0, maxRes); // get maxRes elements

    return results;
}

From source file:io.anserini.doc.GenerateRegressionDocsTest.java

@Test
public void main() throws Exception {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    URL resource = GenerateRegressionDocsTest.class.getResource("/regression/all.yaml");
    DataModel data = mapper.readValue(Paths.get(resource.toURI()).toFile(), DataModel.class);
    //System.out.println(ReflectionToStringBuilder.toString(data, ToStringStyle.MULTI_LINE_STYLE));

    for (String collection : data.getCollections().keySet()) {
        Map<String, String> valuesMap = new HashMap<>();
        valuesMap.put("index_cmds", data.generateIndexingCommand(collection));
        valuesMap.put("ranking_cmds", data.generateRankingCommand(collection));
        valuesMap.put("eval_cmds", data.generateEvalCommand(collection));
        valuesMap.put("effectiveness", data.generateEffectiveness(collection));
        StrSubstitutor sub = new StrSubstitutor(valuesMap);
        URL template = GenerateRegressionDocsTest.class
                .getResource(String.format("/docgen/templates/%s.template", collection));
        Scanner scanner = new Scanner(Paths.get(template.toURI()).toFile(), "UTF-8");
        String text = scanner.useDelimiter("\\A").next();
        scanner.close();/*from   ww w  .  jav  a  2  s .c  o  m*/
        String resolvedString = sub.replace(text);

        FileUtils.writeStringToFile(new File(String.format("docs/experiments-%s.md", collection)),
                resolvedString, "UTF-8");
    }
}

From source file:OpenCalaisGenerateRdfPropertiesFromWebPages.java

public OpenCalaisGenerateRdfPropertiesFromWebPages(String config_file_path, PrintWriter out)
        throws IOException {
    this.out = out;
    List<String> lines = (List<String>) FileUtils.readLines(new File(config_file_path));
    for (String line : lines) {
        Scanner scanner = new Scanner(line);
        scanner.useDelimiter(" ");
        try {//from  w  w w .j  av a 2 s .  c  o  m
            String starting_url = scanner.next();
            int spider_depth = Integer.parseInt(scanner.next());
            spider(starting_url, spider_depth);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    this.out.close();
}

From source file:org.zrgs.spring.statemachine.CommonConfiguration.java

@Bean
public String stateChartModel() throws IOException {
    ClassPathResource model = new ClassPathResource("statechartmodel.txt");
    InputStream inputStream = model.getInputStream();
    Scanner scanner = new Scanner(inputStream);
    String content = scanner.useDelimiter("\\Z").next();
    scanner.close();//from  w  w w . j a va2 s.  c om
    return content;
}

From source file:org.mcxiaoke.commons.http.util.URIUtilsEx.java

/**
 * Adds all parameters within the Scanner to the list of
 * <code>parameters</code>, as encoded by <code>encoding</code>. For
 * example, a scanner containing the string <code>a=1&b=2&c=3</code> would
 * add the {@link NameValuePair NameValuePairs} a=1, b=2, and c=3 to the
 * list of parameters.//from   w w  w . j  ava2s  . c  o m
 * 
 * @param parameters
 *            List to add parameters to.
 * @param scanner
 *            Input that contains the parameters to parse.
 * @param charset
 *            Encoding to use when decoding the parameters.
 */
public static void parse2(final Map<String, String> parameters, final Scanner scanner, final String charset) {
    scanner.useDelimiter(PARAMETER_SEPARATOR);
    while (scanner.hasNext()) {
        String key = null;
        String value = null;
        String token = scanner.next();
        int i = token.indexOf(NAME_VALUE_SEPARATOR);
        if (i != -1) {
            key = decodeFormFields(token.substring(0, i).trim(), charset);
            value = decodeFormFields(token.substring(i + 1).trim(), charset);
        } else {
            key = decodeFormFields(token.trim(), charset);
        }
        parameters.put(key, value);
    }
}