View Javadoc

1   /*	
2   	Copyright 2007-2014 Fraunhofer IGD, http://www.igd.fraunhofer.de
3   	Fraunhofer-Gesellschaft - Institute for Computer Graphics Research
4   	
5   	See the NOTICE file distributed with this work for additional 
6   	information regarding copyright ownership
7   	
8   	Licensed under the Apache License, Version 2.0 (the "License");
9   	you may not use this file except in compliance with the License.
10  	You may obtain a copy of the License at
11  	
12  	  http://www.apache.org/licenses/LICENSE-2.0
13  	
14  	Unless required by applicable law or agreed to in writing, software
15  	distributed under the License is distributed on an "AS IS" BASIS,
16  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  	See the License for the specific language governing permissions and
18  	limitations under the License.
19   */
20  package org.universAAL.middleware.container.utils;
21  
22  import java.net.InetAddress;
23  import java.util.Random;
24  
25  /**
26   * A set of utility methods for Strings.
27   * 
28   * @author mtazari - <a href="mailto:Saied.Tazari@igd.fraunhofer.de">Saied
29   *         Tazari</a>
30   * @author Carsten Stockloew
31   */
32  public class StringUtils {
33  
34      /** Internal counter for creating unique IDs. */
35      private static int counter = 0;
36  
37      /** The prefix for creating unique IDs. */
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      /** Create a unique ID. */
66      public static String createUniqueID() {
67  	return UUID_prefix + Integer.toHexString(counter++);
68      }
69  
70      /**
71       * Tests whether two property paths are equal, i.e. have the same length and
72       * all elements are equal.
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       * This method tries to derive a meaningful label from a given String. If
90       * the String is a qualified name (see {@link #isQualifiedName}), only the
91       * local part - the part after '#' - is taken. Multiple words starting with
92       * upper case letters and written in one word are separated (e.g. 'myName'
93       * is transformed to 'My Name').
94       */
95      public static String deriveLabel(String arg) {
96  	if (arg == null)
97  	    return null;
98  
99  	// if the arg has a structure like a qualified name, take just its local
100 	// part
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; // 0->start word, 1->within word, 2->undefined
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     /** Determines if the specified character is one of <code>" ' ` ´</code>. */
144     public static boolean isQuote(char c) {
145 	return "\"'`´".indexOf(c) > -1;
146     }
147 
148     /** Determines if the specified character is a digit [0-9]. */
149     public static boolean isDigit(char c) {
150 	return c >= '0' && c <= '9';
151     }
152 
153     /** Determines if the specified character is a letter [a-z,A-Z]. */
154     public static boolean isAsciiLetter(char c) {
155 	return (c >= 'A' && c <= 'Z') || (c <= 'z' && c >= 'a');
156     }
157 
158     /** Determines if the specified String is null or empty. */
159     public static boolean isNullOrEmpty(String arg) {
160 	return arg == null || arg.equals("");
161     }
162 
163     /** Determines if the specified String is not null and not empty. */
164     public static boolean isNonEmpty(String arg) {
165 	return arg != null && !arg.equals("");
166     }
167 
168     /**
169      * Determines if the specified URI String is a qualified name. The following
170      * conditions are checked:
171      * <ul>
172      * <li>the String starts with a URI scheme (see {@link #startsWithURIScheme}
173      * )</li>
174      * <li>the String contains the symbol '#'</li>
175      * <li>there is at least one character following the symbol '#'</li>
176      * </ul>
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; // at least one char is present
184 	// after '#'
185     }
186 
187     /**
188      * Determines if a prefix of the specified String is conform to an URI
189      * definition. The following conditions are checked:
190      * <ul>
191      * <li>the String starts with a letter ([a-z,A-Z])</li>
192      * <li>the String contains the symbol ':'</li>
193      * <li>all characters from the beginning to the symbol ':' are either a
194      * letter, a digit, or one of [+, -, .]</li>
195      * </ul>
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 }