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.portal.service.html;
35
36 import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
37 import fr.paris.lutece.portal.service.util.AppLogService;
38 import fr.paris.lutece.util.UniqueIDGenerator;
39 import fr.paris.lutece.util.xml.XmlTransformer;
40 import fr.paris.lutece.util.xml.XmlUtil;
41
42 import java.io.ByteArrayInputStream;
43 import java.io.StringReader;
44
45 import java.util.Map;
46 import java.util.Properties;
47
48 import javax.xml.transform.Source;
49 import javax.xml.transform.stream.StreamSource;
50
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
60 /**
61 * This method performes XSL transformation.
62 *
63 * @param strXml The XML document content
64 * @param baSource The XSL source
65 * @param params Parameters that can be used by the XSL stylesheet
66 * @return The output document
67 */
68 @Deprecated
69 public static String transformBySource( String strXml, byte[] baSource, Map<String, String> params )
70 {
71 return transformBySource( strXml, baSource, params, null );
72 }
73
74 /**
75 * This method performes XSL transformation.
76 *
77 * @param strXml The XML document content
78 * @param baSource The XSL source
79 * @param params Parameters that can be used by the XSL stylesheet
80 * @param outputProperties properties to use for the xsl transform. Will overload the xsl output definition.
81 * @return The output document
82 */
83 @Deprecated
84 public static String transformBySource( String strXml, byte[] baSource, Map<String, String> params,
85 Properties outputProperties )
86 {
87 Source xslSource = new StreamSource( new ByteArrayInputStream( baSource ) );
88
89 return transformBySource( strXml, xslSource, params, outputProperties );
90 }
91
92 /**
93 * This method performes XSL transformation.
94 *
95 * @param strXml The XML document content
96 * @param sourceStyleSheet The XSL source
97 * @param params Parameters that can be used by the XSL stylesheet
98 * @param outputProperties properties to use for the xsl transform. Will overload the xsl output definition.
99 * @return The output document
100 */
101 @Deprecated
102 public static synchronized String transformBySource( String strXml, Source sourceStyleSheet,
103 Map<String, String> params, Properties outputProperties )
104 {
105 StringReader srInputXml = new StringReader( strXml );
106 StreamSource sourceDocument = new StreamSource( srInputXml );
107 String strContent = null;
108
109 try
110 {
111 strContent = XmlUtil.transform( sourceDocument, sourceStyleSheet, params, outputProperties );
112 }
113 catch ( Exception e )
114 {
115 strContent = e.getMessage( );
116 AppLogService.error( e.getMessage( ), e );
117 }
118
119 return strContent;
120 }
121
122 //-------------------------------
123
124 /**
125 * This method performs XSL transformation with cache.
126 * @param strXml The XML document content
127 * @param xslSource The XSL source
128 * @param params Parameters that can be used by the XSL StyleSheet
129 * @return the output html
130 */
131 public String transformBySourceWithXslCache( String strXml, StyleSheet xslSource, Map<String, String> params )
132 {
133 return transformBySourceWithXslCache( strXml, xslSource.getSource( ),
134 XSLSOURCE_STYLE_PREFIX_ID + xslSource.getId( ), params, null );
135 }
136
137 /**
138 * This method performs XSL transformation with cache.
139 * @param strXml The XML document content
140 * @param xslSource The XSL source
141 * @param params Parameters that can be used by the XSL StyleSheet
142 * @param outputProperties Properties to use for the XSL transform. Will overload the XSL output definition.
143 * @return the output html
144 */
145 public String transformBySourceWithXslCache( String strXml, StyleSheet xslSource, Map<String, String> params,
146 Properties outputProperties )
147 {
148 return transformBySourceWithXslCache( strXml, xslSource.getSource( ),
149 XSLSOURCE_STYLE_PREFIX_ID + xslSource.getId( ), params, outputProperties );
150 }
151
152 /**
153 * This method performs XSL transformation with cache.
154 * @param strXml The XML document content
155 * @param baSource The XSL source
156 * @param strStyleSheetId The StyleSheet Id
157 * @param params Parameters that can be used by the XSL StyleSheet
158 * @return The output document
159 */
160 public String transformBySourceWithXslCache( String strXml, byte[] baSource, String strStyleSheetId,
161 Map<String, String> params )
162 {
163 return transformBySourceWithXslCache( strXml, baSource, strStyleSheetId, params, null );
164 }
165
166 /**
167 * This method performs XSL transformation with cache.
168 * @param strXml The XML document content
169 * @param baSource The XSL source
170 * @param strStyleSheetId The StyleSheet Id
171 * @param params Parameters that can be used by the XSL StyleSheet
172 * @param outputProperties Properties to use for the XSL transform. Will overload the XSL output definition.
173 * @return The output document
174 */
175 public String transformBySourceWithXslCache( String strXml, byte[] baSource, String strStyleSheetId,
176 Map<String, String> params, Properties outputProperties )
177 {
178 Source xslSource = new StreamSource( new ByteArrayInputStream( baSource ) );
179
180 return transformBySourceWithXslCache( strXml, xslSource, strStyleSheetId, params, outputProperties );
181 }
182
183 /**
184 * This method performs XSL transformation with cache.
185 * @param strXml The XML document content
186 * @param sourceStyleSheet The XSL source
187 * @param strStyleSheetId The StyleSheet Id
188 * @param params Parameters that can be used by the XSL StyleSheet
189 * @param outputProperties the output parameter
190 * @return The output document
191 */
192 public String transformBySourceWithXslCache( String strXml, Source sourceStyleSheet, String strStyleSheetId,
193 Map<String, String> params, Properties outputProperties )
194 {
195 StringReader srInputXml = new StringReader( strXml );
196 StreamSource sourceDocument = new StreamSource( srInputXml );
197 String strContent = null;
198 XmlTransformer xmlTransformer = new XmlTransformer( );
199
200 try
201 {
202 if ( AppLogService.isDebugEnabled( LOGGER_XML_CONTENT ) )
203 {
204 AppLogService.debug( LOGGER_XML_CONTENT, strXml );
205 }
206
207 strContent = xmlTransformer.transform( sourceDocument, sourceStyleSheet, strStyleSheetId, params,
208 outputProperties );
209 }
210 catch ( Exception e )
211 {
212 strContent = e.getMessage( );
213 AppLogService.error( e.getMessage( ), e );
214 }
215
216 return strContent;
217 }
218
219 /**
220 * This method performs XSL transformation with cache.
221 * @param sourceXml The XML document content
222 * @param sourceStyleSheet The XSL source
223 * @param strStyleSheetId The StyleSheet Id
224 * @param params Parameters that can be used by the XSL StyleSheet
225 * @param outputProperties the output parameter
226 * @return The output document
227 */
228 public String transformBySourceWithXslCache( Source sourceXml, Source sourceStyleSheet, String strStyleSheetId,
229 Map<String, String> params, Properties outputProperties )
230 {
231 String strContent = null;
232 XmlTransformer xmlTransformer = new XmlTransformer( );
233
234 try
235 {
236 strContent = xmlTransformer.transform( sourceXml, sourceStyleSheet, strStyleSheetId, params,
237 outputProperties );
238 }
239 catch ( Exception e )
240 {
241 strContent = e.getMessage( );
242 AppLogService.error( e.getMessage( ), e );
243 }
244
245 return strContent;
246 }
247
248 /**
249 * This method clean XSL transformer cache
250 */
251 public static void clearXslCache( )
252 {
253 XmlTransformer.cleanTransformerList( );
254 }
255 }