List of usage examples for java.util Scanner useDelimiter
public Scanner useDelimiter(String pattern)
From source file:com.rockhoppertech.music.DurationParser.java
public static List<Double> getDurationsAsList(String durations) { String token = null;/*w ww . ja v a 2s .com*/ List<Double> list = new ArrayList<Double>(); Scanner scanner = new Scanner(durations); if (durations.indexOf(',') != -1) { scanner.useDelimiter(","); } while (scanner.hasNext()) { if (scanner.hasNext(dpattern)) { token = scanner.next(dpattern); double d = getDottedValue(token); list.add(d); logger.debug("'{}' is dotted value is {}", token, d); } else if (scanner.hasNext(pattern)) { token = scanner.next(pattern); double d = durKeyMap.get(token); list.add(d); logger.debug("'{}' is not dotted value is {}", token, d); // } else if (scanner.hasNext(tripletPattern)) { // token = scanner.next(tripletPattern); // double d = durKeyMap.get(token); // ts.add(d); // System.out.println(String // .format("'%s' is not dotted value is %f", // token, // d)); } else if (scanner.hasNextDouble()) { double d = scanner.nextDouble(); list.add(d); logger.debug("{} is a double", d); } else { // just ignore it. or throw exception? String skipped = scanner.next(); logger.debug("skipped '{}'", skipped); } } scanner.close(); return list; }
From source file:de.ingrid.interfaces.csw.tools.FileUtils.java
/** * Convert stream to string./* ww w . j a v a 2s. co m*/ * * @param is * @return */ public static String convertStreamToString(java.io.InputStream is) { Scanner scanner = null; try { scanner = new Scanner(is); scanner.useDelimiter("\\A"); String content = scanner.next(); scanner.close(); return content; } catch (java.util.NoSuchElementException e) { return ""; } finally { if (scanner != null) { scanner.close(); } } }
From source file:org.apache.felix.http.itest.BaseIntegrationTest.java
protected static String slurpAsString(InputStream is) throws IOException { // See <weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html> Scanner scanner = new Scanner(is, "UTF-8"); try {/*from ww w.j a v a 2 s . co m*/ scanner.useDelimiter("\\A"); return scanner.hasNext() ? scanner.next() : null; } finally { try { scanner.close(); } catch (Exception e) { // Ignore... } } }
From source file:com.android.idtt.http.client.util.URLEncodedUtils.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 org.apache.http.NameValuePair NameValuePairs} a=1, b=2, and c=3 to the * list of parameters./*from w w w . j ava 2s .c om*/ * * @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 parse(final List<NameValuePair> parameters, final Scanner scanner, final String charset) { scanner.useDelimiter(PARAMETER_SEPARATOR); while (scanner.hasNext()) { String name = null; String value = null; String token = scanner.next(); int i = token.indexOf(NAME_VALUE_SEPARATOR); if (i != -1) { name = decodeFormFields(token.substring(0, i).trim(), charset); value = decodeFormFields(token.substring(i + 1).trim(), charset); } else { name = decodeFormFields(token.trim(), charset); } parameters.add(new BasicNameValuePair(name, value)); } }
From source file:com.hybris.mobile.model.product.Product.java
/** * This method parses the messages sent from OCC to create HTML with links recognised by the app. Note this code is * brittle and relies on very specific formatting in OCC. This method is specifoc to product promotions. * /*from w ww . ja v a 2s . c o m*/ * @param p * The product * @return the HTML string to place as the promotions text */ public static String generatePromotionString(ProductPromotion productPromotion) { String promotionsString = productPromotion.getDescription(); /* * firedMessages trumps description */ if (productPromotion.getFiredMessages() != null && !productPromotion.getFiredMessages().isEmpty()) { promotionsString = productPromotion.getFiredMessages().get(0); promotionsString.replaceAll("<br>", "\n"); } /* * couldFireMessage trumps firedMessage */ if (productPromotion.getCouldFireMessages() != null && !productPromotion.getCouldFireMessages().isEmpty()) { promotionsString = productPromotion.getCouldFireMessages().get(0); promotionsString.replaceAll("<br>", "\n"); } /* * Builds links in the form http://m.hybris.com/123456 N.B. iOS builds links in the form appName://product/123456 * with the title as the product name */ ArrayList<String> links = new ArrayList<String>(); ArrayList<String> codes = new ArrayList<String>(); ArrayList<String> productNames = new ArrayList<String>(); Pattern codePattern = Pattern.compile("[0-9]{6,7}"); Scanner s1 = new Scanner(promotionsString); s1.useDelimiter("<a href="); while (s1.hasNext()) { String str = s1.next(); if (str.startsWith("$config")) { Scanner s2 = new Scanner(str); s2.useDelimiter("</a>"); String link = s2.next(); links.add(link); } } for (String link : links) { Scanner s3 = new Scanner(link); s3.useDelimiter("<b>"); s3.next(); while (s3.hasNext()) { String str = s3.next(); if (!str.startsWith("$")) { Scanner s4 = new Scanner(str); s4.useDelimiter("</b>"); String name = s4.next(); productNames.add(name); } } // Find the codes Matcher m = codePattern.matcher(link); while (m.find()) { String s = m.group(); codes.add(s); } } // Build the new links int position = 0; for (String link : links) { promotionsString = promotionsString.replace(link, String.format("\"http://m.hybris.com/%s\">%s", codes.get(position), productNames.get(position))); position++; } return promotionsString; }
From source file:com.gistlabs.mechanize.util.apache.URLEncodedUtils.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. ja va 2 s . 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 parse(final List<NameValuePair> parameters, final Scanner scanner, final String charset) { scanner.useDelimiter(PARAMETER_SEPARATOR); while (scanner.hasNext()) { String name = null; String value = null; String token = scanner.next(); int i = token.indexOf(NAME_VALUE_SEPARATOR); if (i != -1) { name = decodeFormFields(token.substring(0, i).trim(), charset); value = decodeFormFields(token.substring(i + 1).trim(), charset); } else name = decodeFormFields(token.trim(), charset); parameters.add(new BasicNameValuePair(name, value)); } }
From source file:org.apache.james.sieverepository.file.SieveFileRepository.java
/** * Read a file with the specified encoding into a String * * @param file// www . j av a 2s . c o m * @param encoding * @return * @throws FileNotFoundException */ static protected String toString(File file, String encoding) throws FileNotFoundException { String script = null; Scanner scanner = null; try { scanner = new Scanner(file, encoding); scanner.useDelimiter("\\A"); script = scanner.next(); } finally { if (scanner != null) { scanner.close(); } } return script; }
From source file:com.mcxiaoke.next.http.util.URLUtils.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 org.apache.http.NameValuePair NameValuePairs} a=1, b=2, and c=3 to the * list of parameters.// w w w.jav a2 s . co m * * @param parameters List to add parameters to. * @param scanner Input that contains the parameters to parse. * @param parameterSepartorPattern The Pattern string for parameter separators, by convention {@code "[&;]"} * @param charset Encoding to use when decoding the parameters. */ public static void parse(final List<NameValuePair> parameters, final Scanner scanner, final String parameterSepartorPattern, final String charset) { scanner.useDelimiter(parameterSepartorPattern); while (scanner.hasNext()) { String name = null; String value = null; final String token = scanner.next(); final int i = token.indexOf(NAME_VALUE_SEPARATOR); if (i != -1) { name = decodeFormFields(token.substring(0, i).trim(), charset); value = decodeFormFields(token.substring(i + 1).trim(), charset); } else { name = decodeFormFields(token.trim(), charset); } parameters.add(new BasicNameValuePair(name, value)); } }
From source file:com.qmetry.qaf.automation.testng.report.ReporterUtil.java
public static String execHostName(String execCommand) { InputStream stream;/*from www . ja va 2s . co m*/ Scanner s; try { Process proc = Runtime.getRuntime().exec(execCommand); stream = proc.getInputStream(); if (stream != null) { s = new Scanner(stream); s.useDelimiter("\\A"); String val = s.hasNext() ? s.next() : ""; stream.close(); s.close(); return val; } } catch (IOException ioException) { ioException.printStackTrace(); } return ""; }
From source file:ar.edu.taco.TacoMain.java
public static String editTestFileToCompile(String junitFile, String sourceClassName, String classPackage, String methodName) {//from w w w .jav a 2s . c om 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(); }