| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
|
| |
|
| 71.7% |
Uncovered Elements: 39 (138) |
Complexity: 46 |
Complexity Density: 0.53 |
|
| 43 |
|
public class TextAreaClass extends StringClass |
| 44 |
|
{ |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 50 |
|
public enum EditorType |
| 51 |
|
{ |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
PURE_TEXT("PureText"), |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
TEXT("Text"), |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
WYSIWYG("Wysiwyg"); |
| 66 |
|
|
| 67 |
|
private final String value; |
| 68 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 69 |
123 |
private EditorType(String value)... |
| 70 |
|
{ |
| 71 |
123 |
this.value = value; |
| 72 |
|
} |
| 73 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 74 |
2272 |
@Override... |
| 75 |
|
public String toString() |
| 76 |
|
{ |
| 77 |
2272 |
return this.value; |
| 78 |
|
} |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
@since |
| 87 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 88 |
|
public enum ContentType |
| 89 |
|
{ |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
PURE_TEXT("PureText"), |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
WIKI_TEXT("FullyRenderedText"), |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
VELOCITY_CODE("VelocityCode"); |
| 104 |
|
|
| 105 |
|
private final String value; |
| 106 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 107 |
129 |
private ContentType(String value)... |
| 108 |
|
{ |
| 109 |
129 |
this.value = value; |
| 110 |
|
} |
| 111 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 112 |
5373 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 123 |
8492 |
public TextAreaClass(PropertyMetaClass wclass)... |
| 124 |
|
{ |
| 125 |
8492 |
super(XCLASSNAME, "Text Area", wclass); |
| 126 |
|
|
| 127 |
8492 |
setSize(40); |
| 128 |
8492 |
setRows(5); |
| 129 |
|
} |
| 130 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 131 |
6647 |
public TextAreaClass()... |
| 132 |
|
{ |
| 133 |
6647 |
this(null); |
| 134 |
|
} |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
@param |
| 138 |
|
@return |
| 139 |
|
|
| |
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
| 140 |
988 |
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 |
| 151 |
|
@return |
| 152 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 153 |
986 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 162 |
7756 |
@Override... |
| 163 |
|
public BaseProperty newProperty() |
| 164 |
|
{ |
| 165 |
7756 |
BaseProperty property = new LargeStringProperty(); |
| 166 |
7756 |
property.setName(getName()); |
| 167 |
|
|
| 168 |
7756 |
return property; |
| 169 |
|
} |
| 170 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 171 |
3079 |
public int getRows()... |
| 172 |
|
{ |
| 173 |
3079 |
return getIntValue("rows"); |
| 174 |
|
} |
| 175 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 176 |
11385 |
public void setRows(int rows)... |
| 177 |
|
{ |
| 178 |
11385 |
setIntValue("rows", rows); |
| 179 |
|
} |
| 180 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 181 |
3079 |
public String getEditor()... |
| 182 |
|
{ |
| 183 |
3079 |
return getStringValue("editor").toLowerCase(); |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
@param |
| 190 |
|
@since |
| 191 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 192 |
1801 |
public void setEditor(String editor)... |
| 193 |
|
{ |
| 194 |
1801 |
setStringValue("editor", editor); |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
@param |
| 201 |
|
@since |
| 202 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 203 |
0 |
public void setEditor(EditorType editorType)... |
| 204 |
|
{ |
| 205 |
0 |
setEditor(editorType.toString()); |
| 206 |
|
|
| 207 |
|
|
| 208 |
0 |
ContentType compatible = getContentType(editorType, null); |
| 209 |
0 |
if (compatible != null) { |
| 210 |
0 |
setContentType(compatible); |
| 211 |
|
} |
| 212 |
|
} |
| 213 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 214 |
2944 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 224 |
506 |
public void setContentType(String contentType)... |
| 225 |
|
{ |
| 226 |
506 |
setStringValue("contenttype", contentType); |
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
|
| 230 |
|
@param |
| 231 |
|
@since |
| 232 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 233 |
0 |
public void setContentType(ContentType contentType)... |
| 234 |
|
{ |
| 235 |
0 |
setContentType(contentType.toString()); |
| 236 |
|
|
| 237 |
|
|
| 238 |
0 |
EditorType compatible = getEditorType(contentType, null); |
| 239 |
0 |
if (compatible != null) { |
| 240 |
0 |
setEditor(compatible); |
| 241 |
|
} |
| 242 |
|
} |
| 243 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 244 |
0 |
public boolean isWysiwyg(XWikiContext context)... |
| 245 |
|
{ |
| 246 |
0 |
return "wysiwyg".equals(getEditorType(context)); |
| 247 |
|
} |
| 248 |
|
|
| |
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 8 |
Complexity Density: 1 |
|
| 249 |
292 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 264 |
876 |
private boolean isEmptyValue(String value)... |
| 265 |
|
{ |
| 266 |
|
|
| 267 |
876 |
return StringUtils.isEmpty(value) || "---".equals(value); |
| 268 |
|
} |
| 269 |
|
|
| 270 |
|
|
| 271 |
|
@return |
| 272 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 4 |
Complexity Density: 1 |
|
| 273 |
4 |
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 |
|
|
| |
|
| 94.4% |
Uncovered Elements: 1 (18) |
Complexity: 3 |
Complexity Density: 0.19 |
|
| 284 |
292 |
@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 |
|
|
| |
|
| 50% |
Uncovered Elements: 12 (24) |
Complexity: 7 |
Complexity Density: 0.44 |
|
| 308 |
153 |
@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 |
|
|
| 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 |
| 340 |
|
|
| 341 |
|
|
| |
|
| 58.3% |
Uncovered Elements: 5 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 342 |
392 |
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 |
|
|
| 355 |
|
|
| 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 |
|
} |