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

File DefaultTestDefinitionParser.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

12
47
14
1
208
139
20
0.43
3.36
14
1.43

Classes

Class Line # Actions
DefaultTestDefinitionParser 51 47 0% 20 24
0.671232967.1%
 

Contributing tests

This file is covered by 15 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   
21    package org.xwiki.security.authorization.testwikis.internal.parser;
22   
23    import java.io.FileNotFoundException;
24    import java.io.IOException;
25    import java.net.URISyntaxException;
26    import java.net.URL;
27    import java.util.HashMap;
28    import java.util.Map;
29    import java.util.Stack;
30   
31    import javax.xml.parsers.ParserConfigurationException;
32    import javax.xml.parsers.SAXParser;
33    import javax.xml.parsers.SAXParserFactory;
34   
35    import org.xml.sax.Attributes;
36    import org.xml.sax.Locator;
37    import org.xml.sax.SAXException;
38    import org.xml.sax.SAXParseException;
39    import org.xml.sax.helpers.DefaultHandler;
40    import org.xwiki.model.reference.EntityReferenceResolver;
41    import org.xwiki.model.reference.EntityReferenceSerializer;
42    import org.xwiki.security.authorization.testwikis.TestDefinition;
43    import org.xwiki.security.authorization.testwikis.TestDefinitionParser;
44   
45    /**
46    * Parser for parsing Tests Wikis XML definition.
47    *
48    * @version $Id: 29cc5b976154fa98c0b50712211d33ae9114631d $
49    * @since 5.0M2
50    */
 
51    public class DefaultTestDefinitionParser extends DefaultHandler implements ElementParser, TestDefinitionParser
52    {
53    /**
54    * Stack of entity factories.
55    * The top level represent the active factories. The key is the element name supported by the factory.
56    * The same factory may be registered for different element names. Only one factory can be registered
57    * per element, the last arrived win.
58    */
59    private Stack<Map<String, EntityFactory>> elementHandlers;
60   
61    /** The locator to retrieve the current parsing location when logging errors. */
62    private Locator locator;
63   
64    /** During a given parsing, the factory handling the root level. */
65    private TestWikisFactory handler;
66   
67    /** During a given parsing, the entity resolver that could be used by factories. */
68    private EntityReferenceResolver<String> resolver;
69   
70    /** During a given parsing, the entity serializer that could be used by factories. */
71    private EntityReferenceSerializer<String> serializer;
72   
73    /** Default constructor. */
 
74  15 toggle public DefaultTestDefinitionParser()
75    {
76    }
77   
 
78  15 toggle @Override
79    public TestDefinition parse(String filename,
80    EntityReferenceResolver<String> resolver,
81    EntityReferenceSerializer<String> serializer)
82    throws IOException, URISyntaxException, ParserConfigurationException, SAXException
83    {
84  15 URL url = ClassLoader.getSystemResource(filename);
85   
86  15 if (url == null) {
87  0 throw new FileNotFoundException(filename);
88    }
89   
90  15 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
91  15 SAXParser parser = parserFactory.newSAXParser();
92   
93  15 this.resolver = resolver;
94  15 this.serializer = serializer;
95  15 elementHandlers = new Stack<Map<String, EntityFactory>>();
96  15 elementHandlers.push(new HashMap<String, EntityFactory>());
97  15 handler = new TestWikisFactory();
98  15 register(handler);
99   
100  15 parser.parse(url.toURI().toString(), this);
101   
102  15 if (elementHandlers.size() > 1) {
103  0 throw new SAXException("Handlers stack as been corrupted while parsing.");
104    }
105   
106  15 elementHandlers = null;
107  15 locator = null;
108  15 this.resolver = null;
109  15 this.serializer = null;
110  15 TestDefinition wikis = getWikis();
111  15 handler = null;
112  15 return wikis;
113    }
114   
 
115  47 toggle @Override
116    public TestDefinition getWikis()
117    {
118  47 if (handler == null) {
119  0 throw new IllegalStateException("Wikis are only reachable during parsing");
120    }
121  47 return handler.getWikis();
122    }
123   
 
124  548 toggle @Override
125    public EntityReferenceResolver<String> getResolver()
126    {
127  548 return resolver;
128    }
129   
 
130  250 toggle @Override
131    public EntityReferenceSerializer<String> getSerializer()
132    {
133  250 return serializer;
134    }
135   
 
136  547 toggle @Override
137    public void register(EntityFactory handler) {
138  547 if (handler == null) {
139  0 return;
140    }
141  547 Map<String, EntityFactory> handlers = elementHandlers.peek();
142  547 for (String tagName : handler.getTagNames()) {
143  1377 handlers.put(tagName, handler);
144    }
145    }
146   
 
147  533 toggle @Override
148    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException
149    {
150  533 Map<String, EntityFactory> handlers = elementHandlers.peek();
151  533 elementHandlers.push(new HashMap<String, EntityFactory>());
152  533 EntityFactory factory = handlers.get(name);
153  533 if (factory == null) {
154  0 throw new SAXException(getLocatedMessage("No factory for element %s", name));
155    }
156  533 factory.newElement(this, name, attributes);
157    }
158   
 
159  533 toggle @Override
160    public void endElement(String uri, String localName, String name) throws SAXException
161    {
162  533 elementHandlers.pop();
163    }
164   
 
165  15 toggle @Override
166    public void setDocumentLocator(Locator locator)
167    {
168  15 super.setDocumentLocator(locator);
169   
170  15 this.locator = locator;
171    }
172   
173    /**
174    * @return the current parsing location in string format " (at line %d, column %d)".
175    */
 
176  0 toggle private String getCurrentLocation()
177    {
178  0 if (locator != null) {
179  0 return String.format(" (at line %d, column %d)", locator.getLineNumber(), locator.getColumnNumber());
180    } else {
181  0 return " (Unknown location)";
182    }
183    }
184   
 
185  0 toggle @Override
186    public String getLocatedMessage(String format, Object... objects)
187    {
188  0 return String.format(format, objects) + getCurrentLocation();
189    }
190   
 
191  0 toggle @Override
192    public void warning(SAXParseException e) throws SAXException
193    {
194  0 throw new SAXException(e.getLocalizedMessage(), e);
195    }
196   
 
197  0 toggle @Override
198    public void fatalError(SAXParseException e) throws SAXException
199    {
200  0 throw new SAXException(e.getLocalizedMessage(), e);
201    }
202   
 
203  0 toggle @Override
204    public void error(SAXParseException e) throws SAXException
205    {
206  0 throw new SAXException(e.getLocalizedMessage(), e);
207    }
208    }