1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 package vogar.expect.util;
19 
20 //import com.google.common.collect.Lists;
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.Reader;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
34 
35 /**
36  * Utility methods for strings.
37  */
38 public class Strings {
39 
40     private static final Pattern XML_INVALID_CHARS
41             = Pattern.compile("[^\\u0009\\u000A\\u000D\\u0020-\\uD7FF\\uE000-\\uFFFD]+");
42 
readStream(Reader reader)43     public static String readStream(Reader reader) throws IOException {
44         StringBuilder result = new StringBuilder();
45         BufferedReader in = new BufferedReader(reader);
46         String line;
47         while ((line = in.readLine()) != null) {
48             result.append(line);
49             result.append('\n');
50         }
51         in.close();
52         return result.toString();
53     }
54 
readFile(File f)55     public static String readFile(File f) throws IOException {
56         return readStream(new InputStreamReader(new FileInputStream(f), "UTF-8"));
57     }
58 
readFileLines(File f)59     public static List<String> readFileLines(File f) throws IOException {
60         BufferedReader in =
61                 new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
62         List<String> list = new ArrayList<String>();
63         String line;
64         while ((line = in.readLine()) != null) {
65             list.add(line);
66         }
67         in.close();
68         return list;
69     }
70 
join(String delimiter, Object... objects)71     public static String join(String delimiter, Object... objects) {
72         return join(Arrays.asList(objects), delimiter);
73     }
74 
join(Iterable<?> objects, String delimiter)75     public static String join(Iterable<?> objects, String delimiter) {
76         Iterator<?> i = objects.iterator();
77         if (!i.hasNext()) {
78             return "";
79         }
80 
81         StringBuilder result = new StringBuilder();
82         result.append(i.next());
83         while(i.hasNext()) {
84             result.append(delimiter).append(i.next());
85         }
86         return result.toString();
87     }
88 
objectsToStrings(Object[] objects)89     public static String[] objectsToStrings(Object[] objects) {
90         String[] result = new String[objects.length];
91         int i = 0;
92         for (Object o : objects) {
93             result[i++] = o.toString();
94         }
95         return result;
96     }
97 
objectsToStrings(Collection<?> objects)98     public static String[] objectsToStrings(Collection<?> objects) {
99         return objectsToStrings(objects.toArray());
100     }
101 
102     /**
103      * Replaces XML-invalid characters with the corresponding U+XXXX code point escapes.
104      */
xmlSanitize(String text)105     public static String xmlSanitize(String text) {
106         StringBuffer result = new StringBuffer();
107         Matcher matcher = XML_INVALID_CHARS.matcher(text);
108         while (matcher.find()) {
109             matcher.appendReplacement(result, "");
110             result.append(escapeCodePoint(matcher.group()));
111         }
112         matcher.appendTail(result);
113         return result.toString();
114     }
115 
escapeCodePoint(CharSequence cs)116     private static String escapeCodePoint(CharSequence cs) {
117         StringBuilder result = new StringBuilder();
118         for (int i = 0; i < cs.length(); ++i) {
119             result.append(String.format("U+%04X", (int) cs.charAt(i)));
120         }
121         return result.toString();
122     }
123 }
124