• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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.Arrays;
28 import java.util.Collection;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33 
34 /**
35  * Utility methods for strings.
36  */
37 public class Strings {
38 
39     private static final Pattern XML_INVALID_CHARS
40             = Pattern.compile("[^\\u0009\\u000A\\u000D\\u0020-\\uD7FF\\uE000-\\uFFFD]+");
41 
isNullOrEmpty(String in)42     public static boolean isNullOrEmpty(String in) {
43         return com.google.common.base.Strings.isNullOrEmpty(in);
44     }
45 
readStream(Reader reader)46     public static String readStream(Reader reader) throws IOException {
47         StringBuilder result = new StringBuilder();
48         BufferedReader in = new BufferedReader(reader);
49         String line;
50         while ((line = in.readLine()) != null) {
51             result.append(line);
52             result.append('\n');
53         }
54         in.close();
55         return result.toString();
56     }
57 
readFile(File f)58     public static String readFile(File f) throws IOException {
59         return readStream(new InputStreamReader(new FileInputStream(f), "UTF-8"));
60     }
61 
readFileLines(File f)62     public static List<String> readFileLines(File f) throws IOException {
63         BufferedReader in =
64                 new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
65         List<String> list = Lists.newArrayList();
66         String line;
67         while ((line = in.readLine()) != null) {
68             list.add(line);
69         }
70         in.close();
71         return list;
72     }
73 
join(String delimiter, Object... objects)74     public static String join(String delimiter, Object... objects) {
75         return join(Arrays.asList(objects), delimiter);
76     }
77 
join(Iterable<?> objects, String delimiter)78     public static String join(Iterable<?> objects, String delimiter) {
79         Iterator<?> i = objects.iterator();
80         if (!i.hasNext()) {
81             return "";
82         }
83 
84         StringBuilder result = new StringBuilder();
85         result.append(i.next());
86         while(i.hasNext()) {
87             result.append(delimiter).append(i.next());
88         }
89         return result.toString();
90     }
91 
objectsToStrings(Object[] objects)92     public static String[] objectsToStrings(Object[] objects) {
93         String[] result = new String[objects.length];
94         int i = 0;
95         for (Object o : objects) {
96             result[i++] = o.toString();
97         }
98         return result;
99     }
100 
objectsToStrings(Collection<?> objects)101     public static String[] objectsToStrings(Collection<?> objects) {
102         return objectsToStrings(objects.toArray());
103     }
104 
105     /**
106      * Replaces XML-invalid characters with the corresponding U+XXXX code point escapes.
107      */
xmlSanitize(String text)108     public static String xmlSanitize(String text) {
109         StringBuffer result = new StringBuffer();
110         Matcher matcher = XML_INVALID_CHARS.matcher(text);
111         while (matcher.find()) {
112             matcher.appendReplacement(result, "");
113             result.append(escapeCodePoint(matcher.group()));
114         }
115         matcher.appendTail(result);
116         return result.toString();
117     }
118 
escapeCodePoint(CharSequence cs)119     private static String escapeCodePoint(CharSequence cs) {
120         StringBuilder result = new StringBuilder();
121         for (int i = 0; i < cs.length(); ++i) {
122             result.append(String.format("U+%04X", (int) cs.charAt(i)));
123         }
124         return result.toString();
125     }
126 }
127