1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.objects.classes

File TextAreaClass.java

 

Coverage histogram

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

Code metrics

32
90
24
3
366
231
50
0.56
3.75
8
2.08

Classes

Class Line # Actions
TextAreaClass 43 86 0% 46 39
0.717391371.7%
TextAreaClass.EditorType 50 2 0% 2 0
1.0100%
TextAreaClass.ContentType 88 2 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 46 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.HashMap;
23    import java.util.Map;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.slf4j.Logger;
27    import org.slf4j.LoggerFactory;
28    import org.xwiki.edit.EditException;
29    import org.xwiki.edit.Editor;
30    import org.xwiki.edit.EditorManager;
31    import org.xwiki.model.reference.EntityReferenceSerializer;
32    import org.xwiki.rendering.syntax.Syntax;
33    import org.xwiki.rendering.syntax.SyntaxContent;
34   
35    import com.xpn.xwiki.XWikiContext;
36    import com.xpn.xwiki.doc.XWikiDocument;
37    import com.xpn.xwiki.objects.BaseCollection;
38    import com.xpn.xwiki.objects.BaseProperty;
39    import com.xpn.xwiki.objects.LargeStringProperty;
40    import com.xpn.xwiki.objects.meta.PropertyMetaClass;
41    import com.xpn.xwiki.web.Utils;
42   
 
43    public class TextAreaClass extends StringClass
44    {
45    /**
46    * Possible values for the editor meta property.
47    * <p>
48    * Indicates which editor should be used to manipulate the content of the property.
49    */
 
50    public enum EditorType
51    {
52    /**
53    * Plain text without any known syntax.
54    */
55    PURE_TEXT("PureText"),
56   
57    /**
58    * Edit wiki syntax using a text editor.
59    */
60    TEXT("Text"),
61   
62    /**
63    * Edit wiki syntax using a visual editor.
64    */
65    WYSIWYG("Wysiwyg");
66   
67    private final String value;
68   
 
69  123 toggle private EditorType(String value)
70    {
71  123 this.value = value;
72    }
73   
 
74  2272 toggle @Override
75    public String toString()
76    {
77  2272 return this.value;
78    }
79    }
80   
81    /**
82    * Possible values for the contenttype meta property.
83    * <p>
84    * Indicates what kind of content this field contains (wiki, plain text, etc.).
85    *
86    * @since 8.3
87    */
 
88    public enum ContentType
89    {
90    /**
91    * Plain text without any known syntax.
92    */
93    PURE_TEXT("PureText"),
94   
95    /**
96    * Wiki content.
97    */
98    WIKI_TEXT("FullyRenderedText"),
99   
100    /**
101    * Velocity content.
102    */
103    VELOCITY_CODE("VelocityCode");
104   
105    private final String value;
106   
 
107  129 toggle private ContentType(String value)
108    {
109  129 this.value = value;
110    }
111   
 
112  5373 toggle @Override
113    public String toString()
114    {
115  5373 return this.value;
116    }
117    }
118   
119    private static final String XCLASSNAME = "textarea";
120   
121    private static final Logger LOGGER = LoggerFactory.getLogger(TextAreaClass.class);
122   
 
123  8492 toggle public TextAreaClass(PropertyMetaClass wclass)
124    {
125  8492 super(XCLASSNAME, "Text Area", wclass);
126   
127  8492 setSize(40);
128  8492 setRows(5);
129    }
130   
 
131  6647 toggle public TextAreaClass()
132    {
133  6647 this(null);
134    }
135   
136    /**
137    * @param contentType the content type
138    * @return the editor type compatible with the passed content type, null if several are compatible
139    */
 
140  988 toggle public static EditorType getEditorType(ContentType contentType, EditorType def)
141    {
142  988 if (contentType != null && contentType != ContentType.WIKI_TEXT) {
143  988 return EditorType.PURE_TEXT;
144    }
145   
146  0 return def;
147    }
148   
149    /**
150    * @param editorType the editor type
151    * @return the content type compatible with the passed editor type, null if several are compatible
152    */
 
153  986 toggle public static ContentType getContentType(EditorType editorType, ContentType def)
154    {
155  986 if (editorType != EditorType.PURE_TEXT) {
156  342 return ContentType.WIKI_TEXT;
157    }
158   
159  644 return def;
160    }
161   
 
162  7756 toggle @Override
163    public BaseProperty newProperty()
164    {
165  7756 BaseProperty property = new LargeStringProperty();
166  7756 property.setName(getName());
167   
168  7756 return property;
169    }
170   
 
171  3079 toggle public int getRows()
172    {
173  3079 return getIntValue("rows");
174    }
175   
 
176  11385 toggle public void setRows(int rows)
177    {
178  11385 setIntValue("rows", rows);
179    }
180   
 
181  3079 toggle public String getEditor()
182    {
183  3079 return getStringValue("editor").toLowerCase();
184    }
185   
186    /**
187    * Sets the editor meta property.
188    *
189    * @param editor the editor type
190    * @since 8.2RC1
191    */
 
192  1801 toggle public void setEditor(String editor)
193    {
194  1801 setStringValue("editor", editor);
195    }
196   
197    /**
198    * Sets the editor meta property.
199    *
200    * @param editorType the editor type
201    * @since 8.3
202    */
 
203  0 toggle public void setEditor(EditorType editorType)
204    {
205  0 setEditor(editorType.toString());
206   
207    // Make sure the content type is compatible
208  0 ContentType compatible = getContentType(editorType, null);
209  0 if (compatible != null) {
210  0 setContentType(compatible);
211    }
212    }
213   
 
214  2944 toggle public String getContentType()
215    {
216  2944 String result = getStringValue("contenttype").toLowerCase();
217  2944 if (result.isEmpty()) {
218  2352 result = ContentType.WIKI_TEXT.toString().toLowerCase();
219    }
220   
221  2944 return result;
222    }
223   
 
224  506 toggle public void setContentType(String contentType)
225    {
226  506 setStringValue("contenttype", contentType);
227    }
228   
229    /**
230    * @param contentType the content type
231    * @since 8.3
232    */
 
233  0 toggle public void setContentType(ContentType contentType)
234    {
235  0 setContentType(contentType.toString());
236   
237    // Make sure the editor type is compatible
238  0 EditorType compatible = getEditorType(contentType, null);
239  0 if (compatible != null) {
240  0 setEditor(compatible);
241    }
242    }
243   
 
244  0 toggle public boolean isWysiwyg(XWikiContext context)
245    {
246  0 return "wysiwyg".equals(getEditorType(context));
247    }
248   
 
249  292 toggle private String getEditorType(XWikiContext context)
250    {
251  292 String editorType = null;
252  292 if (context != null && context.getRequest() != null) {
253  292 editorType = context.getRequest().get("xeditmode");
254    }
255  292 if (isEmptyValue(editorType)) {
256  292 editorType = getEditor();
257  292 if (isEmptyValue(editorType) && context != null && context.getWiki() != null) {
258  65 editorType = context.getWiki().getEditorPreference(context);
259    }
260    }
261  292 return isEmptyValue(editorType) ? null : editorType.toLowerCase();
262    }
263   
 
264  876 toggle private boolean isEmptyValue(String value)
265    {
266    // See XWIKI-10853: Some static lists in XWiki.XWikiPreferences have the "---" value
267  876 return StringUtils.isEmpty(value) || "---".equals(value);
268    }
269   
270    /**
271    * @return true if the content of this text area is not a wiki syntax content
272    */
 
273  4 toggle public boolean isWikiContent()
274    {
275  4 String contentType = getContentType();
276   
277  4 if (contentType != null && !contentType.equals("puretext") && !contentType.equals("velocitycode")) {
278  2 return true;
279    } else {
280  2 return false;
281    }
282    }
283   
 
284  292 toggle @Override
285    public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
286    XWikiContext context)
287    {
288  292 String editorType = getEditorType(context);
289  292 EditorManager editorManager = Utils.getComponent(EditorManager.class);
290  292 Editor<SyntaxContent> editor = editorManager.getDefaultEditor(SyntaxContent.class, editorType);
291  292 Map<String, Object> parameters = new HashMap<>();
292  292 String fieldName = prefix + name;
293  292 parameters.put("id", fieldName);
294  292 parameters.put("name", fieldName);
295  292 parameters.put("cols", getSize());
296  292 parameters.put("rows", getRows());
297  292 parameters.put("disabled", isDisabled());
298  292 parameters.put("sourceDocumentReference", object.getDocumentReference());
299  292 Syntax syntax = "puretext".equals(editorType) ? Syntax.PLAIN_1_0 : getObjectDocumentSyntax(object, context);
300  292 SyntaxContent syntaxContent = new SyntaxContent(object.getStringValue(name), syntax);
301  292 try {
302  292 buffer.append(editor.render(syntaxContent, parameters));
303    } catch (EditException e) {
304  0 LOGGER.error("Failed to display the text area property.", e);
305    }
306    }
307   
 
308  153 toggle @Override
309    public void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object,
310    XWikiContext context)
311    {
312  153 String contentType = getContentType();
313  153 XWikiDocument doc = context.getDoc();
314   
315  153 if ("puretext".equals(contentType) && doc != null) {
316  0 super.displayView(buffer, name, prefix, object, context);
317  153 } else if ("velocitycode".equals(contentType) && context.getWiki() != null) {
318  0 StringBuffer result = new StringBuffer();
319  0 super.displayView(result, name, prefix, object, context);
320  0 if (getObjectDocumentSyntax(object, context).equals(Syntax.XWIKI_1_0)) {
321  0 buffer.append(context.getWiki().parseContent(result.toString(), context));
322    } else {
323    // Don't do anything since this mode is deprecated and not supported in the new rendering.
324  0 buffer.append(result);
325    }
326    } else {
327  153 StringBuffer result = new StringBuffer();
328  153 super.displayView(result, name, prefix, object, context);
329  153 if (doc != null) {
330  153 String syntax = getObjectDocumentSyntax(object, context).toIdString();
331  153 buffer.append(context.getDoc().getRenderedContent(result.toString(), syntax, context));
332    } else {
333  0 buffer.append(result);
334    }
335    }
336    }
337   
338    /**
339    * @return the syntax for the document to which the passed objects belongs to or the XWiki Syntax 1.0 if the object
340    * document cannot be retrieved
341    */
 
342  392 toggle private Syntax getObjectDocumentSyntax(BaseCollection object, XWikiContext context)
343    {
344  392 Syntax syntax;
345   
346  392 try {
347  392 XWikiDocument doc = object.getOwnerDocument();
348  392 if (doc == null) {
349  0 doc = context.getWiki().getDocument(object.getDocumentReference(), context);
350    }
351   
352  392 syntax = doc.getSyntax();
353    } catch (Exception e) {
354    // Used to convert a Document Reference to string (compact form without the wiki part if it matches the
355    // current wiki).
356  0 EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer =
357    Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
358  0 LOGGER.warn("Error while getting the syntax corresponding to object ["
359    + compactWikiEntityReferenceSerializer.serialize(object.getDocumentReference())
360    + "]. Defaulting to using XWiki 1.0 syntax. Internal error [" + e.getMessage() + "]");
361  0 syntax = Syntax.XWIKI_1_0;
362    }
363   
364  392 return syntax;
365    }
366    }