1 /**
2  *  ServingXML
3  *
4  *  Copyright (C) 2006  Daniel Parker
5  *    daniel.parker@servingxml.com
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  **/
20 
21 package com.servingxml.util;
22 
23 /**
24  * The <code>StringHelper</code> provides utility methods for strings
25  *
26  *
27  * @author Daniel A. Parker (daniel.parker@servingxml.com)
28  */
29 
30 public class StringHelper {
31 
toString(String[] values, String sep, String quoteMark)32   public static String toString(String[] values, String sep, String quoteMark) {
33     StringBuilder buffer = new StringBuilder();
34     for (int i = 0; i < values.length; ++i) {
35       if (i > 0) {
36         buffer.append(sep);
37       }
38       buffer.append(quoteMark);
39       buffer.append(values[i]);
40       buffer.append(quoteMark);
41     }
42     return buffer.toString();
43   }
44 
trim(String value, boolean trimLeading, boolean trimTrailing)45   public static final String trim(String value, boolean trimLeading, boolean trimTrailing) {
46 
47     if (trimLeading && trimTrailing) {
48       value = value.trim();
49     } else if (trimLeading) {
50       value = trimLeading(value);
51     } else if (trimTrailing) {
52       value = trimTrailing(value);
53     }
54     return value;
55   }
56 
trimTrailing(String value)57   public static final String trimTrailing(String value) {
58     boolean done = false;
59     int length = value.length();
60     while (!done && length > 0) {
61       if (!Character.isWhitespace(value.charAt(length-1))) {
62         done = true;
63       } else {
64         --length;
65       }
66     }
67     return length == value.length() ? value : value.substring(0,length);
68   }
69 
trimLeading(String value)70   public static final String trimLeading(String value) {
71     boolean done = false;
72     int start = 0;
73     int length = value.length();
74     while (!done && start < length) {
75       if (!Character.isWhitespace(value.charAt(start))) {
76         done = true;
77       } else {
78         ++start;
79       }
80     }
81     return start == 0 ? value : value.substring(start);
82   }
83 
translateEscapeChars(String s)84   public static final String translateEscapeChars(String s) {
85     StringBuilder buf = new StringBuilder();
86     char prev = ' ';
87     for (int i = 0; i < s.length(); ++i) {
88       char c = s.charAt(i);
89       if (prev == '\\') {
90         if (c == 't') {
91           buf.append('\t');
92         } else if (c == 'r') {
93           buf.append('\r');
94         } else if (c == 'n') {
95           buf.append('\n');
96         } else {
97           buf.append(prev);
98           buf.append(c);
99         }
100         prev = c;
101       } else {
102         if (c == '\\' && i+1 < s.length()) {
103         } else {
104           buf.append(c);
105         }
106         prev = c;
107       }
108     }
109     return buf.toString();
110   }
111 
contains(String s, char[] value)112   public final static boolean contains(String s, char[] value) {
113     boolean isContained = false;
114     if (value.length > 0 && value.length <= s.length()) {
115       int end = s.length() - value.length + 1;
116       for (int i = 0; !isContained && i < end; ++i) {
117         char c = s.charAt(i);
118         if (c == value[0]) {
119           boolean found = true;
120           for (int j = 1; found && j < value.length && j < s.length(); ++j) {
121             char d = s.charAt(i+j);
122             if (d != value[j]) {
123               found = false;
124             }
125           }
126           if (found) {
127             isContained = true;
128           }
129         }
130       }
131     }
132     return isContained;
133   }
134 
constructNameFromValue(String tag)135   public static final String constructNameFromValue(String tag) {
136 
137     boolean started = false;
138     boolean whitespace = false;
139     StringBuilder buf = new StringBuilder();
140     for (int i = 0; i < tag.length(); ++i) {
141       char c = tag.charAt(i);
142       if (!started) {
143         if (Character.isWhitespace(c)) {
144           continue;
145         }
146         if (Character.isDigit(c)) {
147           buf.append("_");
148           buf.append(c);
149           started = true;
150         } else if (Character.isLetter(c) || c == '_' || c == '-') {
151           buf.append(c);
152           started = true;
153         }
154       } else {
155         if (Character.isLetterOrDigit(c) || c == '_' || c == '-' || c == '.') {
156           if (whitespace) {
157             buf.append("_");
158             whitespace = false;
159           }
160           buf.append(c);
161         } else if (Character.isWhitespace(c)) {
162           whitespace = true;
163         }
164       }
165     }
166     return buf.toString();
167   }
168 
isWhitespaceOrEmpty(String s)169   public static boolean isWhitespaceOrEmpty(String s) {
170     int len = s.length();
171     int st = 0;
172 
173     while ((st < len) && (s.charAt(st) <= ' ')) {
174       st++;
175     }
176     return st == len ? true : false;
177   }
178 }
179