source: josm/trunk/scripts/TaggingPresetSchemeWikiGenerator.java

Last change on this file was 19050, checked in by taylor.smock, 17 hours ago

Revert most var changes from r19048, fix most new compile warnings and checkstyle issues

Also, document why various ErrorProne checks were originally disabled and fix
generic SonarLint issues.

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