EditorBbcodeService.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.editor;

  35. import fr.paris.lutece.portal.business.editor.ParserComplexElement;
  36. import fr.paris.lutece.portal.business.editor.ParserElement;
  37. import fr.paris.lutece.portal.service.util.AppLogService;
  38. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  39. import fr.paris.lutece.util.parser.BbcodeUtil;

  40. import org.apache.commons.lang3.StringUtils;
  41. import org.jsoup.Jsoup;
  42. import org.jsoup.safety.Safelist;

  43. import java.util.ArrayList;
  44. import java.util.List;

  45. /**
  46.  *
  47.  * This class Provides a parser BBCODE
  48.  *
  49.  */
  50. public class EditorBbcodeService implements IEditorBbcodeService
  51. {
  52.     /** Constants **/
  53.     private static final String CONSTANT_EDITOR_BBCODE_ELEMENT_CODE = "code";
  54.     private static final String CONSTANT_EDITOR_BBCODE_ELEMENT_VALUE = "value";
  55.     private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_TAG_NAME = "tagName";
  56.     private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_OPEN_SUBST_WITH_PARAM = "openSubstWithParam";
  57.     private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_CLOSE_SUBST_WITH_PARAM = "closeSubstWithParam";
  58.     private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_OPEN_SUBST_WITHOUT_PARAM = "openSubstWithoutParam";
  59.     private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_CLOSE_SUBST_WITHOUT_PARAM = "closeSubstWithoutParam";
  60.     private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_INTERNAL_SUBST = "internalSubst";
  61.     private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_PROCESS_INTERNAL_TAGS = "processInternalTags";
  62.     private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_ACCEPT_PARAM = "acceptParam";
  63.     private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_REQUIRES_QUOTED_PARAM = "requiresQuotedParam";

  64.     /** Properties **/
  65.     private static final String PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH = "editors.parser.bbcode.complexElement";
  66.     private static final String PROPERTY_EDITOR_BBCODE_ELEMENT_PATH = "editors.parser.bbcode.element";
  67.     private static final String PROPERTY_PARSER_ELEMENTS = "editors.parser.bbcode.elements";
  68.     private static final String PROPERTY_PARSER_COMPLEX_ELEMENTS = "editors.parser.bbcode.complexElements";
  69.     private static final String SEPARATOR = ",";
  70.     private static EditorBbcodeService _singleton;
  71.     private static List<ParserElement> _listParserElement;
  72.     private static List<ParserComplexElement> _listParserComplexElement;

  73.     /**
  74.      * {@inheritDoc}
  75.      */
  76.     @Override
  77.     public String parse( String strValue )
  78.     {
  79.         try {
  80.             // Parse BBCode and clean HTML using Jsoup with basic whitelisting of tags and attributes
  81.             return Jsoup.clean(BbcodeUtil.parse(strValue, _listParserElement, _listParserComplexElement),
  82.                     Safelist.basicWithImages());

  83.         } catch (Exception e) {
  84.             // Handle any exceptions that might occur during parsing or cleaning
  85.             // Log the error and return an appropriate error message or fallback value
  86.             AppLogService.error("Error occurred while parsing and cleaning the input string", e);
  87.             return "Error occurred during processing. Please try again later.";
  88.         }
  89.     }
  90.     /**
  91.      * {@inheritDoc}
  92.      */
  93.     @Override
  94.     public String parseAndClean( String strValue, Safelist safelist )
  95.     {
  96.         try {
  97.             // Parse BBCode and clean HTML using Jsoup with the provided Safelist
  98.             return Jsoup.clean(BbcodeUtil.parse(strValue, _listParserElement, _listParserComplexElement), safelist);

  99.         } catch (Exception e) {
  100.             // Handle any exceptions that might occur during parsing or cleaning
  101.             // Log the error and return an appropriate error message or fallback value
  102.             AppLogService.error("Error occurred while parsing and cleaning the input string", e);
  103.             return "Error occurred during processing. Please try again later.";
  104.         }    
  105.     }

  106.     /**
  107.      * Returns the unique instance of the service
  108.      *
  109.      * @return The instance of the service
  110.      */
  111.     public static synchronized EditorBbcodeService getInstance( )
  112.     {
  113.         if ( _singleton == null )
  114.         {
  115.             _listParserElement = new ArrayList<>( );
  116.             _listParserComplexElement = new ArrayList<>( );
  117.             EditorBbcodeService service = new EditorBbcodeService( );
  118.             service.init( );
  119.             _singleton = service;
  120.         }

  121.         return _singleton;
  122.     }

  123.     /**
  124.      * Init service
  125.      */
  126.     public void init( )
  127.     {
  128.         // init simple elements
  129.         String strParserElements = AppPropertiesService.getProperty( PROPERTY_PARSER_ELEMENTS );

  130.         if ( StringUtils.isNotBlank( strParserElements ) )
  131.         {
  132.             String [ ] tabParserElements = strParserElements.split( SEPARATOR );
  133.             String strCodeElement;
  134.             String strValueElement;

  135.             for ( int i = 0; i < tabParserElements.length; i++ )
  136.             {
  137.                 strCodeElement = AppPropertiesService
  138.                         .getProperty( PROPERTY_EDITOR_BBCODE_ELEMENT_PATH + "." + tabParserElements [i] + "." + CONSTANT_EDITOR_BBCODE_ELEMENT_CODE );
  139.                 strValueElement = AppPropertiesService
  140.                         .getProperty( PROPERTY_EDITOR_BBCODE_ELEMENT_PATH + "." + tabParserElements [i] + "." + CONSTANT_EDITOR_BBCODE_ELEMENT_VALUE );
  141.                 _listParserElement.add( new ParserElement( strCodeElement, strValueElement ) );
  142.             }
  143.         }

  144.         // init complex elements
  145.         String strParserComplexElements = AppPropertiesService.getProperty( PROPERTY_PARSER_COMPLEX_ELEMENTS );

  146.         if ( StringUtils.isNotBlank( strParserComplexElements ) )
  147.         {
  148.             String [ ] tabParserComplexElements = strParserComplexElements.split( SEPARATOR );
  149.             String strTagName;
  150.             String strOpenSubstWithParam;
  151.             String strCloseSubstWithParam;
  152.             String strOpenSubstWithoutParam;
  153.             String strCloseSubstWithoutParam;
  154.             String strInternalSubst;
  155.             boolean bProcessInternalTags;
  156.             boolean bAcceptParam;
  157.             boolean bRequiresQuotedParam;

  158.             for ( int i = 0; i < tabParserComplexElements.length; i++ )
  159.             {
  160.                 strTagName = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH + "." + tabParserComplexElements [i] + "."
  161.                         + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_TAG_NAME );
  162.                 strOpenSubstWithParam = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH + "." + tabParserComplexElements [i] + "."
  163.                         + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_OPEN_SUBST_WITH_PARAM );
  164.                 strCloseSubstWithParam = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH + "." + tabParserComplexElements [i]
  165.                         + "." + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_CLOSE_SUBST_WITH_PARAM );
  166.                 strOpenSubstWithoutParam = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH + "." + tabParserComplexElements [i]
  167.                         + "." + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_OPEN_SUBST_WITHOUT_PARAM );
  168.                 strCloseSubstWithoutParam = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH + "." + tabParserComplexElements [i]
  169.                         + "." + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_CLOSE_SUBST_WITHOUT_PARAM );
  170.                 strInternalSubst = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH + "." + tabParserComplexElements [i] + "."
  171.                         + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_INTERNAL_SUBST );
  172.                 bProcessInternalTags = AppPropertiesService.getPropertyBoolean( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH + "." + tabParserComplexElements [i]
  173.                         + "." + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_PROCESS_INTERNAL_TAGS, false );
  174.                 bAcceptParam = AppPropertiesService.getPropertyBoolean( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH + "." + tabParserComplexElements [i] + "."
  175.                         + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_ACCEPT_PARAM, false );
  176.                 bRequiresQuotedParam = AppPropertiesService.getPropertyBoolean( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH + "." + tabParserComplexElements [i]
  177.                         + "." + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_REQUIRES_QUOTED_PARAM, false );

  178.                 _listParserComplexElement.add( new ParserComplexElement( strTagName, strOpenSubstWithParam, strCloseSubstWithParam, strOpenSubstWithoutParam,
  179.                         strCloseSubstWithoutParam, strInternalSubst, bProcessInternalTags, bAcceptParam, bRequiresQuotedParam ) );
  180.             }
  181.         }
  182.     }
  183. }