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.portal.service.editor;
35  
36  import fr.paris.lutece.portal.business.editor.ParserComplexElement;
37  import fr.paris.lutece.portal.business.editor.ParserElement;
38  import fr.paris.lutece.portal.service.util.AppPropertiesService;
39  import fr.paris.lutece.util.parser.BbcodeUtil;
40  
41  import org.apache.commons.lang.StringUtils;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  
47  /**
48   *
49   * This class Provides a parser BBCODE
50   *
51   */
52  public class EditorBbcodeService implements IEditorBbcodeService
53  {
54      /** Constants **/
55      private static final String CONSTANT_EDITOR_BBCODE_ELEMENT_CODE = "code";
56      private static final String CONSTANT_EDITOR_BBCODE_ELEMENT_VALUE = "value";
57      private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_TAG_NAME = "tagName";
58      private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_OPEN_SUBST_WITH_PARAM = "openSubstWithParam";
59      private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_CLOSE_SUBST_WITH_PARAM = "closeSubstWithParam";
60      private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_OPEN_SUBST_WITHOUT_PARAM = "openSubstWithoutParam";
61      private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_CLOSE_SUBST_WITHOUT_PARAM = "closeSubstWithoutParam";
62      private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_INTERNAL_SUBST = "internalSubst";
63      private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_PROCESS_INTERNAL_TAGS = "processInternalTags";
64      private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_ACCEPT_PARAM = "acceptParam";
65      private static final String CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_REQUIRES_QUOTED_PARAM = "requiresQuotedParam";
66  
67      /** Properties**/
68      private static final String PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH = "editors.parser.bbcode.complexElement";
69      private static final String PROPERTY_EDITOR_BBCODE_ELEMENT_PATH = "editors.parser.bbcode.element";
70      private static final String PROPERTY_PARSER_ELEMENTS = "editors.parser.bbcode.elements";
71      private static final String PROPERTY_PARSER_COMPLEX_ELEMENTS = "editors.parser.bbcode.complexElements";
72      private static final String SEPARATOR = ",";
73      private static volatile EditorBbcodeService _singleton;
74      private static List<ParserElement> _listParserElement;
75      private static List<ParserComplexElement> _listParserComplexElement;
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public String parse( String strValue )
82      {
83          return BbcodeUtil.parse( strValue, _listParserElement, _listParserComplexElement );
84      }
85  
86      /**
87       * Returns the unique instance of the service
88       * @return The instance of the service
89       */
90      public static EditorBbcodeService getInstance(  )
91      {
92          if ( _singleton == null )
93          {
94              synchronized ( EditorBbcodeService.class )
95              {
96                  EditorBbcodeService service = new EditorBbcodeService(  );
97                  service.init(  );
98                  _singleton = service;
99              }
100         }
101 
102         return _singleton;
103     }
104 
105     /**
106      * Init service
107      */
108     public void init(  )
109     {
110         _listParserElement = new ArrayList<ParserElement>(  );
111         _listParserComplexElement = new ArrayList<ParserComplexElement>(  );
112 
113         //init simple elements
114         String strParserElements = AppPropertiesService.getProperty( PROPERTY_PARSER_ELEMENTS );
115 
116         if ( StringUtils.isNotBlank( strParserElements ) )
117         {
118             String[] tabParserElements = strParserElements.split( SEPARATOR );
119             String strCodeElement;
120             String strValueElement;
121 
122             for ( int i = 0; i < tabParserElements.length; i++ )
123             {
124                 strCodeElement = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_ELEMENT_PATH + "." +
125                         tabParserElements[i] + "." + CONSTANT_EDITOR_BBCODE_ELEMENT_CODE );
126                 strValueElement = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_ELEMENT_PATH + "." +
127                         tabParserElements[i] + "." + CONSTANT_EDITOR_BBCODE_ELEMENT_VALUE );
128                 _listParserElement.add( new ParserElement( strCodeElement, strValueElement ) );
129             }
130         }
131 
132         //init complex elements
133         String strParserComplexElements = AppPropertiesService.getProperty( PROPERTY_PARSER_COMPLEX_ELEMENTS );
134 
135         if ( StringUtils.isNotBlank( strParserComplexElements ) )
136         {
137             String[] tabParserComplexElements = strParserComplexElements.split( SEPARATOR );
138             String strTagName;
139             String strOpenSubstWithParam;
140             String strCloseSubstWithParam;
141             String strOpenSubstWithoutParam;
142             String strCloseSubstWithoutParam;
143             String strInternalSubst;
144             boolean bProcessInternalTags;
145             boolean bAcceptParam;
146             boolean bRequiresQuotedParam;
147 
148             for ( int i = 0; i < tabParserComplexElements.length; i++ )
149             {
150                 strTagName = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH + "." +
151                         tabParserComplexElements[i] + "." + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_TAG_NAME );
152                 strOpenSubstWithParam = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH +
153                         "." + tabParserComplexElements[i] + "." +
154                         CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_OPEN_SUBST_WITH_PARAM );
155                 strCloseSubstWithParam = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH +
156                         "." + tabParserComplexElements[i] + "." +
157                         CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_CLOSE_SUBST_WITH_PARAM );
158                 strOpenSubstWithoutParam = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH +
159                         "." + tabParserComplexElements[i] + "." +
160                         CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_OPEN_SUBST_WITHOUT_PARAM );
161                 strCloseSubstWithoutParam = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH +
162                         "." + tabParserComplexElements[i] + "." +
163                         CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_CLOSE_SUBST_WITHOUT_PARAM );
164                 strInternalSubst = AppPropertiesService.getProperty( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH + "." +
165                         tabParserComplexElements[i] + "." + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_INTERNAL_SUBST );
166                 bProcessInternalTags = AppPropertiesService.getPropertyBoolean( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH +
167                         "." + tabParserComplexElements[i] + "." +
168                         CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_PROCESS_INTERNAL_TAGS, false );
169                 bAcceptParam = AppPropertiesService.getPropertyBoolean( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH +
170                         "." + tabParserComplexElements[i] + "." + CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_ACCEPT_PARAM,
171                         false );
172                 bRequiresQuotedParam = AppPropertiesService.getPropertyBoolean( PROPERTY_EDITOR_BBCODE_COMPLEX_ELEMENT_PATH +
173                         "." + tabParserComplexElements[i] + "." +
174                         CONSTANT_EDITOR_BBCODE_COMPLEX_ELEMENT_REQUIRES_QUOTED_PARAM, false );
175 
176                 _listParserComplexElement.add( new ParserComplexElement( strTagName, strOpenSubstWithParam,
177                         strCloseSubstWithParam, strOpenSubstWithoutParam, strCloseSubstWithoutParam, strInternalSubst,
178                         bProcessInternalTags, bAcceptParam, bRequiresQuotedParam ) );
179             }
180         }
181     }
182 }