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

File DefaultBlockParser.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

34
44
15
1
198
141
35
0.8
2.93
15
2.33

Classes

Class Line # Actions
DefaultBlockParser 44 44 0% 35 77
0.1720430117.2%
 

Contributing tests

This file is covered by 2 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.rendering.xdomxml10.internal.parser;
21   
22    import java.lang.reflect.Method;
23    import java.util.Collections;
24    import java.util.HashMap;
25    import java.util.Map;
26    import java.util.Set;
27   
28    import javax.inject.Inject;
29   
30    import org.xml.sax.Attributes;
31    import org.xml.sax.SAXException;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.component.annotation.InstantiationStrategy;
34    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
35    import org.xwiki.rendering.listener.Listener;
36    import org.xwiki.rendering.listener.descriptor.ListenerDescriptor;
37    import org.xwiki.rendering.listener.descriptor.ListenerDescriptorManager;
38    import org.xwiki.rendering.listener.descriptor.ListenerElement;
39    import org.xwiki.rendering.xdomxml10.internal.XDOMXMLConstants;
40    import org.xwiki.rendering.xdomxml10.internal.parser.parameter.CustomParametersParser;
41   
42    @Component
43    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
44    public class DefaultBlockParser extends AbstractBlockParser
45    {
46    @Inject
47    private ListenerDescriptorManager descriptorManager;
48   
49    private Set<String> parameterNames;
50   
51    private Map<String, String> parameters;
52   
53    private StringBuffer value;
54   
55    private ListenerDescriptor descriptor;
56   
57    private Map<String, String> customParameters = Collections.emptyMap();
58   
59    /**
60    * Called by Component Manager.
61    */
 
62  0 toggle public DefaultBlockParser()
63    {
64    }
65   
 
66  8 toggle protected DefaultBlockParser(Set<String> parameterNames)
67    {
68  8 if (parameterNames != null) {
69  8 this.parameterNames = parameterNames;
70  8 this.parameters = new HashMap<String, String>();
71    }
72    }
73   
 
74  4 toggle @Override
75    public void setListener(Listener listener)
76    {
77  4 super.setListener(listener);
78   
79  4 this.descriptor = this.descriptorManager.getListenerDescriptor(listener.getClass());
80    }
81   
 
82  0 toggle public Map<String, String> getParameters()
83    {
84  0 return this.parameters;
85    }
86   
 
87  0 toggle public Map<String, String> getCustomParameters()
88    {
89  0 return this.customParameters;
90    }
91   
 
92  0 toggle public int getParameterAsInt(String name, int defaultValue)
93    {
94  0 String str = getParameters().get(name);
95   
96  0 return str != null ? Integer.valueOf(str) : defaultValue;
97    }
98   
 
99  0 toggle public boolean getParameterAsBoolean(String name, boolean defaultValue)
100    {
101  0 String str = getParameters().get(name);
102   
103  0 return str != null ? Boolean.valueOf(str) : defaultValue;
104    }
105   
 
106  0 toggle public char getParameterAsChar(String name, char defaultValue)
107    {
108  0 String str = getParameters().get(name);
109   
110  0 return str != null && str.length() > 0 ? str.charAt(0) : defaultValue;
111    }
112   
 
113  0 toggle public String getParameterAsString(String name, String defaultValue)
114    {
115  0 String str = getParameters().get(name);
116   
117  0 return str != null ? str : defaultValue;
118    }
119   
 
120  0 toggle @Override
121    protected void startElementInternal(String uri, String localName, String qName, Attributes attributes)
122    throws SAXException
123    {
124  0 if (getLevel() > 0) {
125  0 if (this.parameterNames != null && this.parameterNames.contains(qName)) {
126  0 this.value = new StringBuffer();
127  0 } else if (qName.equals(XDOMXMLConstants.ELEM_PARAMETERS)) {
128    // Start parsing custom parameters
129  0 setCurrentHandler(new CustomParametersParser());
130    }
131    }
132    }
133   
 
134  0 toggle @Override
135    public void charactersInternal(char[] ch, int start, int length) throws SAXException
136    {
137  0 if (this.value != null) {
138  0 this.value.append(ch, start, length);
139    }
140    }
141   
 
142  10 toggle @Override
143    protected void endElementInternal(String uri, String localName, String qName) throws SAXException
144    {
145  10 if (getLevel() > 0) {
146  6 if (this.value != null) {
147  0 this.parameters.put(qName, this.value.toString());
148  6 } else if (qName.equals(XDOMXMLConstants.ELEM_PARAMETERS)) {
149    // Custom parameters has been parsed
150  0 CustomParametersParser parametersParser = (CustomParametersParser) getCurrentHandler();
151  0 this.customParameters = parametersParser.getValue();
152    }
153    }
154    }
155   
 
156  0 toggle private void sendEvent(Method method) throws SAXException
157    {
158  0 try {
159  0 if (method.getParameterTypes().length == 0) {
160  0 method.invoke(getListener());
161    } else {
162  0 method.invoke(getListener(), this.customParameters);
163    }
164    } catch (Exception e) {
165  0 throw new SAXException("Failed to send listener event [" + method + "]", e);
166    }
167    }
168   
 
169  0 toggle @Override
170    protected void beginBlock() throws SAXException
171    {
172  0 if (getListener() != null) {
173  0 String name = getBlockName().toUpperCase();
174   
175  0 ListenerElement element = this.descriptor.getElements().get(name.toLowerCase());
176   
177  0 if (element.getBeginMethod() != null) {
178  0 sendEvent(element.getBeginMethod());
179    }
180    }
181    }
182   
 
183  0 toggle @Override
184    protected void endBlock() throws SAXException
185    {
186  0 if (getListener() != null) {
187  0 String name = getBlockName().toUpperCase();
188   
189  0 ListenerElement element = descriptor.getElements().get(name.toLowerCase());
190   
191  0 if (element.getOnMethod() != null) {
192  0 sendEvent(element.getOnMethod());
193    } else {
194  0 sendEvent(element.getEndMethod());
195    }
196    }
197    }
198    }