Ignore:
Timestamp:
2014-08-31T01:30:58+02:00 (6 years ago)
Author:
Don-vip
Message:

see #10441 - send capabilities request instead of changesets request when testing API url

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

Legend:

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

    r7012 r7473  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.io.IOException;
    67import java.util.ArrayList;
    78import java.util.Collections;
     
    1011import java.util.Map;
    1112
     13import javax.xml.parsers.ParserConfigurationException;
     14import javax.xml.parsers.SAXParserFactory;
     15
    1216import org.openstreetmap.josm.Main;
     17import org.xml.sax.Attributes;
     18import org.xml.sax.InputSource;
     19import org.xml.sax.SAXException;
     20import org.xml.sax.helpers.DefaultHandler;
    1321
    1422/**
    15  * Represents the server capabilities
     23 * Represents the OSM API server capabilities.
    1624 *
    1725 * Example capabilites document:
     
    4351public class Capabilities {
    4452
    45     private Map<String, HashMap<String,String>> capabilities;
    46     private List<String> imageryBlacklist;
     53    private final Map<String, HashMap<String,String>> capabilities;
     54    private final List<String> imageryBlacklist;
    4755
    4856    /**
     
    5058     */
    5159    public Capabilities() {
    52         clear();
    53     }
    54 
     60        capabilities = new HashMap<>();
     61        imageryBlacklist = new ArrayList<>();
     62    }
     63
     64    /**
     65     * Determines if given element and attribute are defined.
     66     *
     67     * @param element the name of the element
     68     * @param attribute the name of the attribute
     69     * @return {@code true} if defined, {@code false} otherwise
     70     */
    5571    public boolean isDefined(String element, String attribute) {
    5672        if (! capabilities.containsKey(element)) return false;
     
    6076    }
    6177
    62     public String get(String element, String attribute ) {
     78    /**
     79     * Returns the value of configuration item in the capabilities as string value.
     80     *
     81     * @param element the name of the element
     82     * @param attribute the name of the attribute
     83     * @return the value; {@code null}, if the respective configuration item does not exist
     84     */
     85    public String get(String element, String attribute) {
    6386        if (! capabilities.containsKey(element)) return null;
    6487        HashMap<String, String> e = capabilities.get(element);
     
    6891
    6992    /**
    70      * returns the value of configuration item in the capabilities as
    71      * double value
    72      *
    73      * @param element  the name of the element
    74      * @param attribute the name of the attribute
    75      * @return the value; null, if the respective configuration item doesn't exist
    76      * @throws NumberFormatException  if the value is not a valid double
     93     * Returns the value of configuration item in the capabilities as double value.
     94     *
     95     * @param element the name of the element
     96     * @param attribute the name of the attribute
     97     * @return the value; {@code null}, if the respective configuration item does not exist
     98     * @throws NumberFormatException if the value is not a valid double
    7799     */
    78100    public Double getDouble(String element, String attribute) throws NumberFormatException {
     
    82104    }
    83105
     106    /**
     107     * Returns the value of configuration item in the capabilities as long value.
     108     *
     109     * @param element the name of the element
     110     * @param attribute the name of the attribute
     111     * @return the value; {@code null}, if the respective configuration item does not exist
     112     * @throws NumberFormatException if the value is not a valid long
     113     */
    84114    public Long getLong(String element, String attribute) {
    85115        String s = get(element, attribute);
     
    88118    }
    89119
     120    /**
     121     * Adds a new configuration item.
     122     *
     123     * @param element the name of the element
     124     * @param attribute the name of the attribute
     125     * @param value the value as string
     126     */
    90127    public void put(String element, String attribute, String value) {
    91128        if ("blacklist".equals(element)) {
     
    103140    }
    104141
     142    /**
     143     * Clears the API capabilities.
     144     */
    105145    public final void clear() {
    106         capabilities = new HashMap<>();
    107         imageryBlacklist = new ArrayList<>();
    108     }
    109 
     146        capabilities.clear();
     147        imageryBlacklist.clear();
     148    }
     149
     150    /**
     151     * Determines if a given API version is supported.
     152     * @param version The API version to check
     153     * @return {@code true} is version is between the minimum supported version and the maximum one, {@code false} otherwise
     154     */
    110155    public boolean supportsVersion(String version) {
    111156        return get("version", "minimum").compareTo(version) <= 0
     
    137182
    138183    /**
    139      * checks if the given URL is blacklisted by one of the of the
    140      * regular expressions.
    141      */
    142 
    143     public boolean isOnImageryBlacklist(String url)
    144     {
     184     * Checks if the given URL is blacklisted by one of the of the regular expressions.
     185     * @param url Imagery URL to check
     186     * @return {@code true} if URL is blacklisted, {@code false} otherwise
     187     */
     188    public boolean isOnImageryBlacklist(String url) {
    145189        if (url != null && imageryBlacklist != null) {
    146190            for (String blacklistRegex : imageryBlacklist) {
     
    153197
    154198    /**
    155      * returns the full list of blacklist regular expressions.
    156      */
    157     public List<String> getImageryBlacklist()
    158     {
     199     * Returns the full list of imagery blacklist regular expressions.
     200     * @return full list of imagery blacklist regular expressions
     201     */
     202    public List<String> getImageryBlacklist() {
    159203        return Collections.unmodifiableList(imageryBlacklist);
    160204    }
     205
     206    /**
     207     * A parser for the "capabilities" response XML.
     208     * @since 7473
     209     */
     210    public static final class CapabilitiesParser extends DefaultHandler {
     211
     212        private Capabilities capabilities;
     213
     214        @Override
     215        public void startDocument() throws SAXException {
     216            capabilities = new Capabilities();
     217        }
     218
     219        @Override
     220        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
     221            for (int i=0; i< atts.getLength(); i++) {
     222                capabilities.put(qName, atts.getQName(i), atts.getValue(i));
     223            }
     224        }
     225
     226        /**
     227         * Returns the read capabilities.
     228         * @return the read capabilities
     229         */
     230        public Capabilities getCapabilities() {
     231            return capabilities;
     232        }
     233
     234        /**
     235         * Parses and returns capabilities from the given input source.
     236         *
     237         * @param inputSource The input source to read capabilities from
     238         * @return the capabilities
     239         * @throws SAXException if any SAX errors occur during processing
     240         * @throws IOException if any I/O errors occur
     241         * @throws ParserConfigurationException if a parser cannot be created
     242         */
     243        public static Capabilities parse(InputSource inputSource) throws SAXException, IOException, ParserConfigurationException {
     244            CapabilitiesParser parser = new CapabilitiesParser();
     245            SAXParserFactory.newInstance().newSAXParser().parse(inputSource, parser);
     246            return parser.getCapabilities();
     247        }
     248    }
    161249}
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r7434 r7473  
    2727
    2828import javax.xml.parsers.ParserConfigurationException;
    29 import javax.xml.parsers.SAXParserFactory;
    3029
    3130import org.openstreetmap.josm.Main;
     
    3736import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    3837import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     38import org.openstreetmap.josm.io.Capabilities.CapabilitiesParser;
    3939import org.openstreetmap.josm.tools.CheckParameterUtil;
    4040import org.openstreetmap.josm.tools.Utils;
    4141import org.openstreetmap.josm.tools.XmlParsingException;
    42 import org.xml.sax.Attributes;
    4342import org.xml.sax.InputSource;
    4443import org.xml.sax.SAXException;
    4544import org.xml.sax.SAXParseException;
    46 import org.xml.sax.helpers.DefaultHandler;
    4745
    4846/**
     
    111109    }
    112110
    113     /** the server URL */
     111    /** Server URL */
    114112    private String serverUrl;
    115113
    116     /**
    117      * Object describing current changeset
    118      */
     114    /** Object describing current changeset */
    119115    private Changeset changeset;
    120116
    121     /**
    122      * API version used for server communications
    123      */
     117    /** API version used for server communications */
    124118    private String version = null;
    125119
    126     /** the api capabilities */
    127     private Capabilities capabilities = new Capabilities();
    128 
    129     /**
    130      * true if successfully initialized
    131      */
     120    /** API capabilities */
     121    private Capabilities capabilities = null;
     122
     123    /** true if successfully initialized */
    132124    private boolean initialized = false;
    133125
    134126    /**
    135      * A parser for the "capabilities" response XML
    136      */
    137     private class CapabilitiesParser extends DefaultHandler {
    138         @Override
    139         public void startDocument() throws SAXException {
    140             capabilities.clear();
    141         }
    142 
    143         @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    144             for (int i=0; i< atts.getLength(); i++) {
    145                 capabilities.put(qName, atts.getQName(i), atts.getValue(i));
    146             }
    147         }
    148     }
    149 
    150     /**
    151      * creates an OSM api for a specific server URL
     127     * Constructs a new {@code OsmApi} for a specific server URL.
    152128     *
    153129     * @param serverUrl the server URL. Must not be null
     
    293269
    294270    private void initializeCapabilities(String xml) throws SAXException, IOException, ParserConfigurationException {
    295         InputSource inputSource = new InputSource(new StringReader(xml));
    296         SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new CapabilitiesParser());
     271        capabilities = CapabilitiesParser.parse(new InputSource(new StringReader(xml)));
    297272    }
    298273
     
    752727
    753728    /**
    754      * Replies the API capabilities
     729     * Replies the API capabilities.
    755730     *
    756731     * @return the API capabilities, or null, if the API is not initialized yet
Note: See TracChangeset for help on using the changeset viewer.