1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | import java.io.IOException;
|
---|
3 | import java.io.InputStream;
|
---|
4 | import java.util.Iterator;
|
---|
5 | import java.util.stream.IntStream;
|
---|
6 |
|
---|
7 | import javax.xml.XMLConstants;
|
---|
8 | import javax.xml.namespace.NamespaceContext;
|
---|
9 | import javax.xml.parsers.ParserConfigurationException;
|
---|
10 | import javax.xml.xpath.XPath;
|
---|
11 | import javax.xml.xpath.XPathConstants;
|
---|
12 | import javax.xml.xpath.XPathExpressionException;
|
---|
13 | import javax.xml.xpath.XPathFactory;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.preferences.JosmUrls;
|
---|
16 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
|
---|
17 | import org.openstreetmap.josm.io.CachedFile;
|
---|
18 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
19 | import org.openstreetmap.josm.tools.XmlUtils;
|
---|
20 | import org.w3c.dom.Document;
|
---|
21 | import org.w3c.dom.NodeList;
|
---|
22 | import org.xml.sax.SAXException;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * This script generates the wiki content for https://josm.openstreetmap.de/wiki/TaggingPresets#Attributes
|
---|
26 | */
|
---|
27 | public final class TaggingPresetSchemeWikiGenerator {
|
---|
28 |
|
---|
29 | private static Document document;
|
---|
30 | private static XPath xPath;
|
---|
31 |
|
---|
32 | private TaggingPresetSchemeWikiGenerator() {
|
---|
33 | // Hide public constructor for utility class
|
---|
34 | }
|
---|
35 |
|
---|
36 | public static void main(String[] args) throws Exception {
|
---|
37 | document = parseTaggingPresetSchema();
|
---|
38 | xPath = XPathFactory.newInstance().newXPath();
|
---|
39 | xPath.setNamespaceContext(new TaggingNamespaceContext());
|
---|
40 | printAttributes();
|
---|
41 | }
|
---|
42 |
|
---|
43 | private static Document parseTaggingPresetSchema() throws IOException, ParserConfigurationException, SAXException {
|
---|
44 | Config.setUrlsProvider(JosmUrls.getInstance());
|
---|
45 | Document document;
|
---|
46 | try (CachedFile file = new CachedFile(TaggingPresetReader.SCHEMA_SOURCE);
|
---|
47 | InputStream in = file.getInputStream()) {
|
---|
48 | document = XmlUtils.parseSafeDOM(in);
|
---|
49 | }
|
---|
50 | return document;
|
---|
51 | }
|
---|
52 |
|
---|
53 | private static void printAttributes() throws XPathExpressionException {
|
---|
54 | NodeList attributes = (NodeList) xPath.compile("/xs:schema/xs:attributeGroup/xs:attribute").evaluate(document, XPathConstants.NODESET);
|
---|
55 | System.out.println("=== Attributes ===");
|
---|
56 | System.out.println("The attributes of the tags have the following meaning:");
|
---|
57 | IntStream.range(0, attributes.getLength())
|
---|
58 | .mapToObj(attributes::item)
|
---|
59 | .forEach(node -> System.out.format(" `%s` (type: %s)%n %s%n",
|
---|
60 | node.getAttributes().getNamedItem("name").getTextContent(),
|
---|
61 | node.getAttributes().getNamedItem("type").getTextContent(),
|
---|
62 | node.getTextContent().trim()));
|
---|
63 | }
|
---|
64 |
|
---|
65 | private static class TaggingNamespaceContext implements NamespaceContext {
|
---|
66 | @Override
|
---|
67 | public String getNamespaceURI(String prefix) {
|
---|
68 | switch (prefix) {
|
---|
69 | case "tns":
|
---|
70 | return TaggingPresetReader.NAMESPACE;
|
---|
71 | case "xs":
|
---|
72 | return "http://www.w3.org/2001/XMLSchema";
|
---|
73 | default:
|
---|
74 | return XMLConstants.NULL_NS_URI;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public String getPrefix(String namespaceURI) {
|
---|
80 | throw new UnsupportedOperationException();
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public Iterator<String> getPrefixes(String namespaceURI) {
|
---|
85 | throw new UnsupportedOperationException();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|