| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.internal; |
| 16 | |
|
| 17 | |
import org.apache.tapestry5.*; |
| 18 | |
import org.apache.tapestry5.ioc.Messages; |
| 19 | |
import org.apache.tapestry5.ioc.Resource; |
| 20 | |
import org.apache.tapestry5.ioc.internal.util.CollectionFactory; |
| 21 | |
import org.apache.tapestry5.ioc.internal.util.Defense; |
| 22 | |
import org.apache.tapestry5.ioc.internal.util.InternalUtils; |
| 23 | |
|
| 24 | |
import java.io.IOException; |
| 25 | |
import java.io.InputStream; |
| 26 | |
import java.io.OutputStream; |
| 27 | |
import java.util.List; |
| 28 | |
import java.util.Map; |
| 29 | |
import java.util.regex.Pattern; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 0 | public class TapestryInternalUtils |
| 35 | |
{ |
| 36 | |
private static final String SLASH = "/"; |
| 37 | |
|
| 38 | 2 | private static final Pattern SLASH_PATTERN = Pattern.compile(SLASH); |
| 39 | |
|
| 40 | 2 | private static final Pattern NON_WORD_PATTERN = Pattern.compile("[^\\w]"); |
| 41 | |
|
| 42 | 2 | private static final Pattern COMMA_PATTERN = Pattern.compile("\\s*,\\s*"); |
| 43 | |
|
| 44 | |
private static final int BUFFER_SIZE = 5000; |
| 45 | |
|
| 46 | 2 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public static String toUserPresentable(String id) |
| 54 | |
{ |
| 55 | 1941 | StringBuilder builder = new StringBuilder(id.length() * 2); |
| 56 | |
|
| 57 | 1941 | char[] chars = id.toCharArray(); |
| 58 | 1941 | boolean postSpace = true; |
| 59 | 1941 | boolean upcaseNext = true; |
| 60 | |
|
| 61 | 13918 | for (char ch : chars) |
| 62 | |
{ |
| 63 | 11977 | if (upcaseNext) |
| 64 | |
{ |
| 65 | 2005 | builder.append(Character.toUpperCase(ch)); |
| 66 | 2005 | upcaseNext = false; |
| 67 | |
|
| 68 | 2005 | continue; |
| 69 | |
} |
| 70 | |
|
| 71 | 9972 | if (ch == '_') |
| 72 | |
{ |
| 73 | 64 | builder.append(' '); |
| 74 | 64 | upcaseNext = true; |
| 75 | 64 | continue; |
| 76 | |
} |
| 77 | |
|
| 78 | 9908 | boolean upperCase = Character.isUpperCase(ch); |
| 79 | |
|
| 80 | 9908 | if (upperCase && !postSpace) builder.append(' '); |
| 81 | |
|
| 82 | 9908 | builder.append(ch); |
| 83 | |
|
| 84 | 9908 | postSpace = upperCase; |
| 85 | |
} |
| 86 | |
|
| 87 | 1941 | return builder.toString(); |
| 88 | |
} |
| 89 | |
|
| 90 | |
public static Map<String, String> mapFromKeysAndValues(String... keysAndValues) |
| 91 | |
{ |
| 92 | 4 | Map<String, String> result = CollectionFactory.newMap(); |
| 93 | |
|
| 94 | 4 | int i = 0; |
| 95 | 12 | while (i < keysAndValues.length) |
| 96 | |
{ |
| 97 | 8 | String key = keysAndValues[i++]; |
| 98 | 8 | String value = keysAndValues[i++]; |
| 99 | |
|
| 100 | 8 | result.put(key, value); |
| 101 | 8 | } |
| 102 | |
|
| 103 | 4 | return result; |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
public static OptionModel toOptionModel(String input) |
| 114 | |
{ |
| 115 | 98 | Defense.notNull(input, "input"); |
| 116 | |
|
| 117 | 98 | int equalsx = input.indexOf('='); |
| 118 | |
|
| 119 | 98 | if (equalsx < 0) return new OptionModelImpl(input); |
| 120 | |
|
| 121 | 38 | String value = input.substring(0, equalsx); |
| 122 | 38 | String label = input.substring(equalsx + 1); |
| 123 | |
|
| 124 | 38 | return new OptionModelImpl(label, value); |
| 125 | |
} |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public static List<OptionModel> toOptionModels(String input) |
| 135 | |
{ |
| 136 | 31 | Defense.notNull(input, "input"); |
| 137 | |
|
| 138 | 31 | List<OptionModel> result = CollectionFactory.newList(); |
| 139 | |
|
| 140 | 125 | for (String term : input.split(",")) |
| 141 | 94 | result.add(toOptionModel(term.trim())); |
| 142 | |
|
| 143 | 31 | return result; |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public static SelectModel toSelectModel(String input) |
| 153 | |
{ |
| 154 | 11 | List<OptionModel> options = toOptionModels(input); |
| 155 | |
|
| 156 | 11 | return new SelectModelImpl(null, options); |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public static OptionModel toOptionModel(Map.Entry input) |
| 166 | |
{ |
| 167 | 10 | Defense.notNull(input, "input"); |
| 168 | |
|
| 169 | 10 | String label = input.getValue() != null ? String.valueOf(input.getValue()) : ""; |
| 170 | |
|
| 171 | 10 | return new OptionModelImpl(label, input.getKey()); |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public static <K, V> List<OptionModel> toOptionModels(Map<K, V> input) |
| 181 | |
{ |
| 182 | 4 | Defense.notNull(input, "input"); |
| 183 | |
|
| 184 | 4 | List<OptionModel> result = CollectionFactory.newList(); |
| 185 | |
|
| 186 | 4 | for (Map.Entry entry : input.entrySet()) |
| 187 | 8 | result.add(toOptionModel(entry)); |
| 188 | |
|
| 189 | 4 | return result; |
| 190 | |
} |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
public static <K, V> SelectModel toSelectModel(Map<K, V> input) |
| 199 | |
{ |
| 200 | 0 | List<OptionModel> options = toOptionModels(input); |
| 201 | |
|
| 202 | 0 | return new SelectModelImpl(null, options); |
| 203 | |
} |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public static OptionModel toOptionModel(Object input) |
| 212 | |
{ |
| 213 | 38 | String label = (input != null ? String.valueOf(input) : ""); |
| 214 | |
|
| 215 | 38 | return new OptionModelImpl(label, input); |
| 216 | |
} |
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
public static <E> List<OptionModel> toOptionModels(List<E> input) |
| 225 | |
{ |
| 226 | 10 | Defense.notNull(input, "input"); |
| 227 | |
|
| 228 | 10 | List<OptionModel> result = CollectionFactory.newList(); |
| 229 | |
|
| 230 | 10 | for (E element : input) |
| 231 | 36 | result.add(toOptionModel(element)); |
| 232 | |
|
| 233 | 10 | return result; |
| 234 | |
} |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
public static <E> SelectModel toSelectModel(List<E> input) |
| 243 | |
{ |
| 244 | 8 | List<OptionModel> options = toOptionModels(input); |
| 245 | |
|
| 246 | 8 | return new SelectModelImpl(null, options); |
| 247 | |
} |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
public static KeyValue parseKeyValue(String input) |
| 257 | |
{ |
| 258 | 278 | int pos = input.indexOf('='); |
| 259 | |
|
| 260 | 278 | if (pos < 1) throw new IllegalArgumentException(InternalMessages.badKeyValue(input)); |
| 261 | |
|
| 262 | 276 | String key = input.substring(0, pos); |
| 263 | 276 | String value = input.substring(pos + 1); |
| 264 | |
|
| 265 | 276 | return new KeyValue(key.trim(), value.trim()); |
| 266 | |
} |
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
public static String extractIdFromPropertyExpression(String expression) |
| 278 | |
{ |
| 279 | 2256 | return replace(expression, NON_WORD_PATTERN, ""); |
| 280 | |
} |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
public static String defaultLabel(String id, Messages messages, String propertyExpression) |
| 287 | |
{ |
| 288 | 1160 | String key = id + "-label"; |
| 289 | |
|
| 290 | 1160 | if (messages.contains(key)) return messages.get(key); |
| 291 | |
|
| 292 | 1094 | return toUserPresentable(extractIdFromPropertyExpression(lastTerm(propertyExpression))); |
| 293 | |
} |
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
public static String lastTerm(String input) |
| 300 | |
{ |
| 301 | 1616 | int dotx = input.lastIndexOf('.'); |
| 302 | |
|
| 303 | 1616 | return input.substring(dotx + 1); |
| 304 | |
} |
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
public static String toClassAttributeValue(List<String> classes) |
| 314 | |
{ |
| 315 | 11948 | if (classes.isEmpty()) return null; |
| 316 | |
|
| 317 | 9504 | return InternalUtils.join(classes, " "); |
| 318 | |
} |
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
public static String getLabelForEnum(Messages messages, String prefix, Enum value) |
| 334 | |
{ |
| 335 | 713 | String name = value.name(); |
| 336 | |
|
| 337 | 713 | String key = prefix + "." + name; |
| 338 | |
|
| 339 | 713 | if (messages.contains(key)) return messages.get(key); |
| 340 | |
|
| 341 | 711 | if (messages.contains(name)) return messages.get(name); |
| 342 | |
|
| 343 | 689 | return toUserPresentable(name.toLowerCase()); |
| 344 | |
} |
| 345 | |
|
| 346 | |
public static String getLabelForEnum(Messages messages, Enum value) |
| 347 | |
{ |
| 348 | 348 | String prefix = lastTerm(value.getClass().getName()); |
| 349 | |
|
| 350 | 348 | return getLabelForEnum(messages, prefix, value); |
| 351 | |
} |
| 352 | |
|
| 353 | |
private static String replace(String input, Pattern pattern, String replacement) |
| 354 | |
{ |
| 355 | 2256 | return pattern.matcher(input).replaceAll(replacement); |
| 356 | |
} |
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
public static <T> boolean isEqual(T left, T right) |
| 368 | |
{ |
| 369 | 45452 | if (left == right) return true; |
| 370 | |
|
| 371 | 11142 | if (left == null) return right == null; |
| 372 | |
|
| 373 | 11138 | return left.equals(right); |
| 374 | |
} |
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
public static String[] splitPath(String path) |
| 381 | |
{ |
| 382 | 3192 | return SLASH_PATTERN.split(path); |
| 383 | |
} |
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
public static String[] splitAtCommas(String value) |
| 391 | |
{ |
| 392 | 3376 | if (InternalUtils.isBlank(value)) |
| 393 | 1640 | return EMPTY_STRING_ARRAY; |
| 394 | |
|
| 395 | 1736 | return COMMA_PATTERN.split(value.trim()); |
| 396 | |
} |
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
public static void copy(InputStream in, OutputStream out) throws IOException |
| 408 | |
{ |
| 409 | 290 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 410 | |
|
| 411 | |
while (true) |
| 412 | |
{ |
| 413 | 1548 | int length = in.read(buffer); |
| 414 | |
|
| 415 | 1548 | if (length < 0) break; |
| 416 | |
|
| 417 | 1258 | out.write(buffer, 0, length); |
| 418 | 1258 | } |
| 419 | |
|
| 420 | |
|
| 421 | 290 | out.flush(); |
| 422 | 290 | } |
| 423 | |
|
| 424 | |
public static boolean isEqual(EventContext left, EventContext right) |
| 425 | |
{ |
| 426 | 82 | if (left == right) return true; |
| 427 | |
|
| 428 | 80 | int count = left.getCount(); |
| 429 | |
|
| 430 | 80 | if (count != right.getCount()) return false; |
| 431 | |
|
| 432 | 156 | for (int i = 0; i < count; i++) |
| 433 | |
{ |
| 434 | 76 | if (!left.get(Object.class, i).equals(right.get(Object.class, i))) |
| 435 | 0 | return false; |
| 436 | |
} |
| 437 | |
|
| 438 | 80 | return true; |
| 439 | |
} |
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
public static Asset2 toAsset2(final Asset asset) |
| 448 | |
{ |
| 449 | 58 | if (asset instanceof Asset2) |
| 450 | 56 | return (Asset2) asset; |
| 451 | |
|
| 452 | 2 | return new Asset2() |
| 453 | |
{ |
| 454 | |
|
| 455 | |
public boolean isInvariant() |
| 456 | |
{ |
| 457 | 2 | return false; |
| 458 | |
} |
| 459 | |
|
| 460 | |
public Resource getResource() |
| 461 | |
{ |
| 462 | 2 | return asset.getResource(); |
| 463 | |
} |
| 464 | |
|
| 465 | |
public String toClientURL() |
| 466 | |
{ |
| 467 | 2 | return asset.toClientURL(); |
| 468 | |
} |
| 469 | |
|
| 470 | |
@Override |
| 471 | 2 | public String toString() |
| 472 | |
{ |
| 473 | 2 | return asset.toString(); |
| 474 | |
} |
| 475 | |
}; |
| 476 | |
} |
| 477 | |
} |