001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.common;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.io.Writer;
023: import java.util.Calendar;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.Properties;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030:
031: import org.apache.cxf.common.i18n.Message;
032: import org.apache.cxf.common.logging.LogUtils;
033: import org.apache.cxf.helpers.FileUtils;
034: import org.apache.cxf.tools.util.FileWriterUtil;
035: import org.apache.cxf.version.Version;
036: import org.apache.velocity.Template;
037: import org.apache.velocity.VelocityContext;
038: import org.apache.velocity.app.Velocity;
039:
040: public final class VelocityGenerator {
041: private static final Logger LOG = LogUtils
042: .getL7dLogger(VelocityGenerator.class);
043: private final Map<String, Object> attributes = new HashMap<String, Object>();
044: private String baseDir;
045:
046: public VelocityGenerator() {
047: init();
048: }
049:
050: private void init() {
051: initVelocity();
052: }
053:
054: private String getVelocityLogFile(String logfile) {
055: String logdir = System.getProperty("user.home");
056: if (logdir == null || logdir.length() == 0) {
057: logdir = System.getProperty("user.dir");
058: }
059: return logdir + File.separator + logfile;
060: }
061:
062: private void initVelocity() throws ToolException {
063: try {
064: Properties props = new Properties();
065: String clzName = "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader";
066: props.put("resource.loader", "class");
067: props.put("class.resource.loader.class", clzName);
068: props
069: .put("runtime.log",
070: getVelocityLogFile("velocity.log"));
071:
072: Velocity.init(props);
073: } catch (Exception e) {
074: org.apache.cxf.common.i18n.Message msg = new org.apache.cxf.common.i18n.Message(
075: "FAIL_TO_INITIALIZE_VELOCITY_ENGINE", LOG);
076: LOG.log(Level.SEVERE, msg.toString());
077: throw new ToolException(msg, e);
078: }
079: }
080:
081: public void doWrite(String templateName, Writer outputs)
082: throws ToolException {
083: Template tmpl = null;
084: try {
085: tmpl = Velocity.getTemplate(templateName);
086: } catch (Exception e) {
087: Message msg = new Message("TEMPLATE_MISSING", LOG,
088: templateName);
089: throw new ToolException(msg, e);
090: }
091:
092: VelocityContext ctx = new VelocityContext();
093:
094: for (Iterator iter = attributes.keySet().iterator(); iter
095: .hasNext();) {
096: String key = (String) iter.next();
097: ctx.put(key, attributes.get(key));
098: }
099:
100: VelocityWriter writer = new VelocityWriter(outputs);
101: ctx.put("out", writer);
102: try {
103: tmpl.merge(ctx, writer);
104: writer.close();
105: } catch (Exception e) {
106: Message msg = new Message("VELOCITY_ENGINE_WRITE_ERRORS",
107: LOG);
108: throw new ToolException(msg, e);
109: }
110: }
111:
112: public void setBaseDir(String dir) {
113: this .baseDir = dir;
114: }
115:
116: public File parseOutputName(String packageName, String filename)
117: throws ToolException {
118: return parseOutputName(packageName, filename, ".java");
119: }
120:
121: public File parseOutputName(String packageName, String filename,
122: String ext) throws ToolException {
123: FileUtils.mkDir(new File(this .baseDir));
124: FileWriterUtil fw = new FileWriterUtil(this .baseDir);
125: try {
126: return fw.getFileToWrite(packageName, filename + ext);
127: } catch (IOException ioe) {
128: Message msg = new Message("FAIL_TO_WRITE_FILE", LOG,
129: packageName + "." + filename + ext);
130: throw new ToolException(msg, ioe);
131: }
132: }
133:
134: public void setCommonAttributes() {
135: attributes.put("currentdate", Calendar.getInstance().getTime());
136: attributes.put("version", Version.getCurrentVersion());
137: }
138:
139: public void clearAttributes() {
140: attributes.clear();
141: }
142:
143: public void setAttributes(String n, Object value) {
144: attributes.put(n, value);
145: }
146: }
|