001: /*
002: * FieldMember.java Copyright (c) 2006,07 Swaroop Belur
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008:
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013:
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
017: *
018: */
019:
020: package net.sf.jdec.reflection;
021:
022: import net.sf.jdec.main.ConsoleLauncher;
023: import net.sf.jdec.ui.util.UIUtil;
024: import net.sf.jdec.config.Configuration;
025: import net.sf.jdec.util.Constants;
026: import net.sf.jdec.util.Util;
027:
028: public class FieldMember {
029:
030: private String name;
031: private String[] accessSpecifiers = null;
032: private Object value = null;
033: private String dataType = null;
034: private int dimension = -1;
035: private String ArrayType = null;
036:
037: public java.lang.String getUnParsedDataType() {
038: return dataType;
039: }
040:
041: public java.lang.String getName() {
042: return name;
043: }
044:
045: public void setName(String name) {
046: this .name = name;
047: }
048:
049: public String getAccessSpecifiers() {
050:
051: if (accessSpecifiers != null && accessSpecifiers.length > 0) {
052: StringBuffer temp = new StringBuffer();
053: temp.append("[");
054: for (int i = 0; i < accessSpecifiers.length; i++) {
055: temp.append(accessSpecifiers[i]);
056: if (i != accessSpecifiers.length)
057: temp.append("\t");
058: }
059: temp.append("]");
060: return temp.toString();
061: } else
062: return "";
063: //return "Default Package Access";
064:
065: }
066:
067: public void setAccessSpecifiers(String[] accessSpecifiers) {
068: this .accessSpecifiers = accessSpecifiers;
069: }
070:
071: public void setFieldValue(Object o) {
072: this .value = o;
073: }
074:
075: public Object getFieldValue() {
076: if (value != null) {
077: String nonascii = UIUtil.getUIUtil().getInterpretNonAscii();
078: //Collections
079: if (nonascii.equals("true")) {
080: StringBuffer tp = new StringBuffer("");
081: boolean sf = shouldValueBeFormattedForNonAscii(value
082: .toString(), tp);
083: if (sf) {
084: value = formatForUTF(value.toString(), tp);
085: String sv = value.toString();
086: if (value != null) {
087: sv = sv.trim();
088: boolean q = sv.startsWith("\"");
089:
090: if (!q) {
091: sv = "\"" + sv;
092: }
093: q = sv.endsWith("\"");
094: if (!q) {
095: sv = sv + "\"";
096: }
097:
098: return sv;
099: }
100: }
101: return value.toString();
102: } else {
103: String str1 = value.toString();
104: StringBuffer sbf1 = new StringBuffer("");
105: for (int z = 0; z < str1.length(); z++) {
106: char c1 = str1.charAt(z);
107: if (c1 >= 32 && c1 < 127) {
108: sbf1.append(c1);
109: } else {
110: sbf1.append("?");
111: }
112:
113: }
114: String v1 = sbf1.toString();
115: v1 = v1.trim();
116: boolean q = v1.startsWith("\"");
117:
118: if (!q) {
119: v1 = "\"" + v1;
120: }
121: q = v1.endsWith("\"");
122: if (!q) {
123: v1 = v1 + "\"";
124: }
125:
126: return v1;
127:
128: }
129:
130: } else
131: return null;
132: }
133:
134: public String getDataType() {
135: StringBuffer sb = new StringBuffer("");
136: if (dataType.startsWith("L") && dataType.endsWith(";")) {
137: dataType = dataType.substring(1);
138: dataType = dataType.substring(0, dataType.lastIndexOf(";"));
139: } else if (dataType.startsWith("L")) {
140: dataType = dataType.substring(1);
141: }
142: checkForImport(dataType, sb);
143: dataType = findDataType(sb.toString());
144: return dataType;
145: }
146:
147: /***
148: * NOTE: DO NOT USE FOR ARRAY TYPE
149: * INSTEAD :
150: * 1> CALL set setArrayType
151: * 2> Call setDimension
152: * @param dataType
153: */
154:
155: public void setDataType(String dataType) {
156: this .dataType = dataType;
157: }
158:
159: private String findDataType(String dataType) {
160:
161: java.lang.String org = dataType;
162: boolean dotfound = false;
163: if (org.indexOf(".") != -1) {
164: dotfound = true;
165: }
166: if (dataType.equals(Constants.ISINT)) {
167: return "int";
168: } else if (dataType.equals(Constants.ISBOOLEAN)) {
169: return "boolean";
170: } else if (dataType.equals(Constants.ISBYTE)) {
171: return "byte";
172: } else if (dataType.equals(Constants.ISCHAR)) {
173: return "char";
174: } else if (dataType.equals(Constants.ISDOUBLE)) {
175: return "double";
176: } else if (dataType.equals(Constants.ISFLOAT)) {
177: return "float";
178: } else if (dataType.equals(Constants.ISLONG)) {
179: return "long";
180: } else if (dataType.equals(Constants.ISSHORT)) {
181: return "short";
182: } else if (dataType.indexOf(Constants.ISARRAY) != -1) {
183: String temp = "";
184: for (int i = 0; i < dimension; i++) {
185: temp += "[]";
186: }
187: String arrayType = findArrayType(dataType);
188: String datatypedesc = findDataType(arrayType);
189: StringBuffer s3 = new StringBuffer("");
190: checkForImport(datatypedesc, s3);
191: return s3.toString() + "\t" + temp;
192: } else if (dataType.indexOf(Constants.ISREFERENCE) != -1
193: && dataType.indexOf("/") != -1
194: && dataType.indexOf("/") > dataType
195: .indexOf(Constants.ISREFERENCE)) {
196: String classType = dataType.substring(1);
197: int semicolon = classType.indexOf(";");
198: if (semicolon != -1) {
199: classType = classType.substring(0, semicolon);
200: }
201: classType = classType.replace('/', '.');
202: return classType;
203: } else if (dotfound) {
204: int semicolon = dataType.indexOf(";");
205: if (semicolon != -1) {
206: dataType = dataType.substring(0, semicolon);
207: }
208: dataType = dataType.replace('/', '.');
209: return dataType;
210: } else {
211: return dataType;//Constants.UNKNOWNTYPE;
212: }
213: } //BigInteger bg;
214:
215: public int getDimension() {
216: return dimension;
217: }
218:
219: public void setDimension(int dimension) {
220: this .dimension = dimension;
221: }
222:
223: public String getArrayType() {
224: return dataType;
225: }
226:
227: /***
228: * Example Argument [[Ljava/lang/String
229: * @param arrayType
230: */
231:
232: public void setArrayType(String arrayType) {
233: dataType = arrayType;
234: }
235:
236: private java.lang.String findArrayType(String dataType) {
237: /*int bracket = -1;
238: if(dataType.indexOf("[") == 0)
239: bracket = 1;
240: else
241: {
242: bracket = dataType.indexOf("[");
243: }
244: while(bracket!=-1)
245: {
246: dataType=dataType.substring(bracket+1);
247: }*/
248:
249: return dataType.substring(dataType.lastIndexOf("[") + 1);
250: }
251:
252: public String getUserFriendlyAccessSpecifiers() {
253:
254: if (accessSpecifiers != null && accessSpecifiers.length > 0) {
255: StringBuffer temp = new StringBuffer();
256:
257: for (int i = 0; i < accessSpecifiers.length; i++) {
258: temp.append(accessSpecifiers[i]);
259: if (i != accessSpecifiers.length)
260: temp.append(" ");
261: }
262:
263: return temp.toString();
264: } else
265: return "";
266: //return "Default Package Access";
267:
268: }
269:
270: private void checkForImport(java.lang.String input, StringBuffer sb) {
271:
272: if (input.indexOf(".") == -1 && input.indexOf("/") == -1) {
273: sb.append(input);
274: return;
275: }
276: if (Configuration.getShowImport().equalsIgnoreCase("false")) {
277:
278: sb.append(input);
279: return;
280: }
281: if (Configuration.getShowImport().equalsIgnoreCase("true")) {
282: java.lang.String simplename = "";
283: java.lang.String fullName = input;
284: int lastSlash = fullName.lastIndexOf("/");
285: if (lastSlash == -1) {
286: lastSlash = fullName.lastIndexOf(".");
287: }
288: if (lastSlash != -1) {
289: simplename = fullName.substring(lastSlash + 1);
290: } else
291: simplename = fullName;
292: fullName = fullName.replace('/', '.');
293: ConsoleLauncher.addImportClass(fullName);
294: sb.append(simplename);
295: return;
296:
297: }
298: // Default
299: sb.append(input);
300: return;
301:
302: }
303:
304: public static java.lang.String checkForUTF(char c) {
305: if (c >= 127)
306: return "UTF";
307: else if (c < 32)
308: return "octal";
309: return "ascii";
310: }
311:
312: private boolean shouldValueBeFormattedForNonAscii(String value,
313: StringBuffer tp) {
314:
315: if (this .dataType.equals("char")
316: || this .dataType.equals("String")) {
317: if (this .dataType.equals("char")) {
318:
319: } else {
320:
321: for (int k = 0; k < value.length(); k++) {
322: char c1 = value.charAt(k);
323: if (c1 < 32 || c1 >= 127) {
324: if (c1 != '\t' && c1 != '\n' && c1 != '\r'
325: && c1 != '\\' && c1 != '\"'
326: && c1 != '\'') {
327: if (c1 < 32)
328: tp.append("octal");
329: if (c1 > 127)
330: tp.append("UTF");
331: return true;
332: }
333: }
334:
335: }
336: }
337: }
338: return false;
339: }
340:
341: private java.lang.String formatForUTF(String value, StringBuffer tp) {
342: String s = value;
343: if (this .dataType.equals("char")
344: || this .dataType.equals("String")) {
345: String forcenonascii = UIUtil.getUIUtil()
346: .getForceNonAscii();
347: if (this .dataType.equals("char")) {
348: char c = (char) Integer.parseInt(value);
349: java.lang.String charType = "";
350: if (forcenonascii.equals("true")) {
351: charType = tp.toString();//checkForUTF(ch);
352: } else {
353: charType = checkForUTF(c);
354: }
355:
356: if (charType.equals("UTF")) {
357:
358: String hexrep = Integer.toHexString(c);
359: int length = hexrep.length();
360: int diff = 4 - length;
361: String temp = "";
362: for (int x = 1; x <= diff; x++) {
363: temp += "0";
364: }
365:
366: s = "\'" + "\\u" + temp + hexrep + "\'";
367:
368: } else if (charType.equals("octal")) {
369: String octrep = Integer.toOctalString(c);
370: int length = octrep.length();
371: int diff = 3 - length;
372: String temp = "";
373: for (int x = 1; x <= diff; x++) {
374: temp += "0";
375: }
376: s = temp + octrep;
377: } else {
378:
379: s = value;
380: }
381:
382: } else//String
383: {
384: StringBuffer sbf = new StringBuffer();
385: for (int z = 0; z < value.length(); z++) {
386: char ch = value.charAt(z);
387: String str = "";
388:
389: str = Util.interpretCharForUTF(ch, tp);
390: sbf.append(str);
391: }
392: //s="\""+sbf.toString()+"\"";
393: s = sbf.toString();
394: }
395: }
396: return s;
397: }
398:
399: }
|