Ignore:
Timestamp:
2009-07-19T17:38:55+02:00 (15 years ago)
Author:
jttt
Message:

PleaseWait refactoring. Progress is now reported using ProgressMonitor interface, that is available through PleaseWaitRunnable.

File:
1 edited

Legend:

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

    r1790 r1811  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.IOException;
    76import java.io.InputStream;
    87import java.net.HttpURLConnection;
     
    1413import java.util.logging.Logger;
    1514
    16 import org.openstreetmap.josm.Main;
    1715import org.openstreetmap.josm.data.osm.DataSet;
    1816import org.openstreetmap.josm.data.osm.Node;
     
    2321import org.openstreetmap.josm.data.osm.Way;
    2422import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
    25 import org.xml.sax.SAXException;
     23import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     24import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    2625
    2726/**
    2827 * Retrieves a set of {@see OsmPrimitive}s from an OSM server using the so called
    2928 * Multi Fetch API.
    30  * 
     29 *
    3130 * Usage:
    3231 * <pre>
     
    4241 *    }
    4342 * </pre>
    44  * 
     43 *
    4544 *
    4645 */
     
    5352     * which should be safe according to the
    5453     * <a href="http://www.boutell.com/newfaq/misc/urllength.html">WWW FAQ</a>.
    55      * 
     54     *
    5655     */
    5756    static private int MAX_IDS_PER_REQUEST = 200;
     
    7978     * remembers an {@see OsmPrimitive}'s id and its type. The id will
    8079     * later be fetched as part of a Multi Get request.
    81      * 
     80     *
    8281     * Ignore the id if it id <= 0.
    83      * 
     82     *
    8483     * @param id  the id
    8584     * @param type  the type
     
    10099     * an {@see OsmPrimitive} with id=<code>id</code>. The id will
    101100     * later we fetched as part of a Multi Get request.
    102      * 
     101     *
    103102     * Ignore the id if it id <= 0.
    104      * 
     103     *
    105104     * @param ds  the dataset (must not be null)
    106105     * @param id  the id
     
    123122     * appends a list of  ids to the list of ids which will be fetched from the server. ds must
    124123     * include an {@see OsmPrimitive} for each id in ids.
    125      * 
     124     *
    126125     * id is ignored if id <= 0.
    127      * 
     126     *
    128127     * @param ds  the dataset
    129128     * @param ids  the list of ids
    130129     * @return this
    131      * 
     130     *
    132131     */
    133132    public MultiFetchServerObjectReader append(DataSet ds, long ... ids)  {
     
    142141     * appends a collection of  ids to the list of ids which will be fetched from the server. ds must
    143142     * include an {@see OsmPrimitive} for each id in ids.
    144      * 
     143     *
    145144     * id is ignored if id <= 0.
    146      * 
     145     *
    147146     * @param ds  the dataset
    148147     * @param ids  the collection of ids
    149148     * @return this
    150      * 
     149     *
    151150     */
    152151    public MultiFetchServerObjectReader append(DataSet ds, Collection<Long> ids) {
     
    163162     * @param node  the node (ignored, if null)
    164163     * @return this
    165      * 
     164     *
    166165     */
    167166    public MultiFetchServerObjectReader append(Node node) {
     
    177176     * @param way the way (ignored, if null)
    178177     * @return this
    179      * 
     178     *
    180179     */
    181180    public MultiFetchServerObjectReader append(Way way) {
     
    196195     * @param relation  the relation (ignored, if null)
    197196     * @return this
    198      * 
     197     *
    199198     */
    200199    public MultiFetchServerObjectReader append(Relation relation) {
     
    225224     * @param primitives  the list of primitives (ignored, if null)
    226225     * @return this
    227      * 
     226     *
    228227     * @see #append(Node)
    229228     * @see #append(Way)
    230229     * @see #append(Relation)
    231      * 
     230     *
    232231     */
    233232    public MultiFetchServerObjectReader append(Collection<OsmPrimitive> primitives) {
     
    242241     * extracts a subset of max {@see #MAX_IDS_PER_REQUEST} ids from <code>ids</code> and
    243242     * replies the subset. The extracted subset is removed from <code>ids</code>.
    244      * 
     243     *
    245244     * @param ids a set of ids
    246245     * @return the subset of ids
     
    267266     * builds the Multi Get request string for a set of ids and a given
    268267     * {@see OsmPrimitiveType}.
    269      * 
     268     *
    270269     * @param type the type
    271270     * @param idPackage  the package of ids
     
    290289     * builds the Multi Get request string for a single id and a given
    291290     * {@see OsmPrimitiveType}.
    292      * 
     291     *
    293292     * @param type the type
    294293     * @param id the id
     
    306305     * invokes a Multi Get for a set of ids and a given {@see OsmPrimitiveType}.
    307306     * The retrieved primitives are merged to {@see #outputDataSet}.
    308      * 
     307     *
    309308     * @param type the type
    310309     * @param pkg the package of ids
    311310     * @exception OsmTransferException thrown if an error occurs while communicating with the API server
    312      * 
    313      */
    314     protected void multiGetIdPackage(OsmPrimitiveType type, Set<Long> pkg) throws OsmTransferException {
     311     *
     312     */
     313    protected void multiGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor) throws OsmTransferException {
    315314        String request = buildRequestString(type, pkg);
    316         final InputStream in = getInputStream(request, Main.pleaseWaitDlg);
     315        final InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE);
    317316        if (in == null) return;
    318         Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));
     317        progressMonitor.subTask(tr("Downloading OSM data..."));
    319318        try {
    320             final OsmReader osm = OsmReader.parseDataSetOsm(in, Main.pleaseWaitDlg);
     319            final OsmReader osm = OsmReader.parseDataSetOsm(in, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
    321320            merge(osm.getDs());
    322321        } catch(Exception e) {
     
    328327     * invokes a Multi Get for a single id and a given {@see OsmPrimitiveType}.
    329328     * The retrieved primitive is merged to {@see #outputDataSet}.
    330      * 
     329     *
    331330     * @param type the type
    332331     * @param id the id
    333332     * @exception OsmTransferException thrown if an error occurs while communicating with the API server
    334      * 
    335      */
    336     protected void singleGetId(OsmPrimitiveType type, long id) throws OsmTransferException {
     333     *
     334     */
     335    protected void singleGetId(OsmPrimitiveType type, long id, ProgressMonitor progressMonitor) throws OsmTransferException {
    337336        String request = buildRequestString(type, id);
    338         final InputStream in = getInputStream(request, Main.pleaseWaitDlg);
     337        final InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE);
    339338        if (in == null)
    340339            return;
    341         Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));
     340        progressMonitor.subTask(tr("Downloading OSM data..."));
    342341        try {
    343             final OsmReader osm = OsmReader.parseDataSetOsm(in,Main.pleaseWaitDlg);
     342            final OsmReader osm = OsmReader.parseDataSetOsm(in, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
    344343            merge(osm.getDs());
    345344        } catch(Exception e) {
     
    351350     * invokes a sequence of Multi Gets for individual ids in a set of ids and a given {@see OsmPrimitiveType}.
    352351     * The retrieved primitives are merged to {@see #outputDataSet}.
    353      * 
     352     *
    354353     * This method is used if one of the ids in pkg doesn't exist (the server replies with return code 404).
    355354     * If the set is fetched with this method it is possible to find out which of the ids doesn't exist.
    356355     * Unfortunatelly, the server does not provide an error header or an error body for a 404 reply.
    357      * 
     356     *
    358357     * @param type the type
    359358     * @param pkg the set of ids
    360359     * @exception OsmTransferException thrown if an error occurs while communicating with the API server
    361      * 
    362      */
    363     protected void singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg) throws OsmTransferException {
     360     *
     361     */
     362    protected void singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor) throws OsmTransferException {
    364363        for (long id : pkg) {
    365364            try {
    366                 singleGetId(type, id);
     365                singleGetId(type, id, progressMonitor);
    367366            } catch(OsmApiException e) {
    368367                if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
     
    378377    /**
    379378     * merges the dataset <code>from</code> to {@see #outputDataSet}.
    380      * 
     379     *
    381380     * @param from the other dataset
    382      * 
     381     *
    383382     */
    384383    protected void merge(DataSet from) {
     
    389388    /**
    390389     * fetches a set of ids of a given {@see OsmPrimitiveType} from the server
    391      * 
     390     *
    392391     * @param ids the set of ids
    393392     * @param type the  type
    394393     * @exception OsmTransferException thrown if an error occurs while communicating with the API server
    395394     */
    396     protected void fetchPrimitives(Set<Long> ids, OsmPrimitiveType type) throws OsmTransferException{
     395    protected void fetchPrimitives(Set<Long> ids, OsmPrimitiveType type, ProgressMonitor progressMonitor) throws OsmTransferException{
    397396        Set<Long> toFetch = new HashSet<Long>(ids);
    398397        toFetch.addAll(ids);
     
    400399            Set<Long> pkg = extractIdPackage(toFetch);
    401400            try {
    402                 multiGetIdPackage(type, pkg);
     401                multiGetIdPackage(type, pkg, progressMonitor);
    403402            } catch(OsmApiException e) {
    404403                if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
    405404                    logger.warning(tr("Server replied with response code 404, retrying with an individual request for each primitive"));
    406                     singleGetIdPackage(type, pkg);
     405                    singleGetIdPackage(type, pkg, progressMonitor);
    407406                } else
    408407                    throw e;
     
    417416     * the latest version of the primitive (if any), even if the primitive is not visible (i.e. if
    418417     * visible==false).
    419      * 
     418     *
    420419     * Invoke {@see #getMissingPrimitives()} to get a list of primitives which have not been
    421420     * found on  the server (the server response code was 404)
    422      * 
     421     *
    423422     * Invoke {@see #getSkippedWay()} to get a list of ways which this reader could not build from
    424423     * the fetched data because the ways refer to nodes which don't exist on the server.
    425      * 
     424     *
    426425     * @return the parsed data
    427426     * @exception OsmTransferException thrown if an error occurs while communicating with the API server
    428427     * @see #getMissingPrimitives()
    429428     * @see #getSkippedWays()
    430      * 
     429     *
    431430
    432431     */
    433432    @Override
    434     public DataSet parseOsm() throws OsmTransferException {
    435         missingPrimitives = new HashSet<Long>();
    436 
    437         fetchPrimitives(nodes,OsmPrimitiveType.NODE);
    438         fetchPrimitives(ways,OsmPrimitiveType.WAY);
    439         fetchPrimitives(relations,OsmPrimitiveType.RELATION);
    440         return outputDataSet;
     433    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
     434        progressMonitor.beginTask("");
     435        try {
     436            missingPrimitives = new HashSet<Long>();
     437
     438            fetchPrimitives(nodes,OsmPrimitiveType.NODE, progressMonitor);
     439            fetchPrimitives(ways,OsmPrimitiveType.WAY, progressMonitor);
     440            fetchPrimitives(relations,OsmPrimitiveType.RELATION, progressMonitor);
     441            return outputDataSet;
     442        } finally {
     443            progressMonitor.finishTask();
     444        }
    441445    }
    442446
     
    445449     * server was submitted but which are not available from the server (the server
    446450     * replied a return code of 404)
    447      * 
     451     *
    448452     * @return the set of ids of missing primitives
    449453     */
Note: See TracChangeset for help on using the changeset viewer.