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

File XAROutputFilterStream.java

 

Coverage histogram

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

Code metrics

110
201
29
1
655
532
104
0.52
6.93
29
3.59

Classes

Class Line # Actions
XAROutputFilterStream 71 201 0% 104 49
0.8558823585.6%
 

Contributing tests

This file is covered by 29 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: 29443af498e773a330ccb0420285b030967f40c8 $
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  7604 toggle @Override
108    public void close() throws IOException
109    {
110  7604 if (this.writer != null) {
111  1261 try {
112  1261 this.writer.close();
113  1261 this.writer = null;
114    } catch (FilterException e) {
115  0 throw new IOException("Failed to close XML writer", e);
116    }
117    }
118   
119  7604 if (this.wikiWriter != null) {
120  0 this.wikiWriter.close();
121  0 this.wikiWriter = null;
122    }
123   
124  7604 this.properties.getTarget().close();
125    }
126   
 
127  39414 toggle public String toString(Object obj)
128    {
129  39414 return Objects.toString(obj, null);
130    }
131   
 
132  19039 toggle public String toString(Date date)
133    {
134  19039 return date != null ? String.valueOf(date.getTime()) : null;
135    }
136   
 
137  6342 toggle public String toString(Syntax syntax)
138    {
139  6342 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  5831 toggle public String toString(EntityReference reference)
148    {
149  5831 return this.defaultSerializer.serialize(reference);
150    }
151   
152    // events
153   
 
154  0 toggle @Override
155    public void beginWikiFarm(FilterEventParameters parameters) throws FilterException
156    {
157    }
158   
 
159  0 toggle @Override
160    public void endWikiFarm(FilterEventParameters parameters) throws FilterException
161    {
162    }
163   
 
164  0 toggle @Override
165    public void beginWiki(String name, FilterEventParameters parameters) throws FilterException
166    {
167  0 this.wikiWriter = new XARWikiWriter(
168  0 this.properties.getPackageName() != null ? this.properties.getPackageName() : name, this.properties);
169    }
170   
 
171  0 toggle @Override
172    public void endWiki(String name, FilterEventParameters parameters) throws FilterException
173    {
174  0 try {
175  0 this.wikiWriter.close();
176    } catch (IOException e) {
177  0 throw new FilterException("Failed to close XAR writer", e);
178    }
179   
180  0 this.wikiWriter = null;
181    }
182   
 
183  13129 toggle @Override
184    public void beginWikiSpace(String name, FilterEventParameters parameters) throws FilterException
185    {
186  13129 this.currentSpaceReference = currentSpaceReference == null ? new EntityReference(name, EntityType.SPACE)
187    : new EntityReference(name, EntityType.SPACE, this.currentSpaceReference);
188    }
189   
 
190  13 toggle @Override
191    public void endWikiSpace(String name, FilterEventParameters parameters) throws FilterException
192    {
193  13 this.currentSpaceReference = this.currentSpaceReference.getParent();
194    }
195   
 
196  6343 toggle @Override
197    public void beginWikiDocument(String name, FilterEventParameters parameters) throws FilterException
198    {
199  6343 this.currentDocument = name;
200  6343 this.currentDocumentParameters = parameters;
201   
202  6343 this.currentDocumentReference = new LocalDocumentReference(this.currentDocument, this.currentSpaceReference);
203    }
204   
 
205  6343 toggle @Override
206    public void endWikiDocument(String name, FilterEventParameters parameters) throws FilterException
207    {
208  6343 this.currentDocument = null;
209  6343 this.currentDocumentReference = null;
210  6343 this.currentDocumentParameters = null;
211    }
212   
 
213  11 toggle private boolean isTargetTextualContent()
214    {
215  11 return this.properties.getTarget() instanceof WriterOutputTarget
216    || this.properties.getTarget() instanceof ResultOutputTarget;
217    }
218   
 
219  723900 toggle private void checkXMLWriter() throws FilterException
220    {
221  723900 if (this.writer == null) {
222  7604 this.writer = new FilterStreamXMLStreamWriter(this.properties, true);
223    }
224    }
225   
 
226  6343 toggle @Override
227    public void beginWikiDocumentLocale(Locale locale, FilterEventParameters parameters) throws FilterException
228    {
229  6343 if (this.writer == null) {
230  6343 if (this.wikiWriter == null && (this.properties.isForceDocument() || isTargetTextualContent())) {
231  6343 checkXMLWriter();
232    } else {
233  0 if (this.wikiWriter == null) {
234  0 this.wikiWriter = new XARWikiWriter(
235  0 this.properties.getPackageName() != null ? this.properties.getPackageName() : "package",
236    this.properties);
237    }
238   
239  0 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  6343 this.writer.writeStartDocument(this.properties.getEncoding(), "1.1");
246   
247  6343 this.writer.writeStartElement(XarDocumentModel.ELEMENT_DOCUMENT);
248  6343 this.writer.writeAttribute(XarDocumentModel.ATTRIBUTE_DOCUMENT_SPECVERSION, XarDocumentModel.VERSION_CURRENT);
249  6343 this.writer.writeAttribute(XarDocumentModel.ATTRIBUTE_DOCUMENT_REFERENCE,
250    localSerializer.serialize(this.currentDocumentReference));
251  6343 this.writer.writeAttribute(XarDocumentModel.ATTRIBUTE_DOCUMENT_LOCALE, toString(locale));
252   
253    // Legacy space and name
254  6343 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  6183 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  160 this.writer.writeElement(XarDocumentModel.ELEMENT_SPACE,
262    defaultSerializer.serialize(this.currentDocumentReference.getParent()));
263    }
264  6343 this.writer.writeElement(XarDocumentModel.ELEMENT_NAME, this.currentDocumentReference.getName());
265   
266    // Legacy locale
267  6343 this.writer.writeElement(XarDocumentModel.ELEMENT_LOCALE, toString(locale));
268   
269  6343 this.writer.writeElement(XarDocumentModel.ELEMENT_DEFAULTLOCALE,
270    toString(this.currentDocumentParameters.get(XWikiWikiDocumentFilter.PARAMETER_LOCALE)));
271  6343 this.writer.writeElement(XarDocumentModel.ELEMENT_ISTRANSLATION,
272  6343 locale != null && !Locale.ROOT.equals(locale) ? "1" : "0");
273   
274  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CREATION_AUTHOR)) {
275  6338 this.writer.writeElement(XarDocumentModel.ELEMENT_CREATION_AUTHOR,
276    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CREATION_AUTHOR));
277    }
278  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CREATION_DATE)) {
279  6338 this.writer.writeElement(XarDocumentModel.ELEMENT_CREATION_DATE,
280    toString((Date) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CREATION_DATE)));
281    }
282   
283  6343 if (this.properties.isPreserveVersion()
284    && parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_JRCSREVISIONS)) {
285  159 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISIONS,
286    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_JRCSREVISIONS));
287    }
288    }
289   
 
290  6343 toggle @Override
291    public void endWikiDocumentLocale(Locale locale, FilterEventParameters parameters) throws FilterException
292    {
293  6343 this.writer.writeEndElement();
294  6343 this.writer.writeEndDocument();
295   
296  6343 this.writer.close();
297   
298  6343 if (this.wikiWriter != null) {
299  0 this.wikiWriter.closeEntry();
300    }
301   
302  6343 this.writer = null;
303    }
304   
 
305  6343 toggle @Override
306    public void beginWikiDocumentRevision(String revision, FilterEventParameters parameters) throws FilterException
307    {
308  6343 checkXMLWriter();
309   
310  6343 this.currentDocumentVersion = revision;
311   
312  6343 try {
313  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_PARENT)) {
314  5831 this.writer.writeElement(XarDocumentModel.ELEMENT_PARENT,
315    toString((EntityReference) parameters.get(XWikiWikiDocumentFilter.PARAMETER_PARENT)));
316    }
317  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_AUTHOR)) {
318  6338 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_AUTHOR,
319    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_AUTHOR));
320    }
321  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CUSTOMCLASS)) {
322  6 this.writer.writeElement(XarDocumentModel.ELEMENT_CUSTOMCLASS,
323    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CUSTOMCLASS));
324    }
325  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_AUTHOR)) {
326  6338 this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT_AUTHOR,
327    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT_AUTHOR));
328    }
329  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_DATE)) {
330  6338 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_DATE,
331    toString((Date) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_DATE)));
332    }
333  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_DATE)) {
334  6338 this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT_DATE,
335    toString((Date) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT_DATE)));
336    }
337  6343 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION, this.currentDocumentVersion);
338  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_TITLE)) {
339  6338 this.writer.writeElement(XarDocumentModel.ELEMENT_TITLE,
340    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_TITLE));
341    }
342  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_DEFAULTTEMPLATE)) {
343  6 this.writer.writeElement(XarDocumentModel.ELEMENT_DEFAULTTEMPLATE,
344    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_DEFAULTTEMPLATE));
345    }
346  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_VALIDATIONSCRIPT)) {
347  6 this.writer.writeElement(XarDocumentModel.ELEMENT_VALIDATIONSCRIPT,
348    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_VALIDATIONSCRIPT));
349    }
350  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_COMMENT)) {
351  6338 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_COMMENT,
352    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_COMMENT));
353    }
354  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_REVISION_MINOR)) {
355  6338 this.writer.writeElement(XarDocumentModel.ELEMENT_REVISION_MINOR,
356    toString(parameters.get(XWikiWikiDocumentFilter.PARAMETER_REVISION_MINOR)));
357    }
358  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_SYNTAX)) {
359  6342 this.writer.writeElement(XarDocumentModel.ELEMENT_SYNTAX,
360    toString((Syntax) parameters.get(XWikiWikiDocumentFilter.PARAMETER_SYNTAX)));
361    }
362  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_HIDDEN)) {
363  6342 this.writer.writeElement(XarDocumentModel.ELEMENT_HIDDEN,
364    toString(parameters.get(XWikiWikiDocumentFilter.PARAMETER_HIDDEN)));
365    }
366  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT)) {
367  6338 this.writer.writeElement(XarDocumentModel.ELEMENT_CONTENT,
368    (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CONTENT));
369    }
370  6343 if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CONTENT_HTML)) {
371  6 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  6343 toggle @Override
381    public void endWikiDocumentRevision(String revision, FilterEventParameters parameters) throws FilterException
382    {
383  6343 this.currentDocumentVersion = null;
384    }
385   
 
386  25 toggle @Override
387    public void onWikiAttachment(String name, InputStream content, Long size, FilterEventParameters parameters)
388    throws FilterException
389    {
390  25 checkXMLWriter();
391   
392  25 try {
393  25 this.writer.writeStartElement(XARAttachmentModel.ELEMENT_ATTACHMENT);
394   
395  25 this.writer.writeElement(XARAttachmentModel.ELEMENT_NAME, name);
396  25 if (this.properties.isPreserveVersion()
397    && parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS)) {
398  23 this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISIONS,
399    (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS));
400    }
401   
402  25 if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_MIMETYPE)) {
403  1 this.writer.writeElement(XARAttachmentModel.ELEMENT_MIMETYPE,
404    (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_MIMETYPE));
405    }
406  25 if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION_AUTHOR)) {
407  25 this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION_AUTHOR,
408    (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION_AUTHOR));
409    }
410  25 if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION_DATE)) {
411  25 this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION_DATE,
412    toString((Date) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION_DATE)));
413    }
414  25 if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION)) {
415  25 this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION,
416    (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION));
417    }
418  25 if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION_COMMENT)) {
419  25 this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION_COMMENT,
420    (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION_COMMENT));
421    }
422   
423  25 if (content != null) {
424  25 long contentSize = 0;
425   
426  25 this.writer.writeStartElement(XARAttachmentModel.ELEMENT_CONTENT);
427  25 byte[] buffer = new byte[ATTACHMENT_BUFFER_CHUNK_SIZE];
428  25 int readSize;
429  25 do {
430  27 try {
431  27 readSize = content.read(buffer, 0, ATTACHMENT_BUFFER_CHUNK_SIZE);
432    } catch (IOException e) {
433  0 throw new FilterException("Failed to read content stream", e);
434    }
435   
436  27 if (readSize > 0) {
437  27 String chunk;
438  27 if (readSize == ATTACHMENT_BUFFER_CHUNK_SIZE) {
439  2 chunk = Base64.encodeBase64String(buffer);
440    } else {
441  25 chunk = Base64.encodeBase64String(ArrayUtils.subarray(buffer, 0, readSize));
442    }
443  27 this.writer.writeCharacters(chunk);
444  27 contentSize += readSize;
445    }
446  27 } while (readSize == ATTACHMENT_BUFFER_CHUNK_SIZE);
447  25 this.writer.writeEndElement();
448   
449  25 this.writer.writeElement(XARAttachmentModel.ELEMENT_CONTENT_SIZE, toString(contentSize));
450    }
451   
452  25 this.writer.writeEndElement();
453    } catch (Exception e) {
454  0 throw new FilterException(
455    String.format("Failed to write attachment [%s] from document [%s] with version [%s]", name,
456    this.currentDocumentReference, this.currentDocumentVersion),
457    e);
458    }
459    }
460   
 
461  10535 toggle @Override
462    public void beginWikiClass(FilterEventParameters parameters) throws FilterException
463    {
464  10535 checkXMLWriter();
465   
466  10535 try {
467  10535 this.writer.writeStartElement(XARClassModel.ELEMENT_CLASS);
468   
469  10535 if (parameters.containsKey(WikiClassFilter.PARAMETER_NAME)) {
470  10534 this.writer.writeElement(XARClassModel.ELEMENT_NAME,
471    (String) parameters.get(WikiClassFilter.PARAMETER_NAME));
472    } else {
473  1 this.writer.writeElement(XARClassModel.ELEMENT_NAME, this.currentObjectClass != null
474    ? this.currentObjectClass : this.localSerializer.serialize(this.currentDocumentReference));
475    }
476   
477  10535 if (parameters.containsKey(WikiClassFilter.PARAMETER_CUSTOMCLASS)) {
478  10532 this.writer.writeElement(XARClassModel.ELEMENT_CUSTOMCLASS,
479    (String) parameters.get(WikiClassFilter.PARAMETER_CUSTOMCLASS));
480    }
481  10535 if (parameters.containsKey(WikiClassFilter.PARAMETER_CUSTOMMAPPING)) {
482  10532 this.writer.writeElement(XARClassModel.ELEMENT_CUSTOMMAPPING,
483    (String) parameters.get(WikiClassFilter.PARAMETER_CUSTOMMAPPING));
484    }
485  10535 if (parameters.containsKey(WikiClassFilter.PARAMETER_SHEET_DEFAULTVIEW)) {
486  10532 this.writer.writeElement(XARClassModel.ELEMENT_SHEET_DEFAULTVIEW,
487    (String) parameters.get(WikiClassFilter.PARAMETER_SHEET_DEFAULTVIEW));
488    }
489  10535 if (parameters.containsKey(WikiClassFilter.PARAMETER_SHEET_DEFAULTEDIT)) {
490  10532 this.writer.writeElement(XARClassModel.ELEMENT_SHEET_DEFAULTEDIT,
491    (String) parameters.get(WikiClassFilter.PARAMETER_SHEET_DEFAULTEDIT));
492    }
493  10535 if (parameters.containsKey(WikiClassFilter.PARAMETER_DEFAULTSPACE)) {
494  10532 this.writer.writeElement(XARClassModel.ELEMENT_DEFAULTSPACE,
495    (String) parameters.get(WikiClassFilter.PARAMETER_DEFAULTSPACE));
496    }
497  10535 if (parameters.containsKey(WikiClassFilter.PARAMETER_NAMEFIELD)) {
498  10532 this.writer.writeElement(XARClassModel.ELEMENT_NAMEFIELD,
499    (String) parameters.get(WikiClassFilter.PARAMETER_NAMEFIELD));
500    }
501  10535 if (parameters.containsKey(WikiClassFilter.PARAMETER_VALIDATIONSCRIPT)) {
502  10532 this.writer.writeElement(XARClassModel.ELEMENT_VALIDATIONSCRIPT,
503    (String) parameters.get(WikiClassFilter.PARAMETER_VALIDATIONSCRIPT));
504    }
505    } catch (Exception e) {
506  0 throw new FilterException(String.format("Failed to write begin xclass [%s] with version [%s]",
507    this.currentDocumentReference, this.currentDocumentVersion), e);
508    }
509    }
510   
 
511  10535 toggle @Override
512    public void endWikiClass(FilterEventParameters parameters) throws FilterException
513    {
514  10535 try {
515  10535 this.writer.writeEndElement();
516    } catch (Exception e) {
517  0 throw new FilterException(String.format("Failed to write end xclass [%s] with version [%s]",
518    this.currentDocumentReference, this.currentDocumentVersion), e);
519    }
520    }
521   
 
522  68373 toggle @Override
523    public void beginWikiClassProperty(String name, String type, FilterEventParameters parameters)
524    throws FilterException
525    {
526  68373 checkXMLWriter();
527   
528  68373 try {
529  68373 this.writer.writeStartElement(name);
530   
531  68373 if (this.currentObjectProperties != null) {
532  28957 this.currentObjectProperties.put(name, type);
533    }
534    } catch (Exception e) {
535  0 throw new FilterException(
536    String.format("Failed to write begin property [%s] from class [%s] with version [%s]", name,
537    this.currentDocumentReference, this.currentDocumentVersion),
538    e);
539    }
540    }
541   
 
542  68373 toggle @Override
543    public void endWikiClassProperty(String name, String type, FilterEventParameters parameters) throws FilterException
544    {
545  68373 try {
546  68373 this.writer.writeElement(XARClassPropertyModel.ELEMENT_CLASSTYPE, type);
547   
548  68373 this.writer.writeEndElement();
549    } catch (Exception e) {
550  0 throw new FilterException(
551    String.format("Failed to write end property [%s] from class [%s] with version [%s]", name,
552    this.currentDocumentReference, this.currentDocumentVersion),
553    e);
554    }
555    }
556   
 
557  594838 toggle @Override
558    public void onWikiClassPropertyField(String name, String value, FilterEventParameters parameters)
559    throws FilterException
560    {
561  594838 checkXMLWriter();
562   
563  594838 try {
564  594838 this.writer.writeElement(name, value);
565    } catch (Exception e) {
566  0 throw new FilterException(
567    String.format("Failed to write property field [%s:%s] from class [%s] with version [%s]", name, value,
568    this.currentDocumentReference, this.currentDocumentVersion),
569    e);
570    }
571    }
572   
 
573  7680 toggle @Override
574    public void beginWikiObject(String name, FilterEventParameters parameters) throws FilterException
575    {
576  7680 checkXMLWriter();
577   
578  7680 try {
579  7680 this.writer.writeStartElement(XARObjectModel.ELEMENT_OBJECT);
580   
581  7680 this.currentObjectClass = (String) parameters.get(WikiObjectFilter.PARAMETER_CLASS_REFERENCE);
582   
583  7680 if (parameters.containsKey(WikiObjectFilter.PARAMETER_NAME)) {
584  7679 this.writer.writeElement(XARObjectModel.ELEMENT_NAME,
585    (String) parameters.get(WikiObjectFilter.PARAMETER_NAME));
586    } else {
587  1 this.writer.writeElement(XARObjectModel.ELEMENT_NAME,
588    this.localSerializer.serialize(this.currentDocumentReference));
589    }
590  7680 this.writer.writeElement(XARObjectModel.ELEMENT_NUMBER,
591    toString((Integer) parameters.get(WikiObjectFilter.PARAMETER_NUMBER)));
592  7680 this.writer.writeElement(XARObjectModel.ELEMENT_CLASSNAME, this.currentObjectClass);
593   
594  7680 if (parameters.containsKey(WikiObjectFilter.PARAMETER_GUID)) {
595  7675 this.writer.writeElement(XARObjectModel.ELEMENT_GUID,
596    (String) parameters.get(WikiObjectFilter.PARAMETER_GUID));
597    }
598   
599  7680 this.currentObjectProperties = new HashMap<String, String>();
600    } catch (Exception e) {
601  0 throw new FilterException(
602    String.format("Failed to write begin xobject [%s] from document [%s] with version [%s]", name,
603    this.currentDocumentReference, this.currentDocumentVersion),
604    e);
605    }
606    }
607   
 
608  7680 toggle @Override
609    public void endWikiObject(String name, FilterEventParameters parameters) throws FilterException
610    {
611  7680 try {
612  7680 this.writer.writeEndElement();
613   
614  7680 this.currentObjectClass = null;
615  7680 this.currentObjectProperties = null;
616    } catch (Exception e) {
617  0 throw new FilterException(
618    String.format("Failed to write end xobject [%s] from document [%s] with version [%s]", name,
619    this.currentDocumentReference, this.currentDocumentVersion),
620    e);
621    }
622    }
623   
 
624  29763 toggle @Override
625    public void onWikiObjectProperty(String name, Object value, FilterEventParameters parameters) throws FilterException
626    {
627  29763 checkXMLWriter();
628   
629  29763 try {
630  29763 this.writer.writeStartElement(XARObjectPropertyModel.ELEMENT_PROPERTY);
631   
632  29763 this.writer.writeStartElement(name);
633   
634  29763 String type = (String) parameters.get(WikiObjectPropertyFilter.PARAMETER_TYPE);
635  29763 if (type == null && this.currentObjectProperties != null) {
636  29733 type = this.currentObjectProperties.get(name);
637    }
638   
639  29763 try {
640  29763 this.propertySerializerManager.getPropertySerializer(type).write(this.writer.getWriter(), value);
641    } catch (Exception e) {
642  0 throw new FilterException("Failed to write property value", e);
643    }
644   
645  29763 this.writer.writeEndElement();
646   
647  29763 this.writer.writeEndElement();
648    } catch (Exception e) {
649  0 throw new FilterException(
650    String.format("Failed to write xobject property [%s:%s] from document [%s] with version [%s]", name,
651    value, this.currentDocumentReference, this.currentDocumentVersion),
652    e);
653    }
654    }
655    }