view src/kryshen/tema/ant/TemaTask.java @ 15:e9d13c7ffeb1

Update header comment.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 18:51:47 +0300
parents 6c41a0b43e58
children
line source
1 /*
2 * Copyright 2006-2009 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 */
21 package kryshen.tema.ant;
23 import java.io.BufferedReader;
24 import java.io.BufferedWriter;
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.Writer;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import kryshen.tema.TemplateException;
33 import kryshen.tema.TemplateParser;
34 import kryshen.tema.io.NullWriter;
35 import kryshen.tema.io.TemplateReader;
36 import org.apache.tools.ant.BuildException;
37 import org.apache.tools.ant.Task;
39 /**
40 * Tema support for ant.
41 *
42 * @author Mikhail Kryshen
43 */
44 public class TemaTask extends Task {
46 public static class Variable {
47 private String name;
48 private String value;
50 public void setName(String name) {
51 this.name = name;
52 }
54 public void setValue(String value) {
55 this.value = value;
56 }
58 public void setPath(File file) {
59 this.value = file.getAbsolutePath();
60 }
61 }
63 private File basedir = null;
65 private File infile = null;
66 private File outfile = null;
68 private Collection<Variable> variables = new ArrayList<Variable>();
70 @Override
71 public void execute() throws BuildException {
72 if (basedir == null) {
73 basedir = getProject().getBaseDir();
74 }
76 if (infile == null) {
77 throw new BuildException("no infile specified", getLocation());
78 }
80 try {
81 executeTema();
82 } catch (IOException e) {
83 throw new BuildException(e);
84 } catch (TemplateException e) {
85 throw new BuildException(e);
86 }
87 }
89 private void executeTema() throws IOException, TemplateException {
90 TemplateParser parser = new TemplateParser(basedir);
92 for (Variable var : variables) {
93 if (var.name == null) {
94 throw new BuildException(
95 "no name specified for variable", getLocation());
96 }
98 if (var.value == null) {
99 throw new BuildException(
100 "no value specified for variable", getLocation());
101 }
103 parser.getContext().set(var.name, var.value);
104 }
106 TemplateReader in = new TemplateReader(
107 new BufferedReader(new FileReader(infile)),
108 infile.getName());
109 try {
110 Writer out;
112 if (outfile != null) {
113 out = new BufferedWriter(new FileWriter(outfile));
114 } else {
115 out = NullWriter.INSTANCE;
116 }
118 try {
119 parser.parse(in, out);
120 } finally {
121 out.close();
122 }
123 } finally {
124 in.close();
125 }
126 }
128 public void setBasedir(File basedir) {
129 this.basedir = basedir;
130 }
132 public void setInfile(File infile) {
133 this.infile = infile;
134 }
136 public void setOutfile(File outfile) {
137 this.outfile = outfile;
138 }
140 public void addVariable(Variable variable) {
141 variables.add(variable);
142 }
143 }