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

File XWikiDocumentFilterUtils.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

38
78
11
1
349
196
30
0.38
7.09
11
2.73

Classes

Class Line # Actions
XWikiDocumentFilterUtils 75 78 0% 30 25
0.803149680.3%
 

Contributing tests

This file is covered by 39 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;
21   
22    import java.io.IOException;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.component.manager.ComponentLookupException;
30    import org.xwiki.component.manager.ComponentManager;
31    import org.xwiki.component.util.DefaultParameterizedType;
32    import org.xwiki.filter.FilterEventParameters;
33    import org.xwiki.filter.FilterException;
34    import org.xwiki.filter.event.model.WikiDocumentFilter;
35    import org.xwiki.filter.input.BeanInputFilterStream;
36    import org.xwiki.filter.input.BeanInputFilterStreamFactory;
37    import org.xwiki.filter.input.InputFilterStreamFactory;
38    import org.xwiki.filter.input.InputSource;
39    import org.xwiki.filter.instance.input.BeanEntityEventGenerator;
40    import org.xwiki.filter.instance.input.DocumentInstanceInputProperties;
41    import org.xwiki.filter.instance.input.EntityEventGenerator;
42    import org.xwiki.filter.instance.output.DocumentInstanceOutputProperties;
43    import org.xwiki.filter.output.BeanOutputFilterStream;
44    import org.xwiki.filter.output.BeanOutputFilterStreamFactory;
45    import org.xwiki.filter.output.OutputFilterStreamFactory;
46    import org.xwiki.filter.output.OutputTarget;
47    import org.xwiki.filter.output.StringWriterOutputTarget;
48    import org.xwiki.filter.output.WriterOutputTarget;
49    import org.xwiki.filter.xar.input.XARInputProperties;
50    import org.xwiki.filter.xar.input.XARInputProperties.SourceType;
51    import org.xwiki.filter.xar.internal.XARFilter;
52    import org.xwiki.filter.xar.internal.XARFilterUtils;
53    import org.xwiki.filter.xar.output.XAROutputProperties;
54    import org.xwiki.model.reference.DocumentReference;
55    import org.xwiki.model.reference.EntityReference;
56    import org.xwiki.model.reference.SpaceReference;
57   
58    import com.xpn.xwiki.doc.XWikiAttachment;
59    import com.xpn.xwiki.doc.XWikiDocument;
60    import com.xpn.xwiki.internal.filter.output.EntityOutputFilterStream;
61    import com.xpn.xwiki.internal.filter.output.XWikiDocumentOutputFilterStream;
62    import com.xpn.xwiki.objects.BaseObject;
63    import com.xpn.xwiki.objects.BaseProperty;
64    import com.xpn.xwiki.objects.classes.BaseClass;
65    import com.xpn.xwiki.objects.classes.PropertyClass;
66   
67    /**
68    * Various XWikiDocument related helpers.
69    *
70    * @version $Id: 5c8d36abfe47ab07100313bee9c633191807a52a $
71    * @since 9.0RC1
72    */
73    @Component(roles = XWikiDocumentFilterUtils.class)
74    @Singleton
 
75    public class XWikiDocumentFilterUtils
76    {
77    @Inject
78    @Named(XARFilterUtils.ROLEHINT_CURRENT)
79    private InputFilterStreamFactory xarInputFilterStreamFactory;
80   
81    @Inject
82    @Named(XARFilterUtils.ROLEHINT_CURRENT)
83    private OutputFilterStreamFactory xarOutputFilterStreamFactory;
84   
85    @Inject
86    private ComponentManager componentManager;
87   
88    // Import
89   
 
90  20152 toggle private <T> Class<T> getClass(Object entity)
91    {
92  20152 Class<T> entityClass;
93   
94  20152 if (entity instanceof Class) {
95  1 entityClass = (Class<T>) entity;
96  20151 } else if (entity instanceof XWikiDocument) {
97  13266 entityClass = (Class<T>) XWikiDocument.class;
98  6885 } else if (entity instanceof XWikiAttachment) {
99  2 entityClass = (Class<T>) XWikiAttachment.class;
100  6883 } else if (entity instanceof BaseClass) {
101  6881 entityClass = (Class<T>) BaseClass.class;
102  2 } else if (entity instanceof BaseObject) {
103  0 entityClass = (Class<T>) BaseObject.class;
104  2 } else if (entity instanceof BaseProperty) {
105  0 entityClass = (Class<T>) BaseProperty.class;
106  2 } else if (entity instanceof PropertyClass) {
107  2 entityClass = (Class<T>) PropertyClass.class;
108    } else {
109  0 entityClass = (Class<T>) entity.getClass();
110    }
111   
112  20152 return entityClass;
113    }
114   
 
115  12634 toggle private SourceType getSourceType(Class<?> entityClass) throws FilterException
116    {
117  12634 SourceType sourceType;
118   
119  12634 if (entityClass == XWikiDocument.class) {
120  7635 sourceType = SourceType.DOCUMENT;
121  4999 } else if (entityClass == XWikiAttachment.class) {
122  1 sourceType = SourceType.ATTACHMENT;
123  4998 } else if (entityClass == BaseClass.class) {
124  4998 sourceType = SourceType.CLASS;
125  0 } else if (entityClass == BaseObject.class) {
126  0 sourceType = SourceType.OBJECT;
127  0 } else if (entityClass == BaseProperty.class) {
128  0 sourceType = SourceType.OBJECTPROPERTY;
129  0 } else if (entityClass == PropertyClass.class) {
130  0 sourceType = SourceType.CLASSPROPERTY;
131    } else {
132  0 throw new FilterException("Unsupported type [" + entityClass + "]");
133    }
134   
135  12634 return sourceType;
136    }
137   
138    /**
139    * @param entity the entity to write to or its class to create a new one
140    * @param source the stream to read
141    * @return the imported entity, same as {@code entity} if not null
142    * @throws FilterException when failing to import
143    * @throws IOException when failing to import
144    * @throws ComponentLookupException when failing to find a EntityOutputFilterStream corresponding to passed class
145    */
 
146  4999 toggle public <T> T importEntity(Object entity, InputSource source)
147    throws FilterException, IOException, ComponentLookupException
148    {
149    // Output
150  4998 DocumentInstanceOutputProperties documentProperties = new DocumentInstanceOutputProperties();
151   
152    // Input
153  4999 XARInputProperties xarProperties = new XARInputProperties();
154   
155  4999 return importEntity(getClass(entity), entity instanceof Class ? null : (T) entity, source, xarProperties,
156    documentProperties);
157    }
158   
159    /**
160    * @param entityClass to class used to find the {@link EntityOutputFilterStream} component
161    * @param entity the entity to write to or null to create a new entity of the passed class
162    * @param source the stream to read
163    * @param xarProperties the configuration of the input filter
164    * @param documentProperties the configuration of the output filter
165    * @return the imported entity, same as {@code entity} if not null
166    * @throws FilterException when failing to import
167    * @throws IOException when failing to import
168    * @throws ComponentLookupException when failing to find a EntityOutputFilterStream corresponding to passed class
169    */
 
170  12634 toggle public <T> T importEntity(Class<T> entityClass, T entity, InputSource source, XARInputProperties xarProperties,
171    DocumentInstanceOutputProperties documentProperties)
172    throws FilterException, IOException, ComponentLookupException
173    {
174    // Output
175  12634 EntityOutputFilterStream<T> filterStream = this.componentManager
176    .getInstance(new DefaultParameterizedType(null, EntityOutputFilterStream.class, entityClass));
177  12634 filterStream.setProperties(documentProperties);
178  12634 filterStream.setEntity(entity);
179  12634 if (filterStream instanceof XWikiDocumentOutputFilterStream) {
180  7635 ((XWikiDocumentOutputFilterStream) filterStream).disableRenderingEvents();
181    }
182   
183    // Input
184  12634 xarProperties.setSourceType(getSourceType(entityClass));
185  12634 xarProperties.setSource(source);
186  12634 BeanInputFilterStream<XARInputProperties> xarReader =
187    ((BeanInputFilterStreamFactory<XARInputProperties>) this.xarInputFilterStreamFactory)
188    .createInputFilterStream(xarProperties);
189   
190    // Convert
191  12634 xarReader.read(filterStream.getFilter());
192   
193  12634 xarReader.close();
194   
195  12634 return filterStream.getEntity();
196    }
197   
198    /**
199    * @param source the stream to read
200    * @param xarProperties the configuration of the input filter
201    * @param documentProperties the configuration of the output filter
202    * @return the imported document
203    * @throws FilterException when failing to import
204    * @throws IOException when failing to import
205    * @throws ComponentLookupException when failing to find a EntityOutputFilterStream corresponding to passed class
206    */
 
207  5053 toggle public XWikiDocument importDocument(InputSource source, XARInputProperties xarProperties,
208    DocumentInstanceOutputProperties documentProperties)
209    throws FilterException, IOException, ComponentLookupException
210    {
211  5053 return importEntity(XWikiDocument.class, null, source, xarProperties, documentProperties);
212    }
213   
214    // Export
215   
216    /**
217    * @param entity the entity to read
218    * @return the XML as a String
219    * @throws ComponentLookupException failed to find an event generator for passed entity
220    * @throws FilterException when failing to generate export the passed entity
221    * @throws IOException when failing to close the stream
222    */
 
223  1 toggle public String exportEntity(Object entity) throws ComponentLookupException, FilterException, IOException
224    {
225  1 return exportEntity(entity, new XAROutputProperties());
226    }
227   
228    /**
229    * @param entity the entity to read
230    * @param xarProperties the configuration of the output filter
231    * @return the XML as a String
232    * @throws ComponentLookupException failed to find an event generator for passed entity
233    * @throws FilterException when failing to generate export the passed entity
234    * @throws IOException when failing to close the stream
235    */
 
236  1886 toggle public String exportEntity(Object entity, XAROutputProperties xarProperties)
237    throws ComponentLookupException, FilterException, IOException
238    {
239  1886 return exportEntity(entity, xarProperties, new DocumentInstanceInputProperties());
240    }
241   
242    /**
243    * @param entity the entity to read
244    * @param target the target where to write the result
245    * @throws ComponentLookupException failed to find an event generator for passed entity
246    * @throws FilterException when failing to generate export the passed entity
247    * @throws IOException when failing to close the stream
248    */
 
249  0 toggle public void exportEntity(Object entity, OutputTarget target)
250    throws ComponentLookupException, FilterException, IOException
251    {
252  0 exportEntity(entity, target, new DocumentInstanceInputProperties());
253    }
254   
255    /**
256    * @param entity the entity to read
257    * @param target the target where to write the result
258    * @param documentProperties the configuration of the input filter
259    * @throws ComponentLookupException failed to find an event generator for passed entity
260    * @throws FilterException when failing to generate export the passed entity
261    * @throws IOException when failing to close the stream
262    */
 
263  0 toggle public void exportEntity(Object entity, OutputTarget target, DocumentInstanceInputProperties documentProperties)
264    throws ComponentLookupException, FilterException, IOException
265    {
266  0 exportEntity(entity, target, new XAROutputProperties(), documentProperties);
267    }
268   
269    /**
270    * @param entity the entity to read
271    * @param xarProperties the configuration of the output filter
272    * @param documentProperties the configuration of the input filter
273    * @return the XML as a String
274    * @throws ComponentLookupException failed to find an event generator for passed entity
275    * @throws FilterException when failing to generate export the passed entity
276    * @throws IOException when failing to close the stream
277    */
 
278  1886 toggle public String exportEntity(Object entity, XAROutputProperties xarProperties,
279    DocumentInstanceInputProperties documentProperties)
280    throws ComponentLookupException, FilterException, IOException
281    {
282  1886 WriterOutputTarget target = new StringWriterOutputTarget();
283   
284  1886 exportEntity(entity, target, xarProperties, documentProperties);
285   
286  1886 return target.toString();
287    }
288   
289    /**
290    * @param entity the entity to read
291    * @param target the target where to write the result
292    * @param xarProperties the configuration of the output filter
293    * @param documentProperties the configuration of the input filter
294    * @throws ComponentLookupException failed to find an event generator for passed entity
295    * @throws FilterException when failing to generate export the passed entity
296    * @throws IOException when failing to close the stream
297    */
 
298  15153 toggle public void exportEntity(Object entity, OutputTarget target, XAROutputProperties xarProperties,
299    DocumentInstanceInputProperties documentProperties)
300    throws ComponentLookupException, FilterException, IOException
301    {
302    // Input
303  15153 documentProperties.setVerbose(false);
304   
305    // Output
306  15153 xarProperties.setForceDocument(true);
307  15153 if (target != null) {
308  15153 xarProperties.setTarget(target);
309    }
310  15153 xarProperties.setVerbose(false);
311  15153 BeanOutputFilterStream<XAROutputProperties> xarFilter =
312    ((BeanOutputFilterStreamFactory<XAROutputProperties>) this.xarOutputFilterStreamFactory)
313    .createOutputFilterStream(xarProperties);
314  15153 XARFilter filter = (XARFilter) xarFilter.getFilter();
315   
316  15153 BeanEntityEventGenerator<Object, DocumentInstanceInputProperties> generator = this.componentManager
317    .getInstance(new DefaultParameterizedType(null, EntityEventGenerator.class, getClass(entity)));
318   
319    // Spaces and document events
320  15153 FilterEventParameters documentParameters = null;
321  15153 DocumentReference documentReference = null;
322  15153 if (entity instanceof XWikiDocument) {
323  13266 documentReference = ((XWikiDocument) entity).getDocumentReference();
324  13266 for (SpaceReference spaceReference : documentReference.getSpaceReferences()) {
325  15411 filter.beginWikiSpace(spaceReference.getName(), FilterEventParameters.EMPTY);
326    }
327   
328  13266 documentParameters = new FilterEventParameters();
329  13266 documentParameters.put(WikiDocumentFilter.PARAMETER_LOCALE, ((XWikiDocument) entity).getDefaultLocale());
330  13266 filter.beginWikiDocument(documentReference.getName(), documentParameters);
331    }
332   
333    // Document Locale events
334  15153 generator.write(entity, xarFilter, documentProperties);
335   
336    // Document and spaces events
337  15153 if (documentParameters != null) {
338  13266 filter.endWikiDocument(documentReference.getName(), documentParameters);
339   
340  13266 documentReference = ((XWikiDocument) entity).getDocumentReference();
341  13266 for (EntityReference reference =
342  28677 documentReference.getParent(); reference instanceof SpaceReference; reference = reference.getParent()) {
343  15411 filter.beginWikiSpace(reference.getName(), FilterEventParameters.EMPTY);
344    }
345    }
346   
347  15153 xarFilter.close();
348    }
349    }