001: package org.objectweb.celtix.tools.generators;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.util.Calendar;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.Map;
009: import java.util.logging.Logger;
010:
011: import org.apache.velocity.Template;
012: import org.apache.velocity.VelocityContext;
013: import org.apache.velocity.app.Velocity;
014: import org.objectweb.celtix.common.i18n.Message;
015: import org.objectweb.celtix.common.logging.LogUtils;
016: import org.objectweb.celtix.tools.common.ProcessorEnvironment;
017: import org.objectweb.celtix.tools.common.ToolConstants;
018: import org.objectweb.celtix.tools.common.ToolException;
019: import org.objectweb.celtix.tools.common.model.JavaModel;
020: import org.objectweb.celtix.tools.processors.wsdl2.internal.ClassCollector;
021: import org.objectweb.celtix.tools.utils.FileWriterUtil;
022: import org.objectweb.celtix.version.Version;
023:
024: public abstract class AbstractGenerator {
025:
026: public static final String TEMPLATE_BASE = "org/objectweb/celtix/tools/generators/wsdl2/templates";
027: private static final Logger LOG = LogUtils
028: .getL7dLogger(AbstractGenerator.class);
029: protected ProcessorEnvironment env;
030: protected JavaModel javaModel;
031: protected Map<String, Object> attributes = new HashMap<String, Object>();
032: protected String name;
033: protected ClassCollector collector;
034:
035: public AbstractGenerator() {
036:
037: }
038:
039: public AbstractGenerator(JavaModel jmodel, ProcessorEnvironment penv) {
040: javaModel = jmodel;
041: this .env = penv;
042: collector = (ClassCollector) env
043: .get(ToolConstants.GENERATED_CLASS_COLLECTOR);
044: }
045:
046: public abstract boolean passthrough();
047:
048: public abstract void generate() throws ToolException;
049:
050: protected void doWrite(String templateName, Writer outputs)
051: throws ToolException {
052: Template tmpl = null;
053: try {
054: tmpl = Velocity.getTemplate(templateName);
055: } catch (Exception e) {
056: Message msg = new Message("TEMPLATE_MISSING", LOG,
057: templateName);
058: throw new ToolException(msg, e);
059: }
060:
061: VelocityContext ctx = new VelocityContext();
062:
063: for (Iterator iter = attributes.keySet().iterator(); iter
064: .hasNext();) {
065: String key = (String) iter.next();
066: ctx.put(key, attributes.get(key));
067: }
068:
069: VelocityWriter writer = new VelocityWriter(outputs);
070: try {
071: tmpl.merge(ctx, writer);
072: writer.close();
073: } catch (Exception e) {
074: Message msg = new Message("VELOCITY_ENGINE_WRITE_ERRORS",
075: LOG);
076: throw new ToolException(msg, e);
077: }
078: }
079:
080: protected boolean isCollision(String packageName, String filename)
081: throws ToolException {
082: return isCollision(packageName, filename, ".java");
083: }
084:
085: protected boolean isCollision(String packageName, String filename,
086: String ext) throws ToolException {
087: FileWriterUtil fw = new FileWriterUtil((String) env
088: .get(ToolConstants.CFG_OUTPUTDIR));
089: return fw.isCollision(packageName, filename + ext);
090: }
091:
092: protected Writer parseOutputName(String packageName,
093: String filename, String ext) throws ToolException {
094: FileWriterUtil fw = null;
095: Writer writer = null;
096:
097: fw = new FileWriterUtil((String) env
098: .get(ToolConstants.CFG_OUTPUTDIR));
099: try {
100: writer = fw.getWriter(packageName, filename + ext);
101: } catch (IOException ioe) {
102: Message msg = new Message("FAIL_TO_WRITE_FILE", LOG,
103: packageName + "." + filename + ext);
104: throw new ToolException(msg, ioe);
105: }
106:
107: return writer;
108: }
109:
110: protected Writer parseOutputName(String packageName, String filename)
111: throws ToolException {
112: // collector.
113: if (ToolConstants.CLT_GENERATOR.equals(name)) {
114: collector.addClientClassName(packageName, filename,
115: packageName + "." + filename);
116: }
117:
118: if (ToolConstants.FAULT_GENERATOR.equals(name)) {
119: collector.addExceptionClassName(packageName, filename,
120: packageName + "." + filename);
121: }
122: if (ToolConstants.SERVICE_GENERATOR.equals(name)) {
123: collector.addServiceClassName(packageName, filename,
124: packageName + "." + filename);
125: }
126: if (ToolConstants.SVR_GENERATOR.equals(name)) {
127: collector.addServiceClassName(packageName, filename,
128: packageName + "." + filename);
129:
130: }
131:
132: return parseOutputName(packageName, filename, ".java");
133: }
134:
135: protected void setAttributes(String n, Object value) {
136: attributes.put(n, value);
137: }
138:
139: protected void setCommonAttributes() {
140: attributes.put("currentdate", Calendar.getInstance().getTime());
141: attributes.put("version", Version.getCurrentVersion());
142: }
143:
144: protected void clearAttributes() {
145: attributes.clear();
146: }
147:
148: public void setEnvironment(ProcessorEnvironment penv) {
149: this .env = penv;
150:
151: }
152:
153: public ProcessorEnvironment getEnvironment() {
154: return this .env;
155: }
156:
157: public String getName() {
158: return this.name;
159: }
160: }
|