001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library 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 GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.generation.generator.objectid;
018:
019: import org.objectweb.speedo.generation.generator.lib.AbstractVelocityGenerator;
020: import org.objectweb.speedo.api.SpeedoException;
021: import org.objectweb.speedo.generation.generator.api.SpeedoGenerationException;
022: import org.objectweb.speedo.api.SpeedoProperties;
023: import org.objectweb.speedo.lib.Personality;
024: import org.objectweb.speedo.metadata.SpeedoClass;
025: import org.objectweb.speedo.metadata.SpeedoField;
026: import org.objectweb.util.monolog.wrapper.velocity.VelocityLogger;
027: import org.objectweb.jorm.type.api.PType;
028: import org.objectweb.jorm.metainfo.api.TypedElement;
029: import org.apache.velocity.context.Context;
030: import org.apache.velocity.app.Velocity;
031: import org.apache.velocity.VelocityContext;
032:
033: import java.io.FileWriter;
034: import java.util.Iterator;
035: import java.util.ArrayList;
036:
037: /**
038: *
039: * @author S.Chassande-Barrioz
040: */
041: public class ObjectIdGenerator extends AbstractVelocityGenerator {
042: public final static String LOGGER_NAME = SpeedoProperties.LOGGER_NAME
043: + ".generation.generator.objectid";
044: public final static String TEMPLATE_NAME = TEMPLATE_DIR
045: + ".objectid.ObjectId";
046: public final static String OBJECTID_SUFFIX = "_Id";
047:
048: public ObjectIdGenerator(Personality p) {
049: super (p);
050: }
051:
052: // IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
053: //----------------------------------------------------//
054:
055: public boolean init() throws SpeedoException {
056: logger = scp.loggerFactory.getLogger(LOGGER_NAME);
057: return !scp.getXmldescriptor().isEmpty();
058: }
059:
060: // IMPLEMENTATION OF THE VelocityGenerator INTERFACE //
061: //---------------------------------------------------//
062:
063: /**
064: * This method generates the a file since a SpeedoClass meta object.
065: * @param sClass is the speedo meta object.
066: * @param fileName name of the new file.
067: * @exception org.objectweb.speedo.generation.generator.api.SpeedoGenerationException If there is a problem during writing
068: * the new file.
069: */
070: public void generate(SpeedoClass sClass, String fileName)
071: throws SpeedoException {
072: computeTemplate(TEMPLATE_NAME.replace('.', '/') + ".vm");
073: try {
074: Context ctx = getContext(sClass);
075: FileWriter fw = new FileWriter(fileName);
076: ve.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM,
077: new VelocityLogger(logger));
078: template.merge(ctx, fw);
079: fw.flush();
080: fw.close();
081: } catch (Exception e) {
082: throw new SpeedoGenerationException(
083: "Error during the generation of the file "
084: + fileName, e);
085: }
086: }
087:
088: private Context getContext(SpeedoClass sc) {
089: //creation of the Velocity context
090: Context ctx = new VelocityContext();
091: ctx.put("tools", this );
092: ctx.put("jdoClass", sc);
093: ctx.put("package", sc.moPackage.name);
094: ctx.put("classNameObjectId", sc.name + OBJECTID_SUFFIX);
095: ArrayList fields = new ArrayList();
096: Iterator it = sc.fields.values().iterator();
097: while (it.hasNext()) {
098: SpeedoField sf = (SpeedoField) it.next();
099: if (sf.primaryKey) {
100: TypedElement te = sc.jormclass.getTypedElement(sf.name);
101: Field f = new Field(sf.name, te.getType().getJavaName());
102: String encode = f.name;
103: String decode = "current.substring("
104: + (f.name.length() + 1)
105: + " ,(idx = current.indexOf(\";\")))";
106: f.next = "current = current.substring(idx+1);";
107: switch (te.getType().getTypeCode()) {
108: case PType.TYPECODE_BOOLEAN:
109: f.defaultValue = "false";
110: f.decode = "Boolean.getBoolean(" + decode + ")";
111: break;
112: case PType.TYPECODE_CHAR:
113: f.defaultValue = "(char) 0";
114: f.decode = decode + ".charAt(0)";
115: break;
116: case PType.TYPECODE_BYTE:
117: f.defaultValue = "(byte) -1";
118: f.decode = "Byte.parseByte(" + decode + ")";
119: break;
120: case PType.TYPECODE_SHORT:
121: f.defaultValue = "(short) -1";
122: f.decode = "Short.parseShort(" + decode + ")";
123: break;
124: case PType.TYPECODE_INT:
125: f.defaultValue = "-1";
126: f.decode = "Integer.parseInt(" + decode + ")";
127: break;
128: case PType.TYPECODE_LONG:
129: f.defaultValue = "-1";
130: f.decode = "Long.parseLong(" + decode + ")";
131: break;
132: case PType.TYPECODE_FLOAT:
133: f.defaultValue = "-1";
134: f.decode = "Float.parseFloat(" + decode + ")";
135: break;
136: case PType.TYPECODE_DOUBLE:
137: f.defaultValue = "0";
138: f.decode = "Double.parseDouble(" + decode + ")";
139: break;
140: case PType.TYPECODE_OBJBOOLEAN:
141: f.defaultValue = "null";
142: f.decode = "Boolean.valueOf(" + decode + ")";
143: break;
144: case PType.TYPECODE_OBJCHAR:
145: f.defaultValue = "null";
146: f.decode = "new Character(" + decode
147: + ".charAt(0))";
148: break;
149: case PType.TYPECODE_OBJBYTE:
150: f.defaultValue = "null";
151: f.decode = "Byte.valueOf(" + decode + ")";
152: break;
153: case PType.TYPECODE_OBJSHORT:
154: f.defaultValue = "null";
155: f.decode = "Short.valueOf(" + decode + ")";
156: break;
157: case PType.TYPECODE_OBJINT:
158: f.defaultValue = "null";
159: f.decode = "Integer.valueOf(" + decode + ")";
160: break;
161: case PType.TYPECODE_OBJLONG:
162: f.defaultValue = "null";
163: f.decode = "Long.valueOf(" + decode + ")";
164: break;
165: case PType.TYPECODE_OBJFLOAT:
166: f.defaultValue = "null";
167: f.decode = "Float.valueOf(" + decode + ")";
168: break;
169: case PType.TYPECODE_OBJDOUBLE:
170: f.defaultValue = "null";
171: f.decode = "Double.valueOf(" + decode + ")";
172: break;
173: case PType.TYPECODE_STRING:
174: f.defaultValue = "null";
175: f.decode = decode;
176: break;
177: case PType.TYPECODE_DATE:
178: f.defaultValue = "null";
179: encode = f.name + ".getTime()";
180: decode = "new java.util.Date(Long.parseLong("
181: + decode + "))";
182: break;
183: case PType.TYPECODE_CHARARRAY:
184: f.defaultValue = "null";
185: f.decode = decode + ".toCharArray()";
186: encode = "new String(" + f.name + ")";
187: break;
188: case PType.TYPECODE_BYTEARRAY:
189: f.defaultValue = "null";
190: f.decode = decode + ".getBytes()";
191: encode = "new String(" + f.name + ")";
192: break;
193: default:
194: continue;
195: }
196: f.encode = "\"" + f.name + ":\" + " + encode
197: + " + \";\"";
198: fields.add(f);
199: }
200: ctx.put("fields", fields);
201: }
202:
203: return ctx;
204: }
205:
206: public class Field {
207: public String name;
208: public String type;
209: public String defaultValue;
210: public String encode;
211: public String decode;
212: public String next;
213:
214: public Field(String name, String type) {
215: this .name = name;
216: this .type = type;
217: }
218:
219: public String getName() {
220: return name;
221: }
222:
223: public String getType() {
224: return type;
225: }
226:
227: public String getDefaultValue() {
228: return defaultValue;
229: }
230:
231: public String getEncode() {
232: return encode;
233: }
234:
235: public String getDecode() {
236: return decode;
237: }
238:
239: public String getNext() {
240: return next;
241: }
242: }
243: }
|