1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
50
51
52 public class EditorBbcodeService implements IEditorBbcodeService
53 {
54
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
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
79
80 @Override
81 public String parse( String strValue )
82 {
83 return BbcodeUtil.parse( strValue, _listParserElement, _listParserComplexElement );
84 }
85
86
87
88
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
107
108 public void init( )
109 {
110 _listParserElement = new ArrayList<ParserElement>( );
111 _listParserComplexElement = new ArrayList<ParserComplexElement>( );
112
113
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
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 }