source: josm/trunk/scripts/TaggingPresetSchemeWikiGenerator.java@ 17535

Last change on this file since 17535 was 16644, checked in by simon04, 4 years ago

Checkstyle

File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2import java.io.IOException;
3import java.io.InputStream;
4import java.util.Iterator;
5import java.util.stream.IntStream;
6
7import javax.xml.XMLConstants;
8import javax.xml.namespace.NamespaceContext;
9import javax.xml.parsers.ParserConfigurationException;
10import javax.xml.xpath.XPath;
11import javax.xml.xpath.XPathConstants;
12import javax.xml.xpath.XPathExpressionException;
13import javax.xml.xpath.XPathFactory;
14
15import org.openstreetmap.josm.data.preferences.JosmUrls;
16import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
17import org.openstreetmap.josm.io.CachedFile;
18import org.openstreetmap.josm.spi.preferences.Config;
19import org.openstreetmap.josm.tools.XmlUtils;
20import org.w3c.dom.Document;
21import org.w3c.dom.NodeList;
22import org.xml.sax.SAXException;
23
24/**
25 * This script generates the wiki content for https://josm.openstreetmap.de/wiki/TaggingPresets#Attributes
26 */
27public 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}
Note: See TracBrowser for help on using the repository browser.