source: osm/applications/editors/josm/plugins/CommandLine/src/org/openstreetmap/josm/plugins/commandline/Loader.java@ 34574

Last change on this file since 34574 was 34574, checked in by donvip, 6 years ago

rename package

File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins.commandline;
3
4import java.io.File;
5import java.util.ArrayList;
6
7import javax.xml.parsers.SAXParser;
8import javax.xml.parsers.SAXParserFactory;
9
10import org.openstreetmap.josm.tools.Logging;
11import org.xml.sax.Attributes;
12import org.xml.sax.SAXException;
13import org.xml.sax.SAXParseException;
14import org.xml.sax.helpers.DefaultHandler;
15
16public class Loader extends DefaultHandler {
17 private final File dirToScan;
18 private String currentFile; // For debug XML-files
19 private String currentTag;
20 private Command currentCommand;
21 private Parameter currentParameter;
22 private final ArrayList<Command> loadingCommands;
23
24 public Loader(File dir) {
25 dirToScan = dir;
26 currentTag = "";
27 loadingCommands = new ArrayList<>();
28 }
29
30 public ArrayList<Command> load() {
31 try {
32 // Creating parser
33 SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
34
35 // Files loading
36 String[] list = dirToScan.list();
37 if (list != null) {
38 for (int i = 0; i < list.length; i++) {
39 if (list[i].endsWith(".xml")) {
40 currentFile = dirToScan.getPath() + "/" + list[i];
41 loadFile(sp, currentFile);
42 }
43 }
44 }
45 } catch (Exception e) {
46 Logging.error(e);
47 }
48 return loadingCommands;
49 }
50
51 private void loadFile(SAXParser parser, String fileName) {
52 try {
53 String a = new File(fileName).toURI().toString().replace("file:/", "file:///");
54 Logging.info(a);
55 parser.parse(a, this);
56 } catch (Exception e) {
57 Logging.error(e);
58 }
59 // TODO: Create links for each argument
60 }
61
62 @Override
63 public void startElement(String namespaceURI, String localName, String rawName, Attributes attrs) {
64 int len = attrs.getLength();
65 String Name, Value;
66 currentTag = rawName;
67
68 if (rawName.equals("command")) {
69 currentCommand = new Command();
70 for (int i = 0; i < len; i++) {
71 Name = attrs.getQName(i);
72 Value = attrs.getValue(i);
73 if (Name.equals("name"))
74 currentCommand.name = Value;
75 else if (Name.equals("run"))
76 currentCommand.run = Value;
77 else if (Name.equals("tracks")) {
78 if (Value.equals("bbox"))
79 currentCommand.tracks = true;
80 } else if (Name.equals("icon")) {
81 currentCommand.icon = Value;
82 } else if (Name.equals("asynchronous")) {
83 currentCommand.asynchronous = Value.equals("true") ? true : false;
84 }
85 }
86 } else if (rawName.equals("parameter")) {
87 currentParameter = new Parameter();
88 for (int i = 0; i < len; i++) {
89 Name = attrs.getQName(i);
90 Value = attrs.getValue(i);
91 if (Name.equals("required")) {
92 currentParameter.required = Value.equals("true") ? true : false;
93 } else if (Name.equals("type")) {
94 if (Value.equals("node")) currentParameter.type = Type.NODE;
95 else if (Value.equals("way")) currentParameter.type = Type.WAY;
96 else if (Value.equals("relation")) currentParameter.type = Type.RELATION;
97 else if (Value.equals("point")) currentParameter.type = Type.POINT;
98 else if (Value.equals("length")) currentParameter.type = Type.LENGTH;
99 else if (Value.equals("natural")) currentParameter.type = Type.NATURAL;
100 else if (Value.equals("any")) currentParameter.type = Type.ANY;
101 else if (Value.equals("string")) currentParameter.type = Type.STRING;
102 else if (Value.equals("relay")) currentParameter.type = Type.RELAY;
103 else if (Value.equals("username")) currentParameter.type = Type.USERNAME;
104 else if (Value.equals("imageryurl")) currentParameter.type = Type.IMAGERYURL;
105 else if (Value.equals("imageryoffset")) currentParameter.type = Type.IMAGERYOFFSET;
106 } else if (Name.equals("maxinstances")) {
107 currentParameter.maxInstances = Integer.parseInt(Value);
108 } else if (Name.equals("maxvalue")) {
109 currentParameter.maxVal = Float.parseFloat(Value);
110 } else if (Name.equals("minvalue")) {
111 currentParameter.minVal = Float.parseFloat(Value);
112 }
113 }
114 }
115 }
116
117 @Override
118 public void characters(char[] ch, int start, int length) {
119 String text = (new String(ch, start, length)).trim();
120 if (currentParameter != null) {
121 if (currentTag.equals("name")) {
122 currentParameter.name = text;
123 } else if (currentTag.equals("description")) {
124 currentParameter.description = text;
125 } else if (currentTag.equals("value")) {
126 if (currentParameter.type == Type.RELAY) {
127 if (!(currentParameter.getRawValue() instanceof Relay))
128 currentParameter.setValue(new Relay());
129 ((Relay) currentParameter.getRawValue()).addValue(text);
130 } else {
131 currentParameter.setValue(text);
132 }
133 }
134 }
135 }
136
137 @Override
138 public void endElement(String namespaceURI, String localName, String rawName) {
139 if (rawName.equals("command")) {
140 loadingCommands.add(currentCommand);
141 currentCommand = null;
142 } else if (rawName.equals("parameter")) {
143 if (currentParameter.required)
144 currentCommand.parameters.add(currentParameter);
145 else
146 currentCommand.optParameters.add(currentParameter);
147 currentParameter = null;
148 } else {
149 currentTag = "";
150 }
151 }
152
153 @Override
154 public void warning(SAXParseException ex) {
155 Logging.warn("Warning in command xml file " + currentFile + ": " + ex.getMessage());
156 }
157
158 @Override
159 public void error(SAXParseException ex) {
160 Logging.error("Error in command xml file " + currentFile + ": " + ex.getMessage());
161 }
162
163 @Override
164 public void fatalError(SAXParseException ex) throws SAXException {
165 Logging.error("Error in command xml file " + currentFile + ": " + ex.getMessage());
166 throw ex;
167 }
168}
Note: See TracBrowser for help on using the repository browser.