1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.objects.classes

File TextAreaClass.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

40
98
27
3
421
256
57
0.58
3.63
9
2.11

Classes

Class Line # Actions
TextAreaClass 45 92 0% 49 34
0.771812177.2%
TextAreaClass.EditorType 52 3 0% 4 1
0.87587.5%
TextAreaClass.ContentType 107 3 0% 4 1
0.87587.5%
 

Contributing tests

This file is covered by 49 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.objects.classes;
21   
22    import java.util.Arrays;
23    import java.util.HashMap;
24    import java.util.Map;
25    import java.util.stream.Collectors;
26   
27    import org.apache.commons.lang3.StringUtils;
28    import org.slf4j.Logger;
29    import org.slf4j.LoggerFactory;
30    import org.xwiki.edit.EditException;
31    import org.xwiki.edit.Editor;
32    import org.xwiki.edit.EditorManager;
33    import org.xwiki.model.reference.EntityReferenceSerializer;
34    import org.xwiki.rendering.syntax.Syntax;
35    import org.xwiki.rendering.syntax.SyntaxContent;
36   
37    import com.xpn.xwiki.XWikiContext;
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.objects.BaseCollection;
40    import com.xpn.xwiki.objects.BaseProperty;
41    import com.xpn.xwiki.objects.LargeStringProperty;
42    import com.xpn.xwiki.objects.meta.PropertyMetaClass;
43    import com.xpn.xwiki.web.Utils;
44   
 
45    public class TextAreaClass extends StringClass
46    {
47    /**
48    * Possible values for the editor meta property.
49    * <p>
50    * Indicates which editor should be used to manipulate the content of the property.
51    */
 
52    public enum EditorType
53    {
54    /**
55    * Plain text without any known syntax.
56    */
57    PURE_TEXT("PureText"),
58   
59    /**
60    * Edit wiki syntax using a text editor.
61    */
62    TEXT("Text"),
63   
64    /**
65    * Edit wiki syntax using a visual editor.
66    */
67    WYSIWYG("Wysiwyg");
68   
69    private static final Map<String, EditorType> editorTypeMap = Arrays.stream(EditorType.values())
70    .collect(Collectors.toMap(e -> e.value.toLowerCase(), e -> e));
71   
72    private final String value;
73   
74    /**
75    * Retreive the {@link EditorType} based on its value.
76    * <p>
77    * The search is case insensitive.
78    *
79    * @param value the value of the editor type
80    * @return the editor type matching the value or null
81    * @since 10.7RC1
82    */
 
83  7472 toggle public static EditorType getByValue(String value)
84    {
85  7472 return value != null ? editorTypeMap.get(value.toLowerCase()) : null;
86    }
87   
 
88  171 toggle private EditorType(String value)
89    {
90  171 this.value = value;
91    }
92   
 
93  4997 toggle @Override
94    public String toString()
95    {
96  4997 return this.value;
97    }
98    }
99   
100    /**
101    * Possible values for the contenttype meta property.
102    * <p>
103    * Indicates what kind of content this field contains (wiki, plain text, etc.).
104    *
105    * @since 8.3
106    */
 
107    public enum ContentType
108    {
109    /**
110    * Plain text without any known syntax.
111    */
112    PURE_TEXT("PureText"),
113   
114    /**
115    * Wiki content.
116    */
117    WIKI_TEXT("FullyRenderedText"),
118   
119    /**
120    * Velocity content.
121    */
122    VELOCITY_CODE("VelocityCode");
123   
124    private static final Map<String, ContentType> contentTypeMap = Arrays.stream(ContentType.values())
125    .collect(Collectors.toMap(c -> c.value.toLowerCase(), c -> c));
126   
127    private final String value;
128   
129    /**
130    * Retreive the {@link ContentType} based on its value.
131    * <p>
132    * The search is case insensitive.
133    *
134    * @param value the value of the content type
135    * @return the content type matching the value or null
136    * @since 10.7RC1
137    */
 
138  6496 toggle public static ContentType getByValue(String value)
139    {
140  6496 return value != null ? contentTypeMap.get(value.toLowerCase()) : null;
141    }
142   
 
143  171 toggle private ContentType(String value)
144    {
145  171 this.value = value;
146    }
147   
 
148  21759 toggle @Override
149    public String toString()
150    {
151  21759 return this.value;
152    }
153    }
154   
155    private static final String XCLASSNAME = "textarea";
156   
157    private static final Logger LOGGER = LoggerFactory.getLogger(TextAreaClass.class);
158   
 
159  21741 toggle public TextAreaClass(PropertyMetaClass wclass)
160    {
161  21741 super(XCLASSNAME, "Text Area", wclass);
162   
163  21741 setSize(40);
164  21741 setRows(5);
165    }
166   
 
167  19159 toggle public TextAreaClass()
168    {
169  19159 this(null);
170    }
171   
172    /**
173    * @param contentType the content type
174    * @return the editor type compatible with the passed content type, null if several are compatible
175    */
 
176  9115 toggle public static EditorType getEditorType(ContentType contentType, EditorType def)
177    {
178  9115 if (contentType != null && contentType != ContentType.WIKI_TEXT) {
179  2695 return EditorType.PURE_TEXT;
180    }
181   
182  6420 return def;
183    }
184   
185    /**
186    * @param contentType the content type value
187    * @param def the current editor type
188    * @return the editor type compatible with the passed content type, def if several are compatible
189    * @since 10.7RC1
190    */
 
191  6496 toggle public static EditorType getEditorType(String contentType, EditorType def)
192    {
193  6496 return getEditorType(ContentType.getByValue(contentType), def);
194    }
195   
196    /**
197    * @param editorType the editor type
198    * @return the content type compatible with the passed editor type, null if several are compatible
199    */
 
200  1988 toggle public static ContentType getContentType(EditorType editorType, ContentType def)
201    {
202  1988 if (editorType != EditorType.PURE_TEXT) {
203  1183 return ContentType.WIKI_TEXT;
204    }
205   
206  805 return def;
207    }
208   
 
209  52639 toggle @Override
210    public BaseProperty newProperty()
211    {
212  52639 BaseProperty property = new LargeStringProperty();
213  52639 property.setName(getName());
214   
215  52639 return property;
216    }
217   
 
218  7474 toggle public int getRows()
219    {
220  7474 return getIntValue("rows");
221    }
222   
 
223  28094 toggle public void setRows(int rows)
224    {
225  28094 setIntValue("rows", rows);
226    }
227   
 
228  7472 toggle public String getEditor()
229    {
230  7472 String editor = getStringValue("editor").toLowerCase();
231  7472 if (EditorType.getByValue(editor) == null) {
232  6496 EditorType compatibleEditor = getEditorType(getContentType(), null);
233  6496 if (compatibleEditor != null) {
234  76 return compatibleEditor.value.toLowerCase();
235    }
236    }
237   
238  7396 return editor;
239    }
240   
241    /**
242    * Sets the editor meta property.
243    *
244    * @param editor the editor type
245    * @since 8.2RC1
246    */
 
247  6052 toggle public void setEditor(String editor)
248    {
249  6052 setStringValue("editor", editor);
250    }
251   
252    /**
253    * Sets the editor meta property.
254    *
255    * @param editorType the editor type
256    * @since 8.3
257    */
 
258  0 toggle public void setEditor(EditorType editorType)
259    {
260  0 setEditor(editorType.toString());
261   
262    // Make sure the content type is compatible
263  0 ContentType compatible = getContentType(editorType, null);
264  0 if (compatible != null) {
265  0 setContentType(compatible);
266    }
267    }
268   
 
269  15290 toggle public String getContentType()
270    {
271  15290 String result = getStringValue("contenttype").toLowerCase();
272  15290 if (result.isEmpty()) {
273  15144 result = ContentType.WIKI_TEXT.toString().toLowerCase();
274    }
275   
276  15290 return result;
277    }
278   
 
279  2759 toggle public void setContentType(String contentType)
280    {
281  2759 setStringValue("contenttype", contentType);
282    }
283   
284    /**
285    * @param contentType the content type
286    * @since 8.3
287    */
 
288  0 toggle public void setContentType(ContentType contentType)
289    {
290  0 setContentType(contentType.toString());
291   
292    // Make sure the editor type is compatible
293  0 EditorType compatible = getEditorType(contentType, null);
294  0 if (compatible != null) {
295  0 setEditor(compatible);
296    }
297    }
298   
 
299  0 toggle public boolean isWysiwyg(XWikiContext context)
300    {
301  0 return "wysiwyg".equals(getEditorType(context));
302    }
303   
 
304  1144 toggle private String getEditorType(XWikiContext context)
305    {
306  1144 String editorType = null;
307  1144 if (context != null && context.getRequest() != null) {
308  1144 editorType = context.getRequest().get("xeditmode");
309    }
310  1144 if (isEmptyValue(editorType)) {
311  1142 editorType = getEditor();
312  1142 if (isEmptyValue(editorType) && context != null && context.getWiki() != null) {
313  132 editorType = context.getWiki().getEditorPreference(context);
314    }
315    }
316  1144 return isEmptyValue(editorType) ? null : editorType.toLowerCase();
317    }
318   
 
319  3430 toggle private boolean isEmptyValue(String value)
320    {
321    // See XWIKI-10853: Some static lists in XWiki.XWikiPreferences have the "---" value
322  3430 return StringUtils.isEmpty(value) || "---".equals(value);
323    }
324   
325    /**
326    * @return true if the content of this text area is not a wiki syntax content
327    */
 
328  4 toggle public boolean isWikiContent()
329    {
330  4 String contentType = getContentType();
331   
332  4 if (contentType != null && !contentType.equals("puretext") && !contentType.equals("velocitycode")) {
333  2 return true;
334    } else {
335  2 return false;
336    }
337    }
338   
 
339  1144 toggle @Override
340    public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
341    XWikiContext context)
342    {
343  1144 String editorType = getEditorType(context);
344  1144 EditorManager editorManager = Utils.getComponent(EditorManager.class);
345  1144 Editor<SyntaxContent> editor = editorManager.getDefaultEditor(SyntaxContent.class, editorType);
346  1144 Map<String, Object> parameters = new HashMap<>();
347  1144 String fieldName = prefix + name;
348  1144 parameters.put("id", fieldName);
349  1144 parameters.put("name", fieldName);
350  1144 parameters.put("cols", getSize());
351  1144 parameters.put("rows", getRows());
352  1144 parameters.put("disabled", isDisabled());
353  1144 parameters.put("sourceDocumentReference", object.getDocumentReference());
354  1144 Syntax syntax = "puretext".equals(editorType) ? Syntax.PLAIN_1_0 : getObjectDocumentSyntax(object, context);
355  1144 SyntaxContent syntaxContent = new SyntaxContent(object.getStringValue(name), syntax);
356  1144 try {
357  1144 buffer.append(editor.render(syntaxContent, parameters));
358    } catch (EditException e) {
359  0 LOGGER.error("Failed to display the text area property.", e);
360    }
361    }
362   
 
363  2460 toggle @Override
364    public void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object,
365    XWikiContext context)
366    {
367  2460 String contentType = getContentType();
368  2460 XWikiDocument doc = context.getDoc();
369   
370  2460 if ("puretext".equals(contentType) && doc != null) {
371  23 super.displayView(buffer, name, prefix, object, context);
372  2437 } else if ("velocitycode".equals(contentType) && context.getWiki() != null) {
373  0 StringBuffer result = new StringBuffer();
374  0 super.displayView(result, name, prefix, object, context);
375  0 if (getObjectDocumentSyntax(object, context).equals(Syntax.XWIKI_1_0)) {
376  0 buffer.append(context.getWiki().parseContent(result.toString(), context));
377    } else {
378    // Don't do anything since this mode is deprecated and not supported in the new rendering.
379  0 buffer.append(result);
380    }
381    } else {
382  2437 StringBuffer result = new StringBuffer();
383  2437 super.displayView(result, name, prefix, object, context);
384  2437 if (doc != null) {
385  2437 String syntax = getObjectDocumentSyntax(object, context).toIdString();
386  2437 buffer.append(context.getDoc().getRenderedContent(result.toString(), syntax, context));
387    } else {
388  0 buffer.append(result);
389    }
390    }
391    }
392   
393    /**
394    * @return the syntax for the document to which the passed objects belongs to or the XWiki Syntax 1.0 if the object
395    * document cannot be retrieved
396    */
 
397  3180 toggle private Syntax getObjectDocumentSyntax(BaseCollection object, XWikiContext context)
398    {
399  3180 Syntax syntax;
400   
401  3180 try {
402  3180 XWikiDocument doc = object.getOwnerDocument();
403  3180 if (doc == null) {
404  0 doc = context.getWiki().getDocument(object.getDocumentReference(), context);
405    }
406   
407  3180 syntax = doc.getSyntax();
408    } catch (Exception e) {
409    // Used to convert a Document Reference to string (compact form without the wiki part if it matches the
410    // current wiki).
411  0 EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer =
412    Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
413  0 LOGGER.warn("Error while getting the syntax corresponding to object ["
414    + compactWikiEntityReferenceSerializer.serialize(object.getDocumentReference())
415    + "]. Defaulting to using XWiki 1.0 syntax. Internal error [" + e.getMessage() + "]");
416  0 syntax = Syntax.XWIKI_1_0;
417    }
418   
419  3180 return syntax;
420    }
421    }