- Timestamp:
- 2017-07-25T21:38:14+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/io
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r11194 r12510 10 10 import java.net.URL; 11 11 import java.util.List; 12 13 import javax.xml.parsers.ParserConfigurationException; 12 14 13 15 import org.openstreetmap.josm.Main; … … 19 21 import org.openstreetmap.josm.io.auth.CredentialsManager; 20 22 import org.openstreetmap.josm.tools.HttpClient; 23 import org.openstreetmap.josm.tools.Utils; 24 import org.openstreetmap.josm.tools.XmlParsingException; 25 import org.w3c.dom.Document; 26 import org.w3c.dom.Node; 27 import org.xml.sax.SAXException; 21 28 22 29 /** … … 360 367 return null; 361 368 } 369 370 /** 371 * Returns an attribute from the given DOM node. 372 * @param node DOM node 373 * @param name attribute name 374 * @return attribute value for the given attribute 375 * @since 12510 376 */ 377 protected static String getAttribute(Node node, String name) { 378 return node.getAttributes().getNamedItem(name).getNodeValue(); 379 } 380 381 /** 382 * DOM document parser. 383 * @param <R> resulting type 384 * @since 12510 385 */ 386 @FunctionalInterface 387 protected interface DomParser<R> { 388 /** 389 * Parses a given DOM document. 390 * @param doc DOM document 391 * @return parsed data 392 * @throws XmlParsingException if an XML parsing error occurs 393 */ 394 R parse(Document doc) throws XmlParsingException; 395 } 396 397 /** 398 * Fetches generic data from the DOM document resulting an API call. 399 * @param api the OSM API call 400 * @param subtask the subtask translated message 401 * @param parser the parser converting the DOM document (OSM API result) 402 * @param <T> data type 403 * @param monitor The progress monitor 404 * @param reason The reason to show on console. Can be {@code null} if no reason is given 405 * @return The converted data 406 * @throws OsmTransferException if something goes wrong 407 * @since 12510 408 */ 409 public <T> T fetchData(String api, String subtask, DomParser<T> parser, ProgressMonitor monitor, String reason) 410 throws OsmTransferException { 411 try { 412 monitor.beginTask(""); 413 monitor.indeterminateSubTask(subtask); 414 try (InputStream in = getInputStream(api, monitor.createSubTaskMonitor(1, true), reason)) { 415 return parser.parse(Utils.parseSafeDOM(in)); 416 } 417 } catch (OsmTransferException e) { 418 throw e; 419 } catch (IOException | ParserConfigurationException | SAXException e) { 420 throw new OsmTransferException(e); 421 } finally { 422 monitor.finishTask(); 423 } 424 } 362 425 } -
trunk/src/org/openstreetmap/josm/io/OsmServerUserInfoReader.java
r12470 r12510 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.io.IOException;7 import java.io.InputStream;8 6 import java.util.LinkedList; 9 7 import java.util.List; 10 8 11 import javax.xml.parsers.ParserConfigurationException;12 9 import javax.xml.xpath.XPath; 13 10 import javax.xml.xpath.XPathConstants; … … 19 16 import org.openstreetmap.josm.data.osm.UserInfo; 20 17 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 21 import org.openstreetmap.josm.tools.Utils;22 18 import org.openstreetmap.josm.tools.XmlParsingException; 23 19 import org.openstreetmap.josm.tools.date.DateUtils; … … 25 21 import org.w3c.dom.Node; 26 22 import org.w3c.dom.NodeList; 27 import org.xml.sax.SAXException;28 23 29 24 /** … … 32 27 */ 33 28 public class OsmServerUserInfoReader extends OsmServerReader { 34 35 protected static String getAttribute(Node node, String name) {36 return node.getAttributes().getNamedItem(name).getNodeValue();37 }38 29 39 30 /** … … 175 166 */ 176 167 public UserInfo fetchUserInfo(ProgressMonitor monitor, String reason) throws OsmTransferException { 177 try { 178 monitor.beginTask(""); 179 monitor.indeterminateSubTask(tr("Reading user info ...")); 180 try (InputStream in = getInputStream("user/details", monitor.createSubTaskMonitor(1, true), reason)) { 181 return buildFromXML(Utils.parseSafeDOM(in)); 182 } 183 } catch (OsmTransferException e) { 184 throw e; 185 } catch (IOException | ParserConfigurationException | SAXException e) { 186 throw new OsmTransferException(e); 187 } finally { 188 monitor.finishTask(); 189 } 168 return fetchData("user/details", tr("Reading user info ..."), 169 OsmServerUserInfoReader::buildFromXML, monitor, reason); 190 170 } 191 171 } -
trunk/src/org/openstreetmap/josm/io/OsmServerUserPreferencesReader.java
r12502 r12510 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.io.IOException;7 import java.io.InputStream;8 6 import java.util.HashMap; 9 7 import java.util.Map; 10 8 11 import javax.xml.parsers.ParserConfigurationException;12 9 import javax.xml.xpath.XPath; 13 10 import javax.xml.xpath.XPathConstants; … … 17 14 import org.openstreetmap.josm.data.osm.DataSet; 18 15 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 19 import org.openstreetmap.josm.tools.Utils;20 16 import org.openstreetmap.josm.tools.XmlParsingException; 21 17 import org.w3c.dom.Document; 22 18 import org.w3c.dom.Node; 23 19 import org.w3c.dom.NodeList; 24 import org.xml.sax.SAXException;25 20 26 21 /** … … 30 25 */ 31 26 public class OsmServerUserPreferencesReader extends OsmServerReader { 32 33 protected static String getAttribute(Node node, String name) {34 return node.getAttributes().getNamedItem(name).getNodeValue();35 }36 27 37 28 /** … … 98 89 */ 99 90 public Map<String, String> fetchUserPreferences(ProgressMonitor monitor, String reason) throws OsmTransferException { 100 try { 101 monitor.beginTask(""); 102 monitor.indeterminateSubTask(tr("Reading user preferences ...")); 103 try (InputStream in = getInputStream("user/preferences", monitor.createSubTaskMonitor(1, true), reason)) { 104 return buildFromXML(Utils.parseSafeDOM(in)); 105 } 106 } catch (OsmTransferException e) { 107 throw e; 108 } catch (IOException | ParserConfigurationException | SAXException e) { 109 throw new OsmTransferException(e); 110 } finally { 111 monitor.finishTask(); 112 } 91 return fetchData("user/preferences", tr("Reading user preferences ..."), 92 OsmServerUserPreferencesReader::buildFromXML, monitor, reason); 113 93 } 114 94 }
Note:
See TracChangeset
for help on using the changeset viewer.