| 1 |
|
| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
public final class TaggingPresetSchemeWikiGenerator { |
| 29 |
|
| 30 |
private static Document document; |
| 31 |
private static XPath xPath; |
| 32 |
|
| 33 |
private TaggingPresetSchemeWikiGenerator() { |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException, XPathExpressionException { |
| 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 |
} |