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

File AbstractBlockParser.java

 

Coverage histogram

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

Code metrics

18
54
20
1
235
165
33
0.61
2.7
20
1.65

Classes

Class Line # Actions
AbstractBlockParser 33 54 0% 33 19
0.7934782579.3%
 

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 javax.inject.Inject;
23   
24    import org.xml.sax.Attributes;
25    import org.xml.sax.ContentHandler;
26    import org.xml.sax.SAXException;
27    import org.xml.sax.helpers.DefaultHandler;
28    import org.xwiki.component.manager.ComponentLookupException;
29    import org.xwiki.component.manager.ComponentManager;
30    import org.xwiki.rendering.listener.Listener;
31    import org.xwiki.rendering.xdomxml10.internal.XDOMXMLConstants;
32   
 
33    public abstract class AbstractBlockParser extends DefaultHandler implements BlockParser
34    {
35    @Inject
36    private ComponentManager componentManager;
37   
38    private String blockName;
39   
40    private ContentHandler currentHandler;
41   
42    private int currentHandlerLevel;
43   
44    private int level = 0;
45   
46    private String blockVersion;
47   
48    private boolean beginBlockFlushed;
49   
50    private Listener listener;
51   
 
52  0 toggle public void setComponentManager(ComponentManager componentManager)
53    {
54  0 this.componentManager = componentManager;
55    }
56   
 
57  0 toggle public ComponentManager getComponentManager()
58    {
59  0 return componentManager;
60    }
61   
 
62  10 toggle @Override
63    public Listener getListener()
64    {
65  10 return this.listener;
66    }
67   
 
68  4 toggle @Override
69    public void setListener(Listener listener)
70    {
71  4 this.listener = listener;
72    }
73   
 
74  4 toggle @Override
75    public String getVersion()
76    {
77  4 return this.blockVersion;
78    }
79   
 
80  4 toggle @Override
81    public void setVersion(String version)
82    {
83  4 this.blockVersion = version;
84    }
85   
 
86  0 toggle public String getBlockName()
87    {
88  0 return this.blockName;
89    }
90   
 
91  10 toggle public int getLevel()
92    {
93  10 return this.level;
94    }
95   
96    // ContentHandler
97   
 
98  28 toggle @Override
99    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
100    {
101  28 if (this.currentHandler != null) {
102  18 this.currentHandler.startElement(uri, localName, qName, attributes);
103    } else {
104  10 if (qName.equals(XDOMXMLConstants.ELEM_BLOCK)) {
105  6 String name = attributes.getValue(XDOMXMLConstants.ATT_BLOCK_NAME);
106  6 String version = attributes.getValue(XDOMXMLConstants.ATT_BLOCK_VERSION);
107   
108  6 if (this.level == 0 && this.blockName == null) {
109  4 this.blockName = name;
110  4 this.blockVersion = version;
111    } else {
112  2 flushBeginBlock();
113   
114    // start parsing new child block
115  2 try {
116  2 BlockParser blockParser = getBlockParser(name);
117   
118  2 blockParser.setListener(getListener());
119  2 blockParser.setVersion(getVersion());
120   
121  2 setCurrentHandler(blockParser);
122    } catch (ComponentLookupException e) {
123  0 throw new SAXException("Failed to find a block parser for [" + name + "]", e);
124    }
125    }
126    } else {
127  4 startElementInternal(uri, localName, qName, attributes);
128    }
129   
130  10 if (this.currentHandler != null) {
131  6 this.currentHandler.startElement(uri, localName, qName, attributes);
132    }
133    }
134   
135  28 ++this.level;
136    }
137   
 
138  12 toggle @Override
139    public void characters(char[] ch, int start, int length) throws SAXException
140    {
141  12 if (this.currentHandler != null) {
142  12 this.currentHandler.characters(ch, start, length);
143    } else {
144  0 charactersInternal(ch, start, length);
145    }
146    }
147   
 
148  28 toggle @Override
149    public void endElement(String uri, String localName, String qName) throws SAXException
150    {
151  28 --this.level;
152   
153  28 if (this.currentHandler != null) {
154  24 this.currentHandler.endElement(uri, localName, qName);
155   
156  24 if (this.level == this.currentHandlerLevel) {
157  6 endElementInternal(uri, localName, qName);
158  6 this.currentHandler = null;
159    }
160    } else {
161  4 if (this.level == 0) {
162  4 flushBeginBlock();
163  4 endElementInternal(uri, localName, qName);
164  4 endBlock();
165    } else {
166  0 endElementInternal(uri, localName, qName);
167    }
168    }
169    }
170   
171    // to override
172   
 
173  0 toggle protected void startElementInternal(String uri, String localName, String qName, Attributes attributes)
174    throws SAXException
175    {
176    // no op
177    }
178   
 
179  0 toggle protected void charactersInternal(char[] ch, int start, int length) throws SAXException
180    {
181    // no op
182    }
183   
 
184  0 toggle protected void endElementInternal(String uri, String localName, String qName) throws SAXException
185    {
186    // no op
187    }
188   
 
189  0 toggle protected void beginBlock() throws SAXException
190    {
191    // no op
192    }
193   
 
194  0 toggle protected void endBlock() throws SAXException
195    {
196    // no op
197    }
198   
199    // tools
200   
 
201  6 toggle protected void flushBeginBlock() throws SAXException
202    {
203  6 if (!this.beginBlockFlushed) {
204  4 beginBlock();
205  4 this.beginBlockFlushed = true;
206    }
207    }
208   
 
209  6 toggle protected void setCurrentHandler(ContentHandler currentHandler)
210    {
211  6 this.currentHandler = currentHandler;
212  6 this.currentHandlerLevel = this.level;
213    }
214   
 
215  0 toggle public ContentHandler getCurrentHandler()
216    {
217  0 return this.currentHandler;
218    }
219   
 
220  2 toggle protected BlockParser getBlockParser(String name) throws ComponentLookupException
221    {
222  2 BlockParser blockParser;
223  2 try {
224  2 blockParser = this.componentManager.getInstance(BlockParser.class, name.toLowerCase() + "/" + getVersion());
225    } catch (ComponentLookupException e1) {
226  2 try {
227  2 blockParser = this.componentManager.getInstance(BlockParser.class, name.toLowerCase());
228    } catch (ComponentLookupException e2) {
229  0 blockParser = this.componentManager.getInstance(BlockParser.class);
230    }
231    }
232   
233  2 return blockParser;
234    }
235    }