org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/Util.java
changeset 439 57fff6202b74
parent 50 0560e98b9bf6
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/Util.java	Fri Jul 09 17:49:55 2010 -0700
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/Util.java	Mon Jul 12 18:29:46 2010 -0700
@@ -24,146 +24,163 @@
 
 public class Util {
 
-	public static String removeSpaces(String widgetName) {
-		return widgetName != null ? widgetName.replace(" ", "") : null;
-	}
+    public static String removeSpaces(String widgetName) {
+        return widgetName != null ? widgetName.replace(" ", "") : "";
+    }
+
+    public static String removeNonAlphaNum(String projectName) {
+        return projectName != null ? projectName
+                .replaceAll("[^a-zA-Z0-9 ]", "") : null;
+    }
+
+    public static void logEvent(Logger log, Level level, Throwable throwable) {
+        if (level == Level.SEVERE) {
+            log.severe(throwable.getLocalizedMessage());
+            if (throwable.getCause() != null) {
+                log.severe(throwable.getCause().toString());
+            }
+            log.severe(throwable.getStackTrace().toString());
+        }
+        if (level == Level.WARNING) {
+            log.warning(throwable.getLocalizedMessage());
+            if (throwable.getCause() != null) {
+                log.warning(throwable.getCause().toString());
+            }
+            log.warning(throwable.getStackTrace().toString());
+        }
+        if (level == Level.INFO) {
+            log.info(throwable.getLocalizedMessage());
+            if (throwable.getCause() != null) {
+                log.info(throwable.getCause().toString());
+            }
+            log.info(throwable.getStackTrace().toString());
+        }
+
+    }
 
-	public static String removeNonAlphaNum(String projectName) {
-		return projectName != null ? projectName.replaceAll("[^a-zA-Z0-9 ]", "") : null;
-	}
-public static void logEvent(Logger log, Level level, Throwable throwable)
-	{
-		if (level==Level.SEVERE)
-		{
-			log.severe(throwable.getLocalizedMessage());
-			if(throwable.getCause()!=null)
-			log.severe(throwable.getCause().toString());
-			log.severe(throwable.getStackTrace().toString());
-		}
-		if (level==Level.WARNING)
-		{
-			log.warning(throwable.getLocalizedMessage());
-			if(throwable.getCause()!=null)
-			log.warning(throwable.getCause().toString());
-			log.warning(throwable.getStackTrace().toString());
-		}
-		if (level==Level.INFO)
-		{
-			log.info(throwable.getLocalizedMessage());
-			if(throwable.getCause()!=null)
-			log.info(throwable.getCause().toString());
-			log.info(throwable.getStackTrace().toString());
-		}
-		
-	}
-	
-	public static String replaceChar(String input, char asciiOutChar, char asciiInChar){
-		char x;
-		int ascii ;
-		String outString="";
-		int outCharAscii= (asciiOutChar > 127) ? '?' : (char)(asciiOutChar & 0x7F);
-		int inCharAscii= (asciiInChar > 127) ? '?' : (char)(asciiInChar & 0x7F);
-		
-		for (int i = 0; i < input.length(); i++) {
-			 x = input.charAt(i);	
-			 ascii = (x > 127) ? '?' : (char)(x & 0x7F);
-			 
-			 if(ascii==outCharAscii){
-				 outString=outString+asciiInChar;
-			 }else{
-				 outString=outString+x; 
-			 }
-		
-		}
-		return outString;
-	}
-	
-	  public static void showData(String s) {
-		System.out.println(s);
-	}
-	  
-	  public static void showData(List<String> listString, String header) {
-		 
-		  if(listString!=null&&listString.size()>0) {
-			  System.out.println("--------"+header+"------");
-		  for(String s:listString){
-			System.out.println(s);
-			}
-		  }else {
-			  System.out.println("--------Empty/Null "+header+"------");
-		  }
-		}
+    public static String replaceChar(String input, char asciiOutChar,
+            char asciiInChar) {
+        char x;
+        int ascii;
+        String outString = "";
+        int outCharAscii = (asciiOutChar > 127) ? '?'
+                : (char) (asciiOutChar & 0x7F);
+
+        for (int i = 0; i < input.length(); i++) {
+            x = input.charAt(i);
+            ascii = (x > 127) ? '?' : (char) (x & 0x7F);
+
+            if (ascii == outCharAscii) {
+                outString = outString + asciiInChar;
+            } else {
+                outString = outString + x;
+            }
+
+        }
+        return outString;
+    }
 
+    public static void showData(String s) {
+        System.out.println(s);
+    }
+
+    public static void showData(List<String> listString, String header) {
+
+        if (listString != null && listString.size() > 0) {
+            System.out.println("--------" + header + "------");
+            for (String s : listString) {
+                System.out.println(s);
+            }
+        } else {
+            System.out.println("--------Empty/Null " + header + "------");
+        }
+    }
+
+    /* Validation tests for both Windows & Mac OS */
+    private static String commonValidate(String argName) {
+        if (argName.length() == 0) {
+            return ("Can not be empty");
+        }
 
-		@SuppressWarnings("restriction")
-		/* Validation tests for both Windows & Mac OS */
-		private static String commonValidate(String argName)
-		{
-			if (argName.length() == 0 ) {
-				return ("Can not be empty");	    		 
-			}
-			
-        	// filenames starting with dot are not valid for both Widget name & UID
-	    	if ( argName.charAt(0) == '.' ) {
-	    		return("Can not begin with a dot");
-	    	}
+        // filenames starting with dot are not valid for both Widget name & UID
+        if (argName.charAt(0) == '.') {
+            return ("Can not begin with a dot");
+        }
+
+        final char lastChar = argName.charAt(argName.length() - 1);
+        // filenames ending in dot are not valid for both Widget name & UID
+        if (lastChar == '.') {
+            return ("Can not end with dot");
+        }
+
+        return null;
+    }
 
-	    	final char lastChar = argName.charAt(argName.length()-1);
-			// filenames ending in dot are not valid for both Widget name & UID
-			if (lastChar == '.') {
-				return("Can not end with dot");
-			}
-			
-			return null;
-		}
-
+    public static String validateWidgetName(String widgetName) {
+        String strError = null;
+        if ((strError = commonValidate(widgetName)) != null) {
+            return "Invalid Widget name. " + strError;
+        }
+        if (widgetName.indexOf("<") > -1 || widgetName.indexOf(">") > -1) {
+            return ("Invalid Widget name. Angle brackets are not allowed");
+        }
+        final char lastChar = widgetName.charAt(widgetName.length() - 1);
+        // trailing or beginning space is not valid in filenames for Widget name
+        if ((Character.isWhitespace(widgetName.charAt(0)) || Character
+                .isWhitespace(lastChar))) {
+            return ("Invalid Widget name. Beginning or trailing spaces are not allowed");
+        }
 
-		public static String validateWidgetName(String widgetName){    	
-			String strError = null;			
-			if ((strError = commonValidate(widgetName)) != null)
-				return "Invalid Widget name. " + strError;			
-			if (widgetName.indexOf("<") > -1 || widgetName.indexOf(">") > -1){
-        		return("Invalid Widget name. Angle brackets are not allowed");
-        	}
-			final char lastChar = widgetName.charAt(widgetName.length()-1);
-			// trailing or beginning space is not valid in filenames for Widget name
-			if ((Character.isWhitespace(widgetName.charAt(0)) || Character.isWhitespace(lastChar))) {
-				return("Invalid Widget name. Beginning or trailing spaces are not allowed");
-			}
+        if (widgetName.indexOf('\n') != -1 || widgetName.indexOf('\t') != -1) {
+            return ("Invalid Widget name. newline character is not allowed");
+        }
+
+        return null;
+    }
 
-			if (widgetName.indexOf('\n') != -1 || widgetName.indexOf('\t') != -1 ) {
-				return("Invalid Widget name. newline character is not allowed");
-			}
-			
-	        return null;
-		}
+    public static String validateWidgetID(String widgetID) {
+        String strError = null;
+        if ((strError = commonValidate(widgetID)) != null) {
+            return "Invalid Widget identifier. " + strError;
+        }
 
-		public static String validateWidgetID(String widgetID)
-		{
-			String strError = null; 			
-			if ((strError = commonValidate(widgetID)) != null)
-				return "Invalid Widget identifier. " + strError;
+        // file names with white spaces are not allowed for Widget Identifier
+        // (UID)
+        if (widgetID.indexOf(" ") > -1) {
+            return ("Invalid Widget identifier. Whitespaces are not allowed");
+        }
+
+        if (widgetID.length() > 78) {
+            return ("Invalid Widget identifier. Maximum string length exceeded");
+        }
 
-			// file names with white spaces are not allowed for Widget Identifier (UID)
-        	if (widgetID.indexOf(" ") > -1 ){
-        		return("Invalid Widget identifier. Whitespaces are not allowed");
-        	}
-        	
-        	if (widgetID.length() > 78 ) {
-        		return("Invalid Widget identifier. Maximum string length exceeded");
-        	}
-        	
-        	/* test invalid characters, allows only alphanumeric and '.' for UID*/
-			String alphnum = ".0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
-			for (int i = 0; i < widgetID.length(); i++)
-				if (alphnum.indexOf(widgetID.charAt(i),0) == -1) {
-					return("Invalid Widget identifier. Only alphanumeric or '.' is allowed");
-				}
+        /* test invalid characters, allows only alphanumeric and '.' for UID */
+        String alphnum = ".0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+        for (int i = 0; i < widgetID.length(); i++) {
+            if (alphnum.indexOf(widgetID.charAt(i), 0) == -1) {
+                return ("Invalid Widget identifier. Only alphanumeric or '.' is allowed");
+            }
+        }
+
+        if (widgetID.matches(".*[.]{2,}.*")) {
+            return ("Invalid Widget identifier. Consecutive dots are not allowed");
+        }
+
+        return null;
+    }
 
-			if (widgetID.matches(".*[.]{2,}.*")) {				
-				return("Invalid Widget identifier. Consecutive dots are not allowed");				
-			}	        
-
-			return null;
-		}
+    public static String toProjectName(String widgetId) {
+        if (widgetId == null || widgetId.trim().length() == 0) {
+            return "";
+        }
+        StringBuilder builder = new StringBuilder();
+        for (char c : widgetId.trim().toCharArray()) {
+            if (Character.isJavaIdentifierPart(c)) {
+                builder.append(c);
+            } else {
+                builder.append("_");
+            }
+        }
+        return builder.toString();
+    }
 }