1 package org.opensrf.util;
7 * Collection of general, static utility methods
12 * Returns the string representation of a given file.
13 * @param filename The file to turn into a string
15 public static String fileToString(String filename)
16 throws FileNotFoundException, IOException {
18 StringBuffer sb = new StringBuffer();
19 BufferedReader in = new BufferedReader(new FileReader(filename));
21 while ((str = in.readLine()) != null)
31 public static String escape(String string) {
32 StringBuffer sb = new StringBuffer();
38 * Escapes a string. Turns bare newlines into \n, etc.
39 * Escapes \n, \r, \t, ", \f
40 * Encodes non-ascii characters as UTF-8: \u0000
41 * @param string The string to escape
42 * @param sb The string buffer to write the escaped string into
44 public static void escape(String string, StringBuffer sb) {
45 int len = string.length();
48 for( int i = 0; i < len; i++ ) {
73 if (c < 32 || c > 126 ) {
74 /* escape all non-ascii or control characters as UTF-8 */
75 utf = "000" + Integer.toHexString(c);
76 sb.append("\\u" + utf.substring(utf.length() - 4));
86 * Descends into the map along the given XPATH-style path
87 * and returns the object found there.
88 * @param path The XPATH-style path to search. Path
89 * components are separated by '/' characters.
90 * Example: /opensrf/loglevel
91 * @return The found object.
94 public static Object findPath(Map map, String path) {
95 String keys[] = path.split("/", -1);
97 if(path.charAt(0) == '/') i++;
98 for(; i < keys.length - 1; i++ )
99 map = (Map) map.get(keys[i]);
101 return map.get(keys[i]);