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

File XAROutputFilterStream.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

112
204
29
1
661
538
105
0.51
7.03
29
3.62

Classes

Class Line # Actions
XAROutputFilterStream 71 204 0% 105 26
0.924637792.5%
 

Contributing tests

This file is covered by 32 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.filter.xar.internal.output;
21   
22    import java.io.IOException;
23    import java.io.InputStream;
24    import java.util.Date;
25    import java.util.HashMap;
26    import java.util.Locale;
27    import java.util.Map;
28    import java.util.Objects;
29   
30    import javax.inject.Inject;
31    import javax.inject.Named;
32   
33    import org.apache.commons.codec.binary.Base64;
34    import org.apache.commons.lang3.ArrayUtils;
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.component.annotation.InstantiationStrategy;
37    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
38    import org.xwiki.filter.FilterEventParameters;
39    import org.xwiki.filter.FilterException;
40    import org.xwiki.filter.event.model.WikiClassFilter;
41    import org.xwiki.filter.event.model.WikiObjectFilter;
42    import org.xwiki.filter.event.model.WikiObjectPropertyFilter;
43    import org.xwiki.filter.event.xwiki.XWikiWikiAttachmentFilter;
44    import org.xwiki.filter.event.xwiki.XWikiWikiDocumentFilter;
45    import org.xwiki.filter.output.AbstractBeanOutputFilterStream;
46    import org.xwiki.filter.output.WriterOutputTarget;
47    import org.xwiki.filter.xar.internal.XARAttachmentModel;
48    import org.xwiki.filter.xar.internal.XARClassModel;
49    import org.xwiki.filter.xar.internal.XARClassPropertyModel;
50    import org.xwiki.filter.xar.internal.XARFilter;
51    import org.xwiki.filter.xar.internal.XARFilterUtils;
52    import org.xwiki.filter.xar.internal.XARObjectModel;
53    import org.xwiki.filter.xar.internal.XARObjectPropertyModel;
54    import org.xwiki.filter.xar.output.XAROutputProperties;
55    import org.xwiki.filter.xml.internal.output.FilterStreamXMLStreamWriter;
56    import org.xwiki.filter.xml.output.ResultOutputTarget;
57    import org.xwiki.model.EntityType;
58    import org.xwiki.model.reference.EntityReference;
59    import org.xwiki.model.reference.EntityReferenceSerializer;
60    import org.xwiki.model.reference.LocalDocumentReference;
61    import org.xwiki.rendering.syntax.Syntax;
62    import org.xwiki.xar.internal.XarObjectPropertySerializerManager;
63    import org.xwiki.xar.internal.model.XarDocumentModel;
64   
65    /**
66    * @version $Id: 2b8bb61db76903dbbd4f2d21c9b361ce0d2d44b2 $
67    * @since 6.2M1
68    */
69    @Component(hints = {XARFilterUtils.ROLEHINT_13, XARFilterUtils.ROLEHINT_12, XARFilterUtils.ROLEHINT_11})
70    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
71    public class XAROutputFilterStream extends AbstractBeanOutputFilterStream<XAROutputProperties> implements XARFilter
72    {
73    @Inject
74    @Named("local")
75    private EntityReferenceSerializer<String> localSerializer;
76   
77    @Inject
78    private EntityReferenceSerializer<String> defaultSerializer;
79   
80    @Inject
81    private XarObjectPropertySerializerManager propertySerializerManager;
82   
83    private XARWikiWriter wikiWriter;
84   
85    private EntityReference currentSpaceReference;
86   
87    private String currentDocument;
88   
89    private FilterEventParameters currentDocumentParameters;
90   
91    private LocalDocumentReference currentDocumentReference;
92   
93    private String currentDocumentVersion;
94   
95    private String currentObjectClass;
96   
97    private FilterStreamXMLStreamWriter writer;
98   
99    private Map<String, String> currentObjectProperties;
100   
101    /**
102    * the size of the chunks used when attachments are streamed. As the attachment data is base64 encoded in the
103    * process, this size must be a multiple of three to prevent padding between the chunks; see XWIKI-9830
104    */
105    private static final int ATTACHMENT_BUFFER_CHUNK_SIZE = 4095;
106   
 
107  15170 toggle @Override
108    public void close() throws IOException
109    {
110  15170 if (this.writer != null) {
111  1887 try {
112  1887 this.writer.close();
113  1887 this.writer = null;
114    } catch (FilterException e) {
115  0 throw new IOException("Failed to close XML writer", e);
116    }
117    }
118   
119  15170 if (this.wikiWriter != null) {
120  1 this.wikiWriter.close();
121  1 this.wikiWriter = null;
122    }
123   
124  15170 this.properties.getTarget().close();
125    }
126   
 
127  85900 toggle public String toString(Object obj)
128    {
129  85900 return Objects.toString(obj, null);
130    }
131   
 
132  41891 toggle public String toString(Date date)
133    {
134  41892 return date != null ? String.valueOf(date.getTime()) : null;
135    }
136   
 
137  13472 toggle public String toString(Syntax syntax)
138    {
139  13472 return syntax != null ? syntax.toIdString() : null;
140    }
141   
 
142  0 toggle public String toString(byte[] bytes)
143    {
144  0 return Base64.encodeBase64String(bytes);
145    }
146   
 
147  11974 toggle public String toString(EntityReference reference)
148    {
149  11974 return this.defaultSerializer.serialize(reference);
150    }
151   
152    // events
153   
 
154  3 toggle @Override
155    public void beginWikiFarm(FilterEventParameters parameters) throws FilterException
156    {
157    }
158   
 
159  3 toggle @Override
160    public void endWikiFarm(FilterEventParameters parameters) throws FilterException
161    {
162    }
163   
 
164  3 toggle @Override
165    public void beginWiki(String name, FilterEventParameters parameters) throws FilterException
166    {
167  3 this.wikiWriter = new XARWikiWriter(
168  3 this.properties.getPackageName() != null ? this.properties.getPackageName() : name, this.properties);
169    }
170   
 
171  3 toggle @Override
172    public void endWiki(String name, FilterEventParameters parameters) throws FilterException
173    {
174  3 try {
175  3 this.wikiWriter.close();
176    } catch (IOException e) {
177  0 throw new FilterException("Failed to close XAR writer", e);
178    }
179   
180  3 this.wikiWriter = null;
181    }
182   
 
183  31052 toggle @Override
184    public void beginWikiSpace(String name, FilterEventParameters parameters) throws FilterException
185    {
186  31052 this.currentSpaceReference = currentSpaceReference == null ? new EntityReference(name, EntityType.SPACE)
187    : new EntityReference(name, EntityType.SPACE, this.currentSpaceReference);
188    }
189   
 
190  230 toggle @Override
191    public void endWikiSpace(String name, FilterEventParameters parameters) throws FilterException
192    {
193  230 this.currentSpaceReference = this.currentSpaceReference.getParent();
194    }
195   
 
196  13473 toggle @Override
197    public void beginWikiDocument(String name, FilterEventParameters parameters) throws FilterException
198    {
199  13473 this.currentDocument = name;
200  13473 this.currentDocumentParameters = parameters;
201   
202  13473 this.currentDocumentReference = new LocalDocumentReference(this.currentDocument, this.currentSpaceReference);
203    }
204   
 
205  13473 toggle @Override
206    public void endWikiDocument(String name, FilterEventParameters parameters) throws FilterException
207    {
208  13473 this.currentDocument = null;
209  13473 this.currentDocumentReference = null;
210  13473 this.currentDocumentParameters = null;
211    }
212   
 
213  14 toggle private boolean isTargetTextualContent()
214    {
215  14 return this.properties.getTarget() instanceof WriterOutputTarget
216    || this.properties.getTarget() instanceof ResultOutputTarget;
217    }
218   
 
219  2047813 toggle private void checkXMLWriter() throws FilterException
220    {
221  2047812 if (this.writer == null) {
222  15166 this.writer = new FilterStreamXMLStreamWriter(this.properties, true);
223    }
224    }
225   
 
226  13473 toggle @Override
227    public void beginWikiDocumentLocale(Locale locale, FilterEventParameters parameters) throws FilterException
228    {
229  13473 if (this.writer == null) {
230  13473 if (this.wikiWriter == null && (this.properties.isForceDocument() || isTargetTextualContent())) {
231  13279 checkXMLWriter();
232    } else {
233  194 if (this.wikiWriter == null) {
234  1 this.wikiWriter = new XARWikiWriter(
235  1 this.properties.getPackageName() != null ? this.properties.getPackageName() : "package",
236    this.properties);
237    }
238   
239  194 this.writer = new FilterStreamXMLStreamWriter(
240    this.wikiWriter.newEntry(new LocalDocumentReference(this.currentDocumentReference, locale)),
241    this.properties.getEncoding(), this.properties.isFormat(), true);
242    }
243    }
244   
245  13473 this.writer.writeStartDocument(this.properties.getEncoding(), "1.1");
246   
247  13473 this.writer.writeStartElement(XarDocumentModel.ELEMENT_DOCUMENT);
248  13473 this.writer.writeAttribute(XarDocumentModel.ATTRIBUTE_DOCUMENT_SPECVERSION, XarDocumentModel.VERSION_CURRENT);
249  13473 this.writer.writeAttribute(XarDocumentModel.ATTRIBUTE_DOCUMENT_REFERENCE,
250    localSerializer.serialize(this.currentDocumentReference));
251  13473 this.writer.writeAttribute(XarDocumentModel.ATTRIBUTE_DOCUMENT_LOCALE, toString(locale));
252   
253    // Legacy space and name
254  13473 if (this.currentDocumentReference.getParent().getParent() == null) {
255    // If single space behave as it used to and put the space name instead of the reference to keep
256    // compatibility when importing in older version
257  12024 this.writer.writeElement(XarDocumentModel.ELEMENT_SPACE,
258    this.currentDocumentReference.getParent().getName());
259    } else {
260    // If nested space put the space reference in the field
261  1449 this.writer.writeElement(XarDocumentModel.ELEMENT_SPACE,
262    defaultSerializer.serialize(this.currentDocumentReference.getParent()));
263    }
264  13473 this.writer.writeElement(XarDocumentModel.ELEMENT_NAME, this.currentDocumentReference.getName());
265   
266    // Legacy locale
267  13473 this.writer.writeElement(XarDocumentModel.ELEMENT_LOCALE, toString(locale));
268   
269  13473 this.writer.writeElement(XarDocumentModel.ELEMENT_DEFAULTLOCALE,
270    toString(this.currentDocumentParameters.get(XWikiWikiDocumentFilter.PARAMETER_LOCALE)));
271  13473 this.writer.writeElement(XarDocumentModel.ELEMENT_ISTRANSLATION,
272  13473 locale != null && !Locale.ROOT.equals(locale) ? "1" : "0");
273   
274  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CREATION_AUTHOR)) {
275  13468 this.writer.writeElement(XarDocumentModel.ELEMENT_CREATION_AUTHOR,
276    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CREATION_AUTHOR));
277    }
278  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CREATION_DATE)) {
279  13468 this.writer.writeElement(XarDocumentModel.ELEMENT_CREATION_DATE,
280    toString((Date) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CREATION_DATE)));
281    }
282   
283  13473 if (this.properties.isPreserveVersion()
284    && parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_JRCSREVISIONS)) {
285  295 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISIONS,
286    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_JRCSREVISIONS));
287    }
288    }
289   
 
290  13473 toggle @Override
291    public void endWikiDocumentLocale(Locale locale, FilterEventParameters parameters) throws FilterException
292    {
293  13473 this.writer.writeEndElement();
294  13473 this.writer.writeEndDocument();
295   
296  13473 this.writer.close();
297   
298  13473 if (this.wikiWriter != null) {
299  194 this.wikiWriter.closeEntry();
300    }
301   
302  13473 this.writer = null;
303    }
304   
 
305  13473 toggle @Override
306    public void beginWikiDocumentRevision(String revision, FilterEventParameters parameters) throws FilterException
307    {
308  13473 checkXMLWriter();
309   
310  13473 this.currentDocumentVersion = revision;
311   
312  13473 try {
313  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_PARENT)) {
314  11974 this.writer.writeElement(XarDocumentModel.ELEMENT_PARENT,
315    toString((EntityReference) parameters.get(XWikiWikiDocumentFilter.PARAMETER_PARENT)));
316    }
317  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_AUTHOR)) {
318  13468 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_AUTHOR,
319    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_AUTHOR));
320    }
321  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CUSTOMCLASS)) {
322  10 this.writer.writeElement(XarDocumentModel.ELEMENT_CUSTOMCLASS,
323    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CUSTOMCLASS));
324    }
325  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_AUTHOR)) {
326  13468 this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT_AUTHOR,
327    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT_AUTHOR));
328    }
329  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_DATE)) {
330  13468 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_DATE,
331    toString((Date) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_DATE)));
332    }
333  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_DATE)) {
334  13468 this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT_DATE,
335    toString((Date) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT_DATE)));
336    }
337  13473 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION, this.currentDocumentVersion);
338  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_TITLE)) {
339  13468 this.writer.writeElement(XarDocumentModel.ELEMENT_TITLE,
340    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_TITLE));
341    }
342  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_DEFAULTTEMPLATE)) {
343  10 this.writer.writeElement(XarDocumentModel.ELEMENT_DEFAULTTEMPLATE,
344    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_DEFAULTTEMPLATE));
345    }
346  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_VALIDATIONSCRIPT)) {
347  10 this.writer.writeElement(XarDocumentModel.ELEMENT_VALIDATIONSCRIPT,
348    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_VALIDATIONSCRIPT));
349    }
350  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_COMMENT)) {
351  13468 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_COMMENT,
352    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_COMMENT));
353    }
354  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_MINOR)) {
355  13468 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_MINOR,
356    toString(parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_MINOR)));
357    }
358  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_SYNTAX)) {
359  13472 this.writer.writeElement(XarDocumentModel.ELEMENT_SYNTAX,
360    toString((Syntax) parameters.get(XWikiWikiDocumentFilter.PARAMETER_SYNTAX)));
361    }
362  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_HIDDEN)) {
363  13472 this.writer.writeElement(XarDocumentModel.ELEMENT_HIDDEN,
364    toString(parameters.get(XWikiWikiDocumentFilter.PARAMETER_HIDDEN)));
365    }
366  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT)) {
367  13468 this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT,
368    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT));
369    }
370  13473 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_HTML)) {
371  8 this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT_HTML,
372    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT_HTML));
373    }
374    } catch (Exception e) {
375  0 throw new FilterException(String.format("Failed to write begin document [%s] with version [%s]",
376    this.currentDocumentReference, this.currentDocumentVersion), e);
377    }
378    }
379   
 
380  13473 toggle @Override
381    public void endWikiDocumentRevision(String revision, FilterEventParameters parameters) throws FilterException
382    {
383  13473 this.currentDocumentVersion = null;
384    }
385   
 
386  1488 toggle @Override
387    public void onWikiAttachment(String name, InputStream content, Long size, FilterEventParameters parameters)
388    throws FilterException
389    {
390  1488 checkXMLWriter();
391   
392  1488 try {
393  1488 this.writer.writeStartElement(XARAttachmentModel.ELEMENT_ATTACHMENT);
394   
395  1488 this.writer.writeElement(XARAttachmentModel.ELEMENT_NAME, name);
396  1488 if (this.properties.isPreserveVersion()
397    && parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS)) {
398  33 this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISIONS,
399    (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS));
400    }
401   
402  1488 if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_MIMETYPE)) {
403  1032 this.writer.writeElement(XARAttachmentModel.ELEMENT_MIMETYPE,
404    (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_MIMETYPE));
405    }
406  1488 if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_CHARSET)) {
407  46 this.writer.writeElement(XARAttachmentModel.ELEMENT_CHARSET,
408    (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_CHARSET));
409    }
410  1488 if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION_AUTHOR)) {
411  1488 this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION_AUTHOR,
412    (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION_AUTHOR));
413    }
414  1488 if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION_DATE)) {
415  1488 this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION_DATE,
416    toString((Date) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION_DATE)));
417    }
418  1488 if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION)) {
419  1488 this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION,
420    (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION));
421    }
422  1488 if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION_COMMENT)) {
423  1488 this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION_COMMENT,
424    (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION_COMMENT));
425    }
426   
427  1488 if (content != null) {
428  364 long contentSize = 0;
429   
430  364 this.writer.writeStartElement(XARAttachmentModel.ELEMENT_CONTENT);
431  364 byte[] buffer = new byte[ATTACHMENT_BUFFER_CHUNK_SIZE];
432  364 int readSize;
433  364 do {
434  8982 try {
435  8982 readSize = content.read(buffer, 0, ATTACHMENT_BUFFER_CHUNK_SIZE);
436    } catch (IOException e) {
437  0 throw new FilterException("Failed to read content stream", e);
438    }
439   
440  8982 if (readSize > 0) {
441  8979 String chunk;
442  8979 if (readSize == ATTACHMENT_BUFFER_CHUNK_SIZE) {
443  8618 chunk = Base64.encodeBase64String(buffer);
444    } else {
445  361 chunk = Base64.encodeBase64String(ArrayUtils.subarray(buffer, 0, readSize));
446    }
447  8979 this.writer.writeCharacters(chunk);
448  8979 contentSize += readSize;
449    }
450  8982 } while (readSize == ATTACHMENT_BUFFER_CHUNK_SIZE);
451  364 this.writer.writeEndElement();
452   
453  364 this.writer.writeElement(XARAttachmentModel.ELEMENT_CONTENT_SIZE, toString(contentSize));
454    } else {
455  1124 this.writer.writeElement(XARAttachmentModel.ELEMENT_CONTENT_SIZE, toString(size));
456    }
457   
458  1488 this.writer.writeEndElement();
459    } catch (Exception e) {
460  0 throw new FilterException(
461    String.format("Failed to write attachment [%s] from document [%s] with version [%s]", name,
462    this.currentDocumentReference, this.currentDocumentVersion),
463    e);
464    }
465    }
466   
 
467  21522 toggle @Override
468    public void beginWikiClass(FilterEventParameters parameters) throws FilterException
469    {
470  21522 checkXMLWriter();
471   
472  21522 try {
473  21522 this.writer.writeStartElement(XARClassModel.ELEMENT_CLASS);
474   
475  21522 if (parameters.containsKey(WikiClassFilter.PARAMETER_NAME)) {
476  21521 this.writer.writeElement(XARClassModel.ELEMENT_NAME,
477    (String) parameters.get(WikiClassFilter.PARAMETER_NAME));
478    } else {
479  1 this.writer.writeElement(XARClassModel.ELEMENT_NAME, this.currentObjectClass != null
480    ? this.currentObjectClass : this.localSerializer.serialize(this.currentDocumentReference));
481    }
482   
483  21522 if (parameters.containsKey(WikiClassFilter.PARAMETER_CUSTOMCLASS)) {
484  21519 this.writer.writeElement(XARClassModel.ELEMENT_CUSTOMCLASS,
485    (String) parameters.get(WikiClassFilter.PARAMETER_CUSTOMCLASS));
486    }
487  21522 if (parameters.containsKey(WikiClassFilter.PARAMETER_CUSTOMMAPPING)) {
488  21519 this.writer.writeElement(XARClassModel.ELEMENT_CUSTOMMAPPING,
489    (String) parameters.get(WikiClassFilter.PARAMETER_CUSTOMMAPPING));
490    }
491  21522 if (parameters.containsKey(WikiClassFilter.PARAMETER_SHEET_DEFAULTVIEW)) {
492  21519 this.writer.writeElement(XARClassModel.ELEMENT_SHEET_DEFAULTVIEW,
493    (String) parameters.get(WikiClassFilter.PARAMETER_SHEET_DEFAULTVIEW));
494    }
495  21522 if (parameters.containsKey(WikiClassFilter.PARAMETER_SHEET_DEFAULTEDIT)) {
496  21519 this.writer.writeElement(XARClassModel.ELEMENT_SHEET_DEFAULTEDIT,
497    (String) parameters.get(WikiClassFilter.PARAMETER_SHEET_DEFAULTEDIT));
498    }
499  21522 if (parameters.containsKey(WikiClassFilter.PARAMETER_DEFAULTSPACE)) {
500  21519 this.writer.writeElement(XARClassModel.ELEMENT_DEFAULTSPACE,
501    (String) parameters.get(WikiClassFilter.PARAMETER_DEFAULTSPACE));
502    }
503  21522 if (parameters.containsKey(WikiClassFilter.PARAMETER_NAMEFIELD)) {
504  21519 this.writer.writeElement(XARClassModel.ELEMENT_NAMEFIELD,
505    (String) parameters.get(WikiClassFilter.PARAMETER_NAMEFIELD));
506    }
507  21522 if (parameters.containsKey(WikiClassFilter.PARAMETER_VALIDATIONSCRIPT)) {
508  21519 this.writer.writeElement(XARClassModel.ELEMENT_VALIDATIONSCRIPT,
509    (String) parameters.get(WikiClassFilter.PARAMETER_VALIDATIONSCRIPT));
510    }
511    } catch (Exception e) {
512  0 throw new FilterException(String.format("Failed to write begin xclass [%s] with version [%s]",
513    this.currentDocumentReference, this.currentDocumentVersion), e);
514    }
515    }
516   
 
517  21522 toggle @Override
518    public void endWikiClass(FilterEventParameters parameters) throws FilterException
519    {
520  21522 try {
521  21522 this.writer.writeEndElement();
522    } catch (Exception e) {
523  0 throw new FilterException(String.format("Failed to write end xclass [%s] with version [%s]",
524    this.currentDocumentReference, this.currentDocumentVersion), e);
525    }
526    }
527   
 
528  175358 toggle @Override
529    public void beginWikiClassProperty(String name, String type, FilterEventParameters parameters)
530    throws FilterException
531    {
532  175358 checkXMLWriter();
533   
534  175358 try {
535  175358 this.writer.writeStartElement(name);
536   
537  175358 if (this.currentObjectProperties != null) {
538  116186 this.currentObjectProperties.put(name, type);
539    }
540    } catch (Exception e) {
541  0 throw new FilterException(
542    String.format("Failed to write begin property [%s] from class [%s] with version [%s]", name,
543    this.currentDocumentReference, this.currentDocumentVersion),
544    e);
545    }
546    }
547   
 
548  175358 toggle @Override
549    public void endWikiClassProperty(String name, String type, FilterEventParameters parameters) throws FilterException
550    {
551  175358 try {
552  175358 this.writer.writeElement(XARClassPropertyModel.ELEMENT_CLASSTYPE, type);
553   
554  175358 this.writer.writeEndElement();
555    } catch (Exception e) {
556  0 throw new FilterException(
557    String.format("Failed to write end property [%s] from class [%s] with version [%s]", name,
558    this.currentDocumentReference, this.currentDocumentVersion),
559    e);
560    }
561    }
562   
 
563  1693606 toggle @Override
564    public void onWikiClassPropertyField(String name, String value, FilterEventParameters parameters)
565    throws FilterException
566    {
567  1693604 checkXMLWriter();
568   
569  1693601 try {
570  1693601 this.writer.writeElement(name, value);
571    } catch (Exception e) {
572  0 throw new FilterException(
573    String.format("Failed to write property field [%s:%s] from class [%s] with version [%s]", name, value,
574    this.currentDocumentReference, this.currentDocumentVersion),
575    e);
576    }
577    }
578   
 
579  17053 toggle @Override
580    public void beginWikiObject(String name, FilterEventParameters parameters) throws FilterException
581    {
582  17053 checkXMLWriter();
583   
584  17053 try {
585  17053 this.writer.writeStartElement(XARObjectModel.ELEMENT_OBJECT);
586   
587  17053 this.currentObjectClass = (String) parameters.get(WikiObjectFilter.PARAMETER_CLASS_REFERENCE);
588   
589  17053 if (parameters.containsKey(WikiObjectFilter.PARAMETER_NAME)) {
590  17052 this.writer.writeElement(XARObjectModel.ELEMENT_NAME,
591    (String) parameters.get(WikiObjectFilter.PARAMETER_NAME));
592    } else {
593  1 this.writer.writeElement(XARObjectModel.ELEMENT_NAME,
594    this.localSerializer.serialize(this.currentDocumentReference));
595    }
596  17053 this.writer.writeElement(XARObjectModel.ELEMENT_NUMBER,
597    toString((Integer) parameters.get(WikiObjectFilter.PARAMETER_NUMBER)));
598  17053 this.writer.writeElement(XARObjectModel.ELEMENT_CLASSNAME, this.currentObjectClass);
599   
600  17053 if (parameters.containsKey(WikiObjectFilter.PARAMETER_GUID)) {
601  17048 this.writer.writeElement(XARObjectModel.ELEMENT_GUID,
602    (String) parameters.get(WikiObjectFilter.PARAMETER_GUID));
603    }
604   
605  17053 this.currentObjectProperties = new HashMap<String, String>();
606    } catch (Exception e) {
607  0 throw new FilterException(
608    String.format("Failed to write begin xobject [%s] from document [%s] with version [%s]", name,
609    this.currentDocumentReference, this.currentDocumentVersion),
610    e);
611    }
612    }
613   
 
614  17053 toggle @Override
615    public void endWikiObject(String name, FilterEventParameters parameters) throws FilterException
616    {
617  17053 try {
618  17053 this.writer.writeEndElement();
619   
620  17053 this.currentObjectClass = null;
621  17053 this.currentObjectProperties = null;
622    } catch (Exception e) {
623  0 throw new FilterException(
624    String.format("Failed to write end xobject [%s] from document [%s] with version [%s]", name,
625    this.currentDocumentReference, this.currentDocumentVersion),
626    e);
627    }
628    }
629   
 
630  112039 toggle @Override
631    public void onWikiObjectProperty(String name, Object value, FilterEventParameters parameters) throws FilterException
632    {
633  112039 checkXMLWriter();
634   
635  112039 try {
636  112039 this.writer.writeStartElement(XARObjectPropertyModel.ELEMENT_PROPERTY);
637   
638  112039 this.writer.writeStartElement(name);
639   
640  112039 String type = (String) parameters.get(WikiObjectPropertyFilter.PARAMETER_TYPE);
641  112039 if (type == null && this.currentObjectProperties != null) {
642  111997 type = this.currentObjectProperties.get(name);
643    }
644   
645  112039 try {
646  112039 this.propertySerializerManager.getPropertySerializer(type).write(this.writer.getWriter(), value);
647    } catch (Exception e) {
648  0 throw new FilterException("Failed to write property value", e);
649    }
650   
651  112039 this.writer.writeEndElement();
652   
653  112039 this.writer.writeEndElement();
654    } catch (Exception e) {
655  0 throw new FilterException(
656    String.format("Failed to write xobject property [%s:%s] from document [%s] with version [%s]", name,
657    value, this.currentDocumentReference, this.currentDocumentVersion),
658    e);
659    }
660    }
661    }