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: */package org.netbeans.modules.vmd.midp.converter.wizard;
041:
042: import org.netbeans.modules.vmd.api.model.Debug;
043: import org.netbeans.modules.vmd.api.model.DesignComponent;
044: import org.netbeans.modules.vmd.api.model.PropertyValue;
045: import org.netbeans.modules.vmd.api.model.TypeID;
046: import org.netbeans.modules.vmd.midp.components.MidpTypes;
047:
048: import java.util.ArrayList;
049: import java.util.HashMap;
050:
051: /**
052: * @author David Kaspar
053: */
054: public class ConverterUtil {
055:
056: static Boolean getBoolean(String value) {
057: if (value == null)
058: return null;
059: value = decryptStringFromJavaCode(value);
060: return "true".equalsIgnoreCase(value); // NOI18N
061: }
062:
063: static Character getChar(String value) {
064: value = decryptStringFromJavaCode(value);
065: return value.length() > 0 ? value.charAt(0) : null;
066: }
067:
068: static Byte getByte(String value) {
069: if (value == null)
070: return null;
071: value = decryptStringFromJavaCode(value);
072: try {
073: return Byte.parseByte(value);
074: } catch (NumberFormatException e) {
075: Debug.warning(e);
076: return null;
077: }
078: }
079:
080: static Short getShort(String value) {
081: if (value == null)
082: return null;
083: value = decryptStringFromJavaCode(value);
084: try {
085: return Short.parseShort(value);
086: } catch (NumberFormatException e) {
087: Debug.warning(e);
088: return null;
089: }
090: }
091:
092: static Integer getInteger(String value) {
093: if (value == null)
094: return null;
095: value = decryptStringFromJavaCode(value);
096: try {
097: return Integer.parseInt(value);
098: } catch (NumberFormatException e) {
099: Debug.warning(e);
100: return null;
101: }
102: }
103:
104: static Long getLong(String value) {
105: if (value == null)
106: return null;
107: value = decryptStringFromJavaCode(value);
108: try {
109: return Long.parseLong(value);
110: } catch (NumberFormatException e) {
111: Debug.warning(e);
112: return null;
113: }
114: }
115:
116: static Float getFloat(String value) {
117: if (value == null)
118: return null;
119: value = decryptStringFromJavaCode(value);
120: try {
121: return Float.parseFloat(value);
122: } catch (NumberFormatException e) {
123: Debug.warning(e);
124: return null;
125: }
126: }
127:
128: static Double getDouble(String value) {
129: if (value == null)
130: return null;
131: value = decryptStringFromJavaCode(value);
132: try {
133: return Double.parseDouble(value);
134: } catch (NumberFormatException e) {
135: Debug.warning(e);
136: return null;
137: }
138: }
139:
140: static PropertyValue getStringWithUserCode(String value) {
141: if (value == null)
142: return null;
143: if (value.startsWith("CODE:")) // NOI18N
144: return PropertyValue
145: .createUserCode(decryptStringFromJavaCode(value
146: .substring(5)));
147: if (value.startsWith("STRING:")) // NOI18N
148: return MidpTypes
149: .createStringValue(decryptStringFromJavaCode(value
150: .substring(7)));
151: Debug.warning("Invalid string code value", value); // NOI18N
152: return null;
153: }
154:
155: // private static String getCodeValueClass (String value) {
156: // assert value.startsWith ("CODE-");
157: // return value.substring (5);
158: // }
159:
160: public static String decryptStringFromJavaCode(String value) {
161: if (value == null)
162: return null;
163: final int len = value.length();
164: StringBuffer sb = new StringBuffer();
165: int i = 0;
166: while (i < len) {
167: char c = value.charAt(i);
168: i++;
169: if (c != '\\') { // NOI18N
170: sb.append(c);
171: continue;
172: }
173: c = value.charAt(i);
174: i++;
175: switch (c) {
176: case 'r': // NOI18N
177: sb.append('\r'); // NOI18N
178: break;
179: case 'n': // NOI18N
180: sb.append('\n'); // NOI18N
181: break;
182: case 't': // NOI18N
183: sb.append('\t'); // NOI18N
184: break;
185: case 'u': // NOI18N
186: if (i + 4 > len) {
187: Debug.warning("Invalid hex number at the end",
188: value.substring(i)); // NOI18N
189: break;
190: }
191: try {
192: sb.append((char) Integer.parseInt(value.substring(
193: i, i + 4), 16));
194: } catch (NumberFormatException e) {
195: Debug.warning("Invalid hex number format", value
196: .substring(i, i + 4)); // NOI18N
197: }
198: i += 4;
199: break;
200: case '"': // NOI18N
201: case '\'': // NOI18N
202: case '\\': // NOI18N
203: sb.append(c);
204: break;
205: default:
206: if (c < '0' || c > '9') { // NOI18N
207: Debug.warning("Invalid character after slash", c); // NOI18N
208: break;
209: }
210: i--;
211: if (i + 3 > len) {
212: Debug.warning("Invalid octal number at the end: ",
213: value.substring(i)); // NOI18N
214: break;
215: }
216: try {
217: sb.append((char) Integer.parseInt(value.substring(
218: i, i + 3), 8));
219: } catch (NumberFormatException e) {
220: Debug.warning("Invalid octal number format", value
221: .substring(i, i + 3)); // NOI18N
222: }
223: i += 3;
224: }
225: }
226: return sb.toString();
227: }
228:
229: static PropertyValue decryptStringArrayArray(String value,
230: TypeID type, int dimension) {
231: if (value == null)
232: return PropertyValue.createNull();
233: if (dimension <= 0)
234: return MidpTypes.createStringValue(value);
235: type = type.getComponentType();
236:
237: int pos = 0;
238: int number = 0;
239: for (;;) {
240: char c = lookCharAhead(value, pos++);
241: if (!Character.isDigit(c))
242: break;
243: number = (number * 10) + (c - '0'); // NOI18N
244: }
245:
246: ArrayList<PropertyValue> values = new ArrayList<PropertyValue>();
247: for (int i = 0; i < number; i++) {
248: String valuePart = null;
249: if (Character.isDigit(lookCharAhead(value, pos))) {
250: int number2 = 0;
251: for (;;) {
252: char c = lookCharAhead(value, pos++);
253: if (!Character.isDigit(c))
254: break;
255: number2 = (number2 * 10) + (c - '0'); // NOI18N
256: }
257: valuePart = value.substring(pos, pos + number2);
258: pos += number2 + 1;
259: } else {
260: pos++;
261: }
262: values.add(decryptStringArrayArray(valuePart, type,
263: dimension - 1));
264: }
265:
266: return PropertyValue.createArray(type, values);
267: }
268:
269: private static char lookCharAhead(String string, int pos) {
270: return pos < string.length() ? string.charAt(pos) : '\0'; // NOI18N
271: }
272:
273: static void convertStringWithUserCode(DesignComponent component,
274: String propertyName, String value) {
275: PropertyValue propertyValue = getStringWithUserCode(value);
276: if (propertyValue != null)
277: component.writeProperty(propertyName, propertyValue);
278: }
279:
280: static void convertByte(DesignComponent component,
281: String propertyName, String value) {
282: Byte b = getByte(value);
283: if (b != null)
284: component.writeProperty(propertyName, MidpTypes
285: .createByteValue(b));
286: }
287:
288: static void convertShort(DesignComponent component,
289: String propertyName, String value) {
290: Short s = getShort(value);
291: if (s != null)
292: component.writeProperty(propertyName, MidpTypes
293: .createShortValue(s));
294: }
295:
296: static void convertInteger(DesignComponent component,
297: String propertyName, String value) {
298: Integer integer = getInteger(value);
299: if (integer != null)
300: component.writeProperty(propertyName, MidpTypes
301: .createIntegerValue(integer));
302: }
303:
304: static void convertLong(DesignComponent component,
305: String propertyName, String value) {
306: Long l = getLong(value);
307: if (l != null)
308: component.writeProperty(propertyName, MidpTypes
309: .createLongValue(l));
310: }
311:
312: static void convertFloat(DesignComponent component,
313: String propertyName, String value) {
314: Float fl = getFloat(value);
315: if (fl != null)
316: component.writeProperty(propertyName, MidpTypes
317: .createFloatValue(fl));
318: }
319:
320: static void convertDouble(DesignComponent component,
321: String propertyName, String value) {
322: Double d = getDouble(value);
323: if (d != null)
324: component.writeProperty(propertyName, MidpTypes
325: .createDoubleValue(d));
326: }
327:
328: static void convertChar(DesignComponent component,
329: String propertyName, String value) {
330: Character ch = getChar(value);
331: if (ch != null)
332: component.writeProperty(propertyName, MidpTypes
333: .createCharValue(ch));
334: }
335:
336: static void convertString(DesignComponent component,
337: String propertyName, String value) {
338: value = decryptStringFromJavaCode(value);
339: if (value != null)
340: component.writeProperty(propertyName, MidpTypes
341: .createStringValue(value));
342: }
343:
344: static void convertBoolean(DesignComponent component,
345: String propertyName, String value) {
346: Boolean bool = getBoolean(value);
347: if (bool != null)
348: component.writeProperty(propertyName, MidpTypes
349: .createBooleanValue(bool));
350: }
351:
352: static void convertConverterItemComponent(
353: DesignComponent component, String propertyName,
354: HashMap<String, ConverterItem> id2item, String value) {
355: DesignComponent ref = Converter.convertConverterItemComponent(
356: id2item, value, component.getDocument());
357: if (ref != null)
358: component.writeProperty(propertyName, PropertyValue
359: .createComponentReference(ref));
360: }
361:
362: static void convertToPropertyValue(DesignComponent component,
363: String propertyName, TypeID propertyType, String value) {
364: if (MidpTypes.TYPEID_BOOLEAN.equals(propertyType))
365: convertBoolean(component, propertyName, value);
366: else if (MidpTypes.TYPEID_BYTE.equals(propertyType))
367: convertByte(component, propertyName, value);
368: else if (MidpTypes.TYPEID_CHAR.equals(propertyType))
369: component.writeProperty(propertyName, MidpTypes
370: .createCharValue(value.charAt(0)));
371: else if (MidpTypes.TYPEID_DOUBLE.equals(propertyType))
372: convertDouble(component, propertyName, value);
373: else if (MidpTypes.TYPEID_FLOAT.equals(propertyType))
374: convertFloat(component, propertyName, value);
375: else if (MidpTypes.TYPEID_INT.equals(propertyType))
376: convertInteger(component, propertyName, value);
377: else if (MidpTypes.TYPEID_JAVA_CODE.equals(propertyType)) {
378: if (value != null)
379: component.writeProperty(propertyName, MidpTypes
380: .createJavaCodeValue(value));
381: } else if (MidpTypes.TYPEID_JAVA_LANG_STRING
382: .equals(propertyType))
383: convertString(component, propertyName, value);
384: else if (MidpTypes.TYPEID_JAVA_LANG_STRING.equals(propertyType))
385: convertString(component, propertyName, value);
386: else if (MidpTypes.TYPEID_LONG.equals(propertyType))
387: convertLong(component, propertyName, value);
388: else if (MidpTypes.TYPEID_SHORT.equals(propertyType))
389: convertShort(component, propertyName, value);
390: }
391:
392: }
|