1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.universAAL.middleware.container.utils;
21
22 import java.net.InetAddress;
23 import java.util.Random;
24
25
26
27
28
29
30
31
32 public class StringUtils {
33
34
35 private static int counter = 0;
36
37
38 private static final String UUID_prefix;
39
40 static {
41 String aux = "_:";
42 String peerID = System.getProperty("sodapop.peerID");
43 if (peerID != null) {
44 aux = peerID
45 + '+'
46 + Integer
47 .toHexString(new Random(System.currentTimeMillis())
48 .nextInt());
49 } else {
50 try {
51 byte[] ownIP = InetAddress.getLocalHost().getAddress();
52 int val;
53 for (int i = 0; i < ownIP.length; i++) {
54 val = ownIP[i] & 0xFF;
55 aux += (val < 16 ? "0" : "") + Integer.toHexString(val);
56 }
57 } catch (Exception e) {
58 }
59 aux += Integer.toHexString(new Random(System.currentTimeMillis())
60 .nextInt());
61 }
62 UUID_prefix = aux + ":";
63 }
64
65
66 public static String createUniqueID() {
67 return UUID_prefix + Integer.toHexString(counter++);
68 }
69
70
71
72
73
74 public static boolean areEqualPropPaths(String[] pp1, String[] pp2) {
75 if (pp1 == pp2)
76 return true;
77
78 if (pp1 == null || pp2 == null || pp1.length != pp2.length)
79 return false;
80
81 for (int i = pp1.length - 1; i > -1; i--)
82 if (pp1[i] == null || !pp1[i].equals(pp2[i]))
83 return false;
84
85 return true;
86 }
87
88
89
90
91
92
93
94
95 public static String deriveLabel(String arg) {
96 if (arg == null)
97 return null;
98
99
100
101 if (isQualifiedName(arg))
102 arg = arg.substring(arg.lastIndexOf('#') + 1);
103
104 if (arg.length() == 0)
105 return null;
106
107 StringBuffer sb = new StringBuffer(arg.length() + 10);
108 int i = 0;
109 int wordStatus = 0;
110 while (i < arg.length()) {
111 char c = arg.charAt(i++);
112 if (Character.isLetter(c)) {
113 switch (wordStatus) {
114 case 0:
115 sb.append(Character.toUpperCase(c));
116 wordStatus = 1;
117 break;
118 case 1:
119 if (Character.isUpperCase(c))
120 sb.append(' ');
121 sb.append(c);
122 break;
123 case 2:
124 sb.append(' ').append(Character.toUpperCase(c));
125 wordStatus = 1;
126 break;
127 }
128 } else if (isQuote(c)) {
129 sb.append(c);
130 } else if (c == ' ' || c == '_') {
131 sb.append(' ');
132 wordStatus = 0;
133 } else {
134 if (wordStatus == 1)
135 sb.append(' ');
136 sb.append(c);
137 wordStatus = 2;
138 }
139 }
140 return sb.toString();
141 }
142
143
144 public static boolean isQuote(char c) {
145 return "\"'`´".indexOf(c) > -1;
146 }
147
148
149 public static boolean isDigit(char c) {
150 return c >= '0' && c <= '9';
151 }
152
153
154 public static boolean isAsciiLetter(char c) {
155 return (c >= 'A' && c <= 'Z') || (c <= 'z' && c >= 'a');
156 }
157
158
159 public static boolean isNullOrEmpty(String arg) {
160 return arg == null || arg.equals("");
161 }
162
163
164 public static boolean isNonEmpty(String arg) {
165 return arg != null && !arg.equals("");
166 }
167
168
169
170
171
172
173
174
175
176
177
178 public static boolean isQualifiedName(String uri) {
179 if (!startsWithURIScheme(uri))
180 return false;
181
182 int i = uri.lastIndexOf('#');
183 return i > 0 && i < uri.length() - 1;
184
185 }
186
187
188
189
190
191
192
193
194
195
196
197 public static boolean startsWithURIScheme(String arg) {
198 if (arg == null || arg.length() == 0)
199 return false;
200
201 char c = arg.charAt(0);
202 int i = arg.indexOf(':');
203 if (i < 1 || !isAsciiLetter(c))
204 return false;
205
206 while (--i > 0) {
207 c = arg.charAt(i);
208 if (!isAsciiLetter(c) && !isDigit(c) && c != '+' && c != '-'
209 && c != '.')
210 return false;
211 }
212
213 return true;
214 }
215 }