1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.filter.output

File AbstractEntityOutputFilterStream.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
41% 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 48 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  35606 toggle protected void initialize(EntityOutputFilterStream<?>... children)
88    {
89  35606 this.children = new ArrayList<>(children.length);
90   
91  35606 for (EntityOutputFilterStream<?> child : children) {
92  58562 child.disable();
93   
94  58562 this.children.add(child);
95    }
96    }
97   
 
98  194015 toggle protected void disableChildren()
99    {
100  194016 if (this.children != null) {
101  68139 for (EntityOutputFilterStream<?> child : this.children) {
102  83161 child.disable();
103    }
104    }
105    }
106   
 
107  8051 toggle @Override
108    public boolean isEnabled()
109    {
110  8051 return this.enabled;
111    }
112   
 
113  36873 toggle @Override
114    public void enable()
115    {
116  36873 this.enabled = true;
117    }
118   
 
119  194016 toggle @Override
120    public void disable()
121    {
122  194015 this.enabled = false;
123   
124  194016 disableChildren();
125    }
126   
 
127  71213 toggle @Override
128    public Object getFilter()
129    {
130  71213 if (this.filter == null) {
131  71213 this.filter = createFilter();
132    }
133   
134  71212 return this.filter;
135    }
136   
 
137  63561 toggle protected Object createFilter()
138    {
139  63561 if (this.children != null) {
140  27954 List<XWikiDocumentFilter> filters = new ArrayList<>(this.children.size() + 1);
141  27954 for (EntityOutputFilterStream<?> child : this.children) {
142  35606 filters.add((XWikiDocumentFilter) child.getFilter());
143    }
144  27954 filters.add(this);
145   
146  27954 return new XWikiDocumentFilterCollection(filters);
147    }
148   
149  35607 return this;
150    }
151   
 
152  71213 toggle @Override
153    public void setProperties(DocumentInstanceOutputProperties properties)
154    {
155  71213 this.properties = properties;
156   
157  71213 if (this.children != null) {
158  35606 for (EntityOutputFilterStream<?> child : this.children) {
159  58562 child.setProperties(properties);
160    }
161    }
162    }
163   
 
164  287334 toggle @Override
165    public E getEntity()
166    {
167  287333 return this.entity;
168    }
169   
 
170  188881 toggle @Override
171    public void setEntity(E entity)
172    {
173  188879 this.entity = entity;
174    }
175   
 
176  400511 toggle protected <T> T get(Type type, String key, FilterEventParameters parameters, T def)
177    {
178  400511 return get(type, key, parameters, def, true, true);
179    }
180   
 
181  521168 toggle protected <T> T get(Type type, String key, FilterEventParameters parameters, T def, boolean replaceNull,
182    boolean convert)
183    {
184  521168 if (parameters == null) {
185  0 return def;
186    }
187   
188  521168 if (!parameters.containsKey(key)) {
189  280652 return def;
190    }
191   
192  240516 Object value = parameters.get(key);
193   
194  240516 if (value == null) {
195  0 return replaceNull ? def : null;
196    }
197   
198  240516 if (TypeUtils.isInstance(value, type)) {
199  240516 return (T) value;
200    }
201   
202  0 return convert ? this.converter.convert(type, value) : (T) value;
203    }
204   
 
205  69275 toggle protected Date getDate(String key, FilterEventParameters parameters, Date def)
206    {
207  69275 return get(Date.class, key, parameters, def);
208    }
209   
 
210  247340 toggle protected String getString(String key, FilterEventParameters parameters, String def)
211    {
212  247340 return get(String.class, key, parameters, def);
213    }
214   
 
215  45920 toggle protected boolean getBoolean(String key, FilterEventParameters parameters, boolean def)
216    {
217  45920 return get(boolean.class, key, parameters, def);
218    }
219   
 
220  7370 toggle protected int getInt(String key, FilterEventParameters parameters, int def)
221    {
222  7370 return get(int.class, key, parameters, def);
223    }
224   
 
225  22960 toggle protected Syntax getSyntax(String key, FilterEventParameters parameters, Syntax def)
226    {
227  22960 return get(Syntax.class, key, parameters, def);
228    }
229   
 
230  30330 toggle protected EntityReference getEntityReference(String key, FilterEventParameters parameters, EntityReference def)
231    {
232  30330 Object reference = get(Object.class, key, parameters, def, false, false);
233   
234  30330 if (reference != null && !(reference instanceof EntityReference)) {
235  7367 reference = this.relativeResolver.resolve(reference.toString(), EntityType.DOCUMENT);
236    }
237   
238  30330 return (EntityReference) reference;
239    }
240   
 
241  90327 toggle protected DocumentReference getDocumentReference(String key, FilterEventParameters parameters,
242    DocumentReference def)
243    {
244  90327 Object reference = get(Object.class, key, parameters, def, false, false);
245   
246  90327 if (reference != null && !(reference instanceof DocumentReference)) {
247  51788 if (reference instanceof EntityReference) {
248  0 reference =
249    this.documentEntityResolver.resolve((EntityReference) reference, this.currentEntityReference);
250    } else {
251  51788 reference = this.documentStringResolver.resolve(reference.toString(), this.currentEntityReference);
252    }
253    }
254   
255  90327 return (DocumentReference) reference;
256    }
257   
 
258  69913 toggle protected DocumentReference getUserReference(String key, FilterEventParameters parameters, DocumentReference def)
259    {
260  69913 DocumentReference userReference = getDocumentReference(key, parameters, def);
261   
262  69913 if (userReference != null && userReference.getName().equals(XWikiRightService.GUEST_USER)) {
263  77 userReference = null;
264    }
265   
266  69913 return userReference;
267    }
268   
269    // XWikiDocumentFilter
270   
 
271  88 toggle @Override
272    public void beginWiki(String name, FilterEventParameters parameters) throws FilterException
273    {
274  88 this.currentEntityReference = new EntityReference(name, EntityType.WIKI, this.currentEntityReference);
275    }
276   
 
277  88 toggle @Override
278    public void endWiki(String name, FilterEventParameters parameters) throws FilterException
279    {
280  88 this.currentEntityReference = this.currentEntityReference.getParent();
281    }
282   
 
283  69128 toggle @Override
284    public void beginWikiSpace(String name, FilterEventParameters parameters) throws FilterException
285    {
286  69128 this.currentEntityReference = new EntityReference(name, EntityType.SPACE, this.currentEntityReference);
287    }
288   
 
289  69128 toggle @Override
290    public void endWikiSpace(String name, FilterEventParameters parameters) throws FilterException
291    {
292  69128 this.currentEntityReference = this.currentEntityReference.getParent();
293    }
294   
 
295  61248 toggle @Override
296    public void beginWikiDocument(String name, FilterEventParameters parameters) throws FilterException
297    {
298  61248 this.currentEntityReference = new EntityReference(name, EntityType.DOCUMENT, this.currentEntityReference);
299    }
300   
 
301  61248 toggle @Override
302    public void endWikiDocument(String name, FilterEventParameters parameters) throws FilterException
303    {
304  61248 this.currentEntityReference = this.currentEntityReference.getParent();
305    }
306   
 
307  53564 toggle @Override
308    public void beginWikiDocumentLocale(Locale locale, FilterEventParameters parameters) throws FilterException
309    {
310   
311    }
312   
 
313  53564 toggle @Override
314    public void endWikiDocumentLocale(Locale locale, FilterEventParameters parameters) throws FilterException
315    {
316   
317    }
318   
 
319  53564 toggle @Override
320    public void beginWikiDocumentRevision(String revision, FilterEventParameters parameters) throws FilterException
321    {
322   
323    }
324   
 
325  53564 toggle @Override
326    public void endWikiDocumentRevision(String revision, FilterEventParameters parameters) throws FilterException
327    {
328   
329    }
330   
 
331  6192 toggle @Override
332    public void onWikiAttachment(String name, InputStream content, Long size, FilterEventParameters parameters)
333    throws FilterException
334    {
335   
336    }
337   
 
338  53304 toggle @Override
339    public void beginWikiClass(FilterEventParameters parameters) throws FilterException
340    {
341   
342    }
343   
 
344  37202 toggle @Override
345    public void endWikiClass(FilterEventParameters parameters) throws FilterException
346    {
347   
348    }
349   
 
350  328371 toggle @Override
351    public void beginWikiClassProperty(String name, String type, FilterEventParameters parameters)
352    throws FilterException
353    {
354   
355    }
356   
 
357  191020 toggle @Override
358    public void endWikiClassProperty(String name, String type, FilterEventParameters parameters) throws FilterException
359    {
360   
361    }
362   
 
363  3231334 toggle @Override
364    public void onWikiClassPropertyField(String name, String value, FilterEventParameters parameters)
365    throws FilterException
366    {
367   
368    }
369   
 
370  58960 toggle @Override
371    public void beginWikiObject(String name, FilterEventParameters parameters) throws FilterException
372    {
373  58960 if (name != null) {
374  58960 this.currentEntityReference = new EntityReference(name, EntityType.OBJECT, this.currentEntityReference);
375    }
376    }
377   
 
378  58960 toggle @Override
379    public void endWikiObject(String name, FilterEventParameters parameters) throws FilterException
380    {
381  58960 if (this.currentEntityReference.getType() == EntityType.OBJECT) {
382  58960 this.currentEntityReference = this.currentEntityReference.getParent();
383    }
384    }
385   
 
386  239196 toggle @Override
387    public void onWikiObjectProperty(String name, Object value, FilterEventParameters parameters) throws FilterException
388    {
389   
390    }
391    }