1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.filter

File AbstractBeanFilterStreamFactory.java

 

Coverage histogram

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

Code metrics

2
20
9
1
145
73
11
0.55
2.22
9
1.22

Classes

Class Line # Actions
AbstractBeanFilterStreamFactory 39 20 0% 11 3
0.903225890.3%
 

Contributing tests

This file is covered by 95 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;
21   
22    import java.lang.reflect.ParameterizedType;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26   
27    import org.xwiki.component.phase.Initializable;
28    import org.xwiki.component.phase.InitializationException;
29    import org.xwiki.component.util.ReflectionUtils;
30    import org.xwiki.filter.descriptor.DefaultFilterStreamBeanDescriptor;
31    import org.xwiki.filter.type.FilterStreamType;
32    import org.xwiki.properties.BeanManager;
33   
34    /**
35    * @param <P> the type of the class containing the parameters of the filter
36    * @version $Id: ecf60442a209e2bf2b004bad900ead51d065d405 $
37    * @since 6.2M1
38    */
 
39    public abstract class AbstractBeanFilterStreamFactory<P> extends AbstractFilterStreamFactory implements
40    FilterStreamFactory, Initializable
41    {
42    /**
43    * The {@link BeanManager} component.
44    */
45    @Inject
46    protected BeanManager beanManager;
47   
48    private String name;
49   
50    private String description;
51   
52    /**
53    * Properties bean class used to generate the macro descriptor.
54    */
55    private Class<P> propertiesBeanClass;
56   
 
57  499 toggle public AbstractBeanFilterStreamFactory(FilterStreamType type)
58    {
59  499 super(type);
60    }
61   
 
62  499 toggle @Override
63    public void initialize() throws InitializationException
64    {
65    // Get bean properties type
66  499 ParameterizedType genericType =
67    (ParameterizedType) ReflectionUtils.resolveType(AbstractBeanFilterStreamFactory.class, getClass());
68  499 this.propertiesBeanClass = ReflectionUtils.getTypeClass(genericType.getActualTypeArguments()[0]);
69   
70    // Initialize Filter Descriptor.
71  499 DefaultFilterStreamBeanDescriptor descriptor =
72    new DefaultFilterStreamBeanDescriptor(getName(), getDescription(),
73    this.beanManager.getBeanDescriptor(this.propertiesBeanClass));
74   
75  499 setDescriptor(descriptor);
76    }
77   
 
78  122 toggle protected P createPropertiesBean(Map<String, Object> properties) throws FilterException
79    {
80  122 Class<P> beanClass = getPropertiesBeanClass();
81   
82  122 if (beanClass.isInstance(properties)) {
83  20 return (P) properties;
84    }
85   
86  102 P parametersBean;
87  102 try {
88  102 parametersBean = beanClass.newInstance();
89   
90  102 this.beanManager.populate(parametersBean, properties);
91    } catch (Exception e) {
92  0 throw new FilterException(String.format("Failed to read parameters [%s]", properties), e);
93    }
94   
95  102 return parametersBean;
96    }
97   
98    /**
99    * @param name the name to set
100    */
 
101  499 toggle public void setName(String name)
102    {
103  499 this.name = name;
104    }
105   
106    /**
107    * @return the name
108    */
 
109  499 toggle public String getName()
110    {
111  499 return this.name;
112    }
113   
114    /**
115    * @param description the description to set
116    */
 
117  499 toggle public void setDescription(String description)
118    {
119  499 this.description = description;
120    }
121   
122    /**
123    * @return the description
124    */
 
125  499 toggle public String getDescription()
126    {
127  499 return this.description;
128    }
129   
130    /**
131    * @param propertiesBeanClass the parametersBeanClass to set
132    */
 
133  0 toggle public void setPropertiesBeanClass(Class<P> propertiesBeanClass)
134    {
135  0 this.propertiesBeanClass = propertiesBeanClass;
136    }
137   
138    /**
139    * @return the properties bean class
140    */
 
141  28017 toggle public Class<P> getPropertiesBeanClass()
142    {
143  28017 return this.propertiesBeanClass;
144    }
145    }