2
* Copyright (C) 2001-2012 Michael Fuchs
4
* This file is part of herold.
6
* herold is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* herold 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 General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with herold. If not, see <http://www.gnu.org/licenses/>.
19
package org.dbdoclet.template;
21
import java.io.BufferedReader;
23
import java.io.FileReader;
24
import java.io.FileWriter;
25
import java.io.IOException;
26
import java.io.PrintWriter;
27
import java.io.StringReader;
28
import java.io.StringWriter;
29
import java.util.Iterator;
32
import org.dbdoclet.service.StringServices;
34
public class TemplateTransformer {
36
private BufferedReader reader;
38
public static TemplateTransformer newInstance(File template)
39
throws TemplateTransformException {
41
if (template == null) {
42
throw new IllegalArgumentException("The argument template may not be null!");
45
BufferedReader reader = null;
48
reader = new BufferedReader(new FileReader(template));
49
} catch (IOException oops) {
50
throw new TemplateTransformException(oops.getMessage(), oops);
53
return new TemplateTransformer(reader);
56
public static TemplateTransformer newInstance(BufferedReader template)
57
throws TemplateTransformException {
59
if (template == null) {
60
throw new IllegalArgumentException("The argument template may not be null!");
63
return new TemplateTransformer(template);
66
public static TemplateTransformer newInstance(String template)
67
throws TemplateTransformException {
69
if (template == null) {
70
throw new IllegalArgumentException("The argument template may not be null!");
73
return new TemplateTransformer(new BufferedReader(new StringReader(template)));
76
private TemplateTransformer(BufferedReader reader) {
81
public String transform(Map<String, String> vars)
82
throws TemplateTransformException {
84
StringWriter buffer = new StringWriter();
86
transform(vars, new PrintWriter(buffer));
88
return buffer.toString();
91
public void transform(Map<String, String> vars, String fileName)
92
throws TemplateTransformException {
94
if (fileName == null) {
95
throw new IllegalArgumentException("The argument fileName must not be null!");
98
transform(vars, new File(fileName));
101
public void transform(Map<String, String> vars, File file)
102
throws TemplateTransformException {
105
throw new IllegalArgumentException("The argument file must not be null!");
110
// if (file.getName().equals("default.css")) {
114
transform(vars, new FileWriter(file));
116
} catch (IOException oops) {
118
throw new TemplateTransformException(oops.getMessage(), oops);
122
public void transform(Map<String, String> vars, FileWriter fileWriter)
123
throws TemplateTransformException {
125
if (fileWriter == null) {
126
throw new IllegalArgumentException("The argument fileWriter must not be null!");
129
PrintWriter writer = new PrintWriter(fileWriter);
130
transform(vars, writer);
134
public void transform(Map<String, String> vars, PrintWriter writer)
135
throws TemplateTransformException {
137
if (reader == null) {
138
throw new IllegalStateException("The field reader may not be null!");
142
throw new IllegalArgumentException("The argument vars may not be null!");
145
if (writer == null) {
146
throw new IllegalArgumentException("The argument writer may not be null!");
151
Iterator<String> iterator;
157
StringBuffer buffer = new StringBuffer();
159
while ((line = reader.readLine()) != null ) {
161
if (line.trim().length() == 0) {
168
if (line.indexOf('$') == -1) {
176
// System.out.println("line=" + line);
179
iterator = vars.keySet().iterator();
181
while (iterator.hasNext()) {
183
name = iterator.next();
184
value = vars.get(name);
188
name = "${" + name + "}";
190
// if (trace && name.equals("${css.hl1.font}")) {
191
// System.out.println("name=" + name);
194
line = StringServices.replace(line, name, value.toString());
196
// if (trace && name.equals("${css.hl1.font}")) {
197
// System.out.println("new line=" + line);
209
// String str = ReplaceServices.replaceAll(buffer.toString(), "\\$\\{\\w*\\}", "");
210
String str = buffer.toString();
212
BufferedReader bufferReader = new BufferedReader(new StringReader(str));
214
while ((line = bufferReader.readLine()) != null ) {
215
writer.println(line);
218
bufferReader.close();
220
} catch (Exception oops) {
221
throw new TemplateTransformException(oops.getMessage(), oops);