|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| StringUtilities.java | 11.9% | 10.8% | 18.2% | 11.9% |
|
||||||||||||||
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | * | |
| 17 | * $Id: StringUtilities.java 645305 2008-04-06 20:42:36Z natalia $ | |
| 18 | */ | |
| 19 | ||
| 20 | package org.apache.xindice.util; | |
| 21 | ||
| 22 | import java.util.StringTokenizer; | |
| 23 | import java.io.UnsupportedEncodingException; | |
| 24 | ||
| 25 | /** | |
| 26 | * StringUtilities provides a set of commonly used String parsing and | |
| 27 | * processing routines. | |
| 28 | * | |
| 29 | * @version $Revision: 645305 $, $Date: 2008-04-06 20:42:36 +0000 (Sun, 06 Apr 2008) $ | |
| 30 | */ | |
| 31 | public final class StringUtilities { | |
| 32 | private static final String ALLOWED_CHARS = "$-_.+!*'(),;/:@&="; | |
| 33 | ||
| 34 | 0 | private StringUtilities() { |
| 35 | } | |
| 36 | ||
| 37 | /** | |
| 38 | * stringToInt quickly converts a String value to an int, returning | |
| 39 | * a default value if a conversion error occurs. | |
| 40 | * | |
| 41 | * @param value The value to convert | |
| 42 | * @param defvalue The default value to return | |
| 43 | * @return The converted value | |
| 44 | */ | |
| 45 | 0 | public static int stringToInt(String value, int defvalue) { |
| 46 | 0 | try { |
| 47 | 0 | return Integer.parseInt(value); |
| 48 | } catch (Exception e) { | |
| 49 | 0 | return defvalue; |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | /** | |
| 54 | * stringToInt quickly converts a String value to an int, returning | |
| 55 | * 0 if a conversion error occurs. | |
| 56 | * | |
| 57 | * @param value The value to convert | |
| 58 | * @return The converted value | |
| 59 | */ | |
| 60 | 0 | public static int stringToInt(String value) { |
| 61 | 0 | return stringToInt(value, 0); |
| 62 | } | |
| 63 | ||
| 64 | /** | |
| 65 | * leftJustify left-justifies a String value, space padding to width | |
| 66 | * characters if the string is less than width. | |
| 67 | * | |
| 68 | * @param value The value to left-justify | |
| 69 | * @param width The width to left-justify to | |
| 70 | * @return The left-justified value | |
| 71 | */ | |
| 72 | 0 | public static String leftJustify(String value, int width) { |
| 73 | 0 | StringBuffer sb = new StringBuffer(width); |
| 74 | 0 | sb.append(value); |
| 75 | 0 | for (int i = sb.length(); i < width; i++) { |
| 76 | 0 | sb.append(' '); |
| 77 | } | |
| 78 | ||
| 79 | 0 | if (sb.length() > width) { |
| 80 | 0 | sb.setLength(width); |
| 81 | } | |
| 82 | 0 | return sb.toString(); |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * rightJustify right-justifies a String value, space padding to | |
| 87 | * width characters if the string is less than width. | |
| 88 | * | |
| 89 | * @param value The value to right-justify | |
| 90 | * @param width The width to right-justify to | |
| 91 | * @return The right-justified value | |
| 92 | */ | |
| 93 | 0 | public static String rightJustify(String value, int width) { |
| 94 | 0 | StringBuffer sb = new StringBuffer(width); |
| 95 | 0 | sb.append(value); |
| 96 | 0 | for (int i = sb.length(); i < width; i++) { |
| 97 | 0 | sb.insert(0, ' '); |
| 98 | } | |
| 99 | ||
| 100 | 0 | if (sb.length() > width) { |
| 101 | 0 | return sb.toString().substring(sb.length() - width); |
| 102 | } else { | |
| 103 | 0 | return sb.toString(); |
| 104 | } | |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * parseBuffer parses through a String buffer and produces a | |
| 109 | * Map table using the provided delimiters. This method can also | |
| 110 | * urldecode values and produce newline-delimited multi-value properties if | |
| 111 | * multiple values for the same key are detected in the buffer. | |
| 112 | * | |
| 113 | * @param retprops The Map to populate | |
| 114 | * @param inbuffer The String buffer to parse | |
| 115 | * @param delimiters The delimiters to use | |
| 116 | * @param urldecode Whether or not to URL-decode the values | |
| 117 | * @param multivalue Whether or not to parse multiple values | |
| 118 | */ | |
| 119 | /* | |
| 120 | public static void parseBuffer(Map retprops, String inbuffer, String delimiters, boolean urldecode, boolean multivalue) { | |
| 121 | if ( inbuffer.length() == 0 ) | |
| 122 | return; | |
| 123 | int idx = 0; | |
| 124 | String line; | |
| 125 | String key; | |
| 126 | String value; | |
| 127 | String prop; | |
| 128 | String temp; | |
| 129 | StringTokenizer st = new StringTokenizer(inbuffer, delimiters, false); | |
| 130 | while ( st.hasMoreTokens() ) { | |
| 131 | line = st.nextToken(); | |
| 132 | idx = line.indexOf('='); | |
| 133 | if ( idx != -1 ) { | |
| 134 | key = line.substring(0, idx).trim(); | |
| 135 | try { | |
| 136 | value = urldecode ? URLDecoder.decode(line.substring(idx+1).trim()) | |
| 137 | : line.substring(idx+1).trim(); | |
| 138 | } | |
| 139 | catch ( Exception e ) { | |
| 140 | value = line.substring(idx+1).trim(); | |
| 141 | } | |
| 142 | if ( multivalue ) { | |
| 143 | temp = (String)retprops.get(key); | |
| 144 | if ( temp != null && temp.length() > 0 ) | |
| 145 | value = temp + "\u0001" + value; | |
| 146 | retprops.put(key, value); | |
| 147 | } | |
| 148 | else | |
| 149 | retprops.put(key, value); | |
| 150 | } | |
| 151 | } | |
| 152 | } | |
| 153 | */ | |
| 154 | ||
| 155 | /** | |
| 156 | * findWhiteSpace scans a String value for whitespace characters | |
| 157 | * from a specified starting position and returns the location of the first | |
| 158 | * whitespace character found. | |
| 159 | * | |
| 160 | * @param value The value to scan | |
| 161 | * @param start The starting position | |
| 162 | * @return The first whitespace location | |
| 163 | */ | |
| 164 | 0 | public static int findWhiteSpace(String value, int start) { |
| 165 | 0 | char[] chars = value.toCharArray(); |
| 166 | 0 | for (int i = start; i < chars.length; i++) { |
| 167 | 0 | if (Character.isWhitespace(chars[i])) { |
| 168 | 0 | return i; |
| 169 | } | |
| 170 | } | |
| 171 | 0 | return -1; |
| 172 | } | |
| 173 | ||
| 174 | /** | |
| 175 | * findWhiteSpace scans a String value for whitespace characters | |
| 176 | * and returns the location of the first whitespace character found. | |
| 177 | * | |
| 178 | * @param value The value to scan | |
| 179 | * @return The first whitespace location | |
| 180 | */ | |
| 181 | 0 | public static int findWhiteSpace(String value) { |
| 182 | 0 | return findWhiteSpace(value, 0); |
| 183 | } | |
| 184 | ||
| 185 | /** | |
| 186 | * javaEncode converts a String value to a Java-printable String. | |
| 187 | * All non-printable characters, such as newline and backspace are converted | |
| 188 | * to their respective escape sequences. This method does not convert | |
| 189 | * UNICODE characters. | |
| 190 | * | |
| 191 | * @param value The value to convert | |
| 192 | * @return The converted value | |
| 193 | */ | |
| 194 | 0 | public static String javaEncode(String value) { |
| 195 | 0 | StringTokenizer st = new StringTokenizer(value, "\b\t\n\f\r\"\'\\", true); |
| 196 | 0 | StringBuffer sb = new StringBuffer(value.length()); |
| 197 | 0 | while (st.hasMoreTokens()) { |
| 198 | 0 | String token = st.nextToken(); |
| 199 | 0 | if (token.equals("\b")) { |
| 200 | 0 | sb.append("\\b"); |
| 201 | 0 | } else if (token.equals("\t")) { |
| 202 | 0 | sb.append("\\t"); |
| 203 | 0 | } if (token.equals("\n")) { |
| 204 | 0 | sb.append("\\n"); |
| 205 | 0 | } else if (token.equals("\f")) { |
| 206 | 0 | sb.append("\\f"); |
| 207 | 0 | } else if (token.equals("\r")) { |
| 208 | 0 | sb.append("\\r"); |
| 209 | 0 | } else if (token.equals("\"")) { |
| 210 | 0 | sb.append("\\\""); |
| 211 | 0 | } else if (token.equals("\'")) { |
| 212 | 0 | sb.append("\\\'"); |
| 213 | 0 | } else if (token.equals("\\")) { |
| 214 | 0 | sb.append("\\\\"); |
| 215 | } else { | |
| 216 | 0 | sb.append(token); |
| 217 | } | |
| 218 | } | |
| 219 | 0 | return sb.toString(); |
| 220 | } | |
| 221 | ||
| 222 | 62 | public static boolean equals(String s1, String s2) { |
| 223 | 62 | if (s1 == null || s2 == null) { |
| 224 | 24 | return (s1 == null && s2 == null) || "".equals(s1) || "".equals(s2); |
| 225 | } else { | |
| 226 | 38 | return s1.equals(s2); |
| 227 | } | |
| 228 | } | |
| 229 | ||
| 230 | 33 | public static boolean isBlank(String str) { |
| 231 | 33 | for (int i = 0; i < str.length(); i++) { |
| 232 | 9 | if (!Character.isWhitespace(str.charAt(i))) { |
| 233 | 9 | return false; |
| 234 | } | |
| 235 | } | |
| 236 | ||
| 237 | 24 | return true; |
| 238 | } | |
| 239 | ||
| 240 | /** | |
| 241 | * Escapes a path in URL by converting all unsafe characters to a sequence | |
| 242 | * of %xx according to RFC 1738 (Uniform Resource Locators (URL)). It is | |
| 243 | * similar, but not identical to URLEncoder. | |
| 244 | * @param path String representation of a URL path to be escaped | |
| 245 | * @return Escaped path | |
| 246 | */ | |
| 247 | 0 | public static String escapePath(String path) { |
| 248 | 0 | StringBuffer buf = new StringBuffer(); |
| 249 | ||
| 250 | 0 | for (int i = 0; i < path.length(); i++) { |
| 251 | 0 | char ch = path.charAt(i); |
| 252 | ||
| 253 | 0 | if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') && |
| 254 | ALLOWED_CHARS.indexOf(ch) < 0) { | |
| 255 | 0 | try { |
| 256 | 0 | byte[] b = Character.toString(ch).getBytes("utf-8"); |
| 257 | 0 | for (int j = 0; j < b.length; j++) { |
| 258 | 0 | char upper = Character.forDigit(((b[j] >> 4) & 0xF), 16); |
| 259 | 0 | char lower = Character.forDigit((b[j] & 0xF), 16); |
| 260 | 0 | buf.append('%').append(upper).append(lower); |
| 261 | } | |
| 262 | } catch (UnsupportedEncodingException e) { | |
| 263 | // won't happen | |
| 264 | } | |
| 265 | } else { | |
| 266 | 0 | buf.append(ch); |
| 267 | } | |
| 268 | } | |
| 269 | ||
| 270 | 0 | return buf.toString(); |
| 271 | } | |
| 272 | } |
|
||||||||||