| 1 | package org.jtoolkit.essence.utils; |
| 2 | |
| 3 | import org.jetbrains.annotations.NotNull; |
| 4 | import org.jetbrains.annotations.Nullable; |
| 5 | import org.jtoolkit.essence.app.pojo.impl.DataValueClass; |
| 6 | |
| 7 | import java.text.DateFormat; |
| 8 | import java.text.ParseException; |
| 9 | import java.text.SimpleDateFormat; |
| 10 | import java.util.Date; |
| 11 | import java.util.Map; |
| 12 | |
| 13 | /* |
| 14 | Copyright 2006 Peter Lawrey |
| 15 | |
| 16 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 | you may not use this file except in compliance with the License. |
| 18 | You may obtain a copy of the License at |
| 19 | |
| 20 | http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | |
| 22 | Unless required by applicable law or agreed to in writing, software |
| 23 | distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | See the License for the specific language governing permissions and |
| 26 | limitations under the License. |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * @author Peter Lawrey |
| 31 | */ |
| 32 | public class StringUtils { |
| 33 | private static final String BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 34 | private static final String START_DELIM = "${"; |
| 35 | private static final char END_DELIM = '}'; |
| 36 | private static final int EXPAND_RUNS = 13; |
| 37 | public static final String NEW_LINE = System.getProperty("line.separator"); |
| 38 | private static final DateFormat[] DATE_FORMATS = { |
| 39 | new SimpleDateFormat(), // default for local. |
| 40 | new SimpleDateFormat("EEE MMM d HH:mm:ss Z yyyy"), |
| 41 | new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS"), |
| 42 | new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"), |
| 43 | new SimpleDateFormat("yyyy/MM/dd HH:mm"), |
| 44 | new SimpleDateFormat("yyyy/MM/dd") |
| 45 | }; |
| 46 | |
| 47 | private StringUtils() { |
| 48 | } |
| 49 | |
| 50 | @NotNull public static String expand(@NotNull String text, @NotNull Named.Source<? extends Object> properties) { |
| 51 | for (int r = 0; r < EXPAND_RUNS; r++) { |
| 52 | int pos = text.indexOf(START_DELIM); |
| 53 | if (pos < 0) |
| 54 | break; |
| 55 | StringBuilder sb = new StringBuilder(text.length() + 64); |
| 56 | int start = 0; |
| 57 | while (pos >= 0) { |
| 58 | int end = text.indexOf(END_DELIM, pos + 1); |
| 59 | if (end < 0) break; |
| 60 | sb.append(text.substring(start, pos)); |
| 61 | String key = text.substring(pos + 2, end); |
| 62 | Object value = properties.getValue(key); |
| 63 | // is it nested or not setCopy? |
| 64 | if (value == null) { |
| 65 | sb.append(START_DELIM); |
| 66 | start = pos + 2; |
| 67 | } else { |
| 68 | sb.append(value); |
| 69 | start = end + 1; |
| 70 | } |
| 71 | pos = text.indexOf(START_DELIM, start); |
| 72 | } |
| 73 | sb.append(text.substring(start)); |
| 74 | text = sb.toString(); |
| 75 | } |
| 76 | return text; |
| 77 | } |
| 78 | |
| 79 | public static boolean isBlank(CharSequence timeout) { |
| 80 | return timeout == null || timeout.length() == 0; |
| 81 | } |
| 82 | |
| 83 | public static boolean isSet(CharSequence timeout) { |
| 84 | return timeout != null && timeout.length() > 0; |
| 85 | } |
| 86 | |
| 87 | @Nullable public static Boolean parseBoolean(@Nullable String text) { |
| 88 | if (text == null || text.length() == 0) return null; |
| 89 | if ("tT1yY".indexOf(text.charAt(0)) >= 0) return true; |
| 90 | if ("fF0nN".indexOf(text.charAt(0)) >= 0) return false; |
| 91 | throw new IllegalArgumentException("Unable to convert '" + text + "' to Boolean"); |
| 92 | } |
| 93 | |
| 94 | public static Date parseDate(String dateStr) throws IllegalArgumentException { |
| 95 | for (DateFormat df : DATE_FORMATS) { |
| 96 | try { |
| 97 | synchronized (df) { |
| 98 | return df.parse(dateStr); |
| 99 | } |
| 100 | } catch (ParseException ignored) { |
| 101 | // ignored |
| 102 | } |
| 103 | } |
| 104 | throw new IllegalArgumentException("Unable to parse " + dateStr + " as a date"); |
| 105 | } |
| 106 | |
| 107 | public static String toCamelCase(@NotNull String text) { |
| 108 | if (isBlank(text)) return text; |
| 109 | // if all in UPPERCASE convert to lower case and include _ as a seperator. |
| 110 | String sep = "-.~!$/ "; |
| 111 | if (text.toUpperCase().equals(text)) { |
| 112 | text = text.toLowerCase(); |
| 113 | sep = "-_.~!$/ "; |
| 114 | } |
| 115 | StringBuilder ret = new StringBuilder(text); |
| 116 | for (int i = 0; i < ret.length(); i++) { |
| 117 | char ch = ret.charAt(i); |
| 118 | if (sep.indexOf(ch) >= 0) { |
| 119 | ret.delete(i, i + 1); |
| 120 | if (i + 1 < ret.length()) |
| 121 | ret.setCharAt(i, Character.toUpperCase(ret.charAt(i))); |
| 122 | } |
| 123 | } |
| 124 | return ret.toString(); |
| 125 | } |
| 126 | |
| 127 | public static String toUpperCase(@NotNull String text) { |
| 128 | if (text.length() == 0) |
| 129 | return text; |
| 130 | StringBuilder ret = new StringBuilder(text.length()); |
| 131 | char ch0 = text.charAt(0); |
| 132 | ret.append(Character.toUpperCase(ch0)); |
| 133 | boolean wasLower = Character.isLowerCase(ch0); |
| 134 | for (int i = 1; i < text.length(); i++) { |
| 135 | char ch = text.charAt(i); |
| 136 | |
| 137 | if (Character.isUpperCase(ch)) { |
| 138 | if (wasLower) ret.append('_'); |
| 139 | ret.append(ch); |
| 140 | wasLower = false; |
| 141 | } else { |
| 142 | ret.append(Character.toUpperCase(ch)); |
| 143 | wasLower = Character.isLowerCase(ch); |
| 144 | } |
| 145 | } |
| 146 | return ret.toString(); |
| 147 | } |
| 148 | |
| 149 | @NotNull public static String toBase64(long l) { |
| 150 | StringBuilder ret = new StringBuilder(); |
| 151 | if (l < 0) { |
| 152 | ret.append('-'); |
| 153 | l = -l; |
| 154 | } |
| 155 | if (l == 0) return "A"; |
| 156 | int start = ret.length(); |
| 157 | while (l > 0) { |
| 158 | ret.insert(start, BASE64.charAt((int) (l & 63))); |
| 159 | l >>= 6; |
| 160 | } |
| 161 | return ret.toString(); |
| 162 | } |
| 163 | |
| 164 | private static CharSequence toHex(int num, int len) { |
| 165 | StringBuilder sb = new StringBuilder(len); |
| 166 | sb.append(Integer.toHexString(num)); |
| 167 | while (sb.length() < len) |
| 168 | sb.insert(0, '0'); |
| 169 | return sb; |
| 170 | } |
| 171 | |
| 172 | public static String truncate(Object obj) { |
| 173 | if (obj == null) //noinspection ConstantConditions |
| 174 | return null; |
| 175 | String text = obj.toString(); |
| 176 | if (text.length() > 256) |
| 177 | return text.substring(0, 256) + "..."; |
| 178 | return text; |
| 179 | } |
| 180 | |
| 181 | @NotNull public static <T> String asStringByLine(String description, @NotNull T dataValue) { |
| 182 | Map<String, Object> map = DataValueClass.asMap(dataValue); |
| 183 | StringBuilder ret = new StringBuilder(); |
| 184 | if (description != null) |
| 185 | ret.append(description).append(NEW_LINE); |
| 186 | for (Map.Entry<String, Object> entry : map.entrySet()) { |
| 187 | Object value = entry.getValue(); |
| 188 | String valueStr; |
| 189 | if (value instanceof String) { |
| 190 | valueStr = (String) value; |
| 191 | } else { |
| 192 | valueStr = String.valueOf(value); |
| 193 | if (valueStr.contains("@")) |
| 194 | valueStr = DataValueClass.asMap(value).toString(); |
| 195 | } |
| 196 | ret.append(entry.getKey()).append('=').append(truncate(valueStr)).append(NEW_LINE); |
| 197 | } |
| 198 | return ret.toString(); |
| 199 | } |
| 200 | } |