~ubuntu-branches/ubuntu/vivid/herold/vivid

« back to all changes in this revision

Viewing changes to java/org/dbdoclet/template/TemplateTransformer.java

  • Committer: Package Import Robot
  • Author(s): Mathieu Malaterre
  • Date: 2012-09-20 10:00:14 UTC
  • Revision ID: package-import@ubuntu.com-20120920100014-5pcwbw2err6on8yg
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (C) 2001-2012 Michael Fuchs
 
3
 *
 
4
 * This file is part of herold.
 
5
 * 
 
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.
 
10
 * 
 
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.
 
15
 * 
 
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/>.  
 
18
 */
 
19
package org.dbdoclet.template;
 
20
 
 
21
import java.io.BufferedReader;
 
22
import java.io.File;
 
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;
 
30
import java.util.Map;
 
31
 
 
32
import org.dbdoclet.service.StringServices;
 
33
 
 
34
public class TemplateTransformer {
 
35
 
 
36
    private BufferedReader reader;
 
37
    
 
38
    public static TemplateTransformer newInstance(File template)
 
39
        throws TemplateTransformException {
 
40
 
 
41
        if (template == null) {
 
42
            throw new IllegalArgumentException("The argument template may not be null!");
 
43
        }
 
44
 
 
45
        BufferedReader reader = null;
 
46
        
 
47
        try {
 
48
            reader = new BufferedReader(new FileReader(template));
 
49
        } catch (IOException oops) {
 
50
            throw new TemplateTransformException(oops.getMessage(), oops);
 
51
        }
 
52
 
 
53
        return new TemplateTransformer(reader);
 
54
    }
 
55
 
 
56
    public static TemplateTransformer newInstance(BufferedReader template)
 
57
        throws TemplateTransformException {
 
58
 
 
59
        if (template == null) {
 
60
            throw new IllegalArgumentException("The argument template may not be null!");
 
61
        }
 
62
 
 
63
        return new TemplateTransformer(template);
 
64
    }
 
65
 
 
66
    public static TemplateTransformer newInstance(String template)
 
67
        throws TemplateTransformException {
 
68
 
 
69
        if (template == null) {
 
70
            throw new IllegalArgumentException("The argument template may not be null!");
 
71
        }
 
72
 
 
73
        return new TemplateTransformer(new BufferedReader(new StringReader(template)));
 
74
    }
 
75
 
 
76
    private TemplateTransformer(BufferedReader reader) {
 
77
 
 
78
        this.reader = reader;
 
79
    }
 
80
 
 
81
    public String transform(Map<String, String> vars)
 
82
        throws TemplateTransformException {
 
83
 
 
84
        StringWriter buffer = new StringWriter();
 
85
        
 
86
        transform(vars, new PrintWriter(buffer));
 
87
 
 
88
        return buffer.toString();
 
89
    }
 
90
 
 
91
    public void transform(Map<String, String> vars, String fileName)
 
92
        throws TemplateTransformException {
 
93
 
 
94
        if (fileName == null) {
 
95
            throw new IllegalArgumentException("The argument fileName must not be null!");
 
96
        }
 
97
 
 
98
        transform(vars, new File(fileName));
 
99
    }
 
100
 
 
101
    public void transform(Map<String, String> vars, File file)
 
102
        throws TemplateTransformException {
 
103
 
 
104
        if (file == null) {
 
105
            throw new IllegalArgumentException("The argument file must not be null!");
 
106
        }
 
107
 
 
108
        try {
 
109
 
 
110
            // if (file.getName().equals("default.css")) {
 
111
            //     trace = true;
 
112
            // }
 
113
 
 
114
            transform(vars, new FileWriter(file));
 
115
 
 
116
        } catch (IOException oops) {
 
117
 
 
118
            throw new TemplateTransformException(oops.getMessage(), oops);
 
119
        }
 
120
    }
 
121
 
 
122
    public void transform(Map<String, String> vars, FileWriter fileWriter)
 
123
        throws TemplateTransformException {
 
124
 
 
125
        if (fileWriter == null) {
 
126
            throw new IllegalArgumentException("The argument fileWriter must not be null!");
 
127
        }
 
128
        
 
129
        PrintWriter writer = new PrintWriter(fileWriter);
 
130
        transform(vars, writer);
 
131
        writer.close();
 
132
    }
 
133
 
 
134
    public void transform(Map<String, String> vars, PrintWriter writer)
 
135
        throws TemplateTransformException {
 
136
 
 
137
        if (reader == null) {
 
138
            throw new IllegalStateException("The field reader may not be null!");
 
139
        }
 
140
 
 
141
        if (vars == null) {
 
142
            throw new IllegalArgumentException("The argument vars may not be null!");
 
143
        }
 
144
 
 
145
        if (writer == null) {
 
146
            throw new IllegalArgumentException("The argument writer may not be null!");
 
147
        }
 
148
 
 
149
        try {
 
150
 
 
151
            Iterator<String> iterator;
 
152
            String name;
 
153
            Object value;
 
154
            
 
155
            String line;
 
156
            
 
157
            StringBuffer buffer = new StringBuffer();
 
158
            
 
159
            while ((line = reader.readLine()) != null ) {
 
160
 
 
161
                if (line.trim().length() == 0) {
 
162
 
 
163
                    buffer.append(line);
 
164
                    buffer.append('\n');
 
165
                    continue;
 
166
                }
 
167
 
 
168
                if (line.indexOf('$') == -1) {
 
169
 
 
170
                    buffer.append(line);
 
171
                    buffer.append('\n');
 
172
                    continue;
 
173
                }
 
174
 
 
175
                // if (trace) {
 
176
                //     System.out.println("line=" + line);
 
177
                // }
 
178
 
 
179
                iterator = vars.keySet().iterator();
 
180
                
 
181
                while (iterator.hasNext()) {
 
182
                    
 
183
                    name = iterator.next();
 
184
                    value = vars.get(name);
 
185
                    
 
186
                    if (value != null) {
 
187
 
 
188
                        name = "${" + name + "}";
 
189
 
 
190
                        // if (trace && name.equals("${css.hl1.font}")) {
 
191
                        //     System.out.println("name=" + name);
 
192
                        // }
 
193
 
 
194
                        line = StringServices.replace(line, name, value.toString());
 
195
 
 
196
                        // if (trace && name.equals("${css.hl1.font}")) {
 
197
                        //     System.out.println("new line=" + line);
 
198
                        // }
 
199
 
 
200
                    }
 
201
                }
 
202
 
 
203
                buffer.append(line);
 
204
                buffer.append('\n');
 
205
            }
 
206
 
 
207
            reader.close();
 
208
 
 
209
            // String str = ReplaceServices.replaceAll(buffer.toString(), "\\$\\{\\w*\\}", "");
 
210
            String str = buffer.toString();
 
211
            
 
212
            BufferedReader bufferReader = new BufferedReader(new StringReader(str));
 
213
 
 
214
            while ((line = bufferReader.readLine()) != null ) {
 
215
                writer.println(line);
 
216
            }
 
217
            
 
218
            bufferReader.close();
 
219
            
 
220
        } catch (Exception oops) {
 
221
            throw new TemplateTransformException(oops.getMessage(), oops);
 
222
        }
 
223
    }
 
224
}