001: /*
002: * Created on Aug 30, 2005
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package org.netbeans.modules.jdbcwizard.builder.util;
008:
009: import java.util.ArrayList;
010:
011: /**
012: * @author M.S.Veerendra
013: *
014: * TODO To change the template for this generated type comment go to
015: * Window - Preferences - Java - Code Style - Code Templates
016: */
017: public final class XMLCharUtil {
018:
019: public static final char[] specialChars = { '~', '!', '@', '#',
020: '$', '%', '^', '&', '*', '(', ')', '+', '|', '}', '{', '"',
021: ':', '?', '>', '<', '`', '-', '=', '\\', ']', '[', '\'',
022: ';', '/', '.', ',' };
023: public static final String[] replaceChars = { "_T_", "_E_", "_R_",
024: "_ASH_", "_D_", "_P_", "_C_", "_A_", "_S_", "_OP_", "_CL_",
025: "_PL_", "_OR_", "_CB_", "_OB_", "_DQ_", "_CLN_", "_Q_",
026: "_GT_", "_LT_", "_QO_", "_MI_", "_EQ_", "_BS_", "_BC_",
027: "_BO_", "_AP_", "_SCL_", "_FS_", "_DOT_", "_COM_" };
028: public static final String[] appendCols = { "_C", "_CN", "_FD",
029: "_FS", "_MF", "_M", "_MD", "_MR", "_Q", "_RS", "_RC",
030: "_RT", "_R", "_RSA", "_T", "_UC", "_W" };
031: public static final String[] reservedCols = { "Concurrency",
032: "CursorName", "FetchDirection", "FetchSize",
033: "MaxFiledSize", "MaxRows", "MetaData", "MoreResults",
034: "QueryTimeout", "ResultSet", "ResultSetConcurrency",
035: "ResultSetType", "Row", "RSAgent", "Type", "UpdateCount",
036: "Warnings" };
037:
038: public static String getReplacement(char c) {
039:
040: for (int i = 0; i < specialChars.length; i++) {
041: System.out
042: .println(specialChars[i] + " " + replaceChars[i]);
043: }
044:
045: for (int i = 0; i < specialChars.length; i++) {
046: if (specialChars[i] == c) {
047: return replaceChars[i];
048: }
049: }
050:
051: System.out.println(specialChars.length);
052: System.out.println(specialChars.length);
053:
054: return null;
055:
056: }
057:
058: public static String makeValidNCName(String name) {
059: StringBuffer nCName = new StringBuffer();
060: if (name == null)
061: name = "";
062:
063: name = name.trim();
064: int size = name.length();
065:
066: char ncChars[] = name.toCharArray();
067:
068: int i = 0;
069:
070: for (i = 0; i < size; i++) {
071:
072: char ch = ncChars[i];
073:
074: if (((i == 0)
075: && !(Character.isJavaIdentifierStart(ch) && (ch != '$')) && !Character
076: .isDigit(ch))
077: || ((i > 0) && !(Character.isJavaIdentifierPart(ch) && (ch != '$')))) {
078: String replace = getReplacement(ch);
079: if (replace != null) {
080: nCName.insert(nCName.length(), replace);
081:
082: } else {
083: nCName.insert(nCName.length(), "_Z_");
084:
085: }
086: } else {
087: nCName.append(ncChars[i]);
088: }
089:
090: }
091:
092: if ((i > 0) && Character.isDigit(nCName.charAt(0))) {
093: nCName.insert(0, "X_");
094: }
095: if ((i > 0) && nCName.charAt(0) == '_') {
096: nCName.insert(0, "X");
097: }
098:
099: return nCName.toString();
100:
101: }
102:
103: public static boolean isJavaName(String name) {
104: if (name == null) {
105: name = "";
106: }
107: name = name.trim();
108:
109: int size = name.length();
110: char ncChars[] = name.toCharArray();
111: int i = 0;
112:
113: for (i = 1; i < size; i++) {
114: char ch = ncChars[i];
115: char ch0 = ncChars[0];
116: if (!Character.isJavaIdentifierStart(ch0)
117: || Character.isDigit(ch0)
118: || !Character.isJavaIdentifierPart(ch)) {
119: return false;
120: }
121: }
122: return true;
123: }
124:
125: public static String validColName(String name) {
126:
127: StringBuffer nCName = new StringBuffer();
128: nCName = nCName.insert(0, name);
129: for (int k = 0; k < reservedCols.length; k++) {
130: String temp = reservedCols[k];
131: if (temp.equals(name)) {
132: nCName = nCName.insert(nCName.length(), appendCols[k]);
133: break;
134: }
135: }
136: return nCName.toString();
137: }
138:
139: }
|