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

File DefaultXMLParser.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

108
172
25
2
522
394
97
0.56
6.88
12.5
3.88

Classes

Class Line # Actions
DefaultXMLParser 65 118 0% 66 37
0.822966582.3%
DefaultXMLParser.Block 88 54 0% 31 21
0.7812578.1%
 

Contributing tests

This file is covered by 589 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.xml.internal.parser;
21   
22    import java.lang.reflect.InvocationTargetException;
23    import java.lang.reflect.Method;
24    import java.lang.reflect.Type;
25    import java.util.ArrayList;
26    import java.util.Arrays;
27    import java.util.Deque;
28    import java.util.LinkedList;
29    import java.util.List;
30    import java.util.regex.Matcher;
31   
32    import javax.xml.parsers.ParserConfigurationException;
33   
34    import org.apache.commons.lang3.ArrayUtils;
35    import org.apache.xerces.parsers.XMLParser;
36    import org.slf4j.Logger;
37    import org.slf4j.LoggerFactory;
38    import org.w3c.dom.Element;
39    import org.w3c.dom.Node;
40    import org.w3c.dom.NodeList;
41    import org.xml.sax.Attributes;
42    import org.xml.sax.ContentHandler;
43    import org.xml.sax.SAXException;
44    import org.xml.sax.helpers.DefaultHandler;
45    import org.xwiki.component.util.ReflectionUtils;
46    import org.xwiki.filter.FilterDescriptor;
47    import org.xwiki.filter.FilterElementDescriptor;
48    import org.xwiki.filter.FilterElementParameterDescriptor;
49    import org.xwiki.filter.FilterEventParameters;
50    import org.xwiki.filter.UnknownFilter;
51    import org.xwiki.filter.xml.XMLConfiguration;
52    import org.xwiki.filter.xml.internal.XMLUtils;
53    import org.xwiki.filter.xml.internal.parameter.ParameterManager;
54    import org.xwiki.properties.ConverterManager;
55    import org.xwiki.properties.converter.ConversionException;
56    import org.xwiki.xml.Sax2Dom;
57   
58    /**
59    * Default implementation of {@link XMLParser}.
60    *
61    * @version $Id: ed004efbc23625ec577c2e3ca2007a19b81e6d11 $
62    * @since 5.2M1
63    */
64    // TODO: move from ContentHandler to XMLEventConsumer or XMLEventWriter
 
65    public class DefaultXMLParser extends DefaultHandler implements ContentHandler
66    {
67    /**
68    * Logging helper object.
69    */
70    protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultXMLParser.class);
71   
72    private ParameterManager parameterManager;
73   
74    private ConverterManager stringConverter;
75   
76    private FilterDescriptor filterDescriptor;
77   
78    private Object filter;
79   
80    private Deque<Block> blockStack = new LinkedList<>();
81   
82    private int elementDepth = 0;
83   
84    private StringBuilder content;
85   
86    private XMLConfiguration configuration;
87   
 
88    public static class Block
89    {
90    public String name;
91   
92    public FilterElementDescriptor filterElement;
93   
94    public boolean beginSent = false;
95   
96    public List<Object> parameters = new ArrayList<Object>();
97   
98    public FilterEventParameters namedParameters = new FilterEventParameters();
99   
100    public Sax2Dom parametersDOMBuilder;
101   
102    public int elementDepth;
103   
104    private Object[] parametersTable;
105   
 
106  7654 toggle public Block(String name, FilterElementDescriptor listenerElement, int elementDepth)
107    {
108  7654 this.name = name;
109  7654 this.filterElement = listenerElement;
110  7654 this.elementDepth = elementDepth;
111    }
112   
 
113  15308 toggle public boolean isContainer()
114    {
115  15308 return this.filterElement == null || this.filterElement.getBeginMethod() != null;
116    }
117   
 
118  0 toggle public void setParameter(String name, Object value)
119    {
120  0 this.namedParameters.put(name, value);
121    }
122   
 
123  5561 toggle public void setParameter(int index, Object value)
124    {
125  11172 for (int i = this.parameters.size(); i <= index; ++i) {
126  5611 this.parameters.add(this.filterElement.getParameters()[i].getDefaultValue());
127    }
128   
129  5561 this.parameters.set(index, value);
130  5561 this.parametersTable = null;
131    }
132   
 
133  4725 toggle public List<Object> getParametersList()
134    {
135  4725 return this.parameters;
136    }
137   
 
138  10563 toggle public Object[] getParametersTable()
139    {
140  10563 if (this.parametersTable == null) {
141  7644 if (this.parameters.isEmpty()) {
142  3070 this.parametersTable = ArrayUtils.EMPTY_OBJECT_ARRAY;
143    }
144   
145  7644 this.parametersTable = this.parameters.toArray();
146    }
147   
148  10563 return this.parametersTable;
149    }
150   
 
151  2929 toggle public void fireBeginEvent(Object listener) throws SAXException
152    {
153  2929 if (this.filterElement != null) {
154  2919 fireEvent(this.filterElement.getBeginMethod(), listener);
155  10 } else if (listener instanceof UnknownFilter) {
156  0 try {
157  0 ((UnknownFilter) listener).beginUnknwon(this.name, this.namedParameters);
158    } catch (Exception e) {
159  0 throw new SAXException("Failed to invoke unknown event with name [" + this.name
160    + "] and parameters [" + this.namedParameters + "]", e);
161    }
162    }
163  2929 this.beginSent = true;
164    }
165   
 
166  2929 toggle public void fireEndEvent(Object listener) throws SAXException
167    {
168  2929 if (this.filterElement != null) {
169  2919 fireEvent(this.filterElement.getEndMethod(), listener);
170  10 } else if (listener instanceof UnknownFilter) {
171  0 try {
172  0 ((UnknownFilter) listener).endUnknwon(this.name, this.namedParameters);
173    } catch (Exception e) {
174  0 throw new SAXException("Failed to invoke unknown event with name [" + this.name
175    + "] and parameters [" + this.namedParameters + "]", e);
176    }
177    }
178    }
179   
 
180  4725 toggle public void fireOnEvent(Object listener) throws SAXException
181    {
182  4725 if (this.filterElement != null) {
183  4725 fireEvent(this.filterElement.getOnMethod(), listener);
184  0 } else if (listener instanceof UnknownFilter) {
185  0 try {
186  0 ((UnknownFilter) listener).onUnknwon(this.name, this.namedParameters);
187    } catch (Exception e) {
188  0 throw new SAXException("Failed to invoke unknown event with name [" + this.name
189    + "] and parameters [" + this.namedParameters + "]", e);
190    }
191    }
192    }
193   
 
194  10563 toggle private void fireEvent(Method eventMethod, Object listener) throws SAXException
195    {
196  10563 Object[] parameters = getParametersTable();
197  10563 Class<?>[] methodParameters = eventMethod.getParameterTypes();
198   
199  10563 Object[] properParameters;
200    // Missing parameters
201  10563 if (methodParameters.length > parameters.length) {
202  4626 properParameters = new Object[methodParameters.length];
203  12336 for (int i = 0; i < methodParameters.length; ++i) {
204  7710 if (i < parameters.length) {
205  2671 properParameters[i] = parameters[i];
206    } else {
207  5039 properParameters[i] = this.filterElement.getParameters()[i].getDefaultValue();
208    }
209    }
210    } else {
211  5937 properParameters = parameters;
212    }
213   
214    // Invalid primitive
215  22900 for (int i = 0; i < properParameters.length; ++i) {
216  12337 Object parameter = properParameters[i];
217   
218  12337 if (parameter == null) {
219  157 Class<?> methodParameter = methodParameters[i];
220   
221  157 if (methodParameter.isPrimitive()) {
222  123 properParameters[i] = XMLUtils.emptyValue(methodParameter);
223    }
224    }
225    }
226   
227    // Send event
228  10563 try {
229  10563 eventMethod.invoke(listener, properParameters);
230    } catch (InvocationTargetException e) {
231  0 throw new SAXException("Event [" + eventMethod + "] thrown exception",
232  0 e.getCause() instanceof Exception ? (Exception) e.getCause() : e);
233    } catch (Exception e) {
234  0 throw new SAXException("Failed to invoke event [" + eventMethod + "]", e);
235