Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BaseVariantXMLSource |
|
| 2.4;2.4 |
1 | /* Copyright © 2013 Matthew Champion | |
2 | All rights reserved. | |
3 | ||
4 | Redistribution and use in source and binary forms, with or without | |
5 | modification, are permitted provided that the following conditions are met: | |
6 | * Redistributions of source code must retain the above copyright | |
7 | notice, this list of conditions and the following disclaimer. | |
8 | * Redistributions in binary form must reproduce the above copyright | |
9 | notice, this list of conditions and the following disclaimer in the | |
10 | documentation and/or other materials provided with the distribution. | |
11 | * Neither the name of mattunderscore.com nor the | |
12 | names of its contributors may be used to endorse or promote products | |
13 | derived from this software without specific prior written permission. | |
14 | ||
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
18 | DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY | |
19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | |
25 | ||
26 | package com.mattunderscore.filter.contentnegotiation.variantsource; | |
27 | ||
28 | import java.io.InputStream; | |
29 | import java.util.List; | |
30 | import java.util.logging.Level; | |
31 | import java.util.logging.Logger; | |
32 | ||
33 | import javax.xml.parsers.SAXParser; | |
34 | import javax.xml.parsers.SAXParserFactory; | |
35 | import javax.xml.validation.Schema; | |
36 | ||
37 | import com.mattunderscore.filter.contentnegotiation.parser.Variant; | |
38 | ||
39 | /** | |
40 | * A variant source that parses XML for variants. It uses SAX to parse the XML. The XML is passed in | |
41 | * through an input stream. A SAX handler to use can also be passed in. By default it uses the | |
42 | * {@link VariantXMLParser} class. This class will try to validate the XML file against the schema. | |
43 | * | |
44 | * @author Matt Champion | |
45 | * @since 0.2.0 | |
46 | */ | |
47 | public class BaseVariantXMLSource implements InputStreamVariantSource | |
48 | { | |
49 | 4 | private static final Logger log = Logger.getLogger(FilterVariantXMLSource.class |
50 | 2 | .getCanonicalName()); |
51 | ||
52 | private InputStream inputStream; | |
53 | private VariantSAXHandler parser; | |
54 | ||
55 | public BaseVariantXMLSource() | |
56 | { | |
57 | 32 | this(new VariantXMLParser()); |
58 | 32 | } |
59 | ||
60 | public BaseVariantXMLSource(final VariantSAXHandler parser) | |
61 | 32 | { |
62 | 32 | this.parser = parser; |
63 | 32 | } |
64 | ||
65 | /** | |
66 | * Setter for the SAX handler. | |
67 | * | |
68 | * @param handler | |
69 | * The SAX handler | |
70 | * @since 0.2.0 | |
71 | */ | |
72 | public void setHandler(final VariantSAXHandler handler) | |
73 | { | |
74 | 2 | this.parser = handler; |
75 | 2 | } |
76 | ||
77 | @Override | |
78 | public List<Variant> getVariants() throws VariantSourceException | |
79 | { | |
80 | 24 | if (inputStream == null) |
81 | { | |
82 | 2 | throw new VariantSourceConfigurationException("No input stream has been set"); |
83 | } | |
84 | else | |
85 | { | |
86 | try | |
87 | { | |
88 | 22 | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
89 | try | |
90 | { | |
91 | 22 | final Schema schema = parser.getSchema(); |
92 | 22 | if (schema != null) |
93 | { | |
94 | 22 | factory.setValidating(false); |
95 | 22 | factory.setNamespaceAware(true); |
96 | 22 | factory.setSchema(schema); |
97 | } | |
98 | else | |
99 | { | |
100 | 0 | log.log(Level.FINE, "XML Validation not required"); |
101 | } | |
102 | } | |
103 | 0 | catch (final NoSchemaException exception) |
104 | { | |
105 | 0 | log.log(Level.FINE, "XML Validation not required"); |
106 | 22 | } |
107 | 22 | final SAXParser saxParser = factory.newSAXParser(); |
108 | 22 | saxParser.parse(inputStream, parser); |
109 | 18 | return parser.getVariants(); |
110 | } | |
111 | 4 | catch (final Throwable throwable) |
112 | { | |
113 | 4 | log.log(Level.WARNING, "BaseVariantXMLSource unable to return variants.", |
114 | throwable); | |
115 | 4 | throw new VariantSourceException("BaseVariantXMLSource unable to return variants.", |
116 | throwable); | |
117 | } | |
118 | } | |
119 | } | |
120 | ||
121 | @Override | |
122 | public void setInputStream(final InputStream inputStream) | |
123 | { | |
124 | 22 | this.inputStream = inputStream; |
125 | 22 | } |
126 | } |