| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.seasar.teeda.core.util; |
| 17 | |
|
| 18 | |
import java.io.UnsupportedEncodingException; |
| 19 | |
import java.util.Arrays; |
| 20 | |
import java.util.HashSet; |
| 21 | |
import java.util.Set; |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | 0 | public class JavaScriptUtil { |
| 27 | |
|
| 28 | |
public static String getClearHiddenCommandFormParamsFunctionName( |
| 29 | |
String formName) { |
| 30 | 20 | return "clear_" + getValidJavascriptName(formName, false); |
| 31 | |
} |
| 32 | |
|
| 33 | 1 | private static final Set RESERVED_WORDS = new HashSet(Arrays |
| 34 | |
.asList(new String[] { "abstract", "boolean", "break", "byte", |
| 35 | |
"case", "catch", "char", "class", "const", "continue", |
| 36 | |
"default", "delete", "do", "double", "else", "enum", |
| 37 | |
"export", "extends", "false", "final", "finally", "float", |
| 38 | |
"for", "function", "goto", "if", "implements", "in", |
| 39 | |
"instanceof", "int", "long", "native", "new", "null", |
| 40 | |
"package", "private", "protected", "public", "return", |
| 41 | |
"short", "static", "super", "switch", "synchronized", |
| 42 | |
"this", "throw", "throws", "transient", "true", "try", |
| 43 | |
"typeof", "var", "void", "while", "with" })); |
| 44 | |
|
| 45 | |
private static String getValidJavascriptName(String s, |
| 46 | |
boolean checkForReservedWord) { |
| 47 | 20 | if (checkForReservedWord && RESERVED_WORDS.contains(s)) { |
| 48 | 0 | return s + "_"; |
| 49 | |
} |
| 50 | |
|
| 51 | 20 | StringBuffer buf = null; |
| 52 | 88 | for (int i = 0, len = s.length(); i < len; i++) { |
| 53 | 68 | char c = s.charAt(i); |
| 54 | 68 | if (Character.isLetterOrDigit(c)) { |
| 55 | 66 | if (buf != null) |
| 56 | 6 | buf.append(c); |
| 57 | |
} else { |
| 58 | 2 | if (buf == null) { |
| 59 | 2 | buf = new StringBuffer(s.length() + 10); |
| 60 | 2 | buf.append(s.substring(0, i)); |
| 61 | |
} |
| 62 | |
|
| 63 | 2 | buf.append('_'); |
| 64 | 2 | if (c < 16) { |
| 65 | 0 | buf.append('0'); |
| 66 | |
} |
| 67 | |
|
| 68 | 2 | if (c < 128) { |
| 69 | 2 | buf.append(Integer.toHexString(c).toUpperCase()); |
| 70 | |
} else { |
| 71 | |
byte[] bytes; |
| 72 | |
try { |
| 73 | 0 | bytes = Character.toString(c).getBytes("UTF-8"); |
| 74 | 0 | } catch (UnsupportedEncodingException e) { |
| 75 | 0 | throw new RuntimeException(e); |
| 76 | 0 | } |
| 77 | |
|
| 78 | 0 | for (int j = 0; j < bytes.length; j++) { |
| 79 | 0 | int intVal = bytes[j]; |
| 80 | 0 | if (intVal < 0) { |
| 81 | 0 | intVal = 256 + intVal; |
| 82 | 0 | } else if (intVal < 16) { |
| 83 | 0 | buf.append('0'); |
| 84 | |
} |
| 85 | 0 | buf.append(Integer.toHexString(intVal).toUpperCase()); |
| 86 | |
} |
| 87 | |
} |
| 88 | |
} |
| 89 | |
} |
| 90 | 20 | return buf == null ? s : buf.toString(); |
| 91 | |
} |
| 92 | |
|
| 93 | |
} |