1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau;
18
19 import static javax.xml.stream.XMLStreamConstants.*;
20
21 import java.io.*;
22 import java.lang.reflect.*;
23 import java.util.*;
24
25 import javax.xml.stream.*;
26
27 import org.apache.juneau.parser.*;
28 import org.apache.juneau.xml.*;
29
30
31
32
33
34
35
36
37 @SuppressWarnings("unchecked")
38 public class XmlValidatorParser extends XmlParser {
39
40
41
42
43
44 public XmlValidatorParser() {
45 super(create());
46 }
47
48 @Override
49 public XmlParserSession.Builder createSession() {
50 return new XmlParserSession.Builder(XmlParser.DEFAULT) {
51 @Override
52 public XmlParserSession build() {
53 return new XmlParserSession(this) {
54
55 @Override
56 protected <T> T doParse(ParserPipe pipe, ClassMeta<T> type) throws IOException, ParseException, ExecutableException {
57 try {
58 return type.cast(validate(pipe.getReader()));
59 } catch (Exception e) {
60 throw new ParseException(e);
61 }
62 }
63
64 @Override
65 protected <K,V> Map<K,V> doParseIntoMap(ParserPipe pipe, Map<K,V> m, Type keyType, Type valueType) throws Exception {
66 return (Map<K,V>)validate(pipe.getReader());
67 }
68
69 @Override
70 protected <E> Collection<E> doParseIntoCollection(ParserPipe pipe, Collection<E> c, Type elementType) throws Exception {
71 return (Collection<E>)validate(pipe.getReader());
72 }
73 };
74 }
75 };
76 }
77
78 public <T> T validate(Reader r) throws Exception {
79 var sr = getStaxReader(r);
80 while (sr.next() != END_DOCUMENT) {}
81 return null;
82 }
83
84 protected XMLStreamReader getStaxReader(Reader in) throws Exception {
85 var factory = XMLInputFactory.newInstance();
86 factory.setProperty("javax.xml.stream.isNamespaceAware", false);
87 var parser = factory.createXMLStreamReader(in);
88 parser.nextTag();
89 return parser;
90 }
91 }