1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
/*
* Copyright (C) 2001-2013 Michael Fuchs
*
* This file is part of herold.
*
* herold is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* herold is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with herold. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dbdoclet.template;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.Map;
import org.dbdoclet.service.StringServices;
public class TemplateTransformer {
private BufferedReader reader;
public static TemplateTransformer newInstance(File template)
throws TemplateTransformException {
if (template == null) {
throw new IllegalArgumentException("The argument template may not be null!");
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(template));
} catch (IOException oops) {
throw new TemplateTransformException(oops.getMessage(), oops);
}
return new TemplateTransformer(reader);
}
public static TemplateTransformer newInstance(BufferedReader template)
throws TemplateTransformException {
if (template == null) {
throw new IllegalArgumentException("The argument template may not be null!");
}
return new TemplateTransformer(template);
}
public static TemplateTransformer newInstance(String template)
throws TemplateTransformException {
if (template == null) {
throw new IllegalArgumentException("The argument template may not be null!");
}
return new TemplateTransformer(new BufferedReader(new StringReader(template)));
}
private TemplateTransformer(BufferedReader reader) {
this.reader = reader;
}
public String transform(Map<String, String> vars)
throws TemplateTransformException {
StringWriter buffer = new StringWriter();
transform(vars, new PrintWriter(buffer));
return buffer.toString();
}
public void transform(Map<String, String> vars, String fileName)
throws TemplateTransformException {
if (fileName == null) {
throw new IllegalArgumentException("The argument fileName must not be null!");
}
transform(vars, new File(fileName));
}
public void transform(Map<String, String> vars, File file)
throws TemplateTransformException {
if (file == null) {
throw new IllegalArgumentException("The argument file must not be null!");
}
try {
// if (file.getName().equals("default.css")) {
// trace = true;
// }
transform(vars, new FileWriter(file));
} catch (IOException oops) {
throw new TemplateTransformException(oops.getMessage(), oops);
}
}
public void transform(Map<String, String> vars, FileWriter fileWriter)
throws TemplateTransformException {
if (fileWriter == null) {
throw new IllegalArgumentException("The argument fileWriter must not be null!");
}
PrintWriter writer = new PrintWriter(fileWriter);
transform(vars, writer);
writer.close();
}
public void transform(Map<String, String> vars, PrintWriter writer)
throws TemplateTransformException {
if (reader == null) {
throw new IllegalStateException("The field reader may not be null!");
}
if (vars == null) {
throw new IllegalArgumentException("The argument vars may not be null!");
}
if (writer == null) {
throw new IllegalArgumentException("The argument writer may not be null!");
}
try {
Iterator<String> iterator;
String name;
Object value;
String line;
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null ) {
if (line.trim().length() == 0) {
buffer.append(line);
buffer.append('\n');
continue;
}
if (line.indexOf('$') == -1) {
buffer.append(line);
buffer.append('\n');
continue;
}
// if (trace) {
// System.out.println("line=" + line);
// }
iterator = vars.keySet().iterator();
while (iterator.hasNext()) {
name = iterator.next();
value = vars.get(name);
if (value != null) {
name = "${" + name + "}";
// if (trace && name.equals("${css.hl1.font}")) {
// System.out.println("name=" + name);
// }
line = StringServices.replace(line, name, value.toString());
// if (trace && name.equals("${css.hl1.font}")) {
// System.out.println("new line=" + line);
// }
}
}
buffer.append(line);
buffer.append('\n');
}
reader.close();
// String str = ReplaceServices.replaceAll(buffer.toString(), "\\$\\{\\w*\\}", "");
String str = buffer.toString();
BufferedReader bufferReader = new BufferedReader(new StringReader(str));
while ((line = bufferReader.readLine()) != null ) {
writer.println(line);
}
bufferReader.close();
} catch (Exception oops) {
throw new TemplateTransformException(oops.getMessage(), oops);
}
}
}
|