| 1 |
/* |
| 2 |
* Copyright (C) 2012 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 |
package com.android.utils; |
| 18 |
|
| 19 |
import com.android.annotations.NonNull; |
| 20 |
import com.android.annotations.Nullable; |
| 21 |
import com.google.common.base.CaseFormat; |
| 22 |
import com.google.common.collect.ImmutableList; |
| 23 |
|
| 24 |
import java.io.File; |
| 25 |
import java.io.IOException; |
| 26 |
import java.io.StringWriter; |
| 27 |
import java.net.MalformedURLException; |
| 28 |
import java.net.URISyntaxException; |
| 29 |
import java.net.URL; |
| 30 |
import java.util.List; |
| 31 |
import java.util.Properties; |
| 32 |
|
| 33 |
import static com.android.SdkConstants.DOT_WEBP; |
| 34 |
import static com.android.SdkConstants.DOT_PNG; |
| 35 |
import static com.android.SdkConstants.DOT_GIF; |
| 36 |
import static com.android.SdkConstants.DOT_9PNG; |
| 37 |
import static com.android.SdkConstants.DOT_JPEG; |
| 38 |
import static com.android.SdkConstants.DOT_JPG; |
| 39 |
import static com.android.SdkConstants.DOT_BMP; |
| 40 |
|
| 41 |
/** Miscellaneous utilities used by the Android SDK tools */ |
| 42 |
public class SdkUtils { |
| 43 |
/** |
| 44 |
* Returns true if the given string ends with the given suffix, using a |
| 45 |
* case-insensitive comparison. |
| 46 |
* |
| 47 |
* @param string the full string to be checked |
| 48 |
* @param suffix the suffix to be checked for |
| 49 |
* @return true if the string case-insensitively ends with the given suffix |
| 50 |
*/ |
| 51 |
public static boolean endsWithIgnoreCase(@NonNull String string, @NonNull String suffix) { |
| 52 |
return string.regionMatches(true /* ignoreCase */, string.length() - suffix.length(), |
| 53 |
suffix, 0, suffix.length()); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns true if the given sequence ends with the given suffix (case |
| 58 |
* sensitive). |
| 59 |
* |
| 60 |
* @param sequence the character sequence to be checked |
| 61 |
* @param suffix the suffix to look for |
| 62 |
* @return true if the given sequence ends with the given suffix |
| 63 |
*/ |
| 64 |
public static boolean endsWith(@NonNull CharSequence sequence, @NonNull CharSequence suffix) { |
| 65 |
return endsWith(sequence, sequence.length(), suffix); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns true if the given sequence ends at the given offset with the given suffix (case |
| 70 |
* sensitive) |
| 71 |
* |
| 72 |
* @param sequence the character sequence to be checked |
| 73 |
* @param endOffset the offset at which the sequence is considered to end |
| 74 |
* @param suffix the suffix to look for |
| 75 |
* @return true if the given sequence ends with the given suffix |
| 76 |
*/ |
| 77 |
public static boolean endsWith(@NonNull CharSequence sequence, int endOffset, |
| 78 |
@NonNull CharSequence suffix) { |
| 79 |
if (endOffset < suffix.length()) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
for (int i = endOffset - 1, j = suffix.length() - 1; j >= 0; i--, j--) { |
| 84 |
if (sequence.charAt(i) != suffix.charAt(j)) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns true if the given string starts with the given prefix, using a |
| 94 |
* case-insensitive comparison. |
| 95 |
* |
| 96 |
* @param string the full string to be checked |
| 97 |
* @param prefix the prefix to be checked for |
| 98 |
* @return true if the string case-insensitively starts with the given prefix |
| 99 |
*/ |
| 100 |
public static boolean startsWithIgnoreCase(@NonNull String string, @NonNull String prefix) { |
| 101 |
return string.regionMatches(true /* ignoreCase */, 0, prefix, 0, prefix.length()); |
| 102 |
} |
| 103 |
|
| 104 |
/** For use by {@link #getLineSeparator()} */ |
| 105 |
private static String sLineSeparator; |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns the default line separator to use. |
| 109 |
* <p> |
| 110 |
* NOTE: If you have an associated IDocument (Eclipse), it is better to call |
| 111 |
* TextUtilities#getDefaultLineDelimiter(IDocument) since that will |
| 112 |
* allow (for example) editing a \r\n-delimited document on a \n-delimited |
| 113 |
* platform and keep a consistent usage of delimiters in the file. |
| 114 |
* |
| 115 |
* @return the delimiter string to use |
| 116 |
*/ |
| 117 |
@NonNull |
| 118 |
public static String getLineSeparator() { |
| 119 |
if (sLineSeparator == null) { |
| 120 |
// This is guaranteed to exist: |
| 121 |
sLineSeparator = System.getProperty("line.separator"); //$NON-NLS-1$ |
| 122 |
} |
| 123 |
|
| 124 |
return sLineSeparator; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Wraps the given text at the given line width, with an optional hanging |
| 129 |
* indent. |
| 130 |
* |
| 131 |
* @param text the text to be wrapped |
| 132 |
* @param lineWidth the number of characters to wrap the text to |
| 133 |
* @param hangingIndent the hanging indent (to be used for the second and |
| 134 |
* subsequent lines in each paragraph, or null if not known |
| 135 |
* @return the string, wrapped |
| 136 |
*/ |
| 137 |
@NonNull |
| 138 |
public static String wrap( |
| 139 |
@NonNull String text, |
| 140 |
int lineWidth, |
| 141 |
@Nullable String hangingIndent) { |
| 142 |
if (hangingIndent == null) { |
| 143 |
hangingIndent = ""; |
| 144 |
} |
| 145 |
int explanationLength = text.length(); |
| 146 |
StringBuilder sb = new StringBuilder(explanationLength * 2); |
| 147 |
int index = 0; |
| 148 |
|
| 149 |
while (index < explanationLength) { |
| 150 |
int lineEnd = text.indexOf('\n', index); |
| 151 |
int next; |
| 152 |
|
| 153 |
if (lineEnd != -1 && (lineEnd - index) < lineWidth) { |
| 154 |
next = lineEnd + 1; |
| 155 |
} else { |
| 156 |
// Line is longer than available width; grab as much as we can |
| 157 |
lineEnd = Math.min(index + lineWidth, explanationLength); |
| 158 |
if (lineEnd - index < lineWidth) { |
| 159 |
next = explanationLength; |
| 160 |
} else { |
| 161 |
// then back up to the last space |
| 162 |
int lastSpace = text.lastIndexOf(' ', lineEnd); |
| 163 |
if (lastSpace > index) { |
| 164 |
lineEnd = lastSpace; |
| 165 |
next = lastSpace + 1; |
| 166 |
} else { |
| 167 |
// No space anywhere on the line: it contains something wider than |
| 168 |
// can fit (like a long URL) so just hard break it |
| 169 |
next = lineEnd + 1; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if (sb.length() > 0) { |
| 175 |
sb.append(hangingIndent); |
| 176 |
} else { |
| 177 |
lineWidth -= hangingIndent.length(); |
| 178 |
} |
| 179 |
|
| 180 |
sb.append(text.substring(index, lineEnd)); |
| 181 |
sb.append('\n'); |
| 182 |
index = next; |
| 183 |
} |
| 184 |
|
| 185 |
return sb.toString(); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Returns the corresponding {@link File} for the given file:// url |
| 190 |
* |
| 191 |
* @param url the URL string, e.g. file://foo/bar |
| 192 |
* @return the corresponding {@link File} (which may or may not exist) |
| 193 |
* @throws MalformedURLException if the URL string is malformed or is not a file: URL |
| 194 |
*/ |
| 195 |
@NonNull |
| 196 |
public static File urlToFile(@NonNull String url) throws MalformedURLException { |
| 197 |
return urlToFile(new URL(url)); |
| 198 |
} |
| 199 |
|
| 200 |
@NonNull |
| 201 |
public static File urlToFile(@NonNull URL url) throws MalformedURLException { |
| 202 |
try { |
| 203 |
return new File(url.toURI()); |
| 204 |
} |
| 205 |
catch (IllegalArgumentException e) { |
| 206 |
MalformedURLException ex = new MalformedURLException(e.getLocalizedMessage()); |
| 207 |
ex.initCause(e); |
| 208 |
throw ex; |
| 209 |
} |
| 210 |
catch (URISyntaxException e) { |
| 211 |
return new File(url.getPath()); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Returns the corresponding URL string for the given {@link File} |
| 217 |
* |
| 218 |
* @param file the file to look up the URL for |
| 219 |
* @return the corresponding URL |
| 220 |
* @throws MalformedURLException in very unexpected cases |
| 221 |
*/ |
| 222 |
public static String fileToUrlString(@NonNull File file) throws MalformedURLException { |
| 223 |
String url = fileToUrl(file).toExternalForm(); |
| 224 |
// Use three slashes, which is the form most widely recognized by terminal emulators. |
| 225 |
if (!url.startsWith("file:///")) { |
| 226 |
url = url.replaceFirst("file:/", "file:///"); |
| 227 |
} |
| 228 |
return url; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Returns the corresponding URL for the given {@link File} |
| 233 |
* |
| 234 |
* @param file the file to look up the URL for |
| 235 |
* @return the corresponding URL |
| 236 |
* @throws MalformedURLException in very unexpected cases |
| 237 |
*/ |
| 238 |
public static URL fileToUrl(@NonNull File file) throws MalformedURLException { |
| 239 |
return file.toURI().toURL(); |
| 240 |
} |
| 241 |
|
| 242 |
/** Prefix in comments which mark the source locations for merge results */ |
| 243 |
public static final String FILENAME_PREFIX = "From: "; |
| 244 |
|
| 245 |
/** |
| 246 |
* Creates the path comment XML string. Note that it does not escape characters |
| 247 |
* such as & and <; those are expected to be escaped by the caller (for |
| 248 |
* example, handled by a call to {@link org.w3c.dom.Document#createComment(String)}) |
| 249 |
* |
| 250 |
* |
| 251 |
* @param file the file to create a path comment for |
| 252 |
* @param includePadding whether to include padding. The final comment recognized by |
| 253 |
* error recognizers expect padding between the {@code <!--} and |
| 254 |
* the start marker (From:); you can disable padding if the caller |
| 255 |
* already is in a context where the padding has been added. |
| 256 |
* @return the corresponding XML contents of the string |
| 257 |
*/ |
| 258 |
public static String createPathComment(@NonNull File file, boolean includePadding) |
| 259 |
throws MalformedURLException { |
| 260 |
String url = fileToUrlString(file); |
| 261 |
int dashes = url.indexOf("--"); |
| 262 |
if (dashes != -1) { // Not allowed inside XML comments - for SGML compatibility. Sigh. |
| 263 |
url = url.replace("--", "%2D%2D"); |
| 264 |
} |
| 265 |
|
| 266 |
if (includePadding) { |
| 267 |
return ' ' + FILENAME_PREFIX + url + ' '; |
| 268 |
} else { |
| 269 |
return FILENAME_PREFIX + url; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Translates an XML name (e.g. xml-name) into a Java / C++ constant name (e.g. XML_NAME) |
| 275 |
* @param xmlName the hyphen separated lower case xml name. |
| 276 |
* @return the equivalent constant name. |
| 277 |
*/ |
| 278 |
public static String xmlNameToConstantName(String xmlName) { |
| 279 |
return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, xmlName); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Translates a camel case name (e.g. xmlName) into a Java / C++ constant name (e.g. XML_NAME) |
| 284 |
* @param camelCaseName the camel case name. |
| 285 |
* @return the equivalent constant name. |
| 286 |
*/ |
| 287 |
public static String camelCaseToConstantName(String camelCaseName) { |
| 288 |
return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, camelCaseName); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Translates a Java / C++ constant name (e.g. XML_NAME) into camel case name (e.g. xmlName) |
| 293 |
* @param constantName the constant name. |
| 294 |
* @return the equivalent camel case name. |
| 295 |
*/ |
| 296 |
public static String constantNameToCamelCase(String constantName) { |
| 297 |
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, constantName); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Translates a Java / C++ constant name (e.g. XML_NAME) into a XML case name (e.g. xml-name) |
| 302 |
* @param constantName the constant name. |
| 303 |
* @return the equivalent XML name. |
| 304 |
*/ |
| 305 |
public static String constantNameToXmlName(String constantName) { |
| 306 |
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, constantName); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/** |
| 311 |
* Get the R field name from a resource name, since |
| 312 |
* AAPT will flatten the namespace, turning dots, dashes and colons into _ |
| 313 |
* |
| 314 |
* @param resourceName the name to convert |
| 315 |
* @return the corresponding R field name |
| 316 |
*/ |
| 317 |
@NonNull |
| 318 |
public static String getResourceFieldName(@NonNull String resourceName) { |
| 319 |
// AAPT will flatten the namespace, turning dots, dashes and colons into _ |
| 320 |
for (int i = 0, n = resourceName.length(); i < n; i++) { |
| 321 |
char c = resourceName.charAt(i); |
| 322 |
if (c == '.' || c == ':' || c == '-') { |
| 323 |
return resourceName.replace('.', '_').replace('-', '_').replace(':', '_'); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
return resourceName; |
| 328 |
} |
| 329 |
|
| 330 |
public static final List<String> IMAGE_EXTENSIONS = ImmutableList.of( |
| 331 |
DOT_PNG, DOT_9PNG, DOT_GIF, DOT_JPEG, DOT_JPG, DOT_BMP, DOT_WEBP); |
| 332 |
|
| 333 |
/** |
| 334 |
* Returns true if the given file path points to an image file recognized by |
| 335 |
* Android. See http://developer.android.com/guide/appendix/media-formats.html |
| 336 |
* for details. |
| 337 |
* |
| 338 |
* @param path the filename to be tested |
| 339 |
* @return true if the file represents an image file |
| 340 |
*/ |
| 341 |
public static boolean hasImageExtension(String path) { |
| 342 |
for (String ext: IMAGE_EXTENSIONS) { |
| 343 |
if (endsWithIgnoreCase(path, ext)) { |
| 344 |
return true; |
| 345 |
} |
| 346 |
} |
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Escapes the given property file value (right hand side of property assignment) |
| 352 |
* as required by the property file format (e.g. escapes colons and backslashes) |
| 353 |
* |
| 354 |
* @param value the value to be escaped |
| 355 |
* @return the escaped value |
| 356 |
*/ |
| 357 |
@NonNull |
| 358 |
public static String escapePropertyValue(@NonNull String value) { |
| 359 |
// Slow, stupid implementation, but is 100% compatible with Java's property file |
| 360 |
// implementation |
| 361 |
Properties properties = new Properties(); |
| 362 |
properties.setProperty("k", value); // key doesn't matter |
| 363 |
StringWriter writer = new StringWriter(); |
| 364 |
try { |
| 365 |
properties.store(writer, null); |
| 366 |
String s = writer.toString(); |
| 367 |
int end = s.length(); |
| 368 |
|
| 369 |
// Writer inserts trailing newline |
| 370 |
String lineSeparator = SdkUtils.getLineSeparator(); |
| 371 |
if (s.endsWith(lineSeparator)) { |
| 372 |
end -= lineSeparator.length(); |
| 373 |
} |
| 374 |
|
| 375 |
int start = s.indexOf('='); |
| 376 |
assert start != -1 : s; |
| 377 |
return s.substring(start + 1, end); |
| 378 |
} |
| 379 |
catch (IOException e) { |
| 380 |
return value; // shouldn't happen; we're not going to disk |
| 381 |
} |
| 382 |
} |
| 383 |
} |