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 package fr.paris.lutece.util.xml;
35
36 import java.util.Map;
37 import java.util.Map.Entry;
38
39 import fr.paris.lutece.portal.service.util.AppPropertiesService;
40 import fr.paris.lutece.util.http.SecurityUtil;
41
42
43
44
45 public final class XmlUtil
46 {
47 public static final String PROPERTIES_XML_HEADER = "xml.header";
48 private static final String TAG_BEGIN = "<";
49 private static final String TAG_CLOSE_BEGIN = "</";
50 private static final String TAG_END = ">\r\n";
51 private static final String TAG_CLOSE_END = " />\r\n";
52 private static final String TAG_SEPARATOR = " ";
53 private static final String TAG_ASSIGNMENT = "=";
54 private static final String TAG_ENCLOSED = "\"";
55
56
57
58
59 private XmlUtil( )
60 {
61 }
62
63
64
65
66
67
68 public static String getXmlHeader( )
69 {
70 return AppPropertiesService.getProperty( PROPERTIES_XML_HEADER );
71 }
72
73
74
75
76
77
78
79
80
81
82
83 public static void addElement( StringBuffer strXmlBuffer, String strTag, String strValue )
84 {
85 if ( SecurityUtil.containsXmlExternalEntityInjectionTerms( strValue ) )
86 {
87 return;
88 }
89
90 strXmlBuffer.append( TAG_BEGIN );
91 strXmlBuffer.append( strTag );
92 strXmlBuffer.append( ">" );
93 strXmlBuffer.append( strValue );
94 strXmlBuffer.append( TAG_CLOSE_BEGIN );
95 strXmlBuffer.append( strTag );
96 strXmlBuffer.append( TAG_END );
97 }
98
99
100
101
102
103
104
105
106
107
108
109 public static void addEmptyElement( StringBuffer strXmlBuffer, String strTag, Map<?, ?> attrList )
110 {
111 strXmlBuffer.append( TAG_BEGIN );
112 strXmlBuffer.append( strTag );
113
114 if ( attrList != null )
115 {
116 for ( Entry<?, ?> entry : attrList.entrySet( ) )
117 {
118 String code = (String) entry.getKey( );
119 strXmlBuffer.append( TAG_SEPARATOR + code + TAG_ASSIGNMENT + TAG_ENCLOSED + entry.getValue( ) + TAG_ENCLOSED );
120 }
121 }
122
123 strXmlBuffer.append( TAG_CLOSE_END );
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138 public static void addElement( StringBuffer strXmlBuffer, String strTag, String strValue, Map<?, ?> attrList )
139 {
140 if ( SecurityUtil.containsXmlExternalEntityInjectionTerms( strValue ) )
141 {
142 return;
143 }
144
145 strXmlBuffer.append( TAG_BEGIN );
146 strXmlBuffer.append( strTag );
147
148 if ( attrList != null )
149 {
150 for ( Entry<?, ?> entry : attrList.entrySet( ) )
151 {
152 String code = (String) entry.getKey( );
153 strXmlBuffer.append( TAG_SEPARATOR + code + TAG_ASSIGNMENT + TAG_ENCLOSED + entry.getValue( ) + TAG_ENCLOSED );
154 }
155 }
156
157 strXmlBuffer.append( ">" );
158 strXmlBuffer.append( strValue );
159 strXmlBuffer.append( TAG_CLOSE_BEGIN );
160 strXmlBuffer.append( strTag );
161 strXmlBuffer.append( TAG_END );
162 }
163
164
165
166
167
168
169
170
171
172
173
174 public static void addElement( StringBuffer strXmlBuffer, String strTag, int nValue )
175 {
176 addElement( strXmlBuffer, strTag, String.valueOf( nValue ) );
177 }
178
179
180
181
182
183
184
185
186
187
188
189 public static void addElementHtml( StringBuffer strXmlBuffer, String strTag, String strValue )
190 {
191 if ( SecurityUtil.containsXmlExternalEntityInjectionTerms( strValue ) )
192 {
193 return;
194 }
195
196 strXmlBuffer.append( TAG_BEGIN );
197 strXmlBuffer.append( strTag );
198 strXmlBuffer.append( "><![CDATA[" );
199 strXmlBuffer.append( strValue );
200 strXmlBuffer.append( "]]></" );
201 strXmlBuffer.append( strTag );
202 strXmlBuffer.append( TAG_END );
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216
217 public static void addElementHtml( StringBuffer strXmlBuffer, String strTag, String strValue, Map<?, ?> attrList )
218 {
219 if ( SecurityUtil.containsXmlExternalEntityInjectionTerms( strValue ) )
220 {
221 return;
222 }
223
224 strXmlBuffer.append( TAG_BEGIN );
225 strXmlBuffer.append( strTag );
226
227 if ( attrList != null )
228 {
229 for ( Entry<?, ?> entry : attrList.entrySet( ) )
230 {
231 String code = (String) entry.getKey( );
232 strXmlBuffer.append( TAG_SEPARATOR + code + TAG_ASSIGNMENT + TAG_ENCLOSED + entry.getValue( ) + TAG_ENCLOSED );
233 }
234 }
235
236 strXmlBuffer.append( "><![CDATA[" );
237 strXmlBuffer.append( strValue );
238 strXmlBuffer.append( "]]></" );
239 strXmlBuffer.append( strTag );
240 strXmlBuffer.append( TAG_END );
241
242 }
243
244
245
246
247
248
249
250
251
252 public static void beginElement( StringBuffer strXmlBuffer, String strTag )
253 {
254 beginElement( strXmlBuffer, strTag, null );
255 }
256
257
258
259
260
261
262
263
264
265
266
267 public static void beginElement( StringBuffer strXmlBuffer, String strTag, Map<?, ?> attrList )
268 {
269 strXmlBuffer.append( TAG_BEGIN );
270 strXmlBuffer.append( strTag );
271
272 if ( attrList != null )
273 {
274 for ( Entry<?, ?> entry : attrList.entrySet( ) )
275 {
276 String code = (String) entry.getKey( );
277 strXmlBuffer.append( TAG_SEPARATOR + code + TAG_ASSIGNMENT + TAG_ENCLOSED + entry.getValue( ) + TAG_ENCLOSED );
278 }
279 }
280
281 strXmlBuffer.append( TAG_END );
282 }
283
284
285
286
287
288
289
290
291
292 public static void endElement( StringBuffer strXmlBuffer, String strTag )
293 {
294 strXmlBuffer.append( TAG_CLOSE_BEGIN );
295 strXmlBuffer.append( strTag );
296 strXmlBuffer.append( TAG_END );
297 }
298 }