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

File PropertyClassOutputFilterStream.java

 

Coverage histogram

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

Code metrics

14
31
4
1
152
95
13
0.42
7.75
4
3.25

Classes

Class Line # Actions
PropertyClassOutputFilterStream 49 31 0% 13 6
0.87755187.8%
 

Contributing tests

This file is covered by 28 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.Named;
24    import javax.inject.Provider;
25   
26    import org.apache.commons.lang3.StringUtils;
27    import org.slf4j.Logger;
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.component.annotation.InstantiationStrategy;
30    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.filter.FilterEventParameters;
34    import org.xwiki.filter.FilterException;
35   
36    import com.xpn.xwiki.XWikiException;
37    import com.xpn.xwiki.internal.objects.classes.PropertyClassProvider;
38    import com.xpn.xwiki.internal.objects.meta.PropertyMetaClassInterface;
39    import com.xpn.xwiki.objects.BaseProperty;
40    import com.xpn.xwiki.objects.classes.BaseClass;
41    import com.xpn.xwiki.objects.classes.PropertyClass;
42   
43    /**
44    * @version $Id: 826e25a98c196310466c9d8e901be60839c829cc $
45    * @since 9.0RC1
46    */
47    @Component
48    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
49    public class PropertyClassOutputFilterStream extends AbstractEntityOutputFilterStream<PropertyClass>
50    {
51    @Inject
52    @Named("context")
53    private Provider<ComponentManager> componentManagerProvider;
54   
55    @Inject
56    private Logger logger;
57   
58    private BaseClass currentXClass;
59   
60    private PropertyMetaClassInterface currentClassPropertyMeta;
61   
62    /**
63    * @param currentXClass the current {@link BaseClass}
64    */
 
65  34149 toggle public void setCurrentXClass(BaseClass currentXClass)
66    {
67  34149 this.currentXClass = currentXClass;
68    }
69   
70    // Events
71   
 
72  137352 toggle @Override
73    public void beginWikiClassProperty(String name, String type, FilterEventParameters parameters)
74    throws FilterException
75    {
76  137352 if (this.enabled) {
77  89597 ComponentManager componentManager = this.componentManagerProvider.get();
78   
79  89597 this.currentClassPropertyMeta = null;
80   
81  89597 PropertyClassProvider provider;
82   
83    // First try to use the specified class type as hint.
84  89597 try {
85  89596 if (componentManager.hasComponent(PropertyClassProvider.class, type)) {
86  1 provider = componentManager.getInstance(PropertyClassProvider.class, type);
87    } else {
88    // In previous versions the class type was the full Java class name of the property class
89    // implementation. Extract the hint by removing the Java package prefix and the Class suffix.
90  89596 String classType = StringUtils.removeEnd(StringUtils.substringAfterLast(type, "."), "Class");
91  89596 if (componentManager.hasComponent(PropertyClassProvider.class, classType)) {
92  89595 provider = componentManager.getInstance(PropertyClassProvider.class, classType);
93    } else {
94  1 this.logger.warn("Unknown property type [{}]", type);
95   
96  1 return;
97    }
98    }
99    } catch (ComponentLookupException e) {
100  0 throw new FilterException(
101    String.format("Failed to get instance of the property class provider for type [%s]", type), e);
102    }
103   
104  89596 this.currentClassPropertyMeta = provider.getDefinition();
105   
106  89596 if (this.entity == null) {
107    // We should use PropertyClassInterface (instead of PropertyClass, its default implementation) but it
108    // doesn't have the set methods and adding them would breaks the backwards compatibility. We make the
109    // assumption that all property classes extend PropertyClass.
110  89596 this.entity = (PropertyClass) provider.getInstance();
111    }
112   
113  89596 this.entity.setName(name);
114  89596 this.entity.setObject(this.currentXClass);
115    }
116    }
117   
 
118  137351 toggle @Override
119    public void endWikiClassProperty(String name, String type, FilterEventParameters parameters) throws FilterException
120    {
121  137352 if (this.enabled) {
122  89597 this.currentClassPropertyMeta = null;
123    }
124    }
125   
 
126  1341721 toggle @Override
127    public void onWikiClassPropertyField(String name, String value, FilterEventParameters parameters)
128    throws FilterException
129    {
130  1341722 if (this.entity != null) {
131  869307 PropertyClass propertyClass;
132  869305 try {
133  869306 propertyClass = (PropertyClass) this.currentClassPropertyMeta.get(name);
134    } catch (XWikiException e) {
135  0 throw new FilterException(String.format("Failed to get definition of field [%s] for property type [%s]",
136    name, this.entity.getClassType()), e);
137    }
138   
139    // Make sure the property is known
140  869299 if (propertyClass == null) {
141  0 this.logger.warn("Unknown property meta class field [{}] for property type [{}]", name,
142    this.entity.getClassType());
143   
144  0 return;
145    }
146   
147  869299 BaseProperty<?> field = propertyClass.fromString(value);
148   
149  869305 this.entity.safeput(name, field);
150    }
151    }
152    }