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