1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
package com.mattunderscore.filter.contentnegotiation.variantsource; |
27 | |
|
28 | |
import java.io.InputStream; |
29 | |
import java.util.ArrayList; |
30 | |
import java.util.List; |
31 | |
import java.util.Stack; |
32 | |
|
33 | |
import javax.xml.XMLConstants; |
34 | |
import javax.xml.transform.stream.StreamSource; |
35 | |
import javax.xml.validation.Schema; |
36 | |
import javax.xml.validation.SchemaFactory; |
37 | |
|
38 | |
import org.xml.sax.Attributes; |
39 | |
import org.xml.sax.SAXException; |
40 | |
import org.xml.sax.SAXParseException; |
41 | |
|
42 | |
import com.mattunderscore.filter.contentnegotiation.parser.Header; |
43 | |
import com.mattunderscore.filter.contentnegotiation.parser.HeaderPart; |
44 | |
import com.mattunderscore.filter.contentnegotiation.parser.Output; |
45 | |
import com.mattunderscore.filter.contentnegotiation.parser.Redirect; |
46 | |
import com.mattunderscore.filter.contentnegotiation.parser.Variant; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public final class VariantXMLParser extends VariantSAXHandler |
59 | |
{ |
60 | 34 | private Stack<StringBuilder> elementCharacterData = new Stack<StringBuilder>(); |
61 | |
private List<Variant> variants; |
62 | |
private Variant currentVariant; |
63 | |
private Output currentOutput; |
64 | |
private Header currentHeader; |
65 | |
private Redirect currentRedirect; |
66 | |
private HeaderPart currentHeaderPart; |
67 | |
private StringBuilder tempString; |
68 | |
private List<HeaderPart> currentHeaderParts; |
69 | |
|
70 | |
public VariantXMLParser() |
71 | 34 | { |
72 | 34 | } |
73 | |
|
74 | |
@Override |
75 | |
public void startElement(String s, String s1, String elementName, Attributes attributes) |
76 | |
throws SAXException |
77 | |
{ |
78 | 282 | tempString = new StringBuilder(50); |
79 | 282 | elementCharacterData.push(tempString); |
80 | 282 | if ("variants".equals(elementName)) |
81 | |
{ |
82 | 20 | variants = new ArrayList<Variant>(); |
83 | |
} |
84 | 262 | else if ("variant".equals(elementName)) |
85 | |
{ |
86 | 34 | currentVariant = new Variant(); |
87 | |
} |
88 | 228 | else if ("output".equals(elementName)) |
89 | |
{ |
90 | 34 | currentOutput = new Output(); |
91 | |
} |
92 | 194 | else if ("header".equals(elementName)) |
93 | |
{ |
94 | 4 | currentHeader = new Header(); |
95 | 4 | currentHeaderParts = new ArrayList<HeaderPart>(); |
96 | |
} |
97 | 190 | else if ("redirect".equals(elementName)) |
98 | |
{ |
99 | 18 | currentRedirect = new Redirect(); |
100 | |
} |
101 | 172 | else if ("fail".equals(elementName)) |
102 | |
{ |
103 | 8 | currentOutput.setFail(true); |
104 | |
} |
105 | 164 | else if ("part".equals(elementName)) |
106 | |
{ |
107 | 6 | currentHeaderPart = new HeaderPart(); |
108 | |
} |
109 | 282 | } |
110 | |
|
111 | |
@Override |
112 | |
public void endElement(String prefix, String localName, String qualifiedName) |
113 | |
throws SAXException |
114 | |
{ |
115 | 280 | if ("variant".equals(qualifiedName)) |
116 | |
{ |
117 | 34 | variants.add(currentVariant); |
118 | |
} |
119 | 246 | else if ("urlPattern".equals(qualifiedName)) |
120 | |
{ |
121 | 34 | currentVariant.setUrlPattern(tempString.toString()); |
122 | |
} |
123 | 212 | else if ("content-type".equals(qualifiedName)) |
124 | |
{ |
125 | 34 | currentVariant.setContentType(tempString.toString()); |
126 | |
} |
127 | 178 | else if ("language".equals(qualifiedName)) |
128 | |
{ |
129 | 34 | currentVariant.setLanguage(tempString.toString()); |
130 | |
} |
131 | 144 | else if ("output".equals(qualifiedName)) |
132 | |
{ |
133 | 34 | currentVariant.setOutput(currentOutput); |
134 | |
} |
135 | 110 | else if ("forward".equals(qualifiedName)) |
136 | |
{ |
137 | 4 | currentOutput.setForward(tempString.toString()); |
138 | |
} |
139 | 106 | else if ("redirect".equals(qualifiedName)) |
140 | |
{ |
141 | 18 | currentOutput.setRedirect(currentRedirect); |
142 | |
} |
143 | 88 | else if ("header".equals(qualifiedName)) |
144 | |
{ |
145 | 4 | currentHeader.setPart(currentHeaderParts); |
146 | 4 | currentOutput.setHeader(currentHeader); |
147 | |
} |
148 | 84 | else if ("fail".equals(qualifiedName)) |
149 | |
{ |
150 | 8 | currentOutput.setFail(true); |
151 | |
} |
152 | 76 | else if ("part".equals(qualifiedName)) |
153 | |
{ |
154 | 6 | currentHeaderParts.add(currentHeaderPart); |
155 | |
} |
156 | 70 | else if ("code".equals(qualifiedName)) |
157 | |
{ |
158 | 22 | int code = Integer.parseInt(tempString.toString()); |
159 | 22 | if (currentRedirect != null) |
160 | |
{ |
161 | 18 | currentRedirect.setCode(code); |
162 | |
} |
163 | 4 | else if (currentHeader != null) |
164 | |
{ |
165 | 4 | currentHeader.setCode(code); |
166 | |
} |
167 | 22 | } |
168 | 48 | else if ("location".equals(qualifiedName)) |
169 | |
{ |
170 | 18 | currentRedirect.setLocation(tempString.toString()); |
171 | |
} |
172 | 30 | else if ("name".equals(qualifiedName)) |
173 | |
{ |
174 | 6 | currentHeaderPart.setName(tempString.toString()); |
175 | |
} |
176 | 24 | else if ("value".equals(qualifiedName)) |
177 | |
{ |
178 | 6 | currentHeaderPart.setValue(tempString.toString()); |
179 | |
} |
180 | 280 | elementCharacterData.pop(); |
181 | 280 | if (!elementCharacterData.isEmpty()) |
182 | |
{ |
183 | 262 | tempString = elementCharacterData.peek(); |
184 | |
} |
185 | 280 | } |
186 | |
|
187 | |
@Override |
188 | |
public void characters(char[] chars, int start, int length) |
189 | |
{ |
190 | 166 | tempString.append(new String(chars, start, length)); |
191 | 166 | } |
192 | |
|
193 | |
@Override |
194 | |
public List<Variant> getVariants() |
195 | |
{ |
196 | 18 | return variants; |
197 | |
} |
198 | |
|
199 | |
@Override |
200 | |
public void error(SAXParseException exception) throws SAXParseException |
201 | |
{ |
202 | 2 | throw exception; |
203 | |
} |
204 | |
|
205 | |
@Override |
206 | |
public void fatalError(SAXParseException exception) throws SAXParseException |
207 | |
{ |
208 | 2 | throw exception; |
209 | |
} |
210 | |
|
211 | |
@Override |
212 | |
public void warning(SAXParseException exception) throws SAXParseException |
213 | |
{ |
214 | 0 | throw exception; |
215 | |
} |
216 | |
|
217 | |
@Override |
218 | |
public Schema getSchema() throws NoSchemaException |
219 | |
{ |
220 | |
try |
221 | |
{ |
222 | 22 | InputStream inputStream = VariantXMLParser.class |
223 | 22 | .getResourceAsStream("/negotiation.xsd"); |
224 | 22 | StreamSource source = new StreamSource(inputStream); |
225 | 22 | SchemaFactory schemaFactory = SchemaFactory |
226 | 22 | .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
227 | 22 | return schemaFactory.newSchema(source); |
228 | |
} |
229 | 0 | catch (SAXException exception) |
230 | |
{ |
231 | 0 | throw new NoSchemaException(exception); |
232 | |
} |
233 | |
} |
234 | |
} |