1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.filter.output

File AbstractEntityOutputFilterStream.java

 

Coverage histogram

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

Code metrics

32
67
39
1
391
280
58
0.87
1.72
39
1.49

Classes

Class Line # Actions
AbstractEntityOutputFilterStream 56 67 0% 58 15
0.891304489.1%
 

Contributing tests

This file is covered by 44 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.internal.filter.output;
21   
22    import java.io.InputStream;
23    import java.lang.reflect.Type;
24    import java.util.ArrayList;
25    import java.util.Date;
26    import java.util.List;
27    import java.util.Locale;
28    import java.util.regex.Pattern;
29   
30    import javax.inject.Inject;
31    import javax.inject.Named;
32   
33    import org.apache.commons.lang3.reflect.TypeUtils;
34    import org.xwiki.filter.FilterEventParameters;
35    import org.xwiki.filter.FilterException;
36    import org.xwiki.filter.instance.output.DocumentInstanceOutputProperties;
37    import org.xwiki.model.EntityType;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.model.reference.DocumentReferenceResolver;
40    import org.xwiki.model.reference.EntityReference;
41    import org.xwiki.model.reference.EntityReferenceResolver;
42    import org.xwiki.properties.ConverterManager;
43    import org.xwiki.rendering.syntax.Syntax;
44   
45    import com.xpn.xwiki.internal.filter.XWikiDocumentFilter;
46    import com.xpn.xwiki.internal.filter.XWikiDocumentFilterCollection;
47    import com.xpn.xwiki.user.api.XWikiRightService;
48   
49    /**
50    * Base class to help implement {@link EntityOutputFilterStream}.
51    *
52    * @param <E> the type of the entity
53    * @version $Id: 1db9566cd54350120a28c06a649cbf03f7fd1ebd $
54    * @since 9.0RC1
55    */
 
56    public abstract class AbstractEntityOutputFilterStream<E> implements EntityOutputFilterStream<E>
57    {
58    protected static final Pattern VALID_VERSION = Pattern.compile("\\d*\\.\\d*");
59   
60    protected E entity;
61   
62    protected DocumentInstanceOutputProperties properties;
63   
64    protected Object filter;
65   
66    protected EntityReference currentEntityReference;
67   
68    @Inject
69    @Named("relative")
70    protected EntityReferenceResolver<String> relativeResolver;
71   
72    @Inject
73    @Named("current")
74    protected DocumentReferenceResolver<EntityReference> documentEntityResolver;
75   
76    @Inject
77    @Named("current")
78    protected DocumentReferenceResolver<String> documentStringResolver;
79   
80    @Inject
81    protected ConverterManager converter;
82   
83    protected List<EntityOutputFilterStream<?>> children;
84   
85    protected boolean enabled = true;
86   
 
87  11893 toggle protected void initialize(EntityOutputFilterStream<?>... children)
88    {
89  11893 this.children = new ArrayList<>(children.length);
90   
91  11893 for (EntityOutputFilterStream<?> child : children) {
92  19504 child.disable();
93   
94  19504 this.children.add(child);
95    }
96    }
97   
 
98  71065 toggle protected void disableChildren()
99    {
100  71066 if (this.children != null) {
101  25215 for (EntityOutputFilterStream<?> child : this.children) {
102  30700 child.disable();
103    }
104    }
105    }
106   
 
107  3275 toggle @Override
108    public boolean isEnabled()
109    {
110  3275 return this.enabled;
111    }
112   
 
113  14639 toggle @Override
114    public void enable()
115    {
116  14637 this.enabled = true;
117    }
118   
 
119  71067 toggle @Override
120    public void disable()
121    {
122  71067 this.enabled = false;
123   
124  71067 disableChildren();
125    }
126   
 
127  23786 toggle @Override
128    public Object getFilter()
129    {
130  23786 if (this.filter == null) {
131  23785 this.filter = createFilter();
132    }
133   
134  23786 return this.filter;
135    }
136   
 
137  21248 toggle protected Object createFilter()
138    {
139  21249 if (this.children != null) {
140  9356 List<XWikiDocumentFilter> filters = new ArrayList<>(this.children.size() + 1);
141  9356 for (EntityOutputFilterStream<?> child : this.children) {
142  11893 filters.add((XWikiDocumentFilter) child.getFilter());
143    }
144  9356 filters.add(this);
145   
146  9356 return new XWikiDocumentFilterCollection(filters);
147    }
148   
149  11893 return this;
150    }
151   
 
152  23786 toggle @Override
153    public void setProperties(DocumentInstanceOutputProperties properties)
154    {
155  23786 this.properties = properties;
156   
157  23786 if (this.children != null) {
158  11892 for (EntityOutputFilterStream<?> child : this.children) {
159  19504 child.setProperties(properties);
160    }
161    }
162    }
163   
 
164  110276 toggle @Override
165    public E getEntity()
166    {
167  110280 return this.entity;
168    }
169   
 
170  71344 toggle @Override
171    public void setEntity(E entity)
172    {
173  71344 this.entity = entity;
174    }
175   
 
176  137483 toggle protected <T> T get(Type type, String key, FilterEventParameters parameters, T def)
177    {
178  137486 return get(type, key, parameters, def, true, true);
179    }
180   
 
181  179311 toggle protected <T> T get(Type type, String key, FilterEventParameters parameters, T def, boolean replaceNull,
182    boolean convert)
183    {
184  179317 if (parameters == null) {
185  0 return def;
186    }
187   
188  179322 if (!parameters.containsKey(key)) {
189  84321 return def;
190    }
191   
192  95000 Object value = parameters.get(key);
193   
194  94998 if (value == null) {
195  0 return replaceNull ? def : null;
196    }
197   
198  95000 if (TypeUtils.isInstance(value, type)) {
199  95000 return (T) value;
200    }
201   
202  0 return convert ? this.converter.convert(type, value) : (T) value;
203    }
204   
 
205  22855 toggle protected Date getDate(String key, FilterEventParameters parameters, Date def)
206    {
207  22855 return get(Date.class, key, parameters, def);
208    }
209   
 
210  86299 toggle protected String getString(String key, FilterEventParameters parameters, String def)
211    {
212  86308 return get(String.class, key, parameters, def);
213    }
214   
 
215  15232 toggle protected boolean getBoolean(String key, FilterEventParameters parameters, boolean def)
216    {
217  15232 return get(boolean.class, key, parameters, def);
218    }
219   
 
220  2948 toggle protected int getInt(String key, FilterEventParameters parameters, int def)
221    {
222  2948 return get(int.class, key, parameters, def);
223    }
224   
 
225  7616 toggle protected Syntax getSyntax(String key, FilterEventParameters parameters, Syntax def)
226    {
227  7616 return get(Syntax.class, key, parameters, def);
228    }
229   
 
230  10564 toggle protected EntityReference getEntityReference(String key, FilterEventParameters parameters, EntityReference def)
231    {
232  10564 Object reference = get(Object.class, key, parameters, def, false, false);
233   
234  10564 if (reference != null && !(reference instanceof EntityReference)) {
235  2946 reference = this.relativeResolver.resolve(reference.toString(), EntityType.DOCUMENT);
236    }
237   
238  10564 return (EntityReference) reference;
239    }
240   
 
241  31261 toggle protected DocumentReference getDocumentReference(String key, FilterEventParameters parameters,
242    DocumentReference def)
243    {
244  31261 Object reference = get(Object.class, key, parameters, def, false, false);
245   
246  31259 if (reference != null && !(reference instanceof DocumentReference)) {
247  18427 if (reference instanceof EntityReference) {
248  0 reference =
249    this.documentEntityResolver.resolve((EntityReference) reference, this.currentEntityReference);
250    } else {
251  18426 reference = this.documentStringResolver.resolve(reference.toString(), this.currentEntityReference);
252    }
253    }
254   
255  31261 return (DocumentReference) reference;
256    }
257   
 
258  23297 toggle protected DocumentReference getUserReference(String key, FilterEventParameters parameters, DocumentReference def)
259    {
260  23297 DocumentReference userReference = getDocumentReference(key, parameters, def);
261   
262  23297 if (userReference != null && userReference.getName().equals(XWikiRightService.GUEST_USER)) {
263  77 userReference = null;
264    }
265   
266  23297 return userReference;
267    }
268   
269    // XWikiDocumentFilter
270   
 
271  72 toggle @Override
272    public void beginWiki(String name, FilterEventParameters parameters) throws FilterException
273    {
274  72 this.currentEntityReference = new EntityReference(name, EntityType.WIKI, this.currentEntityReference);
275    }
276   
 
277  72 toggle @Override
278    public void endWiki(String name, FilterEventParameters parameters) throws FilterException
279    {
280  72 this.currentEntityReference = this.currentEntityReference.getParent();
281    }
282   
 
283  20336 toggle @Override
284    public void beginWikiSpace(String name, FilterEventParameters parameters) throws FilterException
285    {
286  20336 this.currentEntityReference = new EntityReference(name, EntityType.SPACE, this.currentEntityReference);
287    }
288   
 
289  20336 toggle @Override
290    public void endWikiSpace(String name, FilterEventParameters parameters) throws FilterException
291    {
292  20336 this.currentEntityReference = this.currentEntityReference.getParent();
293    }
294   
 
295  20320 toggle @Override
296    public void beginWikiDocument(String name, FilterEventParameters parameters) throws FilterException
297    {
298  20320 this.currentEntityReference = new EntityReference(name, EntityType.DOCUMENT, this.currentEntityReference);
299    }
300   
 
301  20320 toggle @Override
302    public void endWikiDocument(String name, FilterEventParameters parameters) throws FilterException
303    {
304  20320 this.currentEntityReference = this.currentEntityReference.getParent();
305    }
306   
 
307  17766 toggle @Override
308    public void beginWikiDocumentLocale(Locale locale, FilterEventParameters parameters) throws FilterException
309    {
310   
311    }
312   
 
313  17766 toggle @Override
314    public void endWikiDocumentLocale(Locale locale, FilterEventParameters parameters) throws FilterException
315    {
316   
317    }
318   
 
319  17766 toggle @Override
320    public void beginWikiDocumentRevision(String revision, FilterEventParameters parameters) throws FilterException
321    {
322   
323    }
324   
 
325  17766 toggle @Override
326    public void endWikiDocumentRevision(String revision, FilterEventParameters parameters) throws FilterException
327    {
328   
329    }
330   
 
331  2694 toggle @Override
332    public void onWikiAttachment(String name, InputStream content, Long size, FilterEventParameters parameters)
333    throws FilterException
334    {
335   
336    }
337   
 
338  21395 toggle @Override
339    public void beginWikiClass(FilterEventParameters parameters) throws FilterException
340    {
341   
342    }
343   
 
344  14845 toggle @Override
345    public void endWikiClass(FilterEventParameters parameters) throws FilterException
346    {
347   
348    }
349   
 
350  109333 toggle @Override
351    public void beginWikiClassProperty(String name, String type, FilterEventParameters parameters)
352    throws FilterException
353    {
354   
355    }
356   
 
357  57440 toggle @Override
358    public void endWikiClassProperty(String name, String type, FilterEventParameters parameters) throws FilterException
359    {
360   
361    }
362   
 
363  965962 toggle @Override
364    public void onWikiClassPropertyField(String name, String value, FilterEventParameters parameters)
365    throws FilterException
366    {
367   
368    }
369   
 
370  23584 toggle @Override
371    public void beginWikiObject(String name, FilterEventParameters parameters) throws FilterException
372    {
373  23584 if (name != null) {
374  23584 this.currentEntityReference = new EntityReference(name, EntityType.OBJECT, this.currentEntityReference);
375    }
376    }
377   
 
378  23584 toggle @Override
379    public void endWikiObject(String name, FilterEventParameters parameters) throws FilterException
380    {
381  23584 if (this.currentEntityReference.getType() == EntityType.OBJECT) {
382  23584 this.currentEntityReference = this.currentEntityReference.getParent();
383    }
384    }
385   
 
386  73806 toggle @Override
387    public void onWikiObjectProperty(String name, Object value, FilterEventParameters parameters) throws FilterException
388    {
389   
390    }
391    }