1 /* 2 * Copyright (C) 2015 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 android.databinding.tool.util; 18 19 import com.google.common.base.StandardSystemProperty; 20 import com.google.common.base.Strings; 21 22 public class StringUtils { 23 24 public static final String LINE_SEPARATOR = StandardSystemProperty.LINE_SEPARATOR.value(); 25 /** The entity for the ampersand character */ 26 private static final String AMP_ENTITY = "&"; 27 /** The entity for the quote character */ 28 private static final String QUOT_ENTITY = """; 29 /** The entity for the apostrophe character */ 30 private static final String APOS_ENTITY = "'"; 31 /** The entity for the less than character */ 32 private static final String LT_ENTITY = "<"; 33 /** The entity for the greater than character */ 34 private static final String GT_ENTITY = ">"; 35 /** The entity for the tab character */ 36 private static final String TAB_ENTITY = "	"; 37 /** The entity for the carriage return character */ 38 private static final String CR_ENTITY = "
"; 39 /** The entity for the line feed character */ 40 private static final String LFEED_ENTITY = "
"; 41 isNotBlank(CharSequence string)42 public static boolean isNotBlank(CharSequence string) { 43 if (string == null) { 44 return false; 45 } 46 for (int i = 0, n = string.length(); i < n; i++) { 47 if (!Character.isWhitespace(string.charAt(i))) { 48 return true; 49 } 50 } 51 return false; 52 } 53 capitalize(String string)54 public static String capitalize(String string) { 55 if (Strings.isNullOrEmpty(string)) { 56 return string; 57 } 58 char ch = string.charAt(0); 59 if (Character.isTitleCase(ch)) { 60 return string; 61 } 62 return Character.toTitleCase(ch) + string.substring(1); 63 } 64 unescapeXml(String escaped)65 public static String unescapeXml(String escaped) { 66 // TODO: unescape unicode codepoints 67 return escaped.replace(QUOT_ENTITY, "\"") 68 .replace(LT_ENTITY, "<") 69 .replace(GT_ENTITY, ">") 70 .replace(APOS_ENTITY, "'") 71 .replace(AMP_ENTITY, "&") 72 .replace(TAB_ENTITY, "\t") 73 .replace(CR_ENTITY, "\r") 74 .replace(LFEED_ENTITY, "\n"); 75 } 76 StringUtils()77 private StringUtils() { 78 } 79 80 } 81