View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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  package fr.paris.lutece.portal.service.html;
35  
36  import java.io.ByteArrayInputStream;
37  import java.io.StringReader;
38  import java.util.Map;
39  import java.util.Properties;
40  
41  import javax.xml.transform.Source;
42  import javax.xml.transform.stream.StreamSource;
43  
44  import org.apache.logging.log4j.LogManager;
45  import org.apache.logging.log4j.Logger;
46  
47  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  import fr.paris.lutece.util.UniqueIDGenerator;
50  import fr.paris.lutece.util.xml.XmlTransformer;
51  
52  /**
53   * This class provides methods to transform XML documents using XSLT.
54   */
55  public final class XmlTransformerService
56  {
57      private static final String XSLSOURCE_STYLE_PREFIX_ID = UniqueIDGenerator.getNewId( );
58      private static final String LOGGER_XML_CONTENT = "lutece.debug.xmlContent";
59      private static final Logger _log = LogManager.getLogger( LOGGER_XML_CONTENT );
60  
61      /**
62       * This method performs XSL transformation with cache.
63       * 
64       * @param strXml
65       *            The XML document content
66       * @param xslSource
67       *            The XSL source
68       * @param params
69       *            Parameters that can be used by the XSL StyleSheet
70       * @return the output html
71       */
72      public String transformBySourceWithXslCache( String strXml, StyleSheet xslSource, Map<String, String> params )
73      {
74          return transformBySourceWithXslCache( strXml, xslSource.getSource( ), XSLSOURCE_STYLE_PREFIX_ID + xslSource.getId( ), params, null );
75      }
76  
77      /**
78       * This method performs XSL transformation with cache.
79       * 
80       * @param strXml
81       *            The XML document content
82       * @param xslSource
83       *            The XSL source
84       * @param params
85       *            Parameters that can be used by the XSL StyleSheet
86       * @param outputProperties
87       *            Properties to use for the XSL transform. Will overload the XSL output definition.
88       * @return the output html
89       */
90      public String transformBySourceWithXslCache( String strXml, StyleSheet xslSource, Map<String, String> params, Properties outputProperties )
91      {
92          return transformBySourceWithXslCache( strXml, xslSource.getSource( ), XSLSOURCE_STYLE_PREFIX_ID + xslSource.getId( ), params, outputProperties );
93      }
94  
95      /**
96       * This method performs XSL transformation with cache.
97       * 
98       * @param strXml
99       *            The XML document content
100      * @param baSource
101      *            The XSL source
102      * @param strStyleSheetId
103      *            The StyleSheet Id
104      * @param params
105      *            Parameters that can be used by the XSL StyleSheet
106      * @return The output document
107      */
108     public String transformBySourceWithXslCache( String strXml, byte [ ] baSource, String strStyleSheetId, Map<String, String> params )
109     {
110         return transformBySourceWithXslCache( strXml, baSource, strStyleSheetId, params, null );
111     }
112 
113     /**
114      * This method performs XSL transformation with cache.
115      * 
116      * @param strXml
117      *            The XML document content
118      * @param baSource
119      *            The XSL source
120      * @param strStyleSheetId
121      *            The StyleSheet Id
122      * @param params
123      *            Parameters that can be used by the XSL StyleSheet
124      * @param outputProperties
125      *            Properties to use for the XSL transform. Will overload the XSL output definition.
126      * @return The output document
127      */
128     public String transformBySourceWithXslCache( String strXml, byte [ ] baSource, String strStyleSheetId, Map<String, String> params,
129             Properties outputProperties )
130     {
131         Source xslSource = new StreamSource( new ByteArrayInputStream( baSource ) );
132 
133         return transformBySourceWithXslCache( strXml, xslSource, strStyleSheetId, params, outputProperties );
134     }
135 
136     /**
137      * This method performs XSL transformation with cache.
138      * 
139      * @param strXml
140      *            The XML document content
141      * @param sourceStyleSheet
142      *            The XSL source
143      * @param strStyleSheetId
144      *            The StyleSheet Id
145      * @param params
146      *            Parameters that can be used by the XSL StyleSheet
147      * @param outputProperties
148      *            the output parameter
149      * @return The output document
150      */
151     public String transformBySourceWithXslCache( String strXml, Source sourceStyleSheet, String strStyleSheetId, Map<String, String> params,
152             Properties outputProperties )
153     {
154         StringReader srInputXml = new StringReader( strXml );
155         StreamSource sourceDocument = new StreamSource( srInputXml );
156         String strContent = null;
157         XmlTransformerr.html#XmlTransformer">XmlTransformer xmlTransformer = new XmlTransformer( );
158 
159         try
160         {
161             _log.debug( strXml );
162             strContent = xmlTransformer.transform( sourceDocument, sourceStyleSheet, strStyleSheetId, params, outputProperties );
163         }
164         catch( Exception e )
165         {
166             strContent = e.getMessage( );
167             AppLogService.error( e.getMessage( ), e );
168         }
169 
170         return strContent;
171     }
172 
173     /**
174      * This method performs XSL transformation with cache.
175      * 
176      * @param sourceXml
177      *            The XML document content
178      * @param sourceStyleSheet
179      *            The XSL source
180      * @param strStyleSheetId
181      *            The StyleSheet Id
182      * @param params
183      *            Parameters that can be used by the XSL StyleSheet
184      * @param outputProperties
185      *            the output parameter
186      * @return The output document
187      */
188     public String transformBySourceWithXslCache( Source sourceXml, Source sourceStyleSheet, String strStyleSheetId, Map<String, String> params,
189             Properties outputProperties )
190     {
191         String strContent = null;
192         XmlTransformerr.html#XmlTransformer">XmlTransformer xmlTransformer = new XmlTransformer( );
193 
194         try
195         {
196             strContent = xmlTransformer.transform( sourceXml, sourceStyleSheet, strStyleSheetId, params, outputProperties );
197         }
198         catch( Exception e )
199         {
200             strContent = e.getMessage( );
201             AppLogService.error( e.getMessage( ), e );
202         }
203 
204         return strContent;
205     }
206 
207     /**
208      * This method clean XSL transformer cache
209      */
210     public static void clearXslCache( )
211     {
212         XmlTransformer.cleanTransformerList( );
213     }
214 }