view src/kryshen/tema/ant/TemaTask.java @ 2:6c41a0b43e58

Tema 0.3 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 19 Feb 2008 20:32:17 +0300
parents
children e9d13c7ffeb1
line source
1 /*
2 * Copyright 2006-2008 Mikhail Kryshen
3 *
4 * This file is part of Tema.
5 *
6 * Tema is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * Tema is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the
17 * GNU Lesser General Public License along with Tema.
18 * If not, see <http://www.gnu.org/licenses/>.
19 *
20 * $Id: TemaTask.java,v 1.17 2008/02/17 23:58:01 mikhail Exp $
21 */
23 package kryshen.tema.ant;
25 import java.io.BufferedReader;
26 import java.io.BufferedWriter;
27 import java.io.File;
28 import java.io.FileReader;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.io.Writer;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import kryshen.tema.TemplateException;
35 import kryshen.tema.TemplateParser;
36 import kryshen.tema.io.NullWriter;
37 import kryshen.tema.io.TemplateReader;
38 import org.apache.tools.ant.BuildException;
39 import org.apache.tools.ant.Task;
41 /**
42 * Tema support for ant.
43 *
44 * @author Mikhail Kryshen
45 */
46 public class TemaTask extends Task {
48 public static class Variable {
49 private String name;
50 private String value;
52 public void setName(String name) {
53 this.name = name;
54 }
56 public void setValue(String value) {
57 this.value = value;
58 }
60 public void setPath(File file) {
61 this.value = file.getAbsolutePath();
62 }
63 }
65 private File basedir = null;
67 private File infile = null;
68 private File outfile = null;
70 private Collection<Variable> variables = new ArrayList<Variable>();
72 @Override
73 public void execute() throws BuildException {
74 if (basedir == null) {
75 basedir = getProject().getBaseDir();
76 }
78 if (infile == null) {
79 throw new BuildException("no infile specified", getLocation());
80 }
82 try {
83 executeTema();
84 } catch (IOException e) {
85 throw new BuildException(e);
86 } catch (TemplateException e) {
87 throw new BuildException(e);
88 }
89 }
91 private void executeTema() throws IOException, TemplateException {
92 TemplateParser parser = new TemplateParser(basedir);
94 for (Variable var : variables) {
95 if (var.name == null) {
96 throw new BuildException(
97 "no name specified for variable", getLocation());
98 }
100 if (var.value == null) {
101 throw new BuildException(
102 "no value specified for variable", getLocation());
103 }
105 parser.getContext().set(var.name, var.value);
106 }
108 TemplateReader in = new TemplateReader(
109 new BufferedReader(new FileReader(infile)),
110 infile.getName());
111 try {
112 Writer out;
114 if (outfile != null) {
115 out = new BufferedWriter(new FileWriter(outfile));
116 } else {
117 out = NullWriter.INSTANCE;
118 }
120 try {
121 parser.parse(in, out);
122 } finally {
123 out.close();
124 }
125 } finally {
126 in.close();
127 }
128 }
130 public void setBasedir(File basedir) {
131 this.basedir = basedir;
132 }
134 public void setInfile(File infile) {
135 this.infile = infile;
136 }
138 public void setOutfile(File outfile) {
139 this.outfile = outfile;
140 }
142 public void addVariable(Variable variable) {
143 variables.add(variable);
144 }
145 }