Changeset 12510 in josm for trunk


Ignore:
Timestamp:
2017-07-25T21:38:14+02:00 (7 years ago)
Author:
Don-vip
Message:

avoid code duplication

Location:
trunk/src/org/openstreetmap/josm/io
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r11194 r12510  
    1010import java.net.URL;
    1111import java.util.List;
     12
     13import javax.xml.parsers.ParserConfigurationException;
    1214
    1315import org.openstreetmap.josm.Main;
     
    1921import org.openstreetmap.josm.io.auth.CredentialsManager;
    2022import org.openstreetmap.josm.tools.HttpClient;
     23import org.openstreetmap.josm.tools.Utils;
     24import org.openstreetmap.josm.tools.XmlParsingException;
     25import org.w3c.dom.Document;
     26import org.w3c.dom.Node;
     27import org.xml.sax.SAXException;
    2128
    2229/**
     
    360367        return null;
    361368    }
     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    }
    362425}
  • trunk/src/org/openstreetmap/josm/io/OsmServerUserInfoReader.java

    r12470 r12510  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.IOException;
    7 import java.io.InputStream;
    86import java.util.LinkedList;
    97import java.util.List;
    108
    11 import javax.xml.parsers.ParserConfigurationException;
    129import javax.xml.xpath.XPath;
    1310import javax.xml.xpath.XPathConstants;
     
    1916import org.openstreetmap.josm.data.osm.UserInfo;
    2017import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    21 import org.openstreetmap.josm.tools.Utils;
    2218import org.openstreetmap.josm.tools.XmlParsingException;
    2319import org.openstreetmap.josm.tools.date.DateUtils;
     
    2521import org.w3c.dom.Node;
    2622import org.w3c.dom.NodeList;
    27 import org.xml.sax.SAXException;
    2823
    2924/**
     
    3227 */
    3328public class OsmServerUserInfoReader extends OsmServerReader {
    34 
    35     protected static String getAttribute(Node node, String name) {
    36         return node.getAttributes().getNamedItem(name).getNodeValue();
    37     }
    3829
    3930    /**
     
    175166     */
    176167    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);
    190170    }
    191171}
  • trunk/src/org/openstreetmap/josm/io/OsmServerUserPreferencesReader.java

    r12502 r12510  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.IOException;
    7 import java.io.InputStream;
    86import java.util.HashMap;
    97import java.util.Map;
    108
    11 import javax.xml.parsers.ParserConfigurationException;
    129import javax.xml.xpath.XPath;
    1310import javax.xml.xpath.XPathConstants;
     
    1714import org.openstreetmap.josm.data.osm.DataSet;
    1815import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    19 import org.openstreetmap.josm.tools.Utils;
    2016import org.openstreetmap.josm.tools.XmlParsingException;
    2117import org.w3c.dom.Document;
    2218import org.w3c.dom.Node;
    2319import org.w3c.dom.NodeList;
    24 import org.xml.sax.SAXException;
    2520
    2621/**
     
    3025 */
    3126public class OsmServerUserPreferencesReader extends OsmServerReader {
    32 
    33     protected static String getAttribute(Node node, String name) {
    34         return node.getAttributes().getNamedItem(name).getNodeValue();
    35     }
    3627
    3728    /**
     
    9889     */
    9990    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);
    11393    }
    11494}
Note: See TracChangeset for help on using the changeset viewer.