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

File PropertyClassOutputFilterStream.java

 

Coverage histogram

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

Code metrics

12
28
4
1
144
90
12
0.43
7
4
3

Classes

Class Line # Actions
PropertyClassOutputFilterStream 49 28 0% 12 5
0.886363688.6%
 

Contributing tests

This file is covered by 26 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: 2f54934a7ebc6a13a86b8da373b24ee6144f499b $
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  13311 toggle public void setCurrentXClass(BaseClass currentXClass)
66    {
67  13310 this.currentXClass = currentXClass;
68    }
69   
70    // Events
71   
 
72  51887 toggle @Override
73    public void beginWikiClassProperty(String name, String type, FilterEventParameters parameters)
74    throws FilterException
75    {
76  51888 if (this.enabled) {
77  37533 ComponentManager componentManager = this.componentManagerProvider.get();
78   
79  37531 this.currentClassPropertyMeta = null;
80   
81  37530 PropertyClassProvider provider;
82   
83    // First try to use the specified class type as hint.
84  37531 try {
85  37532 if (componentManager.hasComponent(PropertyClassProvider.class, type)) {
86  0 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  37532 String classType = StringUtils.removeEnd(StringUtils.substringAfterLast(type, "."), "Class");
91  37533 if (componentManager.hasComponent(PropertyClassProvider.class, classType)) {
92  37532 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  37532 this.currentClassPropertyMeta = provider.getDefinition();
105   
106  37532 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  37532 this.entity = (PropertyClass) provider.getInstance();
111    }
112   
113  37531 this.entity.setName(name);
114  37532 this.entity.setObject(this.currentXClass);
115    }
116    }
117   
 
118  51888 toggle @Override
119    public void endWikiClassProperty(String name, String type, FilterEventParameters parameters) throws FilterException
120    {
121  51890 if (this.enabled) {
122  37530 this.currentClassPropertyMeta = null;
123    }
124    }
125   
 
126  445879 toggle @Override
127    public void onWikiClassPropertyField(String name, String value, FilterEventParameters parameters)
128    throws FilterException
129    {
130  445887 if (this.entity != null) {
131  315871 PropertyClass propertyClass;
132  315873 try {
133  315876 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  315896 BaseProperty<?> field = propertyClass.fromString(value);
140   
141  315884 this.entity.safeput(name, field);
142    }
143    }
144    }