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

File XWikiDocumentFilterUtils.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
64% 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 37
0.7086614470.9%
 

Contributing tests

This file is covered by 37 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  9338 toggle private <T> Class<T> getClass(Object entity)
91    {
92  9338 Class<T> entityClass;
93   
94  9338 if (entity instanceof Class) {
95  1 entityClass = (Class<T>) entity;
96  9336 } else if (entity instanceof XWikiDocument) {
97  6332 entityClass = (Class<T>) XWikiDocument.class;
98  3005 } else if (entity instanceof XWikiAttachment) {
99  0 entityClass = (Class<T>) XWikiAttachment.class;
100  3005 } else if (entity instanceof BaseClass) {
101  3005 entityClass = (Class<T>) BaseClass.class;
102  0 } else if (entity instanceof BaseObject) {
103  0 entityClass = (Class<T>) BaseObject.class;
104  0 } else if (entity instanceof BaseProperty) {
105  0 entityClass = (Class<T>) BaseProperty.class;
106  0 } else if (entity instanceof PropertyClass) {
107  0 entityClass = (Class<T>) PropertyClass.class;
108    } else {
109  0 entityClass = (Class<T>) entity.getClass();
110    }
111   
112  9338 return entityClass;
113    }
114   
 
115  4268 toggle private SourceType getSourceType(Class<?> entityClass) throws FilterException
116    {
117  4268 SourceType sourceType;
118   
119  4268 if (entityClass == XWikiDocument.class) {
120  2523 sourceType = SourceType.DOCUMENT;
121  1745 } else if (entityClass == XWikiAttachment.class) {
122  0 sourceType = SourceType.ATTACHMENT;
123  1745 } else if (entityClass == BaseClass.class) {
124  1745 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  4268 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  1745 toggle public <T> T importEntity(Object entity, InputSource source)
147    throws FilterException, IOException, ComponentLookupException
148    {
149    // Output
150  1745 DocumentInstanceOutputProperties documentProperties = new DocumentInstanceOutputProperties();
151   
152    // Input
153  1745 XARInputProperties xarProperties = new XARInputProperties();
154   
155  1745 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  4268 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  4268 EntityOutputFilterStream<T> filterStream = this.componentManager
176    .getInstance(new DefaultParameterizedType(null, EntityOutputFilterStream.class, entityClass));
177  4268 filterStream.setProperties(documentProperties);
178  4268 filterStream.setEntity(entity);
179  4268 if (filterStream instanceof XWikiDocumentOutputFilterStream) {
180  2523 ((XWikiDocumentOutputFilterStream) filterStream).disableRenderingEvents();
181    }
182   
183    // Input
184  4268 xarProperties.setSourceType(getSourceType(entityClass));
185  4267 xarProperties.setSource(source);
186  4268 BeanInputFilterStream<XARInputProperties> xarReader =
187    ((BeanInputFilterStreamFactory<XARInputProperties>) this.xarInputFilterStreamFactory)
188    .createInputFilterStream(xarProperties);
189   
190    // Convert
191  4268 xarReader.read(filterStream.getFilter());
192   
193  4268 xarReader.close();
194   
195  4267 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  496 toggle public XWikiDocument importDocument(InputSource source, XARInputProperties xarProperties,
208    DocumentInstanceOutputProperties documentProperties)
209    throws FilterException, IOException, ComponentLookupException
210    {
211  496 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  1261 toggle public String exportEntity(Object entity, XAROutputProperties xarProperties)
237    throws ComponentLookupException, FilterException, IOException
238    {
239  1261 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  1261 toggle public String exportEntity(Object entity, XAROutputProperties xarProperties,
279    DocumentInstanceInputProperties documentProperties)
280    throws ComponentLookupException, FilterException, IOException
281    {
282  1261 WriterOutputTarget target = new StringWriterOutputTarget();
283   
284  1261 exportEntity(entity, target, xarProperties, documentProperties);
285   
286  1261 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  7593 toggle public void exportEntity(Object entity, OutputTarget target, XAROutputProperties xarProperties,
299    DocumentInstanceInputProperties documentProperties)
300    throws ComponentLookupException, FilterException, IOException
301    {
302    // Input
303  7593 documentProperties.setVerbose(false);
304   
305    // Output
306  7593 xarProperties.setForceDocument(true);
307  7593 if (target != null) {
308  7593 xarProperties.setTarget(target);
309    }
310  7593 xarProperties.setVerbose(false);
311  7593 BeanOutputFilterStream<XAROutputProperties> xarFilter =
312    ((BeanOutputFilterStreamFactory<XAROutputProperties>) this.xarOutputFilterStreamFactory)
313    .createOutputFilterStream(xarProperties);
314  7593 XARFilter filter = (XARFilter) xarFilter.getFilter();
315   
316  7593 BeanEntityEventGenerator<Object, DocumentInstanceInputProperties> generator = this.componentManager
317    .getInstance(new DefaultParameterizedType(null, EntityEventGenerator.class, getClass(entity)));
318   
319    // Spaces and document events
320  7593 FilterEventParameters documentParameters = null;
321  7593 DocumentReference documentReference = null;
322  7593 if (entity instanceof XWikiDocument) {
323  6332 documentReference = ((XWikiDocument) entity).getDocumentReference();
324  6332 for (SpaceReference spaceReference : documentReference.getSpaceReferences()) {
325  6558 filter.beginWikiSpace(spaceReference.getName(), FilterEventParameters.EMPTY);
326    }
327   
328  6332 documentParameters = new FilterEventParameters();
329  6332 documentParameters.put(WikiDocumentFilter.PARAMETER_LOCALE, ((XWikiDocument) entity).getDefaultLocale());
330  6332 filter.beginWikiDocument(documentReference.getName(), documentParameters);
331    }
332   
333    // Document Locale events
334  7593 generator.write(entity, xarFilter, documentProperties);
335   
336    // Document and spaces events
337  7593 if (documentParameters != null) {
338  6332 filter.endWikiDocument(documentReference.getName(), documentParameters);
339   
340  6332 documentReference = ((XWikiDocument) entity).getDocumentReference();
341  6332 for (EntityReference reference =
342  12890 documentReference.getParent(); reference instanceof SpaceReference; reference = reference.getParent()) {
343  6558 filter.beginWikiSpace(reference.getName(), FilterEventParameters.EMPTY);
344    }
345    }
346   
347  7593 xarFilter.close();
348    }
349    }