View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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  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   * This class provides utils for XML document management.
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       * Instantiates a new xml util.
70       */
71      private XmlUtil(  )
72      {
73      }
74  
75      /**
76       * Gets the header of an XML file
77       * @return The header
78       */
79      public static String getXmlHeader(  )
80      {
81          String strXmlHeader = AppPropertiesService.getProperty( PROPERTIES_XML_HEADER );
82  
83          return strXmlHeader;
84      }
85  
86      /**
87       * This method performs XSL Transformation.
88       * <br />
89       * <b>Deprecated use XmlTransformer.transform</b>
90       * @param source The input XML document
91       * @param stylesheet The XSL stylesheet
92       * @param params parameters to apply to the XSL Stylesheet
93       * @param outputProperties properties to use for the xsl transform. Will overload the xsl output definition.
94       * @return The output document transformed
95       * @throws Exception The exception
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      * Add an element to an XML document buffer
163      *
164      * @param strXmlBuffer The XML document buffer
165      * @param strTag The tag name of the element to add
166      * @param strValue The value of the element
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      * Add an empty element (<   />) to an XML document buffer.
181      *
182      * @param strXmlBuffer The XML document buffer
183      * @param strTag The tag name of the element to add
184      * @param attrList The attributes list
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      * Add an element to an XML document buffer with attributes
206      *
207      * @param strXmlBuffer The XML document buffer
208      * @param strTag The tag name of the element to add
209      * @param strValue The value of the element
210      * @param attrList the attribute list
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      * Add an element to an XML document buffer.
236      *
237      * @param strXmlBuffer The XML document buffer
238      * @param strTag The tag name of the element to add
239      * @param nValue The value of the element
240      */
241     public static void addElement( StringBuffer strXmlBuffer, String strTag, int nValue )
242     {
243         addElement( strXmlBuffer, strTag, String.valueOf( nValue ) );
244     }
245 
246     /**
247      * Add a CDATA type element to XML document buffer.
248      *
249      * @param strXmlBuffer The XML document buffer
250      * @param strTag The tag name of the element to add
251      * @param strValue The value of the element
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      * Add a CDATA type element to XML document buffer.
266      *
267      * @param strXmlBuffer The XML document buffer
268      * @param strTag The tag name of the element to add
269      * @param strValue The value of the element
270      * @param attrList The attributes list
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      * Add an opening tag for an element in a XML document buffer
296      *
297      * @param strXmlBuffer The XML document buffer
298      * @param strTag The tag name of the element to add
299      */
300     public static void beginElement( StringBuffer strXmlBuffer, String strTag )
301     {
302         beginElement( strXmlBuffer, strTag, null );
303     }
304 
305     /**
306      * Add an opening tag for an element in a XML document buffer
307      *
308      * @param strXmlBuffer The XML document buffer
309      * @param strTag The tag name of the element to add
310      * @param attrList The attributes list
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      * Add a closing tag for an element in a XML document buffer
332      *
333      * @param strXmlBuffer The XML document buffer
334      * @param strTag The tag name of the element to add
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 }