Ignore:
Timestamp:
2014-11-04T02:33:20+01:00 (10 years ago)
Author:
Don-vip
Message:

see #10701 - parsing support of changeset discussions

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

Legend:

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

    r7700 r7704  
    88import java.nio.charset.StandardCharsets;
    99import java.text.MessageFormat;
     10import java.util.Date;
    1011import java.util.LinkedList;
    1112import java.util.List;
     
    1617import org.openstreetmap.josm.data.coor.LatLon;
    1718import org.openstreetmap.josm.data.osm.Changeset;
     19import org.openstreetmap.josm.data.osm.ChangesetDiscussionComment;
    1820import org.openstreetmap.josm.data.osm.User;
    1921import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     
    7072        private Changeset current = null;
    7173
     74        /** The current comment */
     75        private ChangesetDiscussionComment comment = null;
     76
     77        /** The current comment text */
     78        private StringBuilder text = null;
     79
    7280        protected void parseChangesetAttributes(Changeset cs, Attributes atts) throws XmlParsingException {
    7381            // -- id
     
    7886            current.setId(parseNumericAttribute(value, 1));
    7987
    80             // -- user
    81             String user = atts.getValue("user");
    82             String uid = atts.getValue("uid");
    83             current.setUser(createUser(uid, user));
     88            // -- user / uid
     89            current.setUser(createUser(atts));
    8490
    8591            // -- created_at
     
    155161        }
    156162
     163        private void parseCommentAttributes(Attributes atts) throws XmlParsingException {
     164            // -- date
     165            String value = atts.getValue("date");
     166            Date date = null;
     167            if (value != null) {
     168                date = DateUtils.fromString(value);
     169            }
     170
     171            comment = new ChangesetDiscussionComment(date, createUser(atts));
     172        }
     173
    157174        private int parseNumericAttribute(String value, int minAllowed) throws XmlParsingException {
    158175            int att = 0;
     
    193210                current.put(key, value);
    194211                break;
     212            case "discussion":
     213                break;
     214            case "comment":
     215                parseCommentAttributes(atts);
     216                break;
     217            case "text":
     218                text = new StringBuilder();
     219                break;
    195220            default:
    196221                throwException(tr("Undefined element ''{0}'' found in input stream. Aborting.", qName));
     222            }
     223        }
     224
     225        @Override
     226        public void characters(char[] ch, int start, int length) throws SAXException {
     227            if (text != null) {
     228                text.append(ch, start, length);
    197229            }
    198230        }
     
    202234            if ("changeset".equals(qName)) {
    203235                changesets.add(current);
    204             }
    205         }
    206 
    207         protected User createUser(String uid, String name) throws XmlParsingException {
     236                current = null;
     237            } else if ("comment".equals(qName)) {
     238                current.addDiscussionComment(comment);
     239                comment = null;
     240            } else if ("text".equals(qName)) {
     241                comment.setText(text.toString());
     242                text = null;
     243            }
     244        }
     245
     246        protected User createUser(Attributes atts) throws XmlParsingException {
     247            String name = atts.getValue("user");
     248            String uid = atts.getValue("uid");
    208249            if (uid == null) {
    209250                if (name == null)
  • trunk/src/org/openstreetmap/josm/io/OsmServerChangesetReader.java

    r7033 r7704  
    2929
    3030    /**
    31      * constructor
    32      *
    33      */
    34     public OsmServerChangesetReader(){
     31     * Constructs a new {@code OsmServerChangesetReader}.
     32     */
     33    public OsmServerChangesetReader() {
    3534        setDoAuthenticate(false);
    3635    }
     
    3837    /**
    3938     * don't use - not implemented!
    40      *
    4139     */
    4240    @Override
    4341    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
    4442        return null;
     43    }
     44
     45    protected final InputStream getChangesetInputStream(long id, boolean includeDiscussion, ProgressMonitor monitor)
     46            throws OsmTransferException {
     47        StringBuilder sb = new StringBuilder();
     48        sb.append("changeset/").append(id);
     49        if (includeDiscussion) {
     50            sb.append("?include_discussion=true");
     51        }
     52        return getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
    4553    }
    4654
     
    8290
    8391    /**
    84      * Reads the changeset with id <code>id</code> from the server
     92     * Reads the changeset with id <code>id</code> from the server.
    8593     *
    86      * @param id  the changeset id. id &gt; 0 required.
     94     * @param id the changeset id. id &gt; 0 required.
     95     * @param includeDiscussion determines if discussion comments must be downloaded or not
    8796     * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
    8897     * @return the changeset read
    8998     * @throws OsmTransferException thrown if something goes wrong
    9099     * @throws IllegalArgumentException if id &lt;= 0
    91      */
    92     public Changeset readChangeset(long id, ProgressMonitor monitor) throws OsmTransferException {
     100     * @since 7704
     101     */
     102    public Changeset readChangeset(long id, boolean includeDiscussion, ProgressMonitor monitor) throws OsmTransferException {
    93103        if (id <= 0)
    94104            throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
     
    99109        try {
    100110            monitor.beginTask(tr("Reading changeset {0} ...",id));
    101             StringBuilder sb = new StringBuilder();
    102             sb.append("changeset/").append(id);
    103             try (InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true))) {
     111            try (InputStream in = getChangesetInputStream(id, includeDiscussion, monitor)) {
    104112                if (in == null)
    105113                    return null;
     
    123131
    124132    /**
    125      * Reads the changeset with id <code>id</code> from the server
     133     * Reads the changesets with id <code>ids</code> from the server.
    126134     *
    127      * @param ids  the list of ids. Ignored if null. Only load changesets for ids &gt; 0.
     135     * @param ids the list of ids. Ignored if null. Only load changesets for ids &gt; 0.
     136     * @param includeDiscussion determines if discussion comments must be downloaded or not
    128137     * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
    129138     * @return the changeset read
    130139     * @throws OsmTransferException thrown if something goes wrong
    131140     * @throws IllegalArgumentException if id &lt;= 0
    132      */
    133     public List<Changeset> readChangesets(Collection<Integer> ids, ProgressMonitor monitor) throws OsmTransferException {
     141     * @since 7704
     142     */
     143    public List<Changeset> readChangesets(Collection<Integer> ids, boolean includeDiscussion, ProgressMonitor monitor) throws OsmTransferException {
    134144        if (ids == null)
    135145            return Collections.emptyList();
     
    147157                }
    148158                i++;
    149                 StringBuilder sb = new StringBuilder();
    150                 sb.append("changeset/").append(id);
    151                 try (InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true))) {
     159                try (InputStream in = getChangesetInputStream(id, includeDiscussion, monitor)) {
    152160                    if (in == null)
    153161                        return null;
Note: See TracChangeset for help on using the changeset viewer.