001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.upgrade.systemoptions;
043:
044: import java.util.Iterator;
045: import java.util.List;
046: import org.netbeans.upgrade.systemoptions.SerParser.ArrayWrapper;
047: import org.netbeans.upgrade.systemoptions.SerParser.NameValue;
048: import org.netbeans.upgrade.systemoptions.SerParser.ObjectWrapper;
049:
050: /**
051: *
052: * @author rmatous
053: */
054: final class Utils {
055:
056: /** Creates a new instance of Utils */
057: private Utils() {
058: }
059:
060: static String valueFromObjectWrapper(final Object value) {
061: String stringvalue = null;
062: if (value instanceof ObjectWrapper) {
063: List l = ((SerParser.ObjectWrapper) value).data;
064: if (l.size() == 1) {
065: Object o = l.get(0);
066: if (o instanceof NameValue) {
067: Object key = null;
068: stringvalue = ((NameValue) o).value.toString();
069: }
070: }
071: if (stringvalue == null) {
072: stringvalue = ((ObjectWrapper) value).classdesc.name;
073: }
074: } else if (value instanceof String && !"null".equals(value)) {
075: stringvalue = value.toString();
076:
077: } else if (value instanceof SerParser.ArrayWrapper
078: && "[Ljava.lang.String;"
079: .equals(((SerParser.ArrayWrapper) value).classdesc.name)) {
080: StringBuffer sb = new StringBuffer();
081: List es = ((SerParser.ArrayWrapper) value).values;
082: for (Iterator it = es.iterator(); it.hasNext();) {
083: sb.append((String) it.next());
084: if (it.hasNext()) {
085: sb.append(" , ");
086: }
087: }
088: stringvalue = sb.toString();
089: } else if (value instanceof SerParser.ArrayWrapper
090: && "[[Ljava.lang.String;"
091: .equals(((SerParser.ArrayWrapper) value).classdesc.name)) {
092: StringBuffer sb = new StringBuffer();
093: List awl = ((SerParser.ArrayWrapper) value).values;
094: for (Iterator it = awl.iterator(); it.hasNext();) {
095: SerParser.ArrayWrapper aw = (SerParser.ArrayWrapper) it
096: .next();
097: sb.append(valueFromObjectWrapper(aw));
098: if (it.hasNext()) {
099: sb.append(" | ");
100: }
101: }
102: stringvalue = sb.toString();
103: } else {
104: stringvalue = "unknown";//value.toString();
105: }
106: return stringvalue;
107: }
108:
109: static String getClassNameFromObject(final Object value) {
110: String clsName = null;
111: if (value instanceof ObjectWrapper) {
112: clsName = prettify(((ObjectWrapper) value).classdesc.name);
113: } else if (value instanceof ArrayWrapper) {
114: clsName = prettify(((ArrayWrapper) value).classdesc.name);
115: } else {
116: clsName = prettify(value.getClass().getName());
117: }
118: return clsName;
119: }
120:
121: static String prettify(String type) {
122: if (type.equals("B")) { // NOI18N
123: return "byte"; // NOI18N
124: } else if (type.equals("S")) { // NOI18N
125: return "short"; // NOI18N
126: } else if (type.equals("I")) { // NOI18N
127: return "int"; // NOI18N
128: } else if (type.equals("J")) { // NOI18N
129: return "long"; // NOI18N
130: } else if (type.equals("F")) { // NOI18N
131: return "float"; // NOI18N
132: } else if (type.equals("D")) { // NOI18N
133: return "double"; // NOI18N
134: } else if (type.equals("C")) { // NOI18N
135: return "char"; // NOI18N
136: } else if (type.equals("Z")) { // NOI18N
137: return "boolean"; // NOI18N
138: } else if (type.startsWith("L") && type.endsWith(";")) { // NOI18N
139: String fqn = type.substring(1, type.length() - 1).replace(
140: '/', '.').replace('$', '.'); // NOI18N
141: return fqn;
142: }
143: if (!type.startsWith("[")) {
144: if (type.startsWith("L")) {
145: return type.substring(1);
146: }
147: if (type.endsWith(";")) {
148: return type.substring(0, type.length() - 1);
149: }
150: }
151: return type;
152: }
153: }
|