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

File BaseObjectOutputFilterStream.java

 

Coverage histogram

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

Code metrics

20
44
8
1
177
119
21
0.48
5.5
8
2.62

Classes

Class Line # Actions
BaseObjectOutputFilterStream 49 44 0% 21 10
0.861111186.1%
 

Contributing tests

This file is covered by 43 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.output;
21   
22    import javax.inject.Inject;
23    import javax.inject.Provider;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.component.annotation.InstantiationStrategy;
28    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
29    import org.xwiki.component.phase.Initializable;
30    import org.xwiki.component.phase.InitializationException;
31    import org.xwiki.filter.FilterEventParameters;
32    import org.xwiki.filter.FilterException;
33    import org.xwiki.filter.event.model.WikiObjectFilter;
34    import org.xwiki.model.reference.EntityReference;
35   
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.XWikiException;
38    import com.xpn.xwiki.objects.BaseObject;
39    import com.xpn.xwiki.objects.BaseObjectReference;
40    import com.xpn.xwiki.objects.BaseProperty;
41    import com.xpn.xwiki.objects.classes.BaseClass;
42   
43    /**
44    * @version $Id: 8b7a0a2b2254ddcff37a1fa7663603802752e02d $
45    * @since 9.0RC1
46    */
47    @Component
48    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
49    public class BaseObjectOutputFilterStream extends AbstractEntityOutputFilterStream<BaseObject> implements Initializable
50    {
51    @Inject
52    private EntityOutputFilterStream<BaseClass> classFilter;
53   
54    @Inject
55    private EntityOutputFilterStream<BaseProperty> propertyFilter;
56   
57    @Inject
58    private Provider<XWikiContext> xcontextProvider;
59   
60    private BaseObject externalEntity;
61   
 
62  2537 toggle @Override
63    public void initialize() throws InitializationException
64    {
65  2537 initialize(this.classFilter, this.propertyFilter);
66    }
67   
 
68  2948 toggle @Override
69    public void setEntity(BaseObject entity)
70    {
71  2948 super.setEntity(entity);
72   
73  2948 this.externalEntity = entity;
74    }
75   
 
76  14740 toggle private BaseClassOutputFilterStream getBaseClassOutputFilterStream()
77    {
78  14740 return (BaseClassOutputFilterStream) this.classFilter;
79    }
80   
 
81  42723 toggle private BasePropertyOutputFilterStream getBasePropertyOutputFilterStream()
82    {
83  42723 return (BasePropertyOutputFilterStream) this.propertyFilter;
84    }
85   
86    // Events
87   
 
88  3275 toggle @Override
89    public void endWikiClass(FilterEventParameters parameters) throws FilterException
90    {
91  3275 if (this.entity != null) {
92  2948 BaseClass xclass = getBaseClassOutputFilterStream().getEntity();
93   
94  2948 if (xclass != null) {
95    // Re-create the object instance if not already provided and if there is a custom class
96  2948 if (this.externalEntity == null && StringUtils.isNotEmpty(xclass.getCustomClass())) {
97  10 BaseObject customObject;
98  10 try {
99  10 customObject = xclass.newCustomClassInstance(this.xcontextProvider.get());
100  0 customObject.setDocumentReference(this.entity.getDocumentReference());
101  0 customObject.setXClassReference(this.entity.getXClassReference());
102  0 customObject.setOwnerDocument(this.entity.getOwnerDocument());
103    // Pass false as an optimization since there is nothing to clean on a new object
104  0 customObject.apply(this.entity, false);
105   
106  0 this.entity = customObject;
107    } catch (XWikiException e) {
108    // TODO: should probably log a warning
109    }
110    }
111   
112  2948 getBasePropertyOutputFilterStream().setCurrentXClass(xclass);
113  2948 getBaseClassOutputFilterStream().setEntity(null);
114    }
115    }
116    }
117   
 
118  2948 toggle @Override
119    public void beginWikiObject(String name, FilterEventParameters parameters) throws FilterException
120    {
121  2948 super.beginWikiObject(name, parameters);
122   
123  2948 if (this.enabled) {
124  2948 if (this.entity == null) {
125  2948 this.entity = new BaseObject();
126    }
127   
128  2948 if (parameters.containsKey(WikiObjectFilter.PARAMETER_NAME)) {
129  2944 this.entity
130    .setDocumentReference(getDocumentReference(WikiObjectFilter.PARAMETER_NAME, parameters, null));
131    }
132   
133  2948 int number = getInt(WikiObjectFilter.PARAMETER_NUMBER, parameters, -1);
134   
135  2948 EntityReference classReference =
136    getEntityReference(WikiObjectFilter.PARAMETER_CLASS_REFERENCE, parameters, null);
137  2948 if (classReference == null) {
138  2 BaseObjectReference reference = new BaseObjectReference(this.currentEntityReference);
139   
140  2 classReference = reference.getXClassReference();
141   
142  2 if (number < 0 && reference.getObjectNumber() != null) {
143  2 number = reference.getObjectNumber();
144    }
145    }
146  2948 this.entity.setXClassReference(classReference);
147   
148  2948 this.entity.setNumber(number);
149   
150  2948 this.entity.setGuid(getString(WikiObjectFilter.PARAMETER_GUID, parameters, null));
151   
152  2948 getBaseClassOutputFilterStream().enable();
153  2948 getBasePropertyOutputFilterStream().enable();
154    }
155    }
156   
 
157  2948 toggle @Override
158    public void endWikiObject(String name, FilterEventParameters parameters) throws FilterException
159    {
160  2948 super.endWikiObject(name, parameters);
161   
162  2948 getBaseClassOutputFilterStream().disable();
163  2948 getBaseClassOutputFilterStream().disable();
164    }
165   
 
166  12301 toggle @Override
167    public void onWikiObjectProperty(String name, Object value, FilterEventParameters parameters) throws FilterException
168    {
169  12301 if (this.enabled) {
170  12301 if (getBasePropertyOutputFilterStream().getEntity() != null) {
171  12263 this.entity.safeput(name, getBasePropertyOutputFilterStream().getEntity());
172   
173  12263 getBasePropertyOutputFilterStream().setEntity(null);
174    }
175    }
176    }
177    }