📄 javautils.java
字号:
} } // getImageFromStream /** * These are java keywords as specified at the following URL (sorted alphabetically). * http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#229308 * Note that false, true, and null are not strictly keywords; they are literal values, * but for the purposes of this array, they can be treated as literals. * ****** PLEASE KEEP THIS LIST SORTED IN ASCENDING ORDER ****** */ static final String keywords[] = { "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while" }; /** Collator for comparing the strings */ static final Collator englishCollator = Collator.getInstance(Locale.ENGLISH); /** Use this character as suffix */ static final char keywordPrefix = '_'; /** * isJavaId * Returns true if the name is a valid java identifier. * @param id to check * @return boolean true/false **/ public static boolean isJavaId(String id) { if (id == null || id.equals("") || isJavaKeyword(id)) return false; if (!Character.isJavaIdentifierStart(id.charAt(0))) return false; for (int i=1; i<id.length(); i++) if (!Character.isJavaIdentifierPart(id.charAt(i))) return false; return true; } /** * checks if the input string is a valid java keyword. * @return boolean true/false */ public static boolean isJavaKeyword(String keyword) { return (Arrays.binarySearch(keywords, keyword, englishCollator) >= 0); } /** * Turn a java keyword string into a non-Java keyword string. (Right now * this simply means appending an underscore.) */ public static String makeNonJavaKeyword(String keyword){ return keywordPrefix + keyword; } /** * Converts text of the form * Foo[] to the proper class name for loading [LFoo */ public static String getLoadableClassName(String text) { if (text == null || text.indexOf("[") < 0 || text.charAt(0) == '[') return text; String className = text.substring(0,text.indexOf("[")); if (className.equals("byte")) className = "B"; else if (className.equals("char")) className = "C"; else if (className.equals("double")) className = "D"; else if (className.equals("float")) className = "F"; else if (className.equals("int")) className = "I"; else if (className.equals("long")) className = "J"; else if (className.equals("short")) className = "S"; else if (className.equals("boolean")) className = "Z"; else className = "L" + className + ";"; int i = text.indexOf("]"); while (i > 0) { className = "[" + className; i = text.indexOf("]", i+1); } return className; } /** * Converts text of the form * [LFoo to the Foo[] */ public static String getTextClassName(String text) { if (text == null || text.indexOf("[") != 0) return text; String className = ""; int index = 0; while(index < text.length() && text.charAt(index) == '[') { index ++; className += "[]"; } if (index < text.length()) { if (text.charAt(index)== 'B') className = "byte" + className; else if (text.charAt(index) == 'C') className = "char" + className; else if (text.charAt(index) == 'D') className = "double" + className; else if (text.charAt(index) == 'F') className = "float" + className; else if (text.charAt(index) == 'I') className = "int" + className; else if (text.charAt(index) == 'J') className = "long" + className; else if (text.charAt(index) == 'S') className = "short" + className; else if (text.charAt(index) == 'Z') className = "boolean" + className; else { className = text.substring(index+1, text.indexOf(";")) + className; } } return className; } /** * Map an XML name to a Java identifier per * the mapping rules of JSR 101 (in version 1.0 this is * "Chapter 20: Appendix: Mapping of XML Names" * * @param name is the xml name * @return the java name per JSR 101 specification */ public static String xmlNameToJava(String name) { // protect ourselves from garbage if (name == null || name.equals("")) return name; char[] nameArray = name.toCharArray(); int nameLen = name.length(); StringBuffer result = new StringBuffer(nameLen); boolean wordStart = false; // The mapping indicates to convert first character. int i = 0; while (i < nameLen && (isPunctuation(nameArray[i]) || !Character.isJavaIdentifierStart(nameArray[i]))) { i++; } if (i < nameLen) { // Decapitalization code used to be here, but we use the // Introspector function now after we filter out all bad chars. result.append(nameArray[i]); //wordStart = !Character.isLetter(nameArray[i]); wordStart = !Character.isLetter(nameArray[i]) && nameArray[i] != "_".charAt(0); } else { // The identifier cannot be mapped strictly according to // JSR 101 if (Character.isJavaIdentifierPart(nameArray[0])) { result.append("_" + nameArray[0]); } else { // The XML identifier does not contain any characters // we can map to Java. Using the length of the string // will make it somewhat unique. result.append("_" + nameArray.length); } } // The mapping indicates to skip over // all characters that are not letters or // digits. The first letter/digit // following a skipped character is // upper-cased. for (++i; i < nameLen; ++i) { char c = nameArray[i]; // if this is a bad char, skip it and remember to capitalize next // good character we encounter if (isPunctuation(c) || !Character.isJavaIdentifierPart(c)) { wordStart = true; continue; } if (wordStart && Character.isLowerCase(c)) { result.append(Character.toUpperCase(c)); } else { result.append(c); } // If c is not a character, but is a legal Java // identifier character, capitalize the next character. // For example: "22hi" becomes "22Hi" //wordStart = !Character.isLetter(c); wordStart = !Character.isLetter(c) && c != "_".charAt(0); } // covert back to a String String newName = result.toString(); // Follow JavaBean rules, but we need to check if the first // letter is uppercase first if (Character.isUpperCase(newName.charAt(0))) newName = Introspector.decapitalize(newName); // check for Java keywords if (isJavaKeyword(newName)) newName = makeNonJavaKeyword(newName); return newName; } // xmlNameToJava /** * Is this an XML punctuation character? */ private static boolean isPunctuation(char c) { return '-' == c || '.' == c || ':' == c || '\u00B7' == c || '\u0387' == c || '\u06DD' == c || '\u06DE' == c; } // isPunctuation /** * replace: * Like String.replace except that the old new items are strings. * * @param name string * @param oldT old text to replace * @param newT new text to use * @return replacement string **/ public static final String replace (String name, String oldT, String newT) { if (name == null) return ""; // Create a string buffer that is twice initial length. // This is a good starting point. StringBuffer sb = new StringBuffer(name.length()* 2); int len = oldT.length (); try { int start = 0; int i = name.indexOf (oldT, start); while (i >= 0) { sb.append(name.substring(start, i)); sb.append(newT); start = i+len; i = name.indexOf(oldT, start); } if (start < name.length()) sb.append(name.substring(start)); } catch (NullPointerException e) { } return new String(sb); } /** * Determines if the Class is a Holder class. If so returns Class of held type * else returns null * @param type the suspected Holder Class * @return class of held type or null */ public static Class getHolderValueType(Class type) { if (type != null) { Class[] intf = type.getInterfaces(); boolean isHolder = false; for (int i=0; i<intf.length && !isHolder; i++) { if (intf[i] == javax.xml.rpc.holders.Holder.class) { isHolder = true; } } if (isHolder == false) { return null; } // Holder is supposed to have a public value field. java.lang.reflect.Field field; try { field = type.getField("value"); } catch (Exception e) { field = null; } if (field != null) { return field.getType(); } } return null; } /** * Gets the Holder value. * @param holder Holder object * @return value object */ public static Object getHolderValue(Object holder) throws HolderException { if (!(holder instanceof javax.xml.rpc.holders.Holder)) { throw new HolderException(Messages.getMessage("badHolder00")); } try { Field valueField = holder.getClass().getField("value"); return valueField.get(holder); } catch (Exception e) { throw new HolderException(Messages.getMessage("exception01", e.getMessage())); } } /** * Sets the Holder value. * @param holder Holder object * @param value is the object value */ public static void setHolderValue(Object holder, Object value) throws HolderException { if (!(holder instanceof javax.xml.rpc.holders.Holder)) { throw new HolderException(Messages.getMessage("badHolder00")); } try { Field valueField = holder.getClass().getField("value"); if (valueField.getType().isPrimitive()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -