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

File FilterUtils.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

16
35
5
1
160
77
18
0.51
7
5
3.6

Classes

Class Line # Actions
FilterUtils 36 35 0% 18 56
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.internal;
21   
22    import java.lang.reflect.Method;
23   
24    import org.xwiki.filter.FilterDescriptor;
25    import org.xwiki.filter.FilterElementDescriptor;
26    import org.xwiki.filter.FilterElementParameterDescriptor;
27    import org.xwiki.filter.FilterEventParameters;
28    import org.xwiki.filter.FilterException;
29    import org.xwiki.filter.UnknownFilter;
30   
31    /**
32    * Provide various filter related tools.
33    *
34    * @version $Id: 9c15b8f48a9f659a03051c1e42db5b96d860279b $
35    */
 
36    public final class FilterUtils
37    {
 
38  0 toggle private FilterUtils()
39    {
40    // Utility class
41    }
42   
43    /**
44    * Call passed begin event if possible.
45    *
46    * @param event the event to send
47    * @param elementDescriptor the descriptor of the event to send
48    * @param filter the filter
49    * @param parameters the parameters of the event
50    * @return true if the event has been sent, false otherwise
51    * @throws FilterException when the passed filter exposes the event but failed anyway
52    */
 
53  0 toggle public static boolean sendEvent(Method event, FilterElementDescriptor elementDescriptor, Object filter,
54    FilterEventParameters parameters) throws FilterException
55    {
56  0 FilterElementParameterDescriptor<?>[] parameterDescriptors = elementDescriptor.getParameters();
57   
58  0 Object[] arguments = new Object[parameterDescriptors.length];
59   
60  0 for (FilterElementParameterDescriptor<?> parameterDescriptor : parameterDescriptors) {
61  0 Object value;
62  0 if (parameterDescriptor.getName() != null && parameters.containsKey(parameterDescriptor.getName())) {
63  0 value = parameters.get(parameterDescriptor.getName());
64  0 } else if (parameters.containsKey(String.valueOf(parameterDescriptor.getIndex()))) {
65  0 value = parameters.get(String.valueOf(parameterDescriptor.getIndex()));
66    } else {
67  0 value = parameterDescriptor.getDefaultValue();
68    }
69   
70  0 arguments[parameterDescriptor.getIndex()] = value;
71    }
72   
73  0 try {
74  0 event.invoke(filter, arguments);
75    } catch (Exception e) {
76  0 throw new FilterException(String.format("Failed to send event [%s] with parameters [%s] to filter [%s]",
77    event, parameters, filter), e);
78    }
79   
80  0 return true;
81    }
82   
83    /**
84    * Call passed begin event if possible.
85    *
86    * @param filter the filter
87    * @param descriptor the descriptor of the filter
88    * @param id the id of the event
89    * @param parameters the parameters of the event
90    * @return true if the event has been sent, false otherwise
91    * @throws FilterException when the passed filter exposes the event but failed anyway
92    */
 
93  0 toggle public static boolean sendBeginEvent(Object filter, FilterDescriptor descriptor, String id,
94    FilterEventParameters parameters) throws FilterException
95    {
96  0 FilterElementDescriptor elementDescriptor = descriptor.getElement(id);
97   
98  0 if (elementDescriptor != null && elementDescriptor.getBeginMethod() != null) {
99  0 sendEvent(elementDescriptor.getBeginMethod(), elementDescriptor, filter, parameters);
100  0 } else if (filter instanceof UnknownFilter) {
101  0 ((UnknownFilter) filter).beginUnknwon(id, parameters);
102    } else {
103  0 return false;
104    }
105   
106  0 return true;
107    }
108   
109    /**
110    * Call passed end event if possible.
111    *
112    * @param filter the filter
113    * @param descriptor the descriptor of the filter
114    * @param id the id of the event
115    * @param parameters the parameters of the event
116    * @return true if the event has been sent, false otherwise
117    * @throws FilterException when the passed filter exposes the event but failed anyway
118    */
 
119  0 toggle public static boolean sendEndEvent(Object filter, FilterDescriptor descriptor, String id,
120    FilterEventParameters parameters) throws FilterException
121    {
122  0 FilterElementDescriptor elementDescriptor = descriptor.getElement(id);
123   
124  0 if (elementDescriptor != null && elementDescriptor.getEndMethod() != null) {
125  0 sendEvent(elementDescriptor.getEndMethod(), elementDescriptor, filter, parameters);
126  0 } else if (filter instanceof UnknownFilter) {
127  0 ((UnknownFilter) filter).endUnknwon(id, parameters);
128    } else {
129  0 return false;
130    }
131   
132  0 return true;
133    }
134   
135    /**
136    * Call passed on event if possible.
137    *
138    * @param filter the filter
139    * @param descriptor the descriptor of the filter
140    * @param id the id of the event
141    * @param parameters the parameters of the event
142    * @return true if the event has been sent, false otherwise
143    * @throws FilterException when the passed filter exposes the event but failed anyway
144    */
 
145  0 toggle public static boolean sendOnEvent(Object filter, FilterDescriptor descriptor, String id,
146    FilterEventParameters parameters) throws FilterException
147    {
148  0 FilterElementDescriptor elementDescriptor = descriptor.getElement(id);
149   
150  0 if (elementDescriptor != null && elementDescriptor.getOnMethod() != null) {
151  0 sendEvent(elementDescriptor.getOnMethod(), elementDescriptor, filter, parameters);
152  0 } else if (filter instanceof UnknownFilter) {
153  0 ((UnknownFilter) filter).onUnknwon(id, parameters);
154    } else {
155  0 return false;
156    }
157   
158  0 return true;
159    }
160    }