Changeset 4645 in josm


Ignore:
Timestamp:
2011-12-10T06:44:44+01:00 (13 years ago)
Author:
framm
Message:

Added three new hooks for plugins, allowing plugins to mess with JOSM's server I/O. Plugins can now add postprocessors to server reading (so they may change what JOSM receives from the OSM server), and they can add postprocessors to server writing (so they can react to an upload action *after* it happened and after IDs for new objects have been assigned - this is in addition to the existing UploadHook which is a server writing preprocessor). Thirdly, plugins can now substitute their own version of OsmWriter. Taken together, these changes make it possible for a plugin to introduce extra information from a non-OSM source into the JOSM editing process, and upon upload split away that extra information and send it to another destination. - Also made minor changes to CredentialDialog to allow plugins to subclass the dialog in case they need their own authentication.

Location:
trunk/src/org/openstreetmap/josm
Files:
3 added
8 edited

Legend:

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

    r4386 r4645  
    5757
    5858    private boolean canceled;
    59     private CredentialPanel pnlCredentials;
     59    protected CredentialPanel pnlCredentials;
    6060    String saveUsernameAndPasswordCheckboxText;
    6161
     
    135135    }
    136136
    137     private static class CredentialPanel extends JPanel {
     137    protected static class CredentialPanel extends JPanel {
    138138        protected JTextField tfUserName;
    139139        protected JPasswordField tfPassword;
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r4310 r4645  
    3131import org.openstreetmap.josm.data.osm.IPrimitive;
    3232import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     33import org.openstreetmap.josm.gui.layer.ImageryLayer;
    3334import org.openstreetmap.josm.gui.layer.Layer;
    34 import org.openstreetmap.josm.gui.layer.ImageryLayer;
    3535import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    3636import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     
    107107     */
    108108    private boolean initialized = false;
    109 
    110     private StringWriter swriter = new StringWriter();
    111     private OsmWriter osmWriter = new OsmWriter(new PrintWriter(swriter), true, null);
    112109
    113110    /**
     
    175172                    serverUrl,
    176173                    version));
    177             osmWriter.setVersion(version);
    178174            initialized = true;
    179175
    180             /* This is an interim solution for openstreetmap.org not currently 
     176            /* This is an interim solution for openstreetmap.org not currently
    181177             * transmitting their imagery blacklist in the capabilities call.
    182178             * remove this as soon as openstreetmap.org adds blacklists. */
     
    191187            /* This checks if there are any layers currently displayed that
    192188             * are now on the blacklist, and removes them. This is a rare
    193              * situaton - probably only occurs if the user changes the API URL
     189             * situation - probably only occurs if the user changes the API URL
    194190             * in the preferences menu. Otherwise they would not have been able
    195191             * to load the layers in the first place becuase they would have
     
    228224     */
    229225    private String toXml(IPrimitive o, boolean addBody) {
     226        StringWriter swriter = new StringWriter();
     227        OsmWriter osmWriter = OsmWriterFactory.createOsmWriter(new PrintWriter(swriter), true, version);
    230228        swriter.getBuffer().setLength(0);
    231229        osmWriter.setWithBody(addBody);
     
    234232        o.visit(osmWriter);
    235233        osmWriter.footer();
    236         osmWriter.out.flush();
     234        osmWriter.flush();
    237235        return swriter.toString();
    238236    }
     
    245243     */
    246244    private String toXml(Changeset s) {
     245        StringWriter swriter = new StringWriter();
     246        OsmWriter osmWriter = OsmWriterFactory.createOsmWriter(new PrintWriter(swriter), true, version);
    247247        swriter.getBuffer().setLength(0);
    248248        osmWriter.header();
    249249        osmWriter.visit(s);
    250250        osmWriter.footer();
    251         osmWriter.out.flush();
     251        osmWriter.flush();
    252252        return swriter.toString();
    253253    }
  • trunk/src/org/openstreetmap/josm/io/OsmChangeBuilder.java

    r4310 r4645  
    3434        this.apiVersion = apiVersion == null ? DEFAULT_API_VERSION : apiVersion;
    3535        writer = new PrintWriter(swriter = new StringWriter());
    36         osmwriter = new OsmWriter(writer, false, apiVersion);
     36        osmwriter = OsmWriterFactory.createOsmWriter(writer, false, apiVersion);
    3737        osmwriter.setChangeset(changeset);
    3838    }
  • trunk/src/org/openstreetmap/josm/io/OsmExporter.java

    r3461 r4645  
    6767            Writer writer = new OutputStreamWriter(out, "UTF-8");
    6868
    69             OsmWriter w = new OsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
     69            OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
    7070            layer.data.getReadLock().lock();
    7171            try {
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r4532 r4645  
    5050    protected XMLStreamReader parser;
    5151
     52    /** Used by plugins to register themselves as data postprocessors. */
     53    public static ArrayList<OsmServerReadPostprocessor> postprocessors;
     54
     55    /** register a new postprocessor */
     56    public static void registerPostprocessor(OsmServerReadPostprocessor pp) {
     57        if (postprocessors == null) {
     58            postprocessors = new ArrayList<OsmServerReadPostprocessor>();
     59        }
     60        postprocessors.add(pp);
     61    }
     62
     63    /** deregister a postprocessor previously registered with registerPostprocessor */
     64    public static void deregisterPostprocessor(OsmServerReadPostprocessor pp) {
     65        if (postprocessors != null) {
     66            postprocessors.remove(pp);
     67        }
     68    }
     69
    5270    /**
    5371     * constructor (for private and subclasses use only)
     
    7189            if (event == XMLStreamConstants.START_ELEMENT) {
    7290                parseRoot();
    73             } else if (event == XMLStreamConstants.END_ELEMENT) {
     91            } else if (event == XMLStreamConstants.END_ELEMENT)
    7492                return;
    75             }
    7693            if (parser.hasNext()) {
    7794                event = parser.next();
     
    8299        parser.close();
    83100    }
    84    
     101
    85102    protected void parseRoot() throws XMLStreamException {
    86103        if (parser.getLocalName().equals("osm")) {
     
    121138                    parseUnknown();
    122139                }
    123             } else if (event == XMLStreamConstants.END_ELEMENT) {
     140            } else if (event == XMLStreamConstants.END_ELEMENT)
    124141                return;
    125             }
    126142        }
    127143    }
     
    172188                    parseUnknown();
    173189                }
    174             } else if (event == XMLStreamConstants.END_ELEMENT) {
     190            } else if (event == XMLStreamConstants.END_ELEMENT)
    175191                return n;
    176             }
    177192        }
    178193    }
     
    301316                        parseUnknown();
    302317                    }
    303                 } else if (event == XMLStreamConstants.END_ELEMENT) {
     318                } else if (event == XMLStreamConstants.END_ELEMENT)
    304319                    return;
    305                 }
    306320            }
    307321        } else {
     
    328342            if (event == XMLStreamConstants.START_ELEMENT) {
    329343                parseUnknown(false); /* no more warning for inner elements */
    330             } else if (event == XMLStreamConstants.END_ELEMENT) {
     344            } else if (event == XMLStreamConstants.END_ELEMENT)
    331345                return;
    332             }
    333346        }
    334347    }
     
    350363            if (event == XMLStreamConstants.START_ELEMENT) {
    351364                parseUnknown(printWarning);
    352             } else if (event == XMLStreamConstants.END_ELEMENT) {
     365            } else if (event == XMLStreamConstants.END_ELEMENT)
    353366                return;
    354             }
    355367        }
    356368    }
     
    555567            prepareDataSet();
    556568            progressMonitor.worked(1);
     569
     570            // iterate over registered postprocessors and give them each a chance
     571            // to modify the dataset we have just loaded.
     572            if (postprocessors != null) {
     573                for (OsmServerReadPostprocessor pp : postprocessors) {
     574                    pp.postprocessDataSet(getDataSet(), progressMonitor);
     575                }
     576            }
    557577            return getDataSet();
    558578        } catch(IllegalDataException e) {
     
    567587                msg = m.group(1);
    568588            }
    569             if (e.getLocation() != null) {
     589            if (e.getLocation() != null)
    570590                throw new IllegalDataException(tr("Line {0} column {1}: ", e.getLocation().getLineNumber(), e.getLocation().getColumnNumber()) + msg, e);
    571             } else {
     591            else
    572592                throw new IllegalDataException(msg, e);
    573             }
    574593        } catch(Exception e) {
    575594            throw new IllegalDataException(e);
     
    578597        }
    579598    }
    580    
     599
    581600    /**
    582601     * Parse the given input source and return the dataset.
  • trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java

    r4191 r4645  
    3636     */
    3737    private Collection<IPrimitive> processed;
     38
     39    private static ArrayList<OsmServerWritePostprocessor> postprocessors;
     40    public static void registerPostprocessor(OsmServerWritePostprocessor pp) {
     41        if (postprocessors == null) {
     42            postprocessors = new ArrayList<OsmServerWritePostprocessor>();
     43        }
     44        postprocessors.add(pp);
     45    }
     46    public static void unregisterPostprocessor(OsmServerWritePostprocessor pp) {
     47        if (postprocessors != null) {
     48            postprocessors.remove(pp);
     49        }
     50    }
    3851
    3952    private OsmApi api = OsmApi.getOsmApi();
     
    205218            throw e;
    206219        } finally {
     220            executePostprocessors(monitor);
    207221            monitor.finishTask();
    208222            api.setChangeset(null);
     
    216230            api.createPrimitive(osm, progressMonitor);
    217231        } else {
    218             api.modifyPrimitive(osm,progressMonitor);
     232            api.modifyPrimitive(osm, progressMonitor);
    219233        }
    220234    }
     
    235249        return processed;
    236250    }
     251
     252    /**
     253     * Calls all registered upload postprocessors.
     254     */
     255    public void executePostprocessors(ProgressMonitor pm) {
     256        if (postprocessors != null) {
     257            for (OsmServerWritePostprocessor pp : postprocessors) {
     258                pp.postprocessUploadedPrimitives(processed, pm);
     259            }
     260        }
     261    }
    237262}
  • trunk/src/org/openstreetmap/josm/io/OsmWriter.java

    r4105 r4645  
    4242    private Changeset changeset;
    4343
    44     public OsmWriter(PrintWriter out, boolean osmConform, String version) {
     44    /**
     45     * Do not call this directly. Use OsmWriterFactory instead.
     46     */
     47    protected OsmWriter(PrintWriter out, boolean osmConform, String version) {
    4548        super(out);
    4649        this.osmConform = osmConform;
     
    6871    }
    6972
    70     private static final Comparator<OsmPrimitive> byIdComparator = new Comparator<OsmPrimitive>() {
     73    protected static final Comparator<OsmPrimitive> byIdComparator = new Comparator<OsmPrimitive>() {
    7174        @Override public int compare(OsmPrimitive o1, OsmPrimitive o2) {
    7275            return (o1.getUniqueId()<o2.getUniqueId() ? -1 : (o1.getUniqueId()==o2.getUniqueId() ? 0 : 1));
     
    7477    };
    7578
    76     private Collection<OsmPrimitive> sortById(Collection<? extends OsmPrimitive> primitives) {
     79    protected Collection<OsmPrimitive> sortById(Collection<? extends OsmPrimitive> primitives) {
    7780        List<OsmPrimitive> result = new ArrayList<OsmPrimitive>(primitives.size());
    7881        result.addAll(primitives);
     
    99102    }
    100103
    101     private boolean shouldWrite(OsmPrimitive osm) {
     104    protected boolean shouldWrite(OsmPrimitive osm) {
    102105        return !osm.isNewOrUndeleted() || !osm.isDeleted();
    103106    }
     
    185188    }
    186189
    187     private static final Comparator<Entry<String, String>> byKeyComparator = new Comparator<Entry<String,String>>() {
     190    protected static final Comparator<Entry<String, String>> byKeyComparator = new Comparator<Entry<String,String>>() {
    188191        @Override public int compare(Entry<String, String> o1, Entry<String, String> o2) {
    189192            return o1.getKey().compareTo(o2.getKey());
     
    191194    };
    192195
    193     private void addTags(Tagged osm, String tagname, boolean tagOpen) {
     196    protected void addTags(Tagged osm, String tagname, boolean tagOpen) {
    194197        if (osm.hasKeys()) {
    195198            if (tagOpen) {
     
    216219     * id, action, user, and visible.
    217220     */
    218     private void addCommon(IPrimitive osm, String tagname) {
     221    protected void addCommon(IPrimitive osm, String tagname) {
    219222        out.print("  <"+tagname);
    220223        if (osm.getUniqueId() != 0) {
     
    261264    }
    262265
     266    @Override
    263267    public void flush() {
    264268        out.flush();
  • trunk/src/org/openstreetmap/josm/io/XmlWriter.java

    r1677 r4645  
    1818    }
    1919
     20    public void flush() {
     21        out.flush();
     22    }
     23
    2024    /**
    2125     * Encode the given string in XML1.0 format.
     
    2731            String encS = XmlWriter.encoding.get(unencoded.charAt(i));
    2832            if (encS != null) {
    29                 if (buffer == null)
     33                if (buffer == null) {
    3034                    buffer = new StringBuilder(unencoded.substring(0,i));
     35                }
    3136                buffer.append(encS);
    32             } else if (buffer != null)
     37            } else if (buffer != null) {
    3338                buffer.append(unencoded.charAt(i));
     39            }
    3440        }
    3541        return (buffer == null) ? unencoded : buffer.toString();
Note: See TracChangeset for help on using the changeset viewer.