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 package fr.paris.lutece.util.html;
38
39 import java.io.BufferedReader;
40 import java.io.FileReader;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.io.InputStreamReader;
44
45 import java.text.FieldPosition;
46 import java.text.SimpleDateFormat;
47
48 import java.util.Locale;
49
50
51
52
53
54
55
56 public class HtmlTemplate
57 {
58 private String _strTemplate;
59
60
61
62
63 public HtmlTemplate( )
64 {
65 }
66
67
68
69
70
71
72 public HtmlTemplate( String strTemplate )
73 {
74 _strTemplate = strTemplate;
75 }
76
77
78
79
80
81
82 public HtmlTemplatef="../../../../../fr/paris/lutece/util/html/HtmlTemplate.html#HtmlTemplate">HtmlTemplate( HtmlTemplate template )
83 {
84 _strTemplate = template.getHtml( );
85 }
86
87
88
89
90
91
92
93 public void load( String strFilename ) throws IOException
94 {
95
96 try ( BufferedReader in = new BufferedReader( new FileReader( strFilename ) ) )
97 {
98 String strLine;
99 StringBuilder sbContent = new StringBuilder( );
100
101 while ( ( strLine = in.readLine( ) ) != null )
102 {
103 sbContent.append( strLine ).append( "\r\n" );
104 }
105 _strTemplate = sbContent.toString( );
106 }
107 }
108
109
110
111
112
113
114
115 public void load( InputStream is ) throws IOException
116 {
117 try ( BufferedReader in = new BufferedReader( new InputStreamReader( is ) ) )
118 {
119 String strLine;
120 StringBuilder sbContent = new StringBuilder( );
121
122 while ( ( strLine = in.readLine( ) ) != null )
123 {
124 sbContent.append( strLine ).append( "\r\n" );
125 }
126 _strTemplate = sbContent.toString( );
127
128 }
129 }
130
131
132
133
134
135
136 public String getHtml( )
137 {
138 return _strTemplate;
139 }
140
141
142
143
144
145
146
147 public void substitute( String strBookmark, String strValue )
148 {
149 _strTemplate = substitute( _strTemplate, strValue, strBookmark );
150 }
151
152
153
154
155
156
157
158 public void substitute( String strBookmark, int nValue )
159 {
160 String strValue = String.valueOf( nValue );
161 substitute( strBookmark, strValue );
162 }
163
164
165
166
167
168
169
170 public void substitute( String strBookmark, java.sql.Date date )
171 {
172 String strValue = getDateString( date );
173 substitute( strBookmark, strValue );
174 }
175
176
177
178
179
180
181
182 public void substitute( String strBookmark, java.sql.Timestamp date )
183 {
184 String strValue = getDateString( date );
185 substitute( strBookmark, strValue );
186 }
187
188
189
190
191
192
193
194 private static String getDateString( java.util.Date date )
195 {
196 if ( date != null )
197 {
198 SimpleDateFormat formatter = new SimpleDateFormat( "dd'/'MM'/'yyyy", Locale.FRANCE );
199 StringBuffer strDate = new StringBuffer( );
200 formatter.format( date, strDate, new FieldPosition( 0 ) );
201
202 return strDate.toString( );
203 }
204
205 return "";
206 }
207
208
209
210
211
212
213
214
215
216 private static String substitute( String strSource, String strValue, String strBookmark )
217 {
218 StringBuilder strResult = new StringBuilder( );
219 int nPos = strSource.indexOf( strBookmark );
220 String strModifySource = strSource;
221
222 while ( nPos != -1 )
223 {
224 strResult.append( strModifySource.substring( 0, nPos ) );
225 strResult.append( strValue );
226 strModifySource = strModifySource.substring( nPos + strBookmark.length( ) );
227 nPos = strModifySource.indexOf( strBookmark );
228 }
229
230 strResult.append( strModifySource );
231
232 return strResult.toString( );
233 }
234 }