1 /*
2 * Copyright (c) 2002-2020, City of Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34
35 ////////////////////////////////////////////////////////////////////////
36 // HtmlTemplate.java
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 * This class represents an HTML template that may include bookmarks that can be
52 * substitute by values.
53 *
54 * @version 1.2.5
55 */
56 public class HtmlTemplate
57 {
58 private String _strTemplate;
59
60 /**
61 * Constructor 1
62 */
63 public HtmlTemplate( )
64 {
65 }
66
67 /**
68 * Constructor 2
69 *
70 * @param strTemplate The template as a string
71 */
72 public HtmlTemplate( String strTemplate )
73 {
74 _strTemplate = strTemplate;
75 }
76
77 /**
78 * Constructor 3
79 *
80 * @param template Copy constructor based on another template.
81 */
82 public HtmlTemplatef="../../../../../fr/paris/lutece/util/html/HtmlTemplate.html#HtmlTemplate">HtmlTemplate( HtmlTemplate template )
83 {
84 _strTemplate = template.getHtml( );
85 }
86
87 /**
88 * Load the template from a file
89 *
90 * @param strFilename The file name to load
91 * @throws IOException If an error occured
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 * Load the template from an InputStream
111 *
112 * @param is The open InputStream that point on the template
113 * @throws IOException If an error occured
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 * Returns the template.
133 *
134 * @return The template as a string.
135 */
136 public String getHtml( )
137 {
138 return _strTemplate;
139 }
140
141 /**
142 * Substitute each appearance of a bookmark by a given value.
143 *
144 * @param strBookmark The bookmark that must be present in the template.
145 * @param strValue The value to substitute as a String.
146 */
147 public void substitute( String strBookmark, String strValue )
148 {
149 _strTemplate = substitute( _strTemplate, strValue, strBookmark );
150 }
151
152 /**
153 * Substitute each appearance of a bookmark by a given value.
154 *
155 * @param strBookmark The bookmark that must be present in the template.
156 * @param nValue The value to substitute as an integer.
157 */
158 public void substitute( String strBookmark, int nValue )
159 {
160 String strValue = String.valueOf( nValue );
161 substitute( strBookmark, strValue );
162 }
163
164 /**
165 * Substitute each appearance of a bookmark by a given value.
166 *
167 * @param strBookmark The bookmark that must be present in the template.
168 * @param date The value to substitute as a Date.
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 * Substitute each occurence of a bookmark by a given value.
178 *
179 * @param strBookmark The bookmark that must be present in the template.
180 * @param date The value to substitute as a Timestamp.
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 * Converts a date value to a String date
190 *
191 * @param date The date
192 * @return The formatted string
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 * This function substitutes all occurences of a given bookmark by a given value
210 *
211 * @param strSource The input string that contains bookmarks to replace
212 * @param strValue The value to substitute to the bookmark
213 * @param strBookmark The bookmark name
214 * @return The output string.
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 }