XmlTransformerService.java

  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. import java.io.ByteArrayInputStream;
  36. import java.io.StringReader;
  37. import java.util.Map;
  38. import java.util.Properties;

  39. import javax.xml.transform.Source;
  40. import javax.xml.transform.stream.StreamSource;

  41. import org.apache.logging.log4j.LogManager;
  42. import org.apache.logging.log4j.Logger;

  43. import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
  44. import fr.paris.lutece.portal.service.util.AppLogService;
  45. import fr.paris.lutece.util.UniqueIDGenerator;
  46. import fr.paris.lutece.util.xml.XmlTransformer;

  47. /**
  48.  * This class provides methods to transform XML documents using XSLT.
  49.  */
  50. public final class XmlTransformerService
  51. {
  52.     private static final String XSLSOURCE_STYLE_PREFIX_ID = UniqueIDGenerator.getNewId( );
  53.     private static final String LOGGER_XML_CONTENT = "lutece.debug.xmlContent";
  54.     private static final Logger _log = LogManager.getLogger( LOGGER_XML_CONTENT );

  55.     /**
  56.      * This method performs XSL transformation with cache.
  57.      *
  58.      * @param strXml
  59.      *            The XML document content
  60.      * @param xslSource
  61.      *            The XSL source
  62.      * @param params
  63.      *            Parameters that can be used by the XSL StyleSheet
  64.      * @return the output html
  65.      */
  66.     public String transformBySourceWithXslCache( String strXml, StyleSheet xslSource, Map<String, String> params )
  67.     {
  68.         return transformBySourceWithXslCache( strXml, xslSource.getSource( ), XSLSOURCE_STYLE_PREFIX_ID + xslSource.getId( ), params, null );
  69.     }

  70.     /**
  71.      * This method performs XSL transformation with cache.
  72.      *
  73.      * @param strXml
  74.      *            The XML document content
  75.      * @param xslSource
  76.      *            The XSL source
  77.      * @param params
  78.      *            Parameters that can be used by the XSL StyleSheet
  79.      * @param outputProperties
  80.      *            Properties to use for the XSL transform. Will overload the XSL output definition.
  81.      * @return the output html
  82.      */
  83.     public String transformBySourceWithXslCache( String strXml, StyleSheet xslSource, Map<String, String> params, Properties outputProperties )
  84.     {
  85.         return transformBySourceWithXslCache( strXml, xslSource.getSource( ), XSLSOURCE_STYLE_PREFIX_ID + xslSource.getId( ), params, outputProperties );
  86.     }

  87.     /**
  88.      * This method performs XSL transformation with cache.
  89.      *
  90.      * @param strXml
  91.      *            The XML document content
  92.      * @param baSource
  93.      *            The XSL source
  94.      * @param strStyleSheetId
  95.      *            The StyleSheet Id
  96.      * @param params
  97.      *            Parameters that can be used by the XSL StyleSheet
  98.      * @return The output document
  99.      */
  100.     public String transformBySourceWithXslCache( String strXml, byte [ ] baSource, String strStyleSheetId, Map<String, String> params )
  101.     {
  102.         return transformBySourceWithXslCache( strXml, baSource, strStyleSheetId, params, null );
  103.     }

  104.     /**
  105.      * This method performs XSL transformation with cache.
  106.      *
  107.      * @param strXml
  108.      *            The XML document content
  109.      * @param baSource
  110.      *            The XSL source
  111.      * @param strStyleSheetId
  112.      *            The StyleSheet Id
  113.      * @param params
  114.      *            Parameters that can be used by the XSL StyleSheet
  115.      * @param outputProperties
  116.      *            Properties to use for the XSL transform. Will overload the XSL output definition.
  117.      * @return The output document
  118.      */
  119.     public String transformBySourceWithXslCache( String strXml, byte [ ] baSource, String strStyleSheetId, Map<String, String> params,
  120.             Properties outputProperties )
  121.     {
  122.         Source xslSource = new StreamSource( new ByteArrayInputStream( baSource ) );

  123.         return transformBySourceWithXslCache( strXml, xslSource, strStyleSheetId, params, outputProperties );
  124.     }

  125.     /**
  126.      * This method performs XSL transformation with cache.
  127.      *
  128.      * @param strXml
  129.      *            The XML document content
  130.      * @param sourceStyleSheet
  131.      *            The XSL source
  132.      * @param strStyleSheetId
  133.      *            The StyleSheet Id
  134.      * @param params
  135.      *            Parameters that can be used by the XSL StyleSheet
  136.      * @param outputProperties
  137.      *            the output parameter
  138.      * @return The output document
  139.      */
  140.     public String transformBySourceWithXslCache( String strXml, Source sourceStyleSheet, String strStyleSheetId, Map<String, String> params,
  141.             Properties outputProperties )
  142.     {
  143.         StringReader srInputXml = new StringReader( strXml );
  144.         StreamSource sourceDocument = new StreamSource( srInputXml );
  145.         String strContent = null;
  146.         XmlTransformer xmlTransformer = new XmlTransformer( );

  147.         try
  148.         {
  149.             _log.debug( strXml );
  150.             strContent = xmlTransformer.transform( sourceDocument, sourceStyleSheet, strStyleSheetId, params, outputProperties );
  151.         }
  152.         catch( Exception e )
  153.         {
  154.             strContent = e.getMessage( );
  155.             AppLogService.error( e.getMessage( ), e );
  156.         }

  157.         return strContent;
  158.     }

  159.     /**
  160.      * This method performs XSL transformation with cache.
  161.      *
  162.      * @param sourceXml
  163.      *            The XML document content
  164.      * @param sourceStyleSheet
  165.      *            The XSL source
  166.      * @param strStyleSheetId
  167.      *            The StyleSheet Id
  168.      * @param params
  169.      *            Parameters that can be used by the XSL StyleSheet
  170.      * @param outputProperties
  171.      *            the output parameter
  172.      * @return The output document
  173.      */
  174.     public String transformBySourceWithXslCache( Source sourceXml, Source sourceStyleSheet, String strStyleSheetId, Map<String, String> params,
  175.             Properties outputProperties )
  176.     {
  177.         String strContent = null;
  178.         XmlTransformer xmlTransformer = new XmlTransformer( );

  179.         try
  180.         {
  181.             strContent = xmlTransformer.transform( sourceXml, sourceStyleSheet, strStyleSheetId, params, outputProperties );
  182.         }
  183.         catch( Exception e )
  184.         {
  185.             strContent = e.getMessage( );
  186.             AppLogService.error( e.getMessage( ), e );
  187.         }

  188.         return strContent;
  189.     }

  190.     /**
  191.      * This method clean XSL transformer cache
  192.      */
  193.     public static void clearXslCache( )
  194.     {
  195.         XmlTransformer.cleanTransformerList( );
  196.     }
  197. }