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

Last change on this file since 16640 was 16640, checked in by simon04, 5 years ago

see #19282 - Add TaggingPresetSchemeWikiGenerator to generate wiki content from tagging-preset.xsd

File size: 3.2 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 class TaggingPresetSchemeWikiGenerator {
28
29 private static Document document;
30 private static XPath xPath;
31
32 public static void main(String[] args) throws Exception {
33 document = parseTaggingPresetSchema();
34 xPath = XPathFactory.newInstance().newXPath();
35 xPath.setNamespaceContext(new TaggingNamespaceContext());
36 printAttributes();
37 }
38
39 private static Document parseTaggingPresetSchema() throws IOException, ParserConfigurationException, SAXException {
40 Config.setUrlsProvider(JosmUrls.getInstance());
41 Document document;
42 try (CachedFile file = new CachedFile(TaggingPresetReader.SCHEMA_SOURCE);
43 InputStream in = file.getInputStream()) {
44 document = XmlUtils.parseSafeDOM(in);
45 }
46 return document;
47 }
48
49 private static void printAttributes() throws XPathExpressionException {
50 NodeList attributes = (NodeList) xPath.compile("/xs:schema/xs:attributeGroup/xs:attribute").evaluate(document, XPathConstants.NODESET);
51 System.out.println("=== Attributes ===");
52 System.out.println("The attributes of the tags have the following meaning:");
53 IntStream.range(0, attributes.getLength())
54 .mapToObj(attributes::item)
55 .forEach(node -> System.out.format(" `%s` (type: %s)%n %s%n",
56 node.getAttributes().getNamedItem("name").getTextContent(),
57 node.getAttributes().getNamedItem("type").getTextContent(),
58 node.getTextContent().trim()));
59 }
60
61 private static class TaggingNamespaceContext implements NamespaceContext {
62 @Override
63 public String getNamespaceURI(String prefix) {
64 switch (prefix) {
65 case "tns":
66 return TaggingPresetReader.NAMESPACE;
67 case "xs":
68 return "http://www.w3.org/2001/XMLSchema";
69 default:
70 return XMLConstants.NULL_NS_URI;
71 }
72 }
73
74 @Override
75 public String getPrefix(String namespaceURI) {
76 throw new UnsupportedOperationException();
77 }
78
79 @Override
80 public Iterator<String> getPrefixes(String namespaceURI) {
81 throw new UnsupportedOperationException();
82 }
83 }
84}
Note: See TracBrowser for help on using the repository browser.