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

File XWikiDocumentLocaleEventGenerator.java

 

Coverage histogram

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

Code metrics

30
66
3
1
225
156
22
0.33
22
3
7.33

Classes

Class Line # Actions
XWikiDocumentLocaleEventGenerator 58 66 0% 22 25
0.7474747374.7%
 

Contributing tests

This file is covered by 18 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.input;
21   
22    import java.lang.reflect.ParameterizedType;
23    import java.util.ArrayList;
24    import java.util.Collections;
25    import java.util.Comparator;
26    import java.util.List;
27   
28    import javax.inject.Inject;
29    import javax.inject.Provider;
30    import javax.inject.Singleton;
31   
32    import org.slf4j.Logger;
33    import org.suigeneris.jrcs.rcs.Version;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.util.DefaultParameterizedType;
36    import org.xwiki.filter.FilterEventParameters;
37    import org.xwiki.filter.FilterException;
38    import org.xwiki.filter.event.model.WikiDocumentFilter;
39    import org.xwiki.filter.event.xwiki.XWikiWikiDocumentFilter;
40    import org.xwiki.filter.instance.input.DocumentInstanceInputProperties;
41    import org.xwiki.filter.instance.input.EntityEventGenerator;
42    import org.xwiki.filter.instance.internal.input.AbstractBeanEntityEventGenerator;
43   
44    import com.xpn.xwiki.XWikiContext;
45    import com.xpn.xwiki.XWikiException;
46    import com.xpn.xwiki.doc.XWikiAttachment;
47    import com.xpn.xwiki.doc.XWikiDocument;
48    import com.xpn.xwiki.internal.filter.XWikiDocumentFilter;
49    import com.xpn.xwiki.objects.BaseObject;
50    import com.xpn.xwiki.objects.classes.BaseClass;
51   
52    /**
53    * @version $Id: 0841d40a5f119781bdeef62abc5358fff3696bd2 $
54    * @since 6.2M1
55    */
56    @Component
57    @Singleton
 
58    public class XWikiDocumentLocaleEventGenerator
59    extends AbstractBeanEntityEventGenerator<XWikiDocument, XWikiDocumentFilter, DocumentInstanceInputProperties>
60    {
61    /**
62    * The role of this component.
63    */
64    public static final ParameterizedType ROLE = new DefaultParameterizedType(null, EntityEventGenerator.class,
65    XWikiDocument.class, DocumentInstanceInputProperties.class);
66   
67    @Inject
68    private Provider<XWikiContext> xcontextProvider;
69   
70    @Inject
71    private Logger logger;
72   
73    @Inject
74    private EntityEventGenerator<XWikiAttachment> attachmentEventGenerator;
75   
76    @Inject
77    private EntityEventGenerator<BaseClass> classEventGenerator;
78   
79    @Inject
80    private EntityEventGenerator<BaseObject> objectEventGenerator;
81   
 
82  13461 toggle @Override
83    public void write(XWikiDocument document, Object filter, XWikiDocumentFilter documentFilter,
84    DocumentInstanceInputProperties properties) throws FilterException
85    {
86  13461 XWikiContext xcontext = this.xcontextProvider.get();
87   
88    // > WikiDocumentLocale
89   
90  13461 FilterEventParameters localeParameters = new FilterEventParameters();
91   
92  13461 if (properties.isWithJRCSRevisions()) {
93  287 try {
94  287 localeParameters.put(XWikiWikiDocumentFilter.PARAMETER_JRCSREVISIONS,
95    document.getDocumentArchive(xcontext).getArchive(xcontext));
96    } catch (XWikiException e) {
97  0 this.logger.error("Document [{}] has malformed history", document.getDocumentReference(), e);
98    }
99    }
100   
101  13461 localeParameters.put(WikiDocumentFilter.PARAMETER_CREATION_AUTHOR, document.getCreator());
102  13461 localeParameters.put(WikiDocumentFilter.PARAMETER_CREATION_DATE, document.getCreationDate());
103  13461 localeParameters.put(WikiDocumentFilter.PARAMETER_LASTREVISION, document.getVersion());
104   
105  13461 documentFilter.beginWikiDocumentLocale(document.getLocale(), localeParameters);
106   
107  13461 if (properties.isWithRevisions()) {
108  0 try {
109  0 for (Version version : document.getRevisions(xcontext)) {
110  0 XWikiDocument revisionDocument =
111    xcontext.getWiki().getDocument(document, version.toString(), xcontext);
112   
113  0 writeRevision(revisionDocument, filter, documentFilter, properties);
114    }
115    } catch (XWikiException e) {
116  0 this.logger.error("Failed to get document [{}] history", document.getDocumentReference(), e);
117    }
118    }
119   
120  13461 writeRevision(document, filter, documentFilter, properties);
121   
122    // < WikiDocumentLocale
123   
124  13461 documentFilter.endWikiDocumentLocale(document.getLocale(), FilterEventParameters.EMPTY);
125    }
126   
 
127  13461 toggle private void writeRevision(XWikiDocument document, Object filter, XWikiDocumentFilter documentFilter,
128    DocumentInstanceInputProperties properties) throws FilterException
129    {
130    // > WikiDocumentRevision
131   
132  13461 FilterEventParameters revisionParameters = new FilterEventParameters();
133   
134  13461 if (document.getRelativeParentReference() != null) {
135  11968 revisionParameters.put(WikiDocumentFilter.PARAMETER_PARENT, document.getRelativeParentReference());
136    }
137  13461 revisionParameters.put(WikiDocumentFilter.PARAMETER_TITLE, document.getTitle());
138  13461 if (!document.getCustomClass().isEmpty()) {
139  2 revisionParameters.put(WikiDocumentFilter.PARAMETER_CUSTOMCLASS, document.getCustomClass());
140    }
141  13460 if (!document.getDefaultTemplate().isEmpty()) {
142  2 revisionParameters.put(WikiDocumentFilter.PARAMETER_DEFAULTTEMPLATE, document.getDefaultTemplate());
143    }
144  13461 if (!document.getValidationScript().isEmpty()) {
145  2 revisionParameters.put(WikiDocumentFilter.PARAMETER_VALIDATIONSCRIPT, document.getValidationScript());
146    }
147  13461 revisionParameters.put(WikiDocumentFilter.PARAMETER_SYNTAX, document.getSyntax());
148  13461 revisionParameters.put(WikiDocumentFilter.PARAMETER_HIDDEN, document.isHidden());
149   
150  13461 revisionParameters.put(WikiDocumentFilter.PARAMETER_REVISION_AUTHOR, document.getAuthor());
151  13461 revisionParameters.put(WikiDocumentFilter.PARAMETER_REVISION_COMMENT, document.getComment());
152  13461 revisionParameters.put(WikiDocumentFilter.PARAMETER_REVISION_DATE, document.getDate());
153  13461 revisionParameters.put(WikiDocumentFilter.PARAMETER_REVISION_MINOR, document.isMinorEdit());
154   
155  13461 revisionParameters.put(WikiDocumentFilter.PARAMETER_CONTENT_AUTHOR, document.getContentAuthor());
156  13461 revisionParameters.put(WikiDocumentFilter.PARAMETER_CONTENT_DATE, document.getContentUpdateDate());
157  13461 revisionParameters.put(WikiDocumentFilter.PARAMETER_CONTENT, document.getContent());
158   
159  13461 if (properties.isWithWikiDocumentContentHTML()) {
160  0 try {
161  0 XWikiContext xcontext = this.xcontextProvider.get();
162   
163  0 revisionParameters.put(WikiDocumentFilter.PARAMETER_CONTENT_HTML,
164    document.getRenderedContent(xcontext));
165    } catch (XWikiException e) {
166  0 this.logger.error("Failed to render content of document [{}] as HTML", document.getDocumentReference(),
167    e);
168    }
169    }
170   
171  13461 documentFilter.beginWikiDocumentRevision(document.getVersion(), revisionParameters);
172   
173    // Attachments
174   
175  13461 if (properties.isWithWikiAttachments()) {
176  13461 List<XWikiAttachment> sortedAttachments = new ArrayList<XWikiAttachment>(document.getAttachmentList());
177  13461 Collections.sort(sortedAttachments, new Comparator<XWikiAttachment>()
178    {
 
179  785 toggle @Override
180    public int compare(XWikiAttachment attachement1, XWikiAttachment attachement2)
181    {
182  785 if (attachement1 == null || attachement2 == null) {
183  0 int result = 0;
184  0 if (attachement1 != null) {
185  0 result = -1;
186  0 } else if (attachement2 != null) {
187  0 result = 1;
188    }
189  0 return result;
190    }
191  785 return attachement1.getFilename().compareTo(attachement2.getFilename());
192    }
193    });
194   
195  13461 for (XWikiAttachment attachment : sortedAttachments) {
196  1481 ((XWikiAttachmentEventGenerator) this.attachmentEventGenerator).write(attachment, filter,
197    documentFilter, properties);
198    }
199    }
200   
201    // Document Class
202  13461 if (properties.isWithWikiClass()) {
203  13461 BaseClass xclass = document.getXClass();
204  13461 if (!xclass.getFieldList().isEmpty()) {
205  2582 ((BaseClassEventGenerator) this.classEventGenerator).write(xclass, filter, documentFilter, properties);
206    }
207    }
208   
209    // Objects (THEIR ORDER IS MOLDED IN STONE!)
210  13461 if (properties.isWithWikiObjects()) {
211  13448 for (List<BaseObject> xobjects : document.getXObjects().values()) {
212  12010 for (BaseObject xobject : xobjects) {
213  17560 if (xobject != null) {
214  17036 ((BaseObjectEventGenerator) this.objectEventGenerator).write(xobject, filter, documentFilter,
215    properties);
216    }
217    }
218    }
219    }
220   
221    // < WikiDocumentRevision
222   
223  13461 documentFilter.endWikiDocumentRevision(document.getVersion(), revisionParameters);
224    }
225    }