1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.component.wiki.internal.bridge

File DefaultWikiComponentBridge.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

12
54
11
1
229
159
21
0.39
4.91
11
1.91

Classes

Class Line # Actions
DefaultWikiComponentBridge 60 54 0% 21 11
0.8571428785.7%
 

Contributing tests

This file is covered by 11 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 org.xwiki.component.wiki.internal.bridge;
21   
22    import java.lang.reflect.Type;
23    import java.util.ArrayList;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Map;
27   
28    import javax.inject.Inject;
29    import javax.inject.Provider;
30    import javax.inject.Singleton;
31   
32    import org.apache.commons.lang3.StringUtils;
33    import org.slf4j.Logger;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.descriptor.ComponentDescriptor;
36    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
37    import org.xwiki.component.util.ReflectionUtils;
38    import org.xwiki.component.wiki.WikiComponentException;
39    import org.xwiki.component.wiki.WikiComponentScope;
40    import org.xwiki.component.wiki.internal.WikiComponentConstants;
41    import org.xwiki.model.reference.DocumentReference;
42    import org.xwiki.rendering.block.XDOM;
43    import org.xwiki.rendering.syntax.Syntax;
44    import org.xwiki.security.authorization.AuthorizationManager;
45    import org.xwiki.security.authorization.Right;
46   
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    * A bridge between Wiki Components and the old model.
54    *
55    * @version $Id: df83a3b1107f295edea26602a16932293dda393b $
56    * @since 4.3M2
57    */
58    @Component
59    @Singleton
 
60    public class DefaultWikiComponentBridge implements WikiComponentConstants, WikiComponentBridge
61    {
62    /**
63    * The logger to log.
64    */
65    @Inject
66    protected Logger logger;
67   
68    @Inject
69    private Provider<XWikiContext> xcontextProvider;
70   
71    /**
72    * The rendering bridge.
73    */
74    @Inject
75    private ContentParser renderingBridge;
76   
77    @Inject
78    private AuthorizationManager authorization;
79   
 
80  2 toggle @Override
81    public Syntax getSyntax(DocumentReference reference) throws WikiComponentException
82    {
83  2 XWikiDocument componentDocument = this.getDocument(reference);
84  2 return componentDocument.getSyntax();
85    }
86   
 
87  3 toggle @Override
88    public Type getRoleType(DocumentReference reference) throws WikiComponentException
89    {
90  3 BaseObject componentObject = getComponentObject(reference);
91  2 String role = componentObject.getStringValue(COMPONENT_ROLE_TYPE_FIELD);
92  2 Type roleType;
93   
94  2 try {
95  2 roleType = ReflectionUtils.unserializeType(role, Thread.currentThread().getContextClassLoader());
96    } catch (ClassNotFoundException e) {
97  1 throw new WikiComponentException(String.format("The role type [%s] does not exist", role), e);
98    }
99   
100  1 return roleType;
101    }
102   
 
103  1 toggle @Override
104    public String getRoleHint(DocumentReference reference) throws WikiComponentException
105    {
106  1 BaseObject componentObject = getComponentObject(reference);
107  1 return StringUtils.defaultIfEmpty(componentObject.getStringValue(COMPONENT_ROLE_HINT_FIELD), "default");
108    }
109   
 
110  1 toggle @Override
111    public DocumentReference getAuthorReference(DocumentReference reference) throws WikiComponentException
112    {
113  1 XWikiDocument componentDocument = this.getDocument(reference);
114  1 return componentDocument.getAuthorReference();
115    }
116   
 
117  2 toggle @Override
118    public WikiComponentScope getScope(DocumentReference reference) throws WikiComponentException
119    {
120  2 BaseObject componentObject = getComponentObject(reference);
121  2 return WikiComponentScope.fromString(componentObject.getStringValue(COMPONENT_SCOPE_FIELD));
122    }
123   
 
124  1 toggle @Override
125    public Map<String, XDOM> getHandledMethods(DocumentReference reference) throws WikiComponentException
126    {
127  1 Map<String, XDOM> handledMethods = new HashMap<String, XDOM>();
128  1 XWikiDocument componentDocument = this.getDocument(reference);
129  1 if (componentDocument.getObjectNumbers(METHOD_CLASS) > 0) {
130  1 for (BaseObject method : componentDocument.getObjects(METHOD_CLASS)) {
131  1 if (!StringUtils.isBlank(method.getStringValue(METHOD_NAME_FIELD))) {
132  1 handledMethods.put(method.getStringValue(METHOD_NAME_FIELD), renderingBridge.parse(
133    method.getStringValue(METHOD_CODE_FIELD), this.getSyntax(reference), reference));
134    }
135    }
136    }
137  1 return handledMethods;
138    }
139   
 
140  1 toggle @Override
141    public List<Class< ? >> getDeclaredInterfaces(DocumentReference reference) throws WikiComponentException
142    {
143  1 List<Class< ? >> interfaces = new ArrayList<Class< ? >>();
144  1 XWikiDocument componentDocument = this.getDocument(reference);
145  1 if (componentDocument.getObjectNumbers(INTERFACE_CLASS) > 0) {
146  1 for (BaseObject iface : componentDocument.getObjects(INTERFACE_CLASS)) {
147  1 if (!StringUtils.isBlank(iface.getStringValue(INTERFACE_NAME_FIELD))) {
148  1 try {
149  1 Class< ? > implemented = Class.forName(iface.getStringValue(INTERFACE_NAME_FIELD));
150  1 interfaces.add(implemented);
151    } catch (Exception e) {
152  0 this.logger.warn("Interface [{}] not found, declared for wiki component [{}]",
153    iface.getStringValue(INTERFACE_NAME_FIELD), componentDocument.getDocumentReference());
154    }
155    }
156    }
157    }
158  1 return interfaces;
159    }
160   
 
161  1 toggle @Override
162    public Map<String, ComponentDescriptor> getDependencies(DocumentReference reference) throws WikiComponentException
163    {
164  1 Map<String, ComponentDescriptor> dependencies = new HashMap<String, ComponentDescriptor>();
165  1 XWikiDocument componentDocument = this.getDocument(reference);
166  1 if (componentDocument.getObjectNumbers(DEPENDENCY_CLASS) > 0) {
167  1 for (BaseObject dependency : componentDocument.getObjects(DEPENDENCY_CLASS)) {
168  1 try {
169  1 DefaultComponentDescriptor cd = new DefaultComponentDescriptor();
170  1 cd.setRoleType(ReflectionUtils.unserializeType(
171    dependency.getStringValue(COMPONENT_ROLE_TYPE_FIELD), Thread.currentThread()
172    .getContextClassLoader()));
173  1 cd.setRoleHint(dependency.getStringValue(COMPONENT_ROLE_HINT_FIELD));
174  1 dependencies.put(dependency.getStringValue(DEPENDENCY_BINDING_NAME_FIELD), cd);
175    } catch (Exception e) {
176  0 this.logger.warn("Interface [{}] not found, declared as dependency for wiki component [{}]",
177    dependency.getStringValue(COMPONENT_ROLE_TYPE_FIELD), componentDocument.getDocumentReference());
178    }
179    }
180    }
181   
182  1 return dependencies;
183    }
184   
 
185  0 toggle @Override
186    public boolean hasProgrammingRights(DocumentReference reference) throws WikiComponentException
187    {
188  0 XWikiDocument document = getDocument(reference);
189   
190  0 return this.authorization.hasAccess(Right.PROGRAM, document.getAuthorReference(), null);
191    }
192   
193    /**
194    * Get the main component object from a wiki component document.
195    *
196    * @param reference a reference to the document holding the component
197    * @return the object defining the component
198    * @throws WikiComponentException if the document can't be retrieved
199    */
 
200  6 toggle private BaseObject getComponentObject(DocumentReference reference) throws WikiComponentException
201    {
202  6 XWikiDocument componentDocument = this.getDocument(reference);
203  6 BaseObject componentObject = componentDocument.getObject(COMPONENT_CLASS);
204   
205  6 if (componentObject == null) {
206  1 throw new WikiComponentException(String.format("No component object could be found in document [%s]",
207    reference));
208    }
209   
210  5 return componentObject;
211    }
212   
213    /**
214    * Get the XWikiDocument corresponding to the given Document Reference.
215    *
216    * @param reference the document reference
217    * @return the found XWikiDocument
218    * @throws WikiComponentException if an error occurs
219    */
 
220  12 toggle private XWikiDocument getDocument(DocumentReference reference) throws WikiComponentException
221    {
222  12 try {
223  12 XWikiContext xcontext = this.xcontextProvider.get();
224  12 return xcontext.getWiki().getDocument(reference, xcontext);
225    } catch (XWikiException e) {
226  0 throw new WikiComponentException(String.format("Failed to retrieve the document [%s]", reference), e);
227    }
228    }
229    }