1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.edit.internal; |
21 |
|
|
22 |
|
import java.lang.reflect.Type; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Provider; |
26 |
|
|
27 |
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
28 |
|
import org.slf4j.Logger; |
29 |
|
import org.xwiki.component.annotation.Component; |
30 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
31 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
32 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
33 |
|
import org.xwiki.component.util.ReflectionUtils; |
34 |
|
import org.xwiki.component.wiki.WikiComponent; |
35 |
|
import org.xwiki.component.wiki.WikiComponentException; |
36 |
|
import org.xwiki.component.wiki.WikiComponentScope; |
37 |
|
import org.xwiki.edit.AbstractEditor; |
38 |
|
import org.xwiki.edit.EditException; |
39 |
|
import org.xwiki.edit.Editor; |
40 |
|
import org.xwiki.edit.EditorDescriptor; |
41 |
|
import org.xwiki.edit.EditorDescriptorBuilder; |
42 |
|
import org.xwiki.model.reference.DocumentReference; |
43 |
|
import org.xwiki.model.reference.LocalDocumentReference; |
44 |
|
import org.xwiki.rendering.syntax.Syntax; |
45 |
|
|
46 |
|
import com.xpn.xwiki.XWiki; |
47 |
|
import com.xpn.xwiki.XWikiContext; |
48 |
|
import com.xpn.xwiki.XWikiException; |
49 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
50 |
|
import com.xpn.xwiki.objects.BaseObject; |
51 |
|
|
52 |
|
|
53 |
|
@link@link |
54 |
|
|
55 |
|
@param |
56 |
|
@version |
57 |
|
@since |
58 |
|
|
59 |
|
@Component(roles = EditorWikiComponent.class) |
60 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
|
|
| 0% |
Uncovered Elements: 56 (56) |
Complexity: 16 |
Complexity Density: 0.38 |
|
61 |
|
public class EditorWikiComponent<D> extends AbstractEditor<D> implements WikiComponent |
62 |
|
{ |
63 |
|
private static final LocalDocumentReference EDITOR_CLASS_REFERENCE = new LocalDocumentReference(XWiki.SYSTEM_SPACE, |
64 |
|
"EditorClass"); |
65 |
|
|
66 |
|
|
67 |
|
@link |
68 |
|
|
69 |
|
private static final String SECURITY_DOCUMENT = "sdoc"; |
70 |
|
|
71 |
|
@Inject |
72 |
|
private Logger logger; |
73 |
|
|
74 |
|
@Inject |
75 |
|
private Provider<XWikiContext> xcontextProvider; |
76 |
|
|
77 |
|
private DocumentReference authorReference; |
78 |
|
|
79 |
|
private DocumentReference editorReference; |
80 |
|
|
81 |
|
private Type roleType; |
82 |
|
|
83 |
|
private WikiComponentScope scope; |
84 |
|
|
85 |
|
@Inject |
86 |
|
private EditorDescriptorBuilder descriptorBuilder; |
87 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
0 |
@Override... |
89 |
|
public DocumentReference getAuthorReference() |
90 |
|
{ |
91 |
0 |
return this.authorReference; |
92 |
|
} |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
94 |
0 |
@Override... |
95 |
|
public DocumentReference getDocumentReference() |
96 |
|
{ |
97 |
0 |
return this.editorReference; |
98 |
|
} |
99 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
100 |
0 |
@Override... |
101 |
|
public String getRoleHint() |
102 |
|
{ |
103 |
0 |
return this.descriptorBuilder.getId(); |
104 |
|
} |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
0 |
@Override... |
107 |
|
public Type getRoleType() |
108 |
|
{ |
109 |
0 |
return this.roleType; |
110 |
|
} |
111 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
112 |
0 |
@Override... |
113 |
|
public WikiComponentScope getScope() |
114 |
|
{ |
115 |
0 |
return this.scope; |
116 |
|
} |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
118 |
0 |
@Override... |
119 |
|
public EditorDescriptor getDescriptor() |
120 |
|
{ |
121 |
0 |
try { |
122 |
0 |
XWikiContext xcontext = this.xcontextProvider.get(); |
123 |
0 |
XWikiDocument editorDocument = xcontext.getWiki().getDocument(this.getDocumentReference(), xcontext); |
124 |
0 |
XWikiDocument translatedEditorDocument = editorDocument.getTranslatedDocument(xcontext); |
125 |
0 |
this.descriptorBuilder.setName(translatedEditorDocument.getRenderedTitle(Syntax.PLAIN_1_0, xcontext)); |
126 |
0 |
this.descriptorBuilder.setDescription(translatedEditorDocument.getRenderedContent(Syntax.PLAIN_1_0, |
127 |
|
xcontext)); |
128 |
|
|
129 |
|
} catch (XWikiException e) { |
130 |
0 |
this.logger.warn("Failed to read the editor name and description. Root cause: " |
131 |
|
+ ExceptionUtils.getRootCauseMessage(e)); |
132 |
|
} |
133 |
0 |
return this.descriptorBuilder.build(); |
134 |
|
} |
135 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
136 |
0 |
@Override... |
137 |
|
protected String render() throws EditException |
138 |
|
{ |
139 |
0 |
try { |
140 |
0 |
XWikiContext xcontext = this.xcontextProvider.get(); |
141 |
|
|
142 |
0 |
XWikiDocument editorDocument = xcontext.getWiki().getDocument(this.getDocumentReference(), xcontext); |
143 |
0 |
BaseObject editorObject = editorDocument.getXObject(EDITOR_CLASS_REFERENCE); |
144 |
0 |
String editorCode = editorObject.getStringValue("code"); |
145 |
|
|
146 |
0 |
XWikiDocument sdoc = editorDocument; |
147 |
|
|
148 |
|
|
149 |
0 |
return xcontext.getDoc().getRenderedContent(editorCode, editorDocument.getSyntax().toIdString(), false, |
150 |
|
sdoc, xcontext); |
151 |
|
} catch (Exception e) { |
152 |
0 |
throw new EditException("Failed to render the editor code.", e); |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
|
161 |
|
@throws |
162 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
163 |
0 |
public void initialize(DocumentReference editorReference) throws WikiComponentException... |
164 |
|
{ |
165 |
0 |
if (this.editorReference != null) { |
166 |
0 |
throw new WikiComponentException("This editor component is already initialized."); |
167 |
|
} |
168 |
|
|
169 |
0 |
this.editorReference = editorReference; |
170 |
|
|
171 |
0 |
try { |
172 |
0 |
XWikiContext xcontext = this.xcontextProvider.get(); |
173 |
0 |
initialize(xcontext.getWiki().getDocument(editorReference, xcontext)); |
174 |
|
} catch (XWikiException e) { |
175 |
0 |
throw new WikiComponentException("Failed to load the editor document.", e); |
176 |
|
} |
177 |
|
} |
178 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
179 |
0 |
private void initialize(XWikiDocument editorDocument) throws WikiComponentException... |
180 |
|
{ |
181 |
0 |
this.authorReference = editorDocument.getAuthorReference(); |
182 |
0 |
BaseObject editorObject = editorDocument.getXObject(EDITOR_CLASS_REFERENCE); |
183 |
0 |
if (editorObject != null) { |
184 |
0 |
initialize(editorObject); |
185 |
|
} else { |
186 |
0 |
throw new WikiComponentException(String.format( |
187 |
|
"The document [%s] is missing the XWiki.EditorClass object.", editorDocument.getDocumentReference())); |
188 |
|
} |
189 |
|
} |
190 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
191 |
0 |
private void initialize(BaseObject editorObject) throws WikiComponentException... |
192 |
|
{ |
193 |
0 |
this.descriptorBuilder.setId(editorObject.getStringValue("roleHint")); |
194 |
0 |
this.descriptorBuilder.setIcon(editorObject.getStringValue("icon")); |
195 |
0 |
this.descriptorBuilder.setCategory(editorObject.getStringValue("category")); |
196 |
|
|
197 |
0 |
this.scope = WikiComponentScope.fromString(editorObject.getStringValue("scope")); |
198 |
|
|
199 |
0 |
String dataTypeName = editorObject.getStringValue("dataType"); |
200 |
0 |
try { |
201 |
0 |
Type dataType = |
202 |
|
ReflectionUtils.unserializeType(dataTypeName, Thread.currentThread().getContextClassLoader()); |
203 |
0 |
this.roleType = new DefaultParameterizedType(null, Editor.class, dataType); |
204 |
|
} catch (ClassNotFoundException e) { |
205 |
0 |
throw new WikiComponentException(String.format("The [%s] data type does not exist.", dataTypeName), e); |
206 |
|
} |
207 |
|
} |
208 |
|
} |