List of usage examples for java.util Scanner useDelimiter
public Scanner useDelimiter(String pattern)
From source file:demo.vmware.commands.CommandProcessor.java
/** * protected so we can unit test/* w w w. ja va 2s.co m*/ * */ Scanner createScanner(InputStream source) { // add support for quoted strings Scanner s = new Scanner(source); s.useDelimiter(UGLY_PARSING_PATTERN); return s; }
From source file:ch.cyberduck.core.importer.S3BrowserBookmarkCollection.java
@Override protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException { try {//from w w w .j av a 2 s . com final BufferedReader in = new BufferedReader( new InputStreamReader(file.getInputStream(), Charset.forName("UTF-8"))); try { Host current = null; String line; while ((line = in.readLine()) != null) { if (line.startsWith("[account_")) { current = new Host(protocols.forType(Protocol.Type.s3)); } else if (StringUtils.isBlank(line)) { this.add(current); current = null; } else { if (null == current) { log.warn("Failed to detect start of bookmark"); continue; } Scanner scanner = new Scanner(line); scanner.useDelimiter(" = "); if (!scanner.hasNext()) { log.warn("Missing key in line:" + line); continue; } String name = scanner.next().toLowerCase(Locale.ROOT); if (!scanner.hasNext()) { log.warn("Missing value in line:" + line); continue; } String value = scanner.next(); if ("name".equals(name)) { current.setNickname(value); } else if ("comment".equals(name)) { current.setComment(value); } else if ("access_key".equals(name)) { current.getCredentials().setUsername(value); } else if ("secret_key".equals(name)) { current.getCredentials().setPassword(value); } } } } finally { IOUtils.closeQuietly(in); } } catch (IOException e) { throw new AccessDeniedException(e.getMessage(), e); } }
From source file:org.apache.uima.ruta.resource.CSVTable.java
private void buildTable(InputStream stream) { Scanner sc = new Scanner(stream, Charset.forName("UTF-8").name()); sc.useDelimiter("\\n"); tableData = new ArrayList<List<String>>(); while (sc.hasNext()) { String line = sc.next().trim(); line = line.replaceAll(";;", "; ;"); String[] lineElements = line.split(";"); List<String> row = Arrays.asList(lineElements); tableData.add(row);//w w w .j a v a2s. c om } sc.close(); }
From source file:ch.cyberduck.core.importer.FlashFxpBookmarkCollection.java
@Override protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException { try {// w w w .j a v a 2 s . c om final BufferedReader in = new BufferedReader( new InputStreamReader(file.getInputStream(), Charset.forName("UTF-8"))); try { Host current = null; String line; while ((line = in.readLine()) != null) { if (line.startsWith("[")) { current = new Host(protocols.forScheme(Scheme.ftp)); current.getCredentials() .setUsername(PreferencesFactory.get().getProperty("connection.login.anon.name")); Pattern pattern = Pattern.compile("\\[(.*)\\]"); Matcher matcher = pattern.matcher(line); if (matcher.matches()) { current.setNickname(matcher.group(1)); } } else if (StringUtils.isBlank(line)) { this.add(current); current = null; } else { if (null == current) { log.warn("Failed to detect start of bookmark"); continue; } Scanner scanner = new Scanner(line); scanner.useDelimiter("="); if (!scanner.hasNext()) { log.warn("Missing key in line:" + line); continue; } String name = scanner.next().toLowerCase(Locale.ROOT); if (!scanner.hasNext()) { log.warn("Missing value in line:" + line); continue; } String value = scanner.next(); if ("ip".equals(name)) { current.setHostname(StringUtils.substringBefore(value, "\u0001")); } else if ("port".equals(name)) { try { current.setPort(Integer.parseInt(value)); } catch (NumberFormatException e) { log.warn("Invalid Port:" + e.getMessage()); } } else if ("path".equals(name)) { current.setDefaultPath(value); } else if ("notes".equals(name)) { current.setComment(value); } else if ("user".equals(name)) { current.getCredentials().setUsername(value); } } } } finally { IOUtils.closeQuietly(in); } } catch (IOException e) { throw new AccessDeniedException(e.getMessage(), e); } }
From source file:org.fhaes.fhfilereader.FHCategoryReader.java
/** * Parses the input category file and stores its contents in the categoryEntries arrayList. * /*from w w w.ja v a 2 s . c o m*/ * @param categoryFile */ public FHCategoryReader(File categoryFile) { try { // Setup the scanner for reading and storing the category entries from the CSV file Scanner sc = new Scanner(categoryFile); sc.useDelimiter(",|\r\n"); // Verify that the category file has the necessary header and version number for (int numValuesRead = 0; numValuesRead <= NUM_INITIAL_VALUES_TO_READ; numValuesRead++) { if (numValuesRead == INDEX_OF_HEADER && sc.hasNext()) { if (!sc.next().equals(FHAES_CATEGORY_FILE_HEADER)) { sc.close(); throw new InvalidCategoryFileException(); } } else if (numValuesRead == INDEX_OF_VERSION && sc.hasNext()) { if (!sc.next().equals(FHAES_CATEGORY_FILE_VERSION)) { sc.close(); throw new InvalidCategoryFileException(); } } else if (numValuesRead == INDEX_OF_FILENAME && sc.hasNext()) { nameOfCorrespondingFHXFile = sc.next(); } else { sc.close(); throw new InvalidCategoryFileException(); } } // Read the contents of the category file into the categoryEntries array while (sc.hasNext()) { String seriesTitle = sc.next(); String category = sc.next(); String content = sc.next(); if (!seriesTitle.equals("") && !category.equals("") && !content.equals("")) { categoryEntries.add(new FHCategoryEntry(seriesTitle, category, content)); } else { sc.close(); throw new InvalidCategoryFileException(); } } sc.close(); } catch (FileNotFoundException ex) { log.info("The category file " + FilenameUtils.getBaseName(categoryFile.getAbsolutePath()) + " does not exist."); } catch (InvalidCategoryFileException ex) { log.error("Could not parse category file. File is in an invalid format or has missing entries."); } }
From source file:eu.scape_project.archiventory.identifiers.UnixFileIdentification.java
@Override public HashMap<String, List<String>> identifyFileList(DualHashBidiMap fileRecidBidiMap) throws IOException { HashMap<String, List<String>> resultMap = new HashMap<String, List<String>>(); String ufidRes = this.identify(fileRecidBidiMap.values()); Scanner s = new Scanner(ufidRes); // one file identification result per line s.useDelimiter("\n"); while (s.hasNext()) { // output syntax of the unix-tool 'file' is ${fileName} : ${mimeType} StringTokenizer st = new StringTokenizer(s.next(), ":"); String fileName = st.nextToken().trim(); // output key String key = (String) fileRecidBidiMap.getKey(fileName); if (key != null) { String containerFileName = key.substring(0, key.indexOf("/")); String containerIdentifier = key.substring(key.indexOf("/") + 1); String outputKey = String.format(outputKeyFormat, containerFileName, containerIdentifier); // output value String property = "mime"; String value = st.nextToken().trim(); String outputValue = String.format(outputValueFormat, tool, property, value); List<String> valueLineList = new ArrayList<String>(); valueLineList.add(outputValue); resultMap.put(outputKey, valueLineList); } else {/*from w w w . j a v a 2 s. co m*/ } } return resultMap; }
From source file:csns.importer.parser.MFTScoreParser.java
public void parse(MFTScoreImporter importer) { Department department = importer.getDepartment(); Date date = importer.getDate(); Scanner scanner = new Scanner(importer.getText()); scanner.useDelimiter("\\s+|\\r\\n|\\r|\\n"); while (scanner.hasNext()) { // last name String lastName = scanner.next(); while (!lastName.endsWith(",")) lastName += " " + scanner.next(); lastName = lastName.substring(0, lastName.length() - 1); // first name String firstName = scanner.next(); // score// w ww . j a va 2s . c om Stack<String> stack = new Stack<String>(); String s = scanner.next(); while (!isScore(s)) { stack.push(s); s = scanner.next(); } int value = Integer.parseInt(s); // authorization code stack.pop(); // cin String cin = null; if (!stack.empty() && isCin(stack.peek())) cin = stack.pop(); // user User user = null; if (cin != null) user = userDao.getUserByCin(cin); else { List<User> users = userDao.getUsers(lastName, firstName); if (users.size() == 1) user = users.get(0); } if (user != null) { MFTScore score = mftScoreDao.getScore(department, date, user); if (score == null) { score = new MFTScore(); score.setDepartment(department); score.setDate(date); score.setUser(user); } else { logger.info(user.getId() + ": " + score.getValue() + " => " + value); } score.setValue(value); importer.getScores().add(score); } else { User failedUser = new User(); failedUser.setLastName(lastName); failedUser.setFirstName(firstName); failedUser.setCin(cin); importer.getFailedUsers().add(failedUser); } logger.debug(lastName + "," + firstName + "," + cin + "," + value); } scanner.close(); }
From source file:ch.cyberduck.core.importer.TotalCommanderBookmarkCollection.java
@Override protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException { try {//from w ww .j a v a 2 s.c om final BufferedReader in = new BufferedReader( new InputStreamReader(file.getInputStream(), Charset.forName("UTF-8"))); try { Host current = null; String line; while ((line = in.readLine()) != null) { if (line.startsWith("[")) { if (current != null) { this.add(current); } current = new Host(protocols.forScheme(Scheme.ftp)); current.getCredentials() .setUsername(PreferencesFactory.get().getProperty("connection.login.anon.name")); Pattern pattern = Pattern.compile("\\[(.*)\\]"); Matcher matcher = pattern.matcher(line); if (matcher.matches()) { current.setNickname(matcher.group(1)); } } else { if (null == current) { log.warn("Failed to detect start of bookmark"); continue; } final Scanner scanner = new Scanner(line); scanner.useDelimiter("="); if (!scanner.hasNext()) { log.warn("Missing key in line:" + line); continue; } final String name = scanner.next().toLowerCase(Locale.ROOT); if (!scanner.hasNext()) { log.warn("Missing value in line:" + line); continue; } final String value = scanner.next(); if ("host".equals(name)) { current.setHostname(value); } else if ("directory".equals(name)) { current.setDefaultPath(value); } else if ("username".equals(name)) { current.getCredentials().setUsername(value); } else { log.warn(String.format("Ignore property %s", name)); } } } if (current != null) { this.add(current); } } finally { IOUtils.closeQuietly(in); } } catch (IOException e) { throw new AccessDeniedException(e.getMessage(), e); } }
From source file:com.farmerbb.secondscreen.util.U.java
public static String generateBlurb(Activity a, String key, String value, boolean isNotification) { String blurb = " "; if (a instanceof TaskerQuickActionsActivity) { blurb = a.getResources().getStringArray(R.array.pref_notification_action_list)[1]; // If this blurb is being generated for the notification, and the value is "Toggle", // set value to the actual "On" or "Off" state if (isNotification && value.equals("Toggle")) { SharedPreferences prefCurrent = getPrefCurrent(a); switch (key) { case "temp_backlight_off": if (prefCurrent.getBoolean("backlight_off", false)) value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; break; case "temp_chrome": if (prefCurrent.getBoolean("chrome", false)) value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; break; case "temp_immersive": case "temp_immersive_new": if (key.equals("temp_immersive_new")) key = "temp_immersive"; switch (prefCurrent.getString("immersive_new", "fallback")) { case "immersive-mode": value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; break; default: value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; break; }//ww w .j av a 2 s .c o m break; case "temp_overscan": if (prefCurrent.getBoolean("overscan", false)) value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; break; case "temp_vibration_off": if (prefCurrent.getBoolean("vibration_off", false)) value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; break; case "temp_freeform": if (prefCurrent.getBoolean("freeform", false)) value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; break; case "temp_hdmi_rotation": switch (prefCurrent.getString("hdmi_rotation", "landscape")) { case "portrait": value = a.getResources().getStringArray(R.array.pref_hdmi_rotation_list)[1]; break; case "landscape": value = a.getResources().getStringArray(R.array.pref_hdmi_rotation_list)[0]; break; } break; } } // Modifications for non-English locales if (value.equals(a.getResources().getStringArray(R.array.pref_quick_actions_values)[0])) value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; else if (value.equals(a.getResources().getStringArray(R.array.pref_quick_actions_values)[1])) { if (key.equals("temp_overscan")) value = a.getResources().getStringArray(R.array.pref_quick_actions_overscan)[0]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; } } switch (key) { case "turn_off": blurb = a.getResources().getString(R.string.quick_turn_off); break; case "lock_device": blurb = a.getResources().getStringArray(R.array.pref_notification_action_list)[2]; break; case "temp_backlight_off": blurb = a.getResources().getString(R.string.quick_backlight) + " " + value; break; case "temp_chrome": blurb = a.getResources().getString(R.string.quick_chrome) + " " + value; break; case "temp_immersive": blurb = a.getResources().getString(R.string.quick_immersive) + " " + value; break; case "temp_immersive_new": switch (value) { case "do-nothing": blurb = a.getResources().getStringArray(R.array.pref_immersive_list_alt)[0]; break; case "status-only": blurb = a.getResources().getStringArray(R.array.pref_immersive_list_alt)[1]; break; case "immersive-mode": blurb = a.getResources().getStringArray(R.array.pref_immersive_list_alt)[2]; break; case "Toggle": blurb = a.getResources().getStringArray(R.array.pref_immersive_list_alt)[3]; break; } break; case "density": case "temp_density": switch (value) { case "reset": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[0]; break; case "120": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[1]; break; case "160": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[2]; break; case "213": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[3]; break; case "240": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[4]; break; case "320": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[6]; break; case "480": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[8]; break; case "640": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[10]; break; default: blurb = value + a.getResources().getString(R.string.dpi); break; } break; case "temp_overscan": blurb = a.getResources().getString(R.string.quick_overscan) + " " + value; break; case "size": case "temp_size": SharedPreferences prefMain = getPrefMain(a); if (value.equals("reset")) blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[0]; else if (prefMain.getBoolean("landscape", false)) { switch (value) { case "1920x1080": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[1]; break; case "1280x720": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[2]; break; case "854x480": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[3]; break; default: blurb = value; break; } } else { switch (value) { case "1080x1920": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[1]; break; case "720x1280": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[2]; break; case "480x854": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[3]; break; default: Scanner scanner = new Scanner(value); scanner.useDelimiter("x"); int height = scanner.nextInt(); int width = scanner.nextInt(); scanner.close(); blurb = Integer.toString(width) + "x" + Integer.toString(height); break; } } break; case "temp_rotation_lock_new": switch (value) { case "do-nothing": blurb = a.getResources().getStringArray(R.array.pref_rotation_list)[0]; break; case "auto-rotate": blurb = a.getResources().getStringArray(R.array.pref_rotation_list)[1]; break; case "landscape": blurb = a.getResources().getStringArray(R.array.pref_rotation_list)[2]; break; } break; case "temp_vibration_off": blurb = a.getResources().getString(R.string.quick_vibration) + " " + value; break; case "temp_freeform": blurb = a.getResources().getString(R.string.quick_freeform) + " " + value; break; case "temp_hdmi_rotation": blurb = a.getResources().getString(R.string.quick_hdmi_rotation) + " " + value; break; } return blurb; }
From source file:com.frankdye.marvelgraphws2.MarvelGraphView.java
private void processLine(String nextLine, Map<String, List<String>> map1, Set<String> set1) { // TODO Auto-generated method stub // use a second Scanner to parse the content of each line Scanner scanner = new Scanner(nextLine); scanner.useDelimiter("\t"); if (scanner.hasNext()) { // assumes the line has a certain structure String first = scanner.next(); first = first.replaceAll("^\"|\"$", ""); String last = scanner.next(); last = last.replaceAll("^\"|\"$", ""); // Query map to see if we have an entry for this comic issue List<String> l = map1.get(last); if (l == null) // No entry for this issue so create it. {/*from ww w.jav a2 s.co m*/ map1.put(last, l = new ArrayList<String>()); } // Issue exists or is created so append the characters name to the // map entry for that issue l.add(first); // Add the characters name to our set to maintain a list of each // unique character set1.add(first); } else { log("Empty or invalid line. Unable to process."); } scanner.close(); }