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 fr.paris.lutece.portal.service.util.AppPropertiesService;
37
38 import java.io.StringWriter;
39
40 import java.util.Map;
41 import java.util.Map.Entry;
42 import java.util.Properties;
43
44 import javax.xml.transform.Result;
45 import javax.xml.transform.Source;
46 import javax.xml.transform.Transformer;
47 import javax.xml.transform.TransformerConfigurationException;
48 import javax.xml.transform.TransformerException;
49 import javax.xml.transform.TransformerFactory;
50 import javax.xml.transform.TransformerFactoryConfigurationError;
51 import javax.xml.transform.stream.StreamResult;
52
53
54
55
56
57 public final class XmlUtil
58 {
59 public static final String PROPERTIES_XML_HEADER = "xml.header";
60 private static final String TAG_BEGIN = "<";
61 private static final String TAG_CLOSE_BEGIN = "</";
62 private static final String TAG_END = ">\r\n";
63 private static final String TAG_CLOSE_END = " />\r\n";
64 private static final String TAG_SEPARATOR = " ";
65 private static final String TAG_ASSIGNMENT = "=";
66 private static final String TAG_ENCLOSED = "\"";
67
68
69
70
71 private XmlUtil( )
72 {
73 }
74
75
76
77
78
79 public static String getXmlHeader( )
80 {
81 String strXmlHeader = AppPropertiesService.getProperty( PROPERTIES_XML_HEADER );
82
83 return strXmlHeader;
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97 @Deprecated
98 public static String transform( Source source, Source stylesheet, Map<String, String> params,
99 Properties outputProperties ) throws Exception
100 {
101 try
102 {
103 TransformerFactory factory = TransformerFactory.newInstance( );
104 Transformer transformer = factory.newTransformer( stylesheet );
105
106 if ( outputProperties != null )
107 {
108 transformer.setOutputProperties( outputProperties );
109 }
110
111 if ( params != null )
112 {
113 transformer.clearParameters( );
114
115 for ( Entry<String, String> entry : params.entrySet( ) )
116 {
117 String name = entry.getKey( );
118 String value = entry.getValue( );
119 transformer.setParameter( name, value );
120 }
121 }
122
123 StringWriter sw = new StringWriter( );
124 Result result = new StreamResult( sw );
125 transformer.transform( source, result );
126
127 return sw.toString( );
128 }
129 catch ( TransformerConfigurationException e )
130 {
131 String strMessage = e.getMessage( );
132
133 if ( e.getLocationAsString( ) != null )
134 {
135 strMessage += ( "- location : " + e.getLocationAsString( ) );
136 }
137
138 throw new Exception( "Error transforming document XSLT : " + strMessage, e.getCause( ) );
139 }
140 catch ( TransformerFactoryConfigurationError e )
141 {
142 throw new Exception( "Error transforming document XSLT : " + e.getMessage( ), e );
143 }
144 catch ( TransformerException e )
145 {
146 String strMessage = e.getMessage( );
147
148 if ( e.getLocationAsString( ) != null )
149 {
150 strMessage += ( "- location : " + e.getLocationAsString( ) );
151 }
152
153 throw new Exception( "Error transforming document XSLT : " + strMessage, e.getCause( ) );
154 }
155 catch ( Exception e )
156 {
157 throw new Exception( "Error transforming document XSLT : " + e.getMessage( ), e );
158 }
159 }
160
161
162
163
164
165
166
167
168 public static void addElement( StringBuffer strXmlBuffer, String strTag, String strValue )
169 {
170 strXmlBuffer.append( TAG_BEGIN );
171 strXmlBuffer.append( strTag );
172 strXmlBuffer.append( ">" );
173 strXmlBuffer.append( strValue );
174 strXmlBuffer.append( TAG_CLOSE_BEGIN );
175 strXmlBuffer.append( strTag );
176 strXmlBuffer.append( TAG_END );
177 }
178
179
180
181
182
183
184
185
186 public static void addEmptyElement( StringBuffer strXmlBuffer, String strTag, Map<?, ?> attrList )
187 {
188 strXmlBuffer.append( TAG_BEGIN );
189 strXmlBuffer.append( strTag );
190
191 if ( attrList != null )
192 {
193 for ( Entry<?, ?> entry : attrList.entrySet( ) )
194 {
195 String code = (String) entry.getKey( );
196 strXmlBuffer.append( TAG_SEPARATOR + code + TAG_ASSIGNMENT + TAG_ENCLOSED + entry.getValue( ) +
197 TAG_ENCLOSED );
198 }
199 }
200
201 strXmlBuffer.append( TAG_CLOSE_END );
202 }
203
204
205
206
207
208
209
210
211
212 public static void addElement( StringBuffer strXmlBuffer, String strTag, String strValue, Map<?, ?> attrList )
213 {
214 strXmlBuffer.append( TAG_BEGIN );
215 strXmlBuffer.append( strTag );
216
217 if ( attrList != null )
218 {
219 for ( Entry<?, ?> entry : attrList.entrySet( ) )
220 {
221 String code = (String) entry.getKey( );
222 strXmlBuffer.append( TAG_SEPARATOR + code + TAG_ASSIGNMENT + TAG_ENCLOSED + entry.getValue( ) +
223 TAG_ENCLOSED );
224 }
225 }
226
227 strXmlBuffer.append( ">" );
228 strXmlBuffer.append( strValue );
229 strXmlBuffer.append( TAG_CLOSE_BEGIN );
230 strXmlBuffer.append( strTag );
231 strXmlBuffer.append( TAG_END );
232 }
233
234
235
236
237
238
239
240
241 public static void addElement( StringBuffer strXmlBuffer, String strTag, int nValue )
242 {
243 addElement( strXmlBuffer, strTag, String.valueOf( nValue ) );
244 }
245
246
247
248
249
250
251
252
253 public static void addElementHtml( StringBuffer strXmlBuffer, String strTag, String strValue )
254 {
255 strXmlBuffer.append( TAG_BEGIN );
256 strXmlBuffer.append( strTag );
257 strXmlBuffer.append( "><![CDATA[" );
258 strXmlBuffer.append( strValue );
259 strXmlBuffer.append( "]]></" );
260 strXmlBuffer.append( strTag );
261 strXmlBuffer.append( TAG_END );
262 }
263
264
265
266
267
268
269
270
271
272 public static void addElementHtml( StringBuffer strXmlBuffer, String strTag, String strValue, Map<?, ?> attrList )
273 {
274 strXmlBuffer.append( TAG_BEGIN );
275 strXmlBuffer.append( strTag );
276
277 if ( attrList != null )
278 {
279 for ( Entry<?, ?> entry : attrList.entrySet( ) )
280 {
281 String code = (String) entry.getKey( );
282 strXmlBuffer.append( TAG_SEPARATOR + code + TAG_ASSIGNMENT + TAG_ENCLOSED + entry.getValue( ) +
283 TAG_ENCLOSED );
284 }
285 }
286
287 strXmlBuffer.append( "><![CDATA[" );
288 strXmlBuffer.append( strValue );
289 strXmlBuffer.append( "]]></" );
290 strXmlBuffer.append( strTag );
291 strXmlBuffer.append( TAG_END );
292 }
293
294
295
296
297
298
299
300 public static void beginElement( StringBuffer strXmlBuffer, String strTag )
301 {
302 beginElement( strXmlBuffer, strTag, null );
303 }
304
305
306
307
308
309
310
311
312 public static void beginElement( StringBuffer strXmlBuffer, String strTag, Map<?, ?> attrList )
313 {
314 strXmlBuffer.append( TAG_BEGIN );
315 strXmlBuffer.append( strTag );
316
317 if ( attrList != null )
318 {
319 for ( Entry<?, ?> entry : attrList.entrySet( ) )
320 {
321 String code = (String) entry.getKey( );
322 strXmlBuffer.append( TAG_SEPARATOR + code + TAG_ASSIGNMENT + TAG_ENCLOSED + entry.getValue( ) +
323 TAG_ENCLOSED );
324 }
325 }
326
327 strXmlBuffer.append( TAG_END );
328 }
329
330
331
332
333
334
335
336 public static void endElement( StringBuffer strXmlBuffer, String strTag )
337 {
338 strXmlBuffer.append( TAG_CLOSE_BEGIN );
339 strXmlBuffer.append( strTag );
340 strXmlBuffer.append( TAG_END );
341 }
342 }