1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.edit.internal

File EditorWikiComponent.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

4
42
10
1
208
140
16
0.38
4.2
10
1.6

Classes

Class Line # Actions
EditorWikiComponent 61 42 0% 16 56
0.00%
 

Contributing tests

No tests hitting this source file were found.

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 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    * An {@link Editor} implemented as a {@link WikiComponent}.
54    *
55    * @param <D> the type of data that can be edited by this editor
56    * @version $Id: 8d9c46b4b720a984ff3dcd2e7a5b28e1c0505dcb $
57    * @since 8.2RC1
58    */
59    @Component(roles = EditorWikiComponent.class)
60    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
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    * The {@link XWikiContext} key that holds the security document.
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   
 
88  0 toggle @Override
89    public DocumentReference getAuthorReference()
90    {
91  0 return this.authorReference;
92    }
93   
 
94  0 toggle @Override
95    public DocumentReference getDocumentReference()
96    {
97  0 return this.editorReference;
98    }
99   
 
100  0 toggle @Override
101    public String getRoleHint()
102    {
103  0 return this.descriptorBuilder.getId();
104    }
105   
 
106  0 toggle @Override
107    public Type getRoleType()
108    {
109  0 return this.roleType;
110    }
111   
 
112  0 toggle @Override
113    public WikiComponentScope getScope()
114    {
115  0 return this.scope;
116    }
117   
 
118  0 toggle @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   
 
136  0 toggle @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    // Make sure the editor code is executed with the rights of the editor document author.
146  0 XWikiDocument sdoc = editorDocument;
147    // Execute the editor code in the context of the current document (because the editor code needs to access
148    // the data that has been put on the script context).
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    * Initializes the editor component based on the definition provided by the specified document.
158    *
159    * @param editorReference the reference to the wiki page that defines the editor (i.e. that has a
160    * {@code XWiki.EditorClass} object)
161    * @throws WikiComponentException if the editor component fails to be initialized
162    */
 
163  0 toggle 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   
 
179  0 toggle 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   
 
191  0 toggle 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    }