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: package org.netbeans.modules.vmd.midp.codegen;
042:
043: import org.netbeans.modules.vmd.api.codegen.CodeReferencePresenter;
044: import org.netbeans.modules.vmd.api.codegen.CodeWriter;
045: import org.netbeans.modules.vmd.api.codegen.CodeSupport;
046: import org.netbeans.modules.vmd.api.model.Debug;
047: import org.netbeans.modules.vmd.api.model.PropertyValue;
048: import org.netbeans.modules.vmd.api.model.TypeID;
049: import org.netbeans.modules.vmd.midp.components.MidpTypes;
050:
051: import java.util.List;
052:
053: /**
054: * @author David Kaspar
055: */
056: // HINT - after making change, update MidpPrimitiveDescriptor, MidpEnumDescriptor too
057: public class MidpCodeSupport {
058:
059: public static void generateCodeForPropertyValue(CodeWriter writer,
060: PropertyValue value) {
061: switch (value.getKind()) {
062: case ARRAY: {
063: TypeID type = value.getType();
064: int dimension = type.getDimension();
065: boolean newLines = dimension > 1;
066: List<PropertyValue> array = value.getArray();
067:
068: writer.write("new "
069: + MidpTypes.getOptimalizedFQNClassName(type)); // NOI18N
070: for (int a = 0; a < dimension; a++)
071: writer.write("[]"); // NOI18N
072: writer.write(" {"); // NOI18N
073: if (array.size() > 0)
074: writer.write(newLines ? "\n" : " "); // NOI18N
075:
076: for (int a = 0; a < array.size(); a++) {
077: if (a > 0)
078: writer.write(newLines ? ",\n" : ", "); // NOI18N
079: PropertyValue propertyValue = array.get(a);
080: generateCodeForPropertyValue(writer, propertyValue);
081: }
082:
083: writer.write(newLines ? "}" : " }"); // NOI18N
084: }
085: break;
086: case ENUM:
087: generateEnumTypes(writer, value);
088: break;
089: case NULL:
090: writer.write("null"); // NOI18N
091: break;
092: case REFERENCE:
093: writer.write(CodeReferencePresenter
094: .generateAccessCode(value.getComponent()));
095: break;
096: case USERCODE:
097: writer.write(value.getUserCode());
098: break;
099: case VALUE:
100: generatePrimitiveTypes(writer, value);
101: break;
102: default:
103: throw Debug.illegalState();
104: }
105: }
106:
107: // TODO - convert to global lookup - or could be handled as a default generator, a custom could be defined by Parameter
108: private static void generatePrimitiveTypes(CodeWriter writer,
109: PropertyValue value) {
110: TypeID type = value.getType();
111: if (MidpTypes.TYPEID_INT.equals(type))
112: writer.write(value.getPrimitiveValue().toString());
113: else if (MidpTypes.TYPEID_LONG.equals(type))
114: writer.write(value.getPrimitiveValue().toString() + "l"); // NOI18N
115: else if (MidpTypes.TYPEID_CHAR.equals(type))
116: writer.write(Integer.toString((Character) value
117: .getPrimitiveValue()));
118: else if (MidpTypes.TYPEID_BYTE.equals(type))
119: writer.write(value.getPrimitiveValue().toString());
120: else if (MidpTypes.TYPEID_SHORT.equals(type))
121: writer.write(value.getPrimitiveValue().toString());
122: else if (MidpTypes.TYPEID_FLOAT.equals(type))
123: writer.write(value.getPrimitiveValue().toString() + "f"); // NOI18N
124: else if (MidpTypes.TYPEID_DOUBLE.equals(type))
125: writer.write(value.getPrimitiveValue().toString());
126: else if (MidpTypes.TYPEID_BOOLEAN.equals(type))
127: writer.write((Boolean) value.getPrimitiveValue() ? "true"
128: : "false"); // NOI18N
129: else if (MidpTypes.TYPEID_JAVA_LANG_STRING.equals(type))
130: writer.write("\""
131: + CodeSupport.encryptStringToJavaCode(value
132: .getPrimitiveValue().toString()) + "\"");
133: else if (MidpTypes.TYPEID_JAVA_CODE.equals(type))
134: writer.write(value.getPrimitiveValue().toString());
135: //TODO
136: }
137:
138: // TODO - convert to global lookup - or could be handled as a default generator, a custom could be defined by Parameter
139: private static void generateEnumTypes(CodeWriter writer,
140: PropertyValue value) {
141: TypeID type = value.getType();
142: if (MidpTypes.TYPEID_ALERT_TYPE.equals(type))
143: writer.write("AlertType."
144: + value.getPrimitiveValue().toString()); // NOI18N
145: //TODO
146: }
147:
148: }
|