Changeset 1574 in josm for trunk/src/org


Ignore:
Timestamp:
2009-05-05T16:44:44+02:00 (16 years ago)
Author:
stoecker
Message:

close #2214 - patch by Henrik Niehaus - GPX export metadata handling

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/GpxExportAction.java

    r1509 r1574  
    2828import org.openstreetmap.josm.Main;
    2929import org.openstreetmap.josm.data.gpx.GpxData;
     30import org.openstreetmap.josm.gui.ExtendedDialog;
    3031import org.openstreetmap.josm.gui.layer.GpxLayer;
    3132import org.openstreetmap.josm.gui.layer.Layer;
    3233import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    3334import org.openstreetmap.josm.io.GpxWriter;
    34 import org.openstreetmap.josm.gui.ExtendedDialog;
    3535import org.openstreetmap.josm.tools.GBC;
    3636import org.openstreetmap.josm.tools.Shortcut;
     
    107107        p.add(warning, GBC.eol().fill(GBC.HORIZONTAL).insets(15,0,0,0));
    108108        addDependencies(author, authorName, email, copyright, predefined, copyrightYear, nameLabel, emailLabel, copyrightLabel, copyrightYearLabel, warning);
     109       
     110        // if the user name is not the email address, but the osm user name
     111        // move it from the email textfield to the author textfield
     112        if(!email.getText().contains("@")) {
     113            authorName.setText(email.getText());
     114            email.setText("");
     115        }
    109116
    110117        p.add(new JLabel(tr("Keywords")), GBC.eol());
     
    133140        else
    134141            gpxData = OsmDataLayer.toGpxData(Main.ds, file);
    135         // TODO: add copyright, etc.
     142       
     143        // add author and copyright details to the gpx data
     144        if(author.isSelected()) {
     145            if(authorName.getText().length() > 0) {
     146                gpxData.attr.put(GpxData.META_AUTHOR_NAME, authorName.getText());
     147                gpxData.attr.put(GpxData.META_COPYRIGHT_AUTHOR, authorName.getText());
     148            }
     149            if(email.getText().length() > 0) gpxData.attr.put(GpxData.META_AUTHOR_EMAIL, email.getText());
     150            if(copyright.getText().length() > 0) gpxData.attr.put(GpxData.META_COPYRIGHT_LICENSE, copyright.getText());
     151            if(copyrightYear.getText().length() > 0) gpxData.attr.put(GpxData.META_COPYRIGHT_YEAR, copyrightYear.getText());
     152        }
     153       
     154        // add the description to the gpx data
     155        if(desc.getText().length() > 0) gpxData.attr.put(GpxData.META_DESC, desc.getText());
     156       
     157        // add keywords to the gpx data
     158        if(keywords.getText().length() > 0) gpxData.attr.put(GpxData.META_KEYWORDS, keywords.getText());
     159       
    136160        try {
    137161            FileOutputStream fo = new FileOutputStream(file);
  • trunk/src/org/openstreetmap/josm/data/gpx/GpxData.java

    r1169 r1574  
    1919 */
    2020public class GpxData extends WithAttributes {
     21   
     22    public static final String META_PREFIX = "meta.";
     23    public static final String META_AUTHOR_NAME = META_PREFIX + "author.name";
     24    public static final String META_AUTHOR_EMAIL = META_PREFIX + "author.email";
     25    public static final String META_AUTHOR_LINK = META_PREFIX + "author.link";
     26    public static final String META_COPYRIGHT_AUTHOR = META_PREFIX + "copyright.author";
     27    public static final String META_COPYRIGHT_LICENSE = META_PREFIX + "copyright.license";
     28    public static final String META_COPYRIGHT_YEAR = META_PREFIX + "copyright.year";
     29    public static final String META_DESC = META_PREFIX + "desc";
     30    public static final String META_KEYWORDS = META_PREFIX + "keywords";
     31    public static final String META_LINKS = META_PREFIX + "links";
     32    public static final String META_NAME = META_PREFIX + "name";
     33    public static final String META_TIME = META_PREFIX + "time";
     34   
    2135    public File storageFile;
    2236    public boolean fromServer;
     
    3751            // TODO: Detect conflicts.
    3852            String k = ent.getKey();
    39             if (k.equals("link") && attr.containsKey("link")) {
    40                 ((Collection<GpxLink>) attr.get("link")).addAll(
     53            if (k.equals(META_LINKS) && attr.containsKey(META_LINKS)) {
     54                ((Collection<GpxLink>) attr.get(META_LINKS)).addAll(
    4155                    (Collection<GpxLink>) ent.getValue());
    4256            } else {
  • trunk/src/org/openstreetmap/josm/io/GpxReader.java

    r1425 r1574  
    4141     */
    4242    public GpxData data;
    43     public enum state { init, metadata, wpt, rte, trk, ext, author, link, trkseg }
     43    public enum state { init, metadata, wpt, rte, trk, ext, author, link, trkseg, copyright}
    4444
    4545    private class Parser extends DefaultHandler {
     
    106106                    currentState = state.link;
    107107                    currentLink = new GpxLink(atts.getValue("href"));
     108                } else if (qName.equals("email")) {
     109                    currentData.attr.put(GpxData.META_AUTHOR_EMAIL, atts.getValue("id") + "@" + atts.getValue("domain"));
    108110                }
    109111                break;
     
    129131                    states.push(currentState);
    130132                    currentState = state.ext;
     133                } else if (qName.equals("copyright")) {
     134                    states.push(currentState);
     135                    currentState = state.copyright;
     136                    currentData.attr.put(GpxData.META_COPYRIGHT_AUTHOR, atts.getValue("author"));
     137                } else if (qName.equals("link")) {
     138                    states.push(currentState);
     139                    currentState = state.link;
     140                    currentLink = new GpxLink(atts.getValue("href"));
    131141                }
    132142                break;
     
    184194            switch (currentState) {
    185195            case metadata:
    186                 if (qName.equals("name") || qName.equals("desc") ||
    187                         qName.equals("time") || qName.equals("keywords")) {
    188                     currentData.attr.put(qName, accumulator.toString());
     196                if (qName.equals("name")) {
     197                    currentData.attr.put(GpxData.META_NAME, accumulator.toString());
     198                } else if (qName.equals("desc")) {
     199                    currentData.attr.put(GpxData.META_DESC, accumulator.toString());
     200                } else if (qName.equals("time")) {
     201                    currentData.attr.put(GpxData.META_TIME, accumulator.toString());
     202                } else if (qName.equals("keywords")) {
     203                    currentData.attr.put(GpxData.META_KEYWORDS, accumulator.toString());
    189204                } else if (qName.equals("metadata")) {
    190205                    currentState = states.pop();
    191206                }
    192                 //TODO: parse copyright, bounds, extensions
     207                //TODO: parse bounds, extensions
    193208                break;
    194209            case author:
    195210                if (qName.equals("author")) {
    196211                    currentState = states.pop();
    197                 } else if (qName.equals("name") || qName.equals("email")) {
    198                     currentData.attr.put("author" + qName, accumulator.toString());
     212                } else if (qName.equals("name")) {
     213                    currentData.attr.put(GpxData.META_AUTHOR_NAME, accumulator.toString());
     214                } else if (qName.equals("email")) {
     215                    // do nothing, has been parsed on startElement
    199216                } else if (qName.equals("link")) {
    200                     currentData.attr.put("authorlink", currentLink);
     217                    currentData.attr.put(GpxData.META_AUTHOR_LINK, currentLink);
     218                }
     219                break;
     220            case copyright:
     221                if (qName.equals("copyright")) {
     222                    currentState = states.pop();
     223                } else if (qName.equals("year")) {
     224                    currentData.attr.put(GpxData.META_COPYRIGHT_YEAR, accumulator.toString());
     225                } else if (qName.equals("license")) {
     226                    currentData.attr.put(GpxData.META_COPYRIGHT_LICENSE, accumulator.toString());
    201227                }
    202228                break;
     
    207233                    currentLink.type = accumulator.toString();
    208234                } else if (qName.equals("link")) {
    209                     // <link>URL</link>
    210                     if (currentLink.uri == null)
    211                         currentLink.uri = accumulator.toString();
    212 
    213235                    currentState = states.pop();
    214236                }
    215237                if (currentState == state.author) {
    216                     currentData.attr.put("authorlink", currentLink);
    217                 } else if (currentState != state.link) {
     238                    currentData.attr.put(GpxData.META_AUTHOR_LINK, currentLink);
     239                } else if (currentState == state.metadata) {
    218240                    Map<String, Object> attr = getAttr();
    219                     if (!attr.containsKey("link")) {
    220                         attr.put("link", new LinkedList<GpxLink>());
     241                    if (!attr.containsKey(GpxData.META_LINKS)) {
     242                        attr.put(GpxData.META_LINKS, new LinkedList<GpxLink>());
    221243                    }
    222                     ((Collection<GpxLink>) attr.get("link")).add(currentLink);
     244                    ((Collection<GpxLink>) attr.get(GpxData.META_LINKS)).add(currentLink);
    223245                }
    224246                break;
  • trunk/src/org/openstreetmap/josm/io/GpxWriter.java

    r1169 r1574  
    4545        this.data = data;
    4646        out.println("<?xml version='1.0' encoding='UTF-8'?>");
    47         out.println("<gpx version=\"1.1\" creator=\"JOSM GPX export\" xmlns=\"http://www.topografix.com/GPX/1/1\">");
     47        out.println("<gpx version=\"1.1\" creator=\"JOSM GPX export\" xmlns=\"http://www.topografix.com/GPX/1/1\"\n" +
     48                        "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" +
     49                        "    xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">");
    4850        indent = "  ";
    4951        writeMetaData();
     
    5658
    5759    private void writeAttr(Map<String, Object> attr) {
    58         boolean hasAuthor = false;
     60        // FIXME this loop is evil, because it does not assure the
     61        // correct element order specified by the xml schema.
     62        // for now it works, but future extension could get very complex and unmaintainable
    5963        for (Map.Entry<String, Object> ent : attr.entrySet()) {
    6064            String k = ent.getKey();
    61             if (k.indexOf("author") == 0) {
    62                 hasAuthor = true;
    63             } else if (k.equals("link")) {
     65            if (k.equals("link")) {
    6466                for (GpxLink link : (Collection<GpxLink>) ent.getValue()) {
    6567                    gpxLink(link);
     
    6971            }
    7072        }
    71 
    72         if (hasAuthor) {
    73             open("author");
    74             simpleTag("name", (String) attr.get("authorname"));
    75             simpleTag("email", (String) attr.get("authoremail"));
    76             gpxLink((GpxLink) attr.get("authorlink"));
     73    }
     74
     75    private void writeMetaData() {
     76        Map<String, Object> attr = data.attr;
     77        openln("metadata");
     78       
     79        // write the description
     80        if (attr.containsKey(GpxData.META_DESC)) simpleTag("desc", (String)attr.get(GpxData.META_DESC));
     81       
     82        // write the author details
     83        if (attr.containsKey(GpxData.META_AUTHOR_NAME)
     84                || attr.containsKey(GpxData.META_AUTHOR_EMAIL)) {
     85            openln("author");
     86            // write the name
     87            simpleTag("name", (String) attr.get(GpxData.META_AUTHOR_NAME));
     88            // write the email address
     89            if(attr.containsKey(GpxData.META_AUTHOR_EMAIL)) {
     90                String[] tmp = ((String)attr.get(GpxData.META_AUTHOR_EMAIL)).split("@");
     91                if(tmp.length == 2) {
     92                    inline("email", "id=\"" + tmp[0] + "\" domain=\""+tmp[1]+"\"");
     93                }
     94            }
     95            // write the author link
     96            gpxLink((GpxLink) attr.get(GpxData.META_AUTHOR_LINK));
    7797            closeln("author");
    7898        }
    7999
    80         // TODO: copyright
    81     }
    82 
    83     private void writeMetaData() {
    84         openln("metadata");
    85         writeAttr(data.attr);
     100        // write the copyright details
     101        if(attr.containsKey(GpxData.META_COPYRIGHT_LICENSE)
     102                || attr.containsKey(GpxData.META_COPYRIGHT_YEAR)) {
     103            openAtt("copyright", "author=\""+ attr.get(GpxData.META_COPYRIGHT_AUTHOR) +"\"");
     104            if(attr.containsKey(GpxData.META_COPYRIGHT_YEAR)) {
     105                simpleTag("year", (String) attr.get(GpxData.META_COPYRIGHT_YEAR));
     106            }
     107            if(attr.containsKey(GpxData.META_COPYRIGHT_LICENSE)) {
     108                simpleTag("license", encode((String) attr.get(GpxData.META_COPYRIGHT_LICENSE)));
     109            }
     110            closeln("copyright");
     111        }
     112       
     113        // write links
     114        if(attr.containsKey(GpxData.META_LINKS)) {
     115            for (GpxLink link : (Collection<GpxLink>) attr.get(GpxData.META_LINKS)) {
     116                gpxLink(link);
     117            }
     118        }
     119       
     120        // write keywords
     121        if (attr.containsKey(GpxData.META_KEYWORDS)) simpleTag("keywords", (String)attr.get(GpxData.META_KEYWORDS));
    86122
    87123        data.recalculateBounds();
Note: See TracChangeset for help on using the changeset viewer.