List of usage examples for java.util Scanner useDelimiter
public Scanner useDelimiter(String pattern)
From source file:eu.project.ttc.resources.ReferenceTermList.java
@Override public void load(DataResource data) throws ResourceInitializationException { InputStream inputStream = null; try {/* w w w.ja va 2s . c o m*/ this.path = data.getUri().toString(); LOGGER.debug("Loading reference term list at {}", this.path); inputStream = data.getInputStream(); Scanner scanner = null; try { scanner = new Scanner(inputStream); scanner.useDelimiter(TermSuiteConstants.LINE_BREAK); while (scanner.hasNext()) { lineNb++; String line = scanner.next().split(TermSuiteConstants.DIESE)[0].trim(); List<String> els = Splitter.on(TermSuiteConstants.TAB).splitToList(line.trim().toLowerCase()); if (!els.isEmpty()) { if (els.size() != 3) LOGGER.warn("Ignoring line {} : should have exactly 3 elements ({})", lineNb, line); else { int id = Integer.parseInt(els.get(0)); RTLTerm refTerm = new RTLTerm(lineNb, id, els.get(2), els.get(1).toLowerCase().equals("v")); if (refTerm.isVariant()) { if (!bases.containsKey(id)) { LOGGER.warn("No such base term id {} for variant term {}", id, refTerm); continue; } else bases.get(id).addVariant(refTerm); } else { bases.put(id, refTerm); } } } } this.bases = ImmutableMap.copyOf(this.bases); int total = 0; for (RTLTerm ref : bases.values()) total += 1 + ref.getVariants().size(); LOGGER.debug("Reference term list loaded (nb terms: {}, nb terms and variants: {})", this.bases.keySet().size(), total); } catch (Exception e) { e.printStackTrace(); throw new ResourceInitializationException(e); } finally { IOUtils.closeQuietly(scanner); } } catch (IOException e) { LOGGER.error("Could not load file {}", data.getUrl()); throw new ResourceInitializationException(e); } finally { IOUtils.closeQuietly(inputStream); } }
From source file:eu.project.ttc.resources.SimpleWordSet.java
public void load(DataResource data) throws ResourceInitializationException { InputStream inputStream = null; this.elements = Sets.newHashSet(); try {//from w ww .ja v a 2 s.c om inputStream = data.getInputStream(); Scanner scanner = null; try { String word, line; String[] str, wordTranslations; scanner = new Scanner(inputStream, "UTF-8"); scanner.useDelimiter(TermSuiteConstants.LINE_BREAK); while (scanner.hasNext()) { line = scanner.next().split(TermSuiteConstants.DIESE)[0].trim(); str = line.split(TermSuiteConstants.TAB); word = str[0]; if (str.length > 0) this.elements.add(word); if (str.length > 1) { // Set the second line as the translation wordTranslations = str[1].split(TermSuiteConstants.COMMA); for (String translation : wordTranslations) { translations.put(word, translation); } } } this.elements = ImmutableSet.copyOf(this.elements); } catch (Exception e) { e.printStackTrace(); throw new ResourceInitializationException(e); } finally { IOUtils.closeQuietly(scanner); } } catch (IOException e) { LOGGER.error("Could not load file {}", data.getUrl()); throw new ResourceInitializationException(e); } finally { IOUtils.closeQuietly(inputStream); } }
From source file:eu.project.ttc.resources.DictionaryResource.java
private Set<Entry<String, String>> parse(InputStream inputStream) throws IOException { Scanner scanner = null; try {//from w w w . j a va 2 s .c o m Set<Entry<String, String>> entries = new HashSet<Entry<String, String>>(); scanner = new Scanner(inputStream, "UTF-8"); scanner.useDelimiter("\\r?\\n"); while (scanner.hasNext()) { String line = scanner.next(); String[] items = line.split("\t"); String source = items[0]; if (source != null) { String target = items[1]; if (target != null) { Entry<String, String> entry = new SimpleEntry<String, String>(source, target); entries.add(entry); } } } return entries; } finally { IOUtils.closeQuietly(scanner); } }
From source file:com.vmware.identity.ssoconfig.OidcClientCommand.java
private void registerOIDCClient() throws Exception { IdmClient client = SSOConfigurationUtils.getIdmClient(); Scanner scanner = new Scanner(new File(metadataFile)); try {//from w w w . j a va 2 s . c o m OIDCClientDTO clientDTO = client.oidcClient().register(tenant, getOIDCClientMetadataDTO(scanner.useDelimiter("\\Z").next())); logger.info(String.format("Successfully registered OIDC Client [%s] for tenant %s", clientDTO.getClientId(), tenant)); } finally { scanner.close(); } }
From source file:org.trafodion.rest.RESTServlet.java
private synchronized void getZkRunning() throws Exception { if (LOG.isDebugEnabled()) LOG.debug("Reading " + parentZnode + Constants.DEFAULT_ZOOKEEPER_ZNODE_SERVERS_RUNNING); List<String> children = getChildren(parentZnode + Constants.DEFAULT_ZOOKEEPER_ZNODE_SERVERS_RUNNING, new RunningWatcher()); if (!children.isEmpty()) { for (String child : children) { //If stop-dcs.sh is executed and DCS_MANAGES_ZK then zookeeper is stopped abruptly. //Second scenario is when ZooKeeper fails for some reason regardless of whether DCS //manages it. When either happens the DcsServer running znodes still exist in ZooKeeper //and we see them at next startup. When they eventually timeout //we get node deleted events for a server that no longer exists. So, only recognize //DcsServer running znodes that have timestamps after last DcsMaster startup. Scanner scn = new Scanner(child); scn.useDelimiter(":"); String hostName = scn.next(); String instance = scn.next(); int infoPort = Integer.parseInt(scn.next()); long serverStartTimestamp = Long.parseLong(scn.next()); scn.close();/*from w w w . j a v a2 s. com*/ //if(serverStartTimestamp < startupTimestamp) // continue; if (!runningServers.contains(child)) { if (LOG.isDebugEnabled()) LOG.debug("Watching running [" + child + "]"); zkc.exists(parentZnode + Constants.DEFAULT_ZOOKEEPER_ZNODE_SERVERS_RUNNING + "/" + child, new RunningWatcher()); runningServers.add(child); } } //metrics.setTotalRunning(runningServers.size()); } //else { // metrics.setTotalRunning(0); //} }
From source file:org.molasdin.wbase.xml.parser.light.basic.BasicParser.java
private Pair<String, List<Pair<String, String>>> extractTag(String value) { Scanner scanner = new Scanner(value); scanner.useDelimiter("\\s+"); String tag = null;/*from w w w .ja v a 2s .c om*/ if (scanner.hasNext()) { tag = scanner.next(); if (!isValidTag(tag)) { return null; } } else { return null; } if (tag.contains("/")) { return null; } List<Pair<String, String>> attributes = new ArrayList<Pair<String, String>>(); while (scanner.hasNext()) { String part = scanner.next().trim(); Matcher matcher = attributePattern.matcher(part); if (!matcher.matches()) { return null; } Pair<String, String> attribute = Pair.of(matcher.group(1), matcher.group(2)); attributes.add(attribute); } return Pair.of(tag, attributes); }
From source file:de.uzk.hki.da.repository.ElasticsearchMetadataIndex.java
@Override public String getIndexedMetadata(String indexName, String objectId) { try {/* w ww . j a v a2s .c om*/ String requestURL = "http://localhost:9200/" + indexName + "/" + C.ORE_AGGREGATION + "/_search?q=_id:" + objectId + "*"; System.out.println("requestURL:" + requestURL); logger.debug("requestURL:" + requestURL); URL wikiRequest; wikiRequest = new URL(requestURL); URLConnection connection; connection = wikiRequest.openConnection(); connection.setDoOutput(true); Scanner scanner; scanner = new Scanner(wikiRequest.openStream()); String response = scanner.useDelimiter("\\Z").next(); System.out.println("R:" + response); scanner.close(); return response; } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:de.uzk.hki.da.repository.ElasticsearchMetadataIndex.java
@Override public String getAllIndexedMetadataFromIdSubstring(String indexName, String objectId) { try {//from w w w .ja va2s . c om String requestURL = "http://localhost:9200/" + indexName + "/" + C.ORE_AGGREGATION + "/_search?q=_id:" + objectId + "" + "*"; System.out.println("requestURL:" + requestURL); logger.debug("requestURL:" + requestURL); URL wikiRequest; wikiRequest = new URL(requestURL); URLConnection connection; connection = wikiRequest.openConnection(); connection.setDoOutput(true); Scanner scanner; scanner = new Scanner(wikiRequest.openStream()); String response = scanner.useDelimiter("\\Z").next(); System.out.println("R:" + response); scanner.close(); return response; } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:com.photon.phresco.plugin.commons.PluginUtils.java
public void executeSql(SettingsInfo info, File basedir, String filePath, String fileName) throws PhrescoException { initDriverMap();/*from w w w . j a va 2 s . com*/ String host = info.getPropertyInfo(Constants.DB_HOST).getValue(); String port = info.getPropertyInfo(Constants.DB_PORT).getValue(); String userName = info.getPropertyInfo(Constants.DB_USERNAME).getValue(); String password = info.getPropertyInfo(Constants.DB_PASSWORD).getValue(); String databaseName = info.getPropertyInfo(Constants.DB_NAME).getValue(); String databaseType = info.getPropertyInfo(Constants.DB_TYPE).getValue(); String version = info.getPropertyInfo(Constants.DB_VERSION).getValue(); String connectionProtocol = findConnectionProtocol(databaseType, host, port, databaseName); Connection con = null; FileInputStream file = null; Statement st = null; try { Class.forName(getDbDriver(databaseType)).newInstance(); file = new FileInputStream(basedir.getPath() + filePath + databaseType.toLowerCase() + File.separator + version + fileName); Scanner s = new Scanner(file); s.useDelimiter("(;(\r)?\n)|(--\n)"); con = DriverManager.getConnection(connectionProtocol, userName, password); con.setAutoCommit(false); st = con.createStatement(); while (s.hasNext()) { String line = s.next().trim(); if (databaseType.equals("oracle")) { if (line.startsWith("--")) { String comment = line.substring(line.indexOf("--"), line.lastIndexOf("--")); line = line.replace(comment, ""); line = line.replace("--", ""); } if (line.startsWith(Constants.REM_DELIMETER)) { String comment = line.substring(0, line.lastIndexOf("\n")); line = line.replace(comment, ""); } } if (line.startsWith("/*!") && line.endsWith("*/")) { line = line.substring(line.indexOf("/*"), line.indexOf("*/") + 2); } if (line.trim().length() > 0) { st.execute(line); } } } catch (SQLException e) { throw new PhrescoException(e); } catch (FileNotFoundException e) { throw new PhrescoException(e); } catch (Exception e) { throw new PhrescoException(e); } finally { try { if (con != null) { con.commit(); con.close(); } if (file != null) { file.close(); } } catch (Exception e) { throw new PhrescoException(e); } } }
From source file:eremeykin.pete.plotter.CartesianPlotterTopComponent.java
@Override public void update() { // for first rpt file if (model == null) { clear();/*from w w w.j av a 2s. c om*/ return; } File[] rptFiles = home.listFiles(filter()); // catch if there is no such file if (rptFiles.length == 0) { clear(); return; } File firstRPT = rptFiles[0]; Scanner scanner; try { scanner = new Scanner(firstRPT); scanner.useDelimiter("\\s+|\n"); } catch (FileNotFoundException ex) { clear(); return; } List<Map.Entry<Double, Double>> tmpList = new ArrayList<>(); for (int i = 0; scanner.hasNext(); i++) { String line = scanner.next(); try { double x1 = Double.valueOf(line); line = scanner.next(); double x2 = Double.valueOf(line); // System.out.println("x1=" + x1 + "\nx2=" + x2); tmpList.add(new AbstractMap.SimpleEntry<>(x1, x2)); } catch (NumberFormatException ex) { // only if it is the third or following line if (i > 1) { LOGGER.error("Error while parsing double from file: " + firstRPT.getAbsolutePath()); JOptionPane.showMessageDialog(this, "Error while parsing result file.", "Parsing error", JOptionPane.ERROR_MESSAGE); } } } if (tmpList.isEmpty()) { clear(); return; } fillData(tmpList); }