001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.generation.generator.lib;
027:
028: import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent;
029: import org.objectweb.speedo.generation.generator.api.SpeedoGenerationException;
030: import org.objectweb.speedo.generation.generator.api.VelocityGenerator;
031: import org.objectweb.speedo.api.SpeedoException;
032: import org.objectweb.speedo.lib.Personality;
033: import org.objectweb.speedo.metadata.SpeedoClass;
034: import org.objectweb.speedo.metadata.SpeedoIdentity;
035: import org.objectweb.jorm.metainfo.api.Class;
036: import org.objectweb.jorm.metainfo.api.TypedElement;
037: import org.apache.velocity.Template;
038: import org.apache.velocity.app.VelocityEngine;
039:
040: import java.util.Collection;
041: import java.io.File;
042:
043: /**
044: * @author S.Chassande-Barrioz
045: */
046: public abstract class AbstractVelocityGenerator extends
047: AbstractGeneratorComponent implements VelocityGenerator {
048:
049: public final static String TEMPLATE_DIR = "org.objectweb.speedo.generation.generator";
050:
051: protected VelocityEngine ve = null;
052:
053: protected static char fs = File.separatorChar;
054:
055: protected Template template = null;
056:
057: public AbstractVelocityGenerator(Personality p) {
058: super (p);
059: }
060:
061: protected void computeTemplate(String templateFN)
062: throws SpeedoException {
063: try {
064: if (template == null) {
065: try {
066: template = ve.getTemplate(templateFN);
067: } catch (Exception e1) {
068: template = null;
069: }
070: if (template == null) {
071: templateFN = templateFN.replace(fs, '/');
072: template = ve.getTemplate(templateFN);
073: if (template == null) {
074: throw new SpeedoException(
075: "The Speedo template "
076: + templateFN
077: + " is not availlable in the classpath");
078: }
079: }
080: }
081: } catch (Exception e) {
082: throw new SpeedoGenerationException(
083: "Impossible to fetch the template " + templateFN, e);
084: }
085: }
086:
087: /**
088: * This method indicates if the type is a boolean, a char, a short, an int,
089: * a long, a float, a double or a String.
090: *
091: * @param type the type to determine.
092: * @return true if the String is one of those above.
093: */
094: protected static boolean isClassicalType(String type) {
095: return type.equals("boolean") || type.equals("char")
096: || type.equals("short") || type.equals("int")
097: || type.equals("long") || type.equals("float")
098: || type.equals("double") || type.equals("String")
099: || type.equals("byte");
100: }
101:
102: public boolean isContainerIdField(Class clazz, TypedElement te,
103: SpeedoClass sc) {
104: return sc.identity.isDataStore()
105: && clazz.getNameDef(scp.projectName).getNameRef()
106: .getProjection().containsValue(te.getName());
107: }
108:
109: // IMPLEMENTATION OF THE VelocityGenerator INTERFACE //
110: //---------------------------------------------------//
111:
112: public VelocityEngine getVelocityEngine() {
113: return ve;
114: }
115:
116: public void setVelocityEngine(VelocityEngine ve) {
117: this .ve = ve;
118: }
119:
120: public Collection getExternalsTemplate() {
121: return null;
122: }
123:
124: /**
125: * generate the file name corresponding to the Speedo meta object given in
126: * parameter.
127: * @param sClass is the speedo meta object which represents a persistent class
128: * @param fileName is the generated file name
129: * @throws SpeedoException
130: */
131: public abstract void generate(SpeedoClass sClass, String fileName)
132: throws SpeedoException;
133:
134: // IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
135: //----------------------------------------------------//
136:
137: public abstract boolean init() throws SpeedoException;
138:
139: public final void process() throws SpeedoException {
140: throw new SpeedoException("Do not use this method");
141: }
142: }
|