KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > upgrade > systemoptions > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.upgrade.systemoptions;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.upgrade.systemoptions.SerParser.ArrayWrapper;
25 import org.netbeans.upgrade.systemoptions.SerParser.NameValue;
26 import org.netbeans.upgrade.systemoptions.SerParser.ObjectWrapper;
27
28 /**
29  *
30  * @author rmatous
31  */

32 final class Utils {
33     
34     /** Creates a new instance of Utils */
35     private Utils() {}
36
37     
38     static String JavaDoc valueFromObjectWrapper(final Object JavaDoc value) {
39         String JavaDoc stringvalue = null;
40         if (value instanceof ObjectWrapper) {
41             List JavaDoc l = ((SerParser.ObjectWrapper)value).data;
42             if (l.size() == 1) {
43                 Object JavaDoc o = l.get(0);
44                 if (o instanceof NameValue) {
45                     Object JavaDoc key = null;
46                     stringvalue = ((NameValue) o).value.toString();
47                 }
48             }
49             if (stringvalue == null) {
50                 stringvalue = ((ObjectWrapper) value).classdesc.name;
51             }
52         } else if (value instanceof String JavaDoc && !"null".equals(value)) {
53             stringvalue = value.toString();
54             
55         } else if (value instanceof SerParser.ArrayWrapper && "[Ljava.lang.String;".equals(((SerParser.ArrayWrapper)value).classdesc.name)) {
56             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
57             List JavaDoc es = ((SerParser.ArrayWrapper)value).values;
58             for (Iterator JavaDoc it = es.iterator(); it.hasNext();) {
59                 sb.append((String JavaDoc)it.next());
60                 if (it.hasNext()) {
61                     sb.append(" , ");
62                 }
63             }
64             stringvalue = sb.toString();
65         } else if (value instanceof SerParser.ArrayWrapper && "[[Ljava.lang.String;".equals(((SerParser.ArrayWrapper)value).classdesc.name)) {
66             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
67             List JavaDoc awl = ((SerParser.ArrayWrapper)value).values;
68             for (Iterator JavaDoc it = awl.iterator(); it.hasNext();) {
69                 SerParser.ArrayWrapper aw = (SerParser.ArrayWrapper)it.next();
70                 sb.append(valueFromObjectWrapper(aw));
71                 if (it.hasNext()) {
72                     sb.append(" | ");
73                 }
74             }
75             stringvalue = sb.toString();
76         } else {
77             stringvalue = "unknown";//value.toString();
78
}
79         return stringvalue;
80     }
81     
82     static String JavaDoc getClassNameFromObject(final Object JavaDoc value) {
83         String JavaDoc clsName = null;
84         if (value instanceof ObjectWrapper) {
85             clsName = prettify(((ObjectWrapper) value).classdesc.name);
86         } else if (value instanceof ArrayWrapper) {
87             clsName = prettify(((ArrayWrapper) value).classdesc.name);
88         } else {
89             clsName = prettify(value.getClass().getName());
90         }
91         return clsName;
92     }
93     
94     static String JavaDoc prettify(String JavaDoc type) {
95         if (type.equals("B")) { // NOI18N
96
return "byte"; // NOI18N
97
} else if (type.equals("S")) { // NOI18N
98
return "short"; // NOI18N
99
} else if (type.equals("I")) { // NOI18N
100
return "int"; // NOI18N
101
} else if (type.equals("J")) { // NOI18N
102
return "long"; // NOI18N
103
} else if (type.equals("F")) { // NOI18N
104
return "float"; // NOI18N
105
} else if (type.equals("D")) { // NOI18N
106
return "double"; // NOI18N
107
} else if (type.equals("C")) { // NOI18N
108
return "char"; // NOI18N
109
} else if (type.equals("Z")) { // NOI18N
110
return "boolean"; // NOI18N
111
} else if (type.startsWith("L") && type.endsWith(";")) { // NOI18N
112
String JavaDoc fqn = type.substring(1, type.length() - 1).replace('/', '.').replace('$', '.'); // NOI18N
113
return fqn;
114         }
115         if (!type.startsWith("[")) {
116             if (type.startsWith("L")) {
117                 return type.substring(1);
118             }
119             if (type.endsWith(";")) {
120                 return type.substring(0,type.length()-1);
121             }
122         }
123         return type;
124     }
125 }
126
Popular Tags