1
2
3
4
5
6 package gov.nist.secauto.metaschema.schemagen.xml.impl;
7
8 import org.jdom2.Document;
9 import org.jdom2.Element;
10 import org.jdom2.JDOMException;
11 import org.jdom2.Namespace;
12 import org.jdom2.filter.Filters;
13 import org.jdom2.input.SAXBuilder;
14 import org.jdom2.xpath.XPathExpression;
15 import org.jdom2.xpath.XPathFactory;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.nio.file.Path;
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.stream.Collectors;
24
25 import edu.umd.cs.findbugs.annotations.NonNull;
26 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27
28 public class JDom2XmlSchemaLoader {
29 @NonNull
30 public static final String NS_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
31
32 @NonNull
33 private final Document document;
34
35 @SuppressWarnings("null")
36 @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields")
37 public JDom2XmlSchemaLoader(@NonNull Path path) throws JDOMException, IOException {
38 this(new SAXBuilder().build(path.toFile()));
39 }
40
41 @SuppressWarnings("null")
42 @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields")
43 public JDom2XmlSchemaLoader(@NonNull InputStream is) throws JDOMException, IOException {
44 this(new SAXBuilder().build(is));
45 }
46
47 @SuppressFBWarnings("EI_EXPOSE_REP2")
48 public JDom2XmlSchemaLoader(@NonNull Document document) {
49 this.document = document;
50 }
51
52 protected Document getNode() {
53 return document;
54 }
55
56 @SuppressWarnings("null")
57 @NonNull
58 public List<Element> getContent(
59 @NonNull String path,
60 @NonNull Map<String, String> prefixToNamespaceMap) {
61
62 Collection<Namespace> namespaces = prefixToNamespaceMap.entrySet().stream()
63 .map(entry -> Namespace.getNamespace(entry.getKey(), entry.getValue()))
64 .collect(Collectors.toList());
65 XPathExpression<Element> xpath = XPathFactory.instance().compile(path, Filters.element(), null, namespaces);
66 return xpath.evaluate(getNode());
67 }
68 }