1 17 package org.apache.ws.jaxme.js; 18 19 23 public class Util { 24 30 public static void checkJavaIdentifier(String pName) { 31 if (pName.length() == 0) { 32 throw new IllegalArgumentException ("A valid Java identifier must not be empty."); 33 } 34 char c = pName.charAt(0); 35 if (!Character.isJavaIdentifierStart(c)) { 36 throw new IllegalArgumentException ("The identifier " + pName + 37 " is no valid Java identifier, because its first character is " + c); 38 } 39 for (int i = 1; i < pName.length(); i++) { 40 if (!Character.isJavaIdentifierPart(c)) { 41 throw new IllegalArgumentException ("The identifier " + pName + 42 " is no valid Java identifier, because it contains the character " + c); 43 } 44 } 45 } 46 47 63 public static String asJavaIdentifier(String pIdentifier) { 64 if (pIdentifier == null || pIdentifier.length() == 0) { 65 throw new IllegalArgumentException ("A null or empty String cannot be converted into a valid Java identifier."); 66 } 67 StringBuffer sb = new StringBuffer (); 68 char c = pIdentifier.charAt(0); 69 sb.append(Character.isJavaIdentifierStart(c) ? c : '_'); 70 for (int i = 1; i < pIdentifier.length(); i++) { 71 c = pIdentifier.charAt(i); 72 sb.append(Character.isJavaIdentifierPart(c) ? c : '_'); 73 } 74 return sb.toString(); 75 } 76 } 77 | Popular Tags |