Changeset 1523 in josm
- Timestamp:
- 2009-04-06T20:18:48+02:00 (16 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 added
- 36 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/Main.java
r1512 r1523 304 304 Main.pref.put("gui.geometry", geometry); 305 305 } 306 } else 306 } else { 307 307 System.out.println("Ignoring malformed geometry: "+geometry); 308 } 308 309 } 309 310 if (bounds == null) … … 432 433 { 433 434 String full = Locale.getDefault().toString(); 434 if (full.equals("iw_IL"))435 if (full.equals("iw_IL")) 435 436 return "he"; 436 437 /* list of non-single codes supported by josm */ 437 else if (full.equals("en_GB"))438 else if (full.equals("en_GB")) 438 439 return full; 439 440 return Locale.getDefault().getLanguage(); … … 444 445 String newGeometry = ""; 445 446 try { 446 if (((JFrame)parent).getExtendedState() == JFrame.NORMAL) {447 if (((JFrame)parent).getExtendedState() == JFrame.NORMAL) { 447 448 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize(); 448 449 Rectangle bounds = parent.getBounds(); … … 451 452 int x = (int)bounds.getX(); 452 453 int y = (int)bounds.getY(); 453 if (width > screenDimension.width)454 if (width > screenDimension.width) 454 455 width = screenDimension.width; 455 if (height > screenDimension.height)456 if (height > screenDimension.height) 456 457 width = screenDimension.height; 457 if (x < 0)458 if (x < 0) 458 459 x = 0; 459 if (y < 0)460 if (y < 0) 460 461 y = 0; 461 462 newGeometry = width + "x" + height + "+" + x + "+" + y; -
trunk/src/org/openstreetmap/josm/actions/CopyAction.java
r1514 r1523 8 8 import java.awt.event.KeyEvent; 9 9 import java.util.ArrayList; 10 import java.util.Collection; 10 11 import java.util.HashMap; 12 import java.util.LinkedList; 11 13 import java.util.List; 12 import java.util.LinkedList;13 import java.util.Collection;14 14 15 15 import javax.swing.JOptionPane; … … 19 19 import org.openstreetmap.josm.data.osm.DataSet; 20 20 import org.openstreetmap.josm.data.osm.DataSource; 21 import org.openstreetmap.josm.data.osm.Node; 22 import org.openstreetmap.josm.data.osm.OsmPrimitive; 21 23 import org.openstreetmap.josm.data.osm.Relation; 22 24 import org.openstreetmap.josm.data.osm.RelationMember; 23 import org.openstreetmap.josm.data.osm.Node;24 import org.openstreetmap.josm.data.osm.OsmPrimitive;25 25 import org.openstreetmap.josm.data.osm.Way; 26 import org.openstreetmap.josm.data.osm.visitor. Visitor;26 import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor; 27 27 import org.openstreetmap.josm.tools.Shortcut; 28 28 … … 65 65 /* scan the selected objects, mapping them to copies; when copying a way or relation, 66 66 * the copy references the copies of their child objects */ 67 new Visitor(){67 new AbstractVisitor() { 68 68 public void visit(Node n) { 69 69 /* check if already in pasteBuffer - e.g. two ways are selected which share a node; -
trunk/src/org/openstreetmap/josm/actions/HistoryInfoAction.java
r1433 r1523 12 12 13 13 import org.openstreetmap.josm.Main; 14 import org.openstreetmap.josm.data.osm.Relation;15 14 import org.openstreetmap.josm.data.osm.Node; 16 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; 16 import org.openstreetmap.josm.data.osm.Relation; 17 17 import org.openstreetmap.josm.data.osm.Way; 18 import org.openstreetmap.josm.data.osm.visitor. Visitor;18 import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor; 19 19 import org.openstreetmap.josm.tools.OpenBrowser; 20 20 import org.openstreetmap.josm.tools.Shortcut; … … 31 31 public void actionPerformed(ActionEvent e) { 32 32 final Collection<Object> sel = new LinkedList<Object>(); 33 new Visitor() {33 new AbstractVisitor() { 34 34 public void visit(Node n) { 35 35 if(n.id <= 0) return; -
trunk/src/org/openstreetmap/josm/actions/ReverseWayAction.java
r1218 r1523 19 19 import org.openstreetmap.josm.corrector.UserCancelException; 20 20 import org.openstreetmap.josm.data.osm.DataSet; 21 import org.openstreetmap.josm.data.osm.Relation;22 21 import org.openstreetmap.josm.data.osm.Node; 23 22 import org.openstreetmap.josm.data.osm.OsmPrimitive; 23 import org.openstreetmap.josm.data.osm.Relation; 24 24 import org.openstreetmap.josm.data.osm.Way; 25 import org.openstreetmap.josm.data.osm.visitor. Visitor;25 import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor; 26 26 import org.openstreetmap.josm.tools.Shortcut; 27 27 … … 35 35 public void actionPerformed(ActionEvent e) { 36 36 final Collection<Way> sel = new LinkedList<Way>(); 37 new Visitor() {37 new AbstractVisitor() { 38 38 public void visit(Node n) { 39 39 } -
trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java
r1397 r1523 6 6 import java.awt.event.ActionEvent; 7 7 import java.io.File; 8 import java.io.FileOutputStream;9 8 import java.io.FileInputStream; 10 9 import java.io.FileNotFoundException; 10 import java.io.FileOutputStream; 11 11 import java.io.IOException; 12 import java.io.PrintWriter; 12 13 13 14 import javax.swing.JFileChooser; … … 18 19 import org.openstreetmap.josm.data.osm.OsmPrimitive; 19 20 import org.openstreetmap.josm.gui.ExtendedDialog; 21 import org.openstreetmap.josm.gui.layer.GpxLayer; 22 import org.openstreetmap.josm.gui.layer.Layer; 20 23 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 21 import org.openstreetmap.josm.gui.layer.Layer; 22 import org.openstreetmap.josm.gui.layer.GpxLayer; 24 import org.openstreetmap.josm.io.GpxWriter; 23 25 import org.openstreetmap.josm.io.OsmWriter; 24 import org.openstreetmap.josm.io.GpxWriter;25 26 import org.openstreetmap.josm.tools.Shortcut; 26 27 … … 159 160 copy(file, tmpFile); 160 161 } 161 OsmWriter.output(new FileOutputStream(file), new OsmWriter.All(layer.data, false)); 162 OsmWriter w = new OsmWriter(new PrintWriter(new FileOutputStream(file)), false, layer.data.version); 163 w.header(); 164 w.writeDataSources(layer.data); 165 w.writeContent(layer.data); 166 w.footer(); 167 // FIXME - how to close? 162 168 if (!Main.pref.getBoolean("save.keepbackup") && (tmpFile != null)) 163 169 tmpFile.delete(); -
trunk/src/org/openstreetmap/josm/actions/SplitWayAction.java
r1499 r1523 31 31 import org.openstreetmap.josm.data.osm.RelationMember; 32 32 import org.openstreetmap.josm.data.osm.Way; 33 import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor; 33 34 import org.openstreetmap.josm.data.osm.visitor.NameVisitor; 34 35 import org.openstreetmap.josm.data.osm.visitor.Visitor; … … 74 75 selectedNodes = null; 75 76 76 Visitor splitVisitor = new Visitor(){77 Visitor splitVisitor = new AbstractVisitor() { 77 78 public void visit(Node n) { 78 79 if (selectedNodes == null) -
trunk/src/org/openstreetmap/josm/actions/UploadAction.java
r1397 r1523 154 154 PleaseWaitRunnable uploadTask = new PleaseWaitRunnable(tr("Uploading data")){ 155 155 @Override protected void realRun() throws SAXException { 156 server.uploadOsm( all);156 server.uploadOsm(Main.ds.version, all); 157 157 } 158 158 @Override protected void finish() { … … 160 160 } 161 161 @Override protected void cancel() { 162 server.cancel();162 // FIXME server.cancel(); 163 163 } 164 164 }; -
trunk/src/org/openstreetmap/josm/command/Command.java
r1169 r1523 12 12 import org.openstreetmap.josm.Main; 13 13 import org.openstreetmap.josm.data.osm.DataSet; 14 import org.openstreetmap.josm.data.osm.Relation;15 14 import org.openstreetmap.josm.data.osm.Node; 16 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; 16 import org.openstreetmap.josm.data.osm.Relation; 17 17 import org.openstreetmap.josm.data.osm.Way; 18 import org.openstreetmap.josm.data.osm.visitor. Visitor;18 import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor; 19 19 import org.openstreetmap.josm.gui.layer.Layer; 20 20 import org.openstreetmap.josm.gui.layer.OsmDataLayer; … … 33 33 abstract public class Command { 34 34 35 private static final class CloneVisitor implementsVisitor {35 private static final class CloneVisitor extends AbstractVisitor { 36 36 public Map<OsmPrimitive, OsmPrimitive> orig = new HashMap<OsmPrimitive, OsmPrimitive>(); 37 37 -
trunk/src/org/openstreetmap/josm/data/osm/Changeset.java
r1169 r1523 2 2 package org.openstreetmap.josm.data.osm; 3 3 4 import org.openstreetmap.josm.Main; 5 import org.openstreetmap.josm.io.XmlWriter; 6 import org.openstreetmap.josm.io.XmlWriter.OsmWriterInterface; 7 import java.io.PrintWriter; 8 import java.util.Map; 9 import java.util.Map.Entry; 10 import java.util.HashMap; 11 import java.util.Collections; 12 import java.util.Collection; 4 import org.openstreetmap.josm.data.osm.visitor.Visitor; 5 13 6 14 7 … … 19 12 * 20 13 */ 21 public final class Changeset /*extends OsmPrimitive*/ implements OsmWriterInterface { 22 /** 23 * The key/value list for this primitive. 24 */ 25 public Map<String, String> keys; 26 27 public long id = 0; 28 29 /** 30 * User that created this changeset, as specified by the server. 31 * Never changed by JOSM. 32 */ 33 public User user = null; 34 14 public final class Changeset extends OsmPrimitive { 35 15 /** 36 16 * Time of last modification to this object. This is not set by JOSM but … … 45 25 public String start_timestamp = null; 46 26 47 private void addTags(PrintWriter out) { 48 if (this.keys != null) { 49 for (Entry<String, String> e : this.keys.entrySet()) 50 out.println(" <tag k='"+ XmlWriter.encode(e.getKey()) + 51 "' v='"+XmlWriter.encode(e.getValue())+ "' />"); 52 } 53 } 54 55 public final void header(PrintWriter out) { 56 out.print("<osm version='"); 57 out.print(Main.pref.get("osm-server.version", "0.6")); 58 out.println("' generator='JOSM'>"); 59 } 60 public final void write(PrintWriter out) { 61 out.print(" <changeset"); 62 if (id != 0) 63 out.print(" id="+id); 64 if (this.user != null) { 65 out.print(" user='"+XmlWriter.encode(this.user.name)+"'"); 66 } 67 out.println(">\n"); 68 addTags( out ); 69 out.println(" </changeset>"); 70 } 71 public final void footer(PrintWriter out) { 72 out.println("</osm>"); 27 public void visit(Visitor v) { 28 v.visit(this); 73 29 } 74 30 75 /****************************************************** 76 * This tag stuff is copied from OsmPrimitive. Perhaps a changeset 77 * really is a primitive, but it's not right now. Perhaps it should 78 * be... 79 ******************************************************/ 80 81 /** 82 * Set the given value to the given key 83 * @param key The key, for which the value is to be set. 84 * @param value The value for the key. 85 */ 86 public final void put(String key, String value) { 87 if (value == null) 88 remove(key); 89 else { 90 if (keys == null) 91 keys = new HashMap<String, String>(); 92 keys.put(key, value); 93 } 31 public int compareTo(OsmPrimitive arg0) { 32 if (arg0 instanceof Changeset) return Long.valueOf(id).compareTo(arg0.id); 33 return 1; 94 34 } 95 /** 96 * Remove the given key from the list. 97 */ 98 public final void remove(String key) { 99 if (keys != null) { 100 keys.remove(key); 101 if (keys.isEmpty()) 102 keys = null; 103 } 104 } 105 106 public final String get(String key) { 107 return keys == null ? null : keys.get(key); 108 } 109 110 public final Collection<Entry<String, String>> entrySet() { 111 if (keys == null) 112 return Collections.emptyList(); 113 return keys.entrySet(); 114 } 115 116 public final Collection<String> keySet() { 117 if (keys == null) 118 return Collections.emptyList(); 119 return keys.keySet(); 120 } 35 36 121 37 } -
trunk/src/org/openstreetmap/josm/data/osm/DataSet.java
r1415 r1523 24 24 public class DataSet implements Cloneable { 25 25 26 /** 27 * The API version that created this data set, if any. 28 */ 29 public String version; 30 26 31 /** 27 32 * All nodes goes here, even when included in other data (ways etc). This enables the instant … … 209 214 for (DataSource source : dataSources) 210 215 ds.dataSources.add(new DataSource(source.bounds, source.origin)); 216 ds.version = version; 211 217 return ds; 212 218 } -
trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java
r1499 r1523 211 211 final int[] ret = new int[1]; 212 212 Visitor v = new Visitor(){ 213 public void visit(Node n) { ret[0] = 1; } 214 public void visit(Way w) { ret[0] = 2; } 215 public void visit(Relation e) { ret[0] = 3; } 213 public void visit(Node n) { ret[0] = 0; } 214 public void visit(Way w) { ret[0] = 1; } 215 public void visit(Relation e) { ret[0] = 2; } 216 public void visit(Changeset cs) { ret[0] = 3; } 216 217 }; 217 218 visit(v); -
trunk/src/org/openstreetmap/josm/data/osm/visitor/AddVisitor.java
r1169 r1523 14 14 * @author imi 15 15 */ 16 public class AddVisitor implementsVisitor {16 public class AddVisitor extends AbstractVisitor { 17 17 18 18 protected final DataSet ds; -
trunk/src/org/openstreetmap/josm/data/osm/visitor/AllNodesVisitor.java
r1169 r1523 16 16 * @author imi 17 17 */ 18 public class AllNodesVisitor implementsVisitor {18 public class AllNodesVisitor extends AbstractVisitor { 19 19 20 20 /** -
trunk/src/org/openstreetmap/josm/data/osm/visitor/BoundingXYVisitor.java
r1415 r1523 17 17 * @author imi 18 18 */ 19 public class BoundingXYVisitor implementsVisitor {19 public class BoundingXYVisitor extends AbstractVisitor { 20 20 21 21 public EastNorth min, max; -
trunk/src/org/openstreetmap/josm/data/osm/visitor/CollectBackReferencesVisitor.java
r1169 r1523 19 19 * @author imi 20 20 */ 21 public class CollectBackReferencesVisitor implementsVisitor {21 public class CollectBackReferencesVisitor extends AbstractVisitor { 22 22 23 23 private final DataSet ds; -
trunk/src/org/openstreetmap/josm/data/osm/visitor/CreateOsmChangeVisitor.java
r1169 r1523 6 6 import java.util.Map; 7 7 8 import org.openstreetmap.josm.Main;9 8 import org.openstreetmap.josm.data.osm.Changeset; 10 9 import org.openstreetmap.josm.data.osm.Node; … … 12 11 import org.openstreetmap.josm.data.osm.Relation; 13 12 import org.openstreetmap.josm.data.osm.Way; 13 import org.openstreetmap.josm.io.OsmApi; 14 14 import org.openstreetmap.josm.io.OsmWriter; 15 15 … … 22 22 * 23 23 */ 24 public class CreateOsmChangeVisitor implementsVisitor {24 public class CreateOsmChangeVisitor extends AbstractVisitor { 25 25 26 StringBuffer document; 27 String currentMode; 28 Changeset changeset; 29 PrintWriter writer; 30 StringWriter swriter; 31 OsmWriter osmwriter; 26 private String currentMode; 27 private PrintWriter writer; 28 private StringWriter swriter; 29 private OsmWriter osmwriter; 30 private OsmApi api; 32 31 33 public CreateOsmChangeVisitor(Changeset changeset ) {32 public CreateOsmChangeVisitor(Changeset changeset, OsmApi api) { 34 33 writer = new PrintWriter(swriter = new StringWriter()); 35 34 writer.write("<osmChange version=\""); 36 writer.write( Main.pref.get("osm-server.version", "0.6"));35 writer.write(api.getVersion()); 37 36 writer.write("\" generator=\"JOSM\">\n"); 38 this.changeset = changeset; 39 osmwriter = new OsmWriter(writer, false, changeset); 37 this.api = api; 38 // need to set osmConform = false here so that negative IDs get transmitted. 39 // this also enables unnecessary and (if the API were more strict) potentially 40 // harmful action="..." attributes. 41 osmwriter = new OsmWriter(writer, false, api.getVersion()); 42 osmwriter.setChangeset(changeset); 40 43 } 41 44 45 // FIXME: This should really NOT use a visitor pattern, it looks 46 // stupid. Just have one method named "write" instead of three "visit"s. 47 42 48 public void visit(Node n) { 43 49 if (n.deleted) { 44 50 switchMode("delete"); 45 writer.write("<node id=\""); 46 writer.write(Long.toString(n.id)); 47 writer.write("\" version=\""); 48 writer.write(Long.toString(n.version)); 49 writer.write("\" changeset=\""); 50 writer.write(Long.toString(changeset.id)); 51 writer.write("\" />\n"); 51 osmwriter.setWithBody(false); 52 osmwriter.visit(n); 52 53 } else { 53 54 switchMode((n.id == 0) ? "create" : "modify"); 54 n.visit(osmwriter); 55 osmwriter.setWithBody(true); 56 osmwriter.visit(n); 55 57 } 56 58 } 57 58 59 public void visit(Way w) { 59 60 if (w.deleted) { 60 61 switchMode("delete"); 61 writer.write("<way id=\""); 62 writer.write(Long.toString(w.id)); 63 writer.write("\" version=\""); 64 writer.write(Long.toString(w.version)); 65 writer.write("\" changeset=\""); 66 writer.write(Long.toString(changeset.id)); 67 writer.write("\" />\n"); 62 osmwriter.setWithBody(false); 63 osmwriter.visit(w); 68 64 } else { 69 65 switchMode((w.id == 0) ? "create" : "modify"); 70 w.visit(osmwriter); 66 osmwriter.setWithBody(true); 67 osmwriter.visit(w); 71 68 } 72 69 } 73 74 70 public void visit(Relation r) { 75 71 if (r.deleted) { 76 72 switchMode("delete"); 77 writer.write("<relation id=\""); 78 writer.write(Long.toString(r.id)); 79 writer.write("\" version=\""); 80 writer.write(Long.toString(r.version)); 81 writer.write("\" changeset=\""); 82 writer.write(Long.toString(changeset.id)); 83 writer.write("\" />\n"); 73 osmwriter.setWithBody(false); 74 osmwriter.visit(r); 84 75 } else { 85 76 switchMode((r.id == 0) ? "create" : "modify"); 86 r.visit(osmwriter); 77 osmwriter.setWithBody(true); 78 osmwriter.visit(r); 87 79 } 88 80 } 89 81 90 82 private void switchMode(String newMode) { 91 83 if ((newMode != null && !newMode.equals(currentMode))||(newMode == null && currentMode != null)) { … … 99 91 writer.write(newMode); 100 92 writer.write(" version=\""); 101 writer.write( Main.pref.get("osm-server.version", "0.6"));93 writer.write(api.getVersion()); 102 94 writer.write("\" generator=\"JOSM\">\n"); 103 95 } -
trunk/src/org/openstreetmap/josm/data/osm/visitor/DeleteVisitor.java
r1169 r1523 14 14 * @author imi 15 15 */ 16 public class DeleteVisitor implementsVisitor {16 public class DeleteVisitor extends AbstractVisitor { 17 17 18 18 private final DataSet ds; -
trunk/src/org/openstreetmap/josm/data/osm/visitor/MapPaintVisitor.java
r1501 r1523 16 16 import java.awt.Stroke; 17 17 import java.awt.geom.GeneralPath; 18 import java.util.ArrayList; 18 19 import java.util.Arrays; 19 import java.util.ArrayList;20 20 import java.util.Collection; 21 import java.util.Iterator; 21 22 import java.util.LinkedList; 22 import java.util.Locale;23 import java.util.Iterator;24 23 25 24 import javax.swing.ImageIcon; 26 25 27 26 import org.openstreetmap.josm.Main; 27 import org.openstreetmap.josm.data.coor.EastNorth; 28 import org.openstreetmap.josm.data.coor.LatLon; 28 29 import org.openstreetmap.josm.data.osm.DataSet; 29 30 import org.openstreetmap.josm.data.osm.Node; … … 32 33 import org.openstreetmap.josm.data.osm.RelationMember; 33 34 import org.openstreetmap.josm.data.osm.Way; 34 import org.openstreetmap.josm.data.osm.visitor.SimplePaintVisitor;35 import org.openstreetmap.josm.data.coor.LatLon;36 import org.openstreetmap.josm.data.coor.EastNorth;37 35 import org.openstreetmap.josm.gui.mappaint.AreaElemStyle; 38 36 import org.openstreetmap.josm.gui.mappaint.ElemStyle; -
trunk/src/org/openstreetmap/josm/data/osm/visitor/MergeVisitor.java
r1499 r1523 22 22 * @author imi 23 23 */ 24 public class MergeVisitor implementsVisitor {24 public class MergeVisitor extends AbstractVisitor { 25 25 26 26 /** -
trunk/src/org/openstreetmap/josm/data/osm/visitor/NameVisitor.java
r1169 r1523 20 20 * @author imi 21 21 */ 22 public class NameVisitor implementsVisitor {22 public class NameVisitor extends AbstractVisitor { 23 23 24 24 /** -
trunk/src/org/openstreetmap/josm/data/osm/visitor/SimplePaintVisitor.java
r1499 r1523 33 33 * @author imi 34 34 */ 35 public class SimplePaintVisitor implementsVisitor {35 public class SimplePaintVisitor extends AbstractVisitor { 36 36 37 37 public final static Color darkerblue = new Color(0,0,96); -
trunk/src/org/openstreetmap/josm/data/osm/visitor/Visitor.java
r1169 r1523 2 2 package org.openstreetmap.josm.data.osm.visitor; 3 3 4 import org.openstreetmap.josm.data.osm.Changeset; 4 5 import org.openstreetmap.josm.data.osm.Relation; 5 6 import org.openstreetmap.josm.data.osm.Node; … … 16 17 void visit(Way w); 17 18 void visit(Relation e); 19 void visit(Changeset cs); 18 20 } -
trunk/src/org/openstreetmap/josm/gui/MainApplication.java
r1496 r1523 3 3 package org.openstreetmap.josm.gui; 4 4 5 import org.xnap.commons.i18n.I18nFactory;6 5 import static org.openstreetmap.josm.tools.I18n.i18n; 7 6 import static org.openstreetmap.josm.tools.I18n.tr; … … 26 25 import org.openstreetmap.josm.tools.BugReportExceptionHandler; 27 26 import org.openstreetmap.josm.tools.ImageProvider; 27 import org.xnap.commons.i18n.I18nFactory; 28 28 29 29 /** … … 94 94 String localeName = null; // The locale to use 95 95 96 // Check if passed as parameter97 if (args.containsKey("language"))96 // Check if passed as parameter 97 if (args.containsKey("language")) 98 98 localeName = (String)(args.get("language").toArray()[0]); 99 99 … … 104 104 Locale l; 105 105 Locale d = Locale.getDefault(); 106 if (localeName.equals("he")) localeName = "iw_IL";106 if (localeName.equals("he")) localeName = "iw_IL"; 107 107 int i = localeName.indexOf('_'); 108 108 if (i > 0) { … … 115 115 i18n = I18nFactory.getI18n(MainApplication.class); 116 116 } catch (MissingResourceException ex) { 117 if(!l.getLanguage().equals("en")) 118 { 117 if (!l.getLanguage().equals("en")) { 119 118 System.out.println(tr("Unable to find translation for the locale {0}. Reverting to {1}.", 120 119 l.getDisplayName(), d.getDisplayName())); 121 120 Locale.setDefault(d); 122 } 123 else 124 { 121 } else { 125 122 i18n = null; 126 123 } … … 165 162 splash.setStatus(tr("Setting defaults")); 166 163 preConstructorInit(args); 164 removeObsoletePreferences(); 167 165 splash.setStatus(tr("Creating main GUI")); 168 166 JFrame mainFrame = new JFrame(tr("Java OpenStreetMap Editor")); … … 181 179 mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); 182 180 183 EventQueue.invokeLater(new Runnable() {181 EventQueue.invokeLater(new Runnable() { 184 182 public void run() { 185 183 main.postConstructorProcessCmdLine(args); … … 188 186 } 189 187 190 } 188 /** 189 * Removes obsolete preference settings. If you throw out a once-used preference 190 * setting, add it to the list here with an expiry date (written as comment). If you 191 * see something with an expiry date in the past, remove it from the list. 192 */ 193 public static void removeObsoletePreferences() { 194 String[] obsolete = { 195 "sample.preference.that.does.not.exist", // sample comment, expiry date should go here 196 "osm-server.version", // remove this around 10/2009 197 "osm-server.additional-versions", // remove this around 10/2009 198 null 199 }; 200 for (String i : obsolete) { 201 if (i == null) continue; 202 if (Main.pref.hasKey(i)) { 203 Main.pref.removeFromCollection(i, Main.pref.get(i)); 204 System.out.println(tr("Preference setting {0} has been removed since it is no longer used.", i)); 205 } 206 } 207 } 208 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/ConflictDialog.java
r1419 r1523 2 2 package org.openstreetmap.josm.gui.dialogs; 3 3 4 import static org.openstreetmap.josm.tools.I18n.marktr; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 import static org.openstreetmap.josm.tools.I18n.marktr;6 6 7 7 import java.awt.BorderLayout; … … 22 22 import javax.swing.DefaultListModel; 23 23 import javax.swing.JList; 24 import javax.swing.JOptionPane;25 24 import javax.swing.JPanel; 26 25 import javax.swing.JScrollPane; … … 33 32 import org.openstreetmap.josm.data.SelectionChangedListener; 34 33 import org.openstreetmap.josm.data.osm.DataSet; 34 import org.openstreetmap.josm.data.osm.Node; 35 import org.openstreetmap.josm.data.osm.OsmPrimitive; 35 36 import org.openstreetmap.josm.data.osm.Relation; 36 37 import org.openstreetmap.josm.data.osm.RelationMember; 37 import org.openstreetmap.josm.data.osm.Node;38 import org.openstreetmap.josm.data.osm.OsmPrimitive;39 38 import org.openstreetmap.josm.data.osm.Way; 39 import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor; 40 40 import org.openstreetmap.josm.data.osm.visitor.Visitor; 41 41 import org.openstreetmap.josm.gui.ConflictResolver; … … 171 171 return; 172 172 g.setColor(preferencesColor); 173 Visitor conflictPainter = new Visitor(){173 Visitor conflictPainter = new AbstractVisitor(){ 174 174 public void visit(Node n) { 175 175 Point p = nc.getPoint(n.eastNorth); -
trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java
r1508 r1523 21 21 import java.awt.image.BufferedImage; 22 22 import java.io.File; 23 import java.util.ArrayList; 23 24 import java.util.Collection; 24 25 import java.util.HashSet; … … 26 27 import java.util.LinkedList; 27 28 import java.util.Set; 28 import java.util.ArrayList;29 29 30 30 import javax.swing.AbstractAction; … … 43 43 import org.openstreetmap.josm.data.coor.EastNorth; 44 44 import org.openstreetmap.josm.data.coor.LatLon; 45 import org.openstreetmap.josm.data.gpx.GpxData; 46 import org.openstreetmap.josm.data.gpx.GpxTrack; 47 import org.openstreetmap.josm.data.gpx.WayPoint; 45 48 import org.openstreetmap.josm.data.osm.DataSet; 46 49 import org.openstreetmap.josm.data.osm.DataSource; 47 import org.openstreetmap.josm.data.osm.Relation;48 50 import org.openstreetmap.josm.data.osm.Node; 49 51 import org.openstreetmap.josm.data.osm.OsmPrimitive; 52 import org.openstreetmap.josm.data.osm.Relation; 50 53 import org.openstreetmap.josm.data.osm.Way; 54 import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor; 51 55 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 52 56 import org.openstreetmap.josm.data.osm.visitor.MapPaintVisitor; 53 57 import org.openstreetmap.josm.data.osm.visitor.MergeVisitor; 54 58 import org.openstreetmap.josm.data.osm.visitor.SimplePaintVisitor; 55 import org.openstreetmap.josm.data.osm.visitor.Visitor;56 import org.openstreetmap.josm.data.gpx.GpxData;57 import org.openstreetmap.josm.data.gpx.GpxTrack;58 import org.openstreetmap.josm.data.gpx.WayPoint;59 59 import org.openstreetmap.josm.gui.MapView; 60 60 import org.openstreetmap.josm.gui.dialogs.ConflictDialog; … … 73 73 public class OsmDataLayer extends Layer { 74 74 75 public final static class DataCountVisitor implementsVisitor {75 public final static class DataCountVisitor extends AbstractVisitor { 76 76 public final int[] normal = new int[3]; 77 77 public final int[] deleted = new int[3]; … … 215 215 tool += undeletedSize(data.nodes)+" "+trn("node", "nodes", undeletedSize(data.nodes))+", "; 216 216 tool += undeletedSize(data.ways)+" "+trn("way", "ways", undeletedSize(data.ways)); 217 if (data.version != null) tool += ", " + tr("version {0}", data.version); 217 218 if (associatedFile != null) 218 219 tool = "<html>"+tool+"<br>"+associatedFile.getPath()+"</html>"; … … 236 237 for (DataSource src : ((OsmDataLayer)from).data.dataSources) 237 238 data.dataSources.add(src); 239 240 // copy the merged layer's API version, downgrade if required 241 if (data.version == null) { 242 data.version = ((OsmDataLayer)from).data.version; 243 } else { 244 if ("0.5".equals(data.version) ^ "0.5".equals(((OsmDataLayer)from).data.version)) { 245 System.err.println("Warning: mixing 0.6 and 0.5 data results in version 0.5"); 246 data.version = "0.5"; 247 } 248 } 238 249 fireDataChange(); 239 250 // repaint to make sure new data is displayed properly. … … 349 360 p.add(new JLabel(s, ImageProvider.get("data", counter.names[i]), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0)); 350 361 } 362 p.add(new JLabel(tr("API version: {0}", (data.version != null) ? data.version : tr("unset")))); 363 351 364 return p; 352 365 } … … 447 460 } 448 461 449 public boolean containsPoint(LatLon coor) 450 { 462 public boolean containsPoint(LatLon coor) { 451 463 // we'll assume that if this has no data sources 452 464 // that it also has no borders -
trunk/src/org/openstreetmap/josm/io/DiffResultReader.java
r1415 r1523 5 5 6 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 7 import java.io.StringReader; 9 8 import java.util.Collection; 10 9 import java.util.HashMap; … … 18 17 import org.openstreetmap.josm.data.osm.Relation; 19 18 import org.openstreetmap.josm.data.osm.Way; 20 import org.openstreetmap.josm.data.osm.visitor. Visitor;19 import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor; 21 20 import org.openstreetmap.josm.gui.PleaseWaitDialog; 22 21 import org.xml.sax.Attributes; … … 27 26 /** 28 27 */ 29 public class DiffResultReader implementsVisitor {28 public class DiffResultReader extends AbstractVisitor { 30 29 31 30 /** … … 65 64 * Parse the given input source and return the dataset. 66 65 */ 67 public static void parseDiffResult( InputStreamsource, Collection<OsmPrimitive> osm, Collection<OsmPrimitive> processed, Map<OsmPrimitive,Long> newIdMap, PleaseWaitDialog pleaseWaitDlg)66 public static void parseDiffResult(String source, Collection<OsmPrimitive> osm, Collection<OsmPrimitive> processed, Map<OsmPrimitive,Long> newIdMap, PleaseWaitDialog pleaseWaitDlg) 68 67 throws SAXException, IOException { 69 68 … … 71 70 drr.processed = processed; 72 71 drr.newIdMap = newIdMap; 73 InputSource inputSource = new InputSource(new InputStreamReader(source, "UTF-8"));72 InputSource inputSource = new InputSource(new StringReader(source)); 74 73 try { 75 74 SAXParserFactory.newInstance().newSAXParser().parse(inputSource, drr.new Parser()); … … 85 84 86 85 for (OsmPrimitive p : osm) { 87 System.out.println("old: "+ p);86 //System.out.println("old: "+ p); 88 87 p.visit(drr); 89 System.out.println("new: "+ p);90 System.out.println("");88 //System.out.println("new: "+ p); 89 //System.out.println(""); 91 90 } 92 91 } -
trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java
r1169 r1523 22 22 InputStream fs = null; 23 23 24 public MirroredInputStream(String name) throws IOException 25 { 24 public MirroredInputStream(String name) throws IOException { 26 25 this(name, null, -1L); 27 26 } 28 27 29 public MirroredInputStream(String name, long maxTime) throws IOException 30 { 28 public MirroredInputStream(String name, long maxTime) throws IOException { 31 29 this(name, null, maxTime); 32 30 } 33 31 34 public MirroredInputStream(String name, String destDir, long maxTime) throws IOException 35 { 32 public MirroredInputStream(String name, String destDir, long maxTime) throws IOException { 36 33 URL url; 37 34 File file = null; 38 try 39 { 35 try { 40 36 url = new URL(name); 41 if(url.getProtocol().equals("file")) 42 { 37 if (url.getProtocol().equals("file")) { 43 38 file = new File(name.substring("file:/".length())); 44 if (!file.exists())39 if (!file.exists()) 45 40 file = new File(name.substring("file://".length())); 41 } else { 42 file = checkLocal(url, destDir, maxTime); 46 43 } 47 else 48 file = checkLocal(url, destDir, maxTime); 49 } 50 catch(java.net.MalformedURLException e) 51 { 52 if(name.startsWith("resource://")) 53 { 44 } catch (java.net.MalformedURLException e) { 45 if(name.startsWith("resource://")) { 54 46 fs = getClass().getResourceAsStream( 55 47 name.substring("resource:/".length())); 56 48 return; 57 49 } 58 else 59 file = new File(name); 50 file = new File(name); 60 51 } 61 if (file == null)52 if (file == null) 62 53 throw new IOException(); 63 54 fs = new FileInputStream(file); 64 55 } 65 56 66 private File checkLocal(URL url, String destDir, long maxTime) 67 { 57 private File checkLocal(URL url, String destDir, long maxTime) { 68 58 String localPath = Main.pref.get("mirror." + url); 69 59 File file = null; 70 if(localPath != null && localPath.length() > 0) 71 { 60 if (localPath != null && localPath.length() > 0) { 72 61 String[] lp = localPath.split(";"); 73 62 file = new File(lp[1]); 74 if (maxTime <= 0)63 if (maxTime <= 0) 75 64 maxTime = Main.pref.getInteger("mirror.maxtime", 7*24*60*60); 76 if(System.currentTimeMillis() - Long.parseLong(lp[0]) < maxTime*1000) 77 { 78 if(file.exists()) 79 { 65 if (System.currentTimeMillis() - Long.parseLong(lp[0]) < maxTime*1000) { 66 if(file.exists()) { 80 67 return file; 81 68 } … … 86 73 87 74 File destDirFile = new File(destDir); 88 if (!destDirFile.exists())75 if (!destDirFile.exists()) 89 76 destDirFile.mkdirs(); 90 77 … … 93 80 BufferedOutputStream bos = null; 94 81 BufferedInputStream bis = null; 95 try 96 { 82 try { 97 83 URLConnection conn = url.openConnection(); 98 84 conn.setConnectTimeout(5000); … … 101 87 byte[] buffer = new byte[4096]; 102 88 int length; 103 while ((length = bis.read(buffer)) > -1)89 while ((length = bis.read(buffer)) > -1) 104 90 bos.write(buffer, 0, length); 105 } 106 catch(IOException ioe) 107 { 108 if(file != null) 91 } catch(IOException ioe) { 92 if (file != null) 109 93 return file; 110 else 111 return null; 112 } 113 finally 114 { 115 if(bis != null) 116 { 117 try 118 { 94 return null; 95 } finally { 96 if (bis != null) { 97 try { 119 98 bis.close(); 120 } 121 catch (IOException e) 122 { 99 } catch (IOException e) { 123 100 e.printStackTrace(); 124 101 } 125 102 } 126 if(bos != null) 127 { 128 try 129 { 103 if (bos != null) { 104 try { 130 105 bos.close(); 131 } 132 catch (IOException e) 133 { 106 } catch (IOException e) { 134 107 e.printStackTrace(); 135 108 } -
trunk/src/org/openstreetmap/josm/io/MyHttpHandler.java
r1169 r1523 9 9 // Basically a copy of sun.net.www.protocol.http.Handler 10 10 public class MyHttpHandler extends sun.net.www.protocol.http.Handler { 11 12 11 protected String proxy; 12 protected int proxyPort; 13 13 14 15 16 17 18 14 public MyHttpHandler() { 15 super(); 16 proxy = null; 17 proxyPort = -1; 18 } 19 19 20 21 22 23 24 25 26 27 20 protected java.net.URLConnection openConnection(URL u) 21 throws IOException { 22 return openConnection(u, (Proxy) null); 23 } 24 public MyHttpHandler(String proxy, int port) { 25 this.proxy = proxy; 26 proxyPort = port; 27 } 28 28 29 30 31 32 29 protected java.net.URLConnection openConnection(URL u, Proxy p) 30 throws IOException { 31 return new MyHttpURLConnection(u, p, this); 32 } 33 33 } -
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r1397 r1523 49 49 */ 50 50 static { 51 //TODO: refactor this crap (maybe just insert the damn auth http-header by yourself) 51 // TODO: current authentication handling is sub-optimal in that it seems to use the same authenticator for 52 // any kind of request. HTTP requests executed by plugins, e.g. to password-protected WMS servers, 53 // will use the same username/password which is undesirable. 52 54 try { 53 55 HttpURLConnection.setFollowRedirects(true); -
trunk/src/org/openstreetmap/josm/io/OsmReader.java
r1499 r1523 8 8 import java.io.InputStreamReader; 9 9 import java.util.ArrayList; 10 import java.util.Arrays;11 10 import java.util.Collection; 12 11 import java.util.HashMap; 13 import java.util.HashSet;14 12 import java.util.LinkedList; 15 13 import java.util.Map; … … 130 128 private Map<OsmPrimitiveData, Collection<RelationMemberData>> relations = new HashMap<OsmPrimitiveData, Collection<RelationMemberData>>(); 131 129 132 /**133 * List of protocol versions that will be accepted on reading134 */135 private HashSet<String> allowedVersions = new HashSet<String>();136 137 130 private class Parser extends DefaultHandler { 138 131 /** … … 164 157 if (atts == null) 165 158 throw new SAXException(tr("Unknown version")); 166 if (!allowedVersions.contains(atts.getValue("version"))) 167 throw new SAXException(tr("Unknown version")+": "+atts.getValue("version")); 159 String v = atts.getValue("version"); 160 if (v == null) 161 throw new SAXException(tr("Version number missing from OSM data")); 162 if (!(v.equals("0.5") || v.equals("0.6"))) 163 throw new SAXException(tr("Unknown version: {0}", v)); 168 164 // save generator attribute for later use when creating DataSource objects 169 165 generator = atts.getValue("generator"); 170 171 172 } else if (qName.equals("bound")) { 173 // old style bounds. 174 // TODO: remove this around 1st October 2008. 175 // - this is a bit of a hack; since we still write out old style bound objects, 176 // we don't want to load them both. so when writing, we add a "note" tag the our 177 // old-style bound, and when reading, ignore those with a "note". 178 String note = atts.getValue("note"); 179 if (note == null) { 180 System.out.println("Notice: old style <bound> element detected; support for these will be dropped in a future release."); 181 String bbox = atts.getValue("box"); 182 String origin = atts.getValue("origin"); 183 if (origin == null) origin = ""; 184 if (bbox != null) { 185 String[] b = bbox.split(","); 186 Bounds bounds = new Bounds(); 187 if (b.length == 4) 188 bounds = new Bounds( 189 new LatLon(Double.parseDouble(b[0]),Double.parseDouble(b[1])), 190 new LatLon(Double.parseDouble(b[2]),Double.parseDouble(b[3]))); 191 DataSource src = new DataSource(bounds, origin); 192 ds.dataSources.add(src); 193 } 194 } 166 ds.version = v; 167 195 168 } else if (qName.equals("bounds")) { 196 169 // new style bounds. … … 277 250 return Double.parseDouble(atts.getValue(value)); 278 251 } 279 }280 281 /**282 * Constructor initializes list of allowed protocol versions.283 */284 public OsmReader() {285 // first add the main server version286 allowedVersions.add(Main.pref.get("osm-server.version", "0.5"));287 // now also add all compatible versions288 String[] additionalVersions =289 Main.pref.get("osm-server.additional-versions", "").split("/,/");290 if (additionalVersions.length == 1 && additionalVersions[0].length() == 0)291 additionalVersions = new String[] {};292 allowedVersions.addAll(Arrays.asList(additionalVersions));293 252 } 294 253 … … 491 450 osm.references = ref == null ? new DataSet() : ref; 492 451 493 494 452 currSource = source; 495 453 -
trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java
r1169 r1523 32 32 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data...")); 33 33 final DataSet data = OsmReader.parseDataSet(in, null, Main.pleaseWaitDlg); 34 // String origin = Main.pref.get("osm-server.url")+"/"+Main.pref.get("osm-server.version", "0.5");35 34 // Bounds bounds = new Bounds(new LatLon(lat1, lon1), new LatLon(lat2, lon2)); 36 35 // DataSource src = new DataSource(bounds, origin); -
trunk/src/org/openstreetmap/josm/io/OsmServerObjectReader.java
r1444 r1523 52 52 final DataSet data = osm.getDs(); 53 53 54 // String origin = Main.pref.get("osm-server.url")+"/"+Main.pref.get("osm-server.version", "0.5");55 54 // Bounds bounds = new Bounds(new LatLon(lat1, lon1), new LatLon(lat2, lon2)); 56 55 // DataSource src = new DataSource(bounds, origin); -
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r1353 r1523 27 27 */ 28 28 public abstract class OsmServerReader extends OsmConnection { 29 30 private OsmApi api = new OsmApi(); 31 29 32 /** 30 33 * Open a connection to the given url and return a reader on the input stream … … 35 38 */ 36 39 protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException { 37 String version = Main.pref.get("osm-server.version", "0.5");38 urlStr = Main.pref.get("osm-server.url")+"/"+version+"/"+ urlStr;40 api.initialize(); 41 urlStr = api.getBaseUrl() + urlStr; 39 42 return getInputStreamRaw(urlStr, pleaseWaitDlg); 40 43 } … … 43 46 44 47 // System.out.println("download: "+urlStr); 45 initAuthentication();46 48 URL url = new URL(urlStr); 47 49 activeConnection = (HttpURLConnection)url.openConnection(); -
trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java
r1415 r1523 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.io.BufferedReader;7 import java.io.ByteArrayOutputStream;8 import java.io.IOException;9 import java.io.InputStream;10 import java.io.InputStreamReader;11 import java.io.OutputStream;12 import java.io.OutputStreamWriter;13 import java.io.PrintWriter;14 import java.io.UnsupportedEncodingException;15 import java.lang.Math;16 import java.net.ConnectException;17 import java.net.HttpURLConnection;18 import java.net.URL;19 import java.net.UnknownHostException;20 import java.net.SocketTimeoutException;21 6 import java.util.Collection; 22 7 import java.util.LinkedList; 8 23 9 import javax.swing.JOptionPane; 24 10 25 11 import org.openstreetmap.josm.Main; 26 import org.openstreetmap.josm.data.osm.Relation;27 import org.openstreetmap.josm.data.osm.Node;28 12 import org.openstreetmap.josm.data.osm.OsmPrimitive; 29 import org.openstreetmap.josm.data.osm.Way;30 import org.openstreetmap.josm.data.osm.Changeset;31 import org.openstreetmap.josm.data.osm.visitor.CreateOsmChangeVisitor;32 13 import org.openstreetmap.josm.data.osm.visitor.NameVisitor; 33 import org.openstreetmap.josm.data.osm.visitor.Visitor;34 import org.xml.sax.SAXException;35 import org.openstreetmap.josm.io.XmlWriter.OsmWriterInterface;36 14 37 15 /** … … 41 19 * those in deleted, which are ignored - All objects in deleted list are 42 20 * deleted. - All remaining objects with modified flag set are updated. 43 *44 * This class implements visitor and will perform the correct upload action on45 * the visited element.46 *47 * @author imi48 21 */ 49 public class OsmServerWriter extends OsmConnection implements Visitor{22 public class OsmServerWriter { 50 23 51 24 /** … … 58 31 public Collection<OsmPrimitive> processed; 59 32 60 /** 61 * Whether the operation should be aborted as soon as possible. 62 */ 63 // use the inherited variable 64 // private boolean cancel = false; 65 66 /** 67 * Object describing current changeset 68 */ 69 private Changeset changeset; 70 71 /** 72 * Send the dataset to the server. Ask the user first and does nothing if he 73 * does not want to send the data. 74 */ 33 private OsmApi api = new OsmApi(); 34 75 35 private static final int MSECS_PER_SECOND = 1000; 76 36 private static final int SECONDS_PER_MINUTE = 60; … … 96 56 } 97 57 98 public void uploadOsm(Collection<OsmPrimitive> list) throws SAXException { 58 /** 59 * Send the dataset to the server. 60 * @param the_version version of the data set 61 * @param list list of objects to send 62 */ 63 public void uploadOsm(String the_version, Collection<OsmPrimitive> list) { 99 64 processed = new LinkedList<OsmPrimitive>(); 100 initAuthentication();65 api.initialize(); 101 66 102 67 Main.pleaseWaitDlg.progress.setMaximum(list.size()); 103 68 Main.pleaseWaitDlg.progress.setValue(0); 104 69 105 // controls whether or not we open and close a changeset. API 0.6 requires changesets. 106 boolean useChangesets = Main.pref.get("osm-server.version", "0.5").equals("0.6"); 70 boolean useChangesets = api.hasChangesetSupport(); 71 72 // controls whether or not we try and upload the whole bunch in one go 73 boolean useDiffUploads = Main.pref.getBoolean("osm-server.atomic-upload", 74 "0.6".equals(api.getVersion())); 107 75 108 // controls whether or not we try and uplaod the whole bunch in one go 109 boolean useDiffUploads = Main.pref.getBoolean("osm-server.atomic-upload", 110 Main.pref.get("osm-server.version", "0.5").equals("0.6")); 111 76 // solicit commit comment from user 112 77 String comment = null; 113 78 while (useChangesets && comment == null) { … … 117 82 if (comment == null) 118 83 return; 119 / * Don't let people just hit enter */120 if ( comment.trim().length() >= 3)84 // Don't let people just hit enter 85 if (comment.trim().length() >= 3) 121 86 break; 122 87 comment = null; 123 88 } 89 90 // create changeset if required 124 91 try { 125 if (useChangesets && !startChangeset(10, comment)) 126 return; 127 } 128 catch (OsmTransferException ex) { 92 if (useChangesets) api.createChangeset(comment); 93 } catch (OsmTransferException ex) { 129 94 dealWithTransferException(ex); 130 95 return; … … 133 98 try { 134 99 if (useDiffUploads) { 135 uploadDiff(10, list); 100 // all in one go 101 processed.addAll(api.uploadDiff(list)); 136 102 } else { 103 // upload changes individually (90% of code is for the status display...) 137 104 NameVisitor v = new NameVisitor(); 138 105 uploadStartTime = System.currentTimeMillis(); 139 106 for (OsmPrimitive osm : list) { 140 if (cancel)141 return;142 107 osm.visit(v); 143 108 int progress = Main.pleaseWaitDlg.progress.getValue(); 144 109 String time_left_str = timeLeft(progress, list.size()); 145 110 Main.pleaseWaitDlg.currentAction.setText( 146 tr("{0}% ({1}/{2}), {3} left. Uploading {4}: {5} (id: {6})", 147 Math.round(100.0*progress/list.size()), progress, 148 list.size(), time_left_str, tr(v.className), v.name, osm.id)); 149 osm.visit(this); 111 tr("{0}% ({1}/{2}), {3} left. Uploading {4}: {5} (id: {6})", 112 Math.round(100.0*progress/list.size()), progress, 113 list.size(), time_left_str, tr(v.className), v.name, osm.id)); 114 makeApiRequest(osm); 115 processed.add(osm); 150 116 Main.pleaseWaitDlg.progress.setValue(progress+1); 151 117 } 152 118 } 153 if (useChangesets) stopChangeset(10);154 } catch ( RuntimeException e) {119 if (useChangesets) api.stopChangeset(); 120 } catch (OsmTransferException e) { 155 121 try { 156 if (useChangesets) stopChangeset(10); 157 } 158 catch (OsmTransferException ex) { 159 dealWithTransferException(ex); 160 } 161 e.printStackTrace(); 162 throw new SAXException(tr("An error occurred: {0}",e.getMessage())); 163 } 164 catch (OsmTransferException e) { 165 try { 166 if (useChangesets) stopChangeset(10); 167 } 168 catch (OsmTransferException ex) { 169 dealWithTransferException(ex); 122 if (useChangesets) api.stopChangeset(); 123 } catch (Exception ee) { 124 // ignore nested exception 170 125 } 171 126 dealWithTransferException(e); … … 173 128 } 174 129 175 /* FIXME: This code is terrible, please fix it!!!! */ 176 177 /* Ok, this needs some explanation: The problem is that the code for 178 * retrying requests is intertwined with the code that generates the 179 * actual request. This means that for the retry code for the 180 * changeset stuff, it's basically a copy/cut/change slightly 181 * process. What actually needs to happen is that the retrying needs 182 * to be split from the creation of the requests and the retry loop 183 * handled in one place (preferably without recursion). While at you 184 * can fix the issue where hitting cancel doesn't do anything while 185 * retrying. - Mv0 Apr 2008 186 * 187 * Cancelling has an effect now, maybe it does not always catch on. Florian Heer, Aug 08 188 */ 189 private boolean startChangeset(int retries, String comment) throws OsmTransferException { 190 Main.pleaseWaitDlg.currentAction.setText(tr("Opening changeset...")); 191 changeset = new Changeset(); 192 changeset.put( "created_by", "JOSM" ); 193 changeset.put( "comment", comment ); 194 try { 195 if (cancel) 196 return false; // assume cancel 197 String version = Main.pref.get("osm-server.version", "0.6"); 198 URL url = new URL( 199 Main.pref.get("osm-server.url") + 200 "/" + version + 201 "/" + "changeset" + 202 "/" + "create"); 203 System.out.print("upload to: "+url+ "..." ); 204 activeConnection = (HttpURLConnection)url.openConnection(); 205 activeConnection.setConnectTimeout(15000); 206 activeConnection.setRequestMethod("PUT"); 207 addAuth(activeConnection); 208 209 activeConnection.setDoOutput(true); 210 OutputStream out = activeConnection.getOutputStream(); 211 OsmWriter.output(out, changeset); 212 out.close(); 213 214 activeConnection.connect(); 215 System.out.println("connected"); 216 217 int retCode = activeConnection.getResponseCode(); 218 if (retCode == 200) 219 changeset.id = readId(activeConnection.getInputStream()); 220 System.out.println("got return: "+retCode+" with id "+changeset.id); 221 String retMsg = activeConnection.getResponseMessage(); 222 activeConnection.disconnect(); 223 if (retCode == 404) { 224 throw new OsmTransferException(tr("Server does not support changesets")); 225 } 226 if (retCode != 200 && retCode != 412) { 227 if (retries >= 0) { 228 retries--; 229 if(sleepAndListen()) return false; 230 System.out.println("retrying ("+retries+" left)"); 231 return startChangeset(retries, comment); 232 } 233 234 // Look for a detailed error message from the server 235 retMsg += "\n" + readString(activeConnection.getInputStream()); 236 237 // Report our error 238 ByteArrayOutputStream o = new ByteArrayOutputStream(); 239 OsmWriter.output(o, changeset); 240 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 241 throw new OsmTransferException (retCode + " " + retMsg); 242 } 243 } catch (UnknownHostException e) { 244 throw new OsmTransferException(tr("Unknown host")+": "+e.getMessage(), e); 245 } catch(SocketTimeoutException e) { 246 System.out.println(" timed out, retries left: " + retries); 247 if (cancel) 248 return false; // assume cancel 249 if (retries-- > 0) 250 startChangeset(retries, comment); 251 else 252 throw new OsmTransferException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 253 } 254 catch (ConnectException e) { 255 System.out.println(" timed out, retries left: " + retries); 256 if (cancel) 257 return false; // assume cancel 258 if (retries-- > 0) 259 startChangeset(retries, comment); 260 else 261 throw new OsmTransferException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 262 } 263 264 catch (Exception e) { 265 if (cancel) 266 return false; // assume cancel 267 if (e instanceof OsmTransferException) 268 throw (OsmTransferException)e; 269 if (e instanceof RuntimeException) 270 throw (RuntimeException)e; 271 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 272 } 273 return true; 274 } 275 276 private void uploadDiff(int retries, Collection<OsmPrimitive> list) throws OsmTransferException { 277 278 CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset); 279 280 for (OsmPrimitive osm : list) { 281 int progress = Main.pleaseWaitDlg.progress.getValue(); 282 Main.pleaseWaitDlg.currentAction.setText(tr("Preparing...")); 283 if (cancel) 284 return; 285 osm.visit(duv); 286 Main.pleaseWaitDlg.progress.setValue(progress+1); 287 } 288 System.out.println("the document:\n"); 289 String diff = duv.getDocument(); 290 System.out.println(diff); 291 292 Main.pleaseWaitDlg.currentAction.setText(tr("Uploading...")); 293 try { 294 if (cancel) 295 return; // assume cancel 296 String version = Main.pref.get("osm-server.version", "0.6"); 297 URL url = new URL( 298 Main.pref.get("osm-server.url") + 299 "/" + version + 300 "/" + "changeset" + 301 "/" + changeset.id + 302 "/upload" ); 303 System.out.print("upload to: "+url+ "..." ); 304 activeConnection = (HttpURLConnection)url.openConnection(); 305 activeConnection.setConnectTimeout(15000); 306 activeConnection.setRequestMethod("POST"); 307 addAuth(activeConnection); 308 309 activeConnection.setDoOutput(true); 310 PrintWriter out; 311 try { 312 out = new PrintWriter(new OutputStreamWriter(activeConnection.getOutputStream(), "UTF-8")); 313 } catch (UnsupportedEncodingException e) { 314 throw new RuntimeException(e); 315 } 316 out.print(diff); 317 out.close(); 318 319 activeConnection.connect(); 320 System.out.println("connected"); 321 322 int retCode = activeConnection.getResponseCode(); 323 String retMsg = ""; 324 325 if (retCode == 200) { 326 DiffResultReader.parseDiffResult(activeConnection.getInputStream(), list, processed, duv.getNewIdMap(), Main.pleaseWaitDlg); 327 } else if (retCode != 200 && retCode != 412) { 328 if (retries >= 0) { 329 retries--; 330 if(sleepAndListen()) return; 331 System.out.println("retrying ("+retries+" left)"); 332 stopChangeset(retries); 333 } else { 334 // Look for a detailed error message from the server 335 retMsg += "\n" + readString(activeConnection.getInputStream()); 336 337 // Report our error 338 ByteArrayOutputStream o = new ByteArrayOutputStream(); 339 OsmWriter.output(o, changeset); 340 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 341 throw new OsmTransferException(retCode+" "+retMsg); 342 } 343 } 344 } catch (UnknownHostException e) { 345 throw new OsmTransferException(tr("Unknown host")+": "+e.getMessage(), e); 346 } catch(SocketTimeoutException e) { 347 System.out.println(" timed out, retries left: " + retries); 348 if (cancel) 349 return; // assume cancel 350 if (retries-- > 0) 351 stopChangeset(retries); 352 else 353 throw new OsmTransferException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 354 } catch(ConnectException e) { 355 System.out.println(" timed out, retries left: " + retries); 356 if (cancel) 357 return; // assume cancel 358 if (retries-- > 0) 359 stopChangeset(retries); 360 else 361 throw new OsmTransferException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 362 } catch (Exception e) { 363 if (cancel) 364 return; // assume cancel 365 if (e instanceof OsmTransferException) 366 throw (OsmTransferException)e; 367 if (e instanceof RuntimeException) 368 throw (RuntimeException)e; 369 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 370 } 371 } 372 373 374 private void stopChangeset(int retries) throws OsmTransferException { 375 Main.pleaseWaitDlg.currentAction.setText(tr("Closing changeset...")); 376 try { 377 if (cancel) 378 return; // assume cancel 379 String version = Main.pref.get("osm-server.version", "0.6"); 380 URL url = new URL( 381 Main.pref.get("osm-server.url") + 382 "/" + version + 383 "/" + "changeset" + 384 "/" + changeset.id + 385 "/close" ); 386 System.out.print("upload to: "+url+ "..." ); 387 activeConnection = (HttpURLConnection)url.openConnection(); 388 activeConnection.setConnectTimeout(15000); 389 activeConnection.setRequestMethod("PUT"); 390 addAuth(activeConnection); 391 392 activeConnection.setDoOutput(true); 393 OutputStream out = activeConnection.getOutputStream(); 394 OsmWriter.output(out, changeset); 395 out.close(); 396 397 activeConnection.connect(); 398 System.out.println("connected"); 399 400 int retCode = activeConnection.getResponseCode(); 401 System.out.println("got return: "+retCode); 402 String retMsg = activeConnection.getResponseMessage(); 403 activeConnection.disconnect(); 404 if (retCode == 404) { 405 System.out.println("Server does not support changesets, or the changeset could not be found, continuing"); 406 return; 407 } 408 if (retCode != 200 && retCode != 412) { 409 if (retries >= 0) { 410 retries--; 411 if(sleepAndListen()) return; 412 System.out.println("retrying ("+retries+" left)"); 413 stopChangeset(retries); 414 } else { 415 // Look for a detailed error message from the server 416 retMsg += readString(activeConnection.getInputStream()); 417 418 // Report our error 419 ByteArrayOutputStream o = new ByteArrayOutputStream(); 420 OsmWriter.output(o, changeset); 421 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 422 throw new OsmTransferException(retCode+" "+retMsg); 423 } 424 } 425 } catch (UnknownHostException e) { 426 throw new OsmTransferException(tr("Unknown host")+": "+e.getMessage(), e); 427 } catch(SocketTimeoutException e) { 428 System.out.println(" timed out, retries left: " + retries); 429 if (cancel) 430 return; // assume cancel 431 if (retries-- > 0) 432 stopChangeset(retries); 433 else 434 throw new OsmTransferException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 435 } catch(ConnectException e) { 436 System.out.println(" timed out, retries left: " + retries); 437 if (cancel) 438 return; // assume cancel 439 if (retries-- > 0) 440 stopChangeset(retries); 441 else 442 throw new OsmTransferException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 443 } catch (Exception e) { 444 if (cancel) 445 return; // assume cancel 446 if (e instanceof OsmTransferException) 447 throw (OsmTransferException)e; 448 if (e instanceof RuntimeException) 449 throw (RuntimeException)e; 450 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 451 } 452 } 453 454 private boolean sleepAndListen() { 455 // System.out.print("backing off for 10 seconds..."); 456 for(int i=0; i < 10; i++) { 457 if(cancel || isAuthCancelled()) { 458 if(!cancel) cancel(); 459 return true; 460 } 461 try { 462 Thread.sleep(1000); 463 } catch (InterruptedException ex) {} 464 } 465 return false; 466 } 467 468 /** 469 * Upload a single node. 470 */ 471 public void visit(Node n) { 472 if (n.deleted) { 473 sendRequest("DELETE", "node", n, true); 130 void makeApiRequest(OsmPrimitive osm) throws OsmTransferException { 131 if (osm.deleted) { 132 api.deletePrimitive(osm); 133 } else if (osm.id == 0) { 134 api.createPrimitive(osm); 474 135 } else { 475 sendRequest("PUT", "node", n, true); 476 } 477 processed.add(n); 478 } 479 480 /** 481 * Upload a whole way with the complete node id list. 482 */ 483 public void visit(Way w) { 484 if (w.deleted) { 485 sendRequest("DELETE", "way", w, true); 486 } else { 487 sendRequest("PUT", "way", w, true); 488 } 489 processed.add(w); 490 } 491 492 /** 493 * Upload an relation with all members. 494 */ 495 public void visit(Relation e) { 496 if (e.deleted) { 497 sendRequest("DELETE", "relation", e, true); 498 } else { 499 sendRequest("PUT", "relation", e, true); 500 } 501 processed.add(e); 502 } 503 504 /** 505 * Read a long from the input stream and return it. 506 */ 507 private long readId(InputStream inputStream) throws IOException { 508 BufferedReader in = new BufferedReader(new InputStreamReader( 509 inputStream)); 510 String s = in.readLine(); 511 if (s == null) 512 return 0; 513 try { 514 return Long.parseLong(s); 515 } catch (NumberFormatException e) { 516 return 0; 517 } 518 } 519 520 /** 521 * Consume the input stream and return it as a string. 522 */ 523 private String readString(InputStream inputStream) throws IOException { 524 BufferedReader in = new BufferedReader(new InputStreamReader( 525 inputStream)); 526 StringBuffer sb = new StringBuffer(); 527 String s; 528 while((s = in.readLine()) != null) { 529 sb.append(s); 530 sb.append("\n"); 531 } 532 return sb.toString(); 533 } 534 535 /** 536 * Send the request. The objects id will be replaced if it was 0 before 537 * (on add requests). 538 * 539 * @param requestMethod The http method used when talking with the server. 540 * @param urlSuffix The suffix to add at the server url. 541 * @param osm The primitive to encode to the server. 542 * @param body the body to be sent 543 */ 544 private void sendRequestRetry(String requestMethod, String urlSuffix, 545 OsmPrimitive osm, OsmWriterInterface body, int retries) throws OsmTransferException { 546 try { 547 if (cancel) 548 return; // assume cancel 549 String version = Main.pref.get("osm-server.version", "0.5"); 550 URL url = new URL( 551 new URL(Main.pref.get("osm-server.url") + 552 "/" + version + "/"), 553 urlSuffix + 554 "/" + (osm.id==0 ? "create" : osm.id), 555 new MyHttpHandler()); 556 System.out.print("upload to: "+url+ "..." ); 557 activeConnection = (HttpURLConnection)url.openConnection(); 558 activeConnection.setConnectTimeout(15000); 559 activeConnection.setRequestMethod(requestMethod); 560 addAuth(activeConnection); 561 if (body != null) { 562 activeConnection.setDoOutput(true); 563 OutputStream out = activeConnection.getOutputStream(); 564 OsmWriter.output(out, body); 565 out.close(); 566 } 567 activeConnection.connect(); 568 System.out.println("connected"); 569 570 int retCode = activeConnection.getResponseCode(); 571 /* When creating new, the returned value is the new id, otherwise it is the new version */ 572 if (retCode == 200) { 573 if (osm.id == 0) { 574 osm.id = readId(activeConnection.getInputStream()); 575 osm.version = 1; 576 } else { 577 int read_version = (int)readId(activeConnection.getInputStream()); 578 if (read_version > 0) 579 osm.version = read_version; 580 } 581 } else { 582 System.out.println("got return: "+retCode+" with id "+osm.id); 583 } 584 activeConnection.disconnect(); 585 if (retCode == 410 && requestMethod.equals("DELETE")) 586 return; // everything fine.. was already deleted. 587 else if (retCode != 200) { 588 if (retries >= 0 && retCode != 412) { 589 retries--; 590 if(sleepAndListen()) return; 591 System.out.println("retrying ("+retries+" left)"); 592 sendRequestRetry(requestMethod, urlSuffix, osm, body, retries); 593 } else { 594 String retMsg = activeConnection.getResponseMessage(); 595 // Look for a detailed error message from the server 596 if (activeConnection.getHeaderField("Error") != null) 597 retMsg += "\n" + activeConnection.getHeaderField("Error"); 598 599 // Report our error 600 ByteArrayOutputStream o = new ByteArrayOutputStream(); 601 OsmWriter.output(o, body); 602 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 603 throw new OsmTransferException(retCode+" "+retMsg); 604 } 605 } 606 } catch (UnknownHostException e) { 607 throw new OsmTransferException(tr("Unknown host")+": "+e.getMessage(), e); 608 } catch(SocketTimeoutException e) { 609 System.out.println(" timed out, retries left: " + retries); 610 if (cancel) 611 return; // assume cancel 612 if (retries-- > 0) 613 sendRequestRetry(requestMethod, urlSuffix, osm, body, retries); 614 else 615 throw new OsmTransferException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 616 } catch(ConnectException e) { 617 System.out.println(" timed out, retries left: " + retries); 618 if (cancel) 619 return; // assume cancel 620 if (retries-- > 0) 621 sendRequestRetry(requestMethod, urlSuffix, osm, body, retries); 622 else 623 throw new OsmTransferException (e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 624 } catch (Exception e) { 625 if (cancel) 626 return; // assume cancel 627 if (e instanceof OsmTransferException) 628 throw (OsmTransferException)e; 629 if (e instanceof RuntimeException) 630 throw (RuntimeException)e; 631 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 632 } 633 } 634 635 private void sendRequest(String requestMethod, String urlSuffix, 636 OsmPrimitive osm, boolean addBody) { 637 XmlWriter.OsmWriterInterface body = null; 638 if (addBody) { 639 body = new OsmWriter.Single(osm, true, changeset); 640 } 641 try { 642 sendRequestRetry(requestMethod, urlSuffix, osm, body, 10); 643 } 644 catch (OsmTransferException e) { 645 dealWithTransferException (e); 136 api.modifyPrimitive(osm); 646 137 } 647 138 } … … 649 140 private void dealWithTransferException (OsmTransferException e) { 650 141 Main.pleaseWaitDlg.currentAction.setText(tr("Transfer aborted due to error (will wait for 5 seconds):") + e.getMessage()); 651 cancel = true;652 142 try { 653 143 Thread.sleep(5000); -
trunk/src/org/openstreetmap/josm/io/OsmWriter.java
r1499 r1523 6 6 import java.util.Map.Entry; 7 7 8 import org.openstreetmap.josm. Main;8 import org.openstreetmap.josm.data.osm.Changeset; 9 9 import org.openstreetmap.josm.data.osm.DataSet; 10 10 import org.openstreetmap.josm.data.osm.DataSource; 11 import org.openstreetmap.josm.data.osm.Node; 12 import org.openstreetmap.josm.data.osm.OsmPrimitive; 11 13 import org.openstreetmap.josm.data.osm.Relation; 12 14 import org.openstreetmap.josm.data.osm.RelationMember; 13 import org.openstreetmap.josm.data.osm.Node;14 import org.openstreetmap.josm.data.osm.OsmPrimitive;15 import org.openstreetmap.josm.data.osm.Changeset;16 15 import org.openstreetmap.josm.data.osm.Way; 17 16 import org.openstreetmap.josm.data.osm.visitor.Visitor; … … 26 25 27 26 /** 28 * The counter for new created objects. Startingat -1 and goes down.27 * The counter for newly created objects. Starts at -1 and goes down. 29 28 */ 30 29 private long newIdCounter = -1; 30 31 31 /** 32 32 * All newly created ids and their primitive that uses it. This is a back reference … … 35 35 public HashMap<OsmPrimitive, Long> usedNewIds = new HashMap<OsmPrimitive, Long>(); 36 36 37 private final boolean osmConform; 38 private final Changeset changeset; 39 40 public abstract static class Osm implements OsmWriterInterface { 41 public void header(PrintWriter out) { 42 out.print("<osm version='"); 43 out.print(Main.pref.get("osm-server.version", "0.5")); 44 out.println("' generator='JOSM'>"); 45 } 46 public void footer(PrintWriter out) { 47 out.println("</osm>"); 48 } 49 } 50 51 // simple helper to write the object's class to the out stream 52 private Visitor typeWriteVisitor = new Visitor() { 53 public void visit(Node n) { out.print("node"); } 54 public void visit(Way w) { out.print("way"); } 55 public void visit(Relation e) { out.print("relation"); } 56 }; 57 58 /** 59 * An output writer for function output that writes everything of the given dataset into 60 * the xml 61 */ 62 public static final class All extends Osm { 63 private final DataSet ds; 64 private final boolean osmConform; 65 66 /** 67 * Construct an writer function 68 * @param osmConform <code>true</code>, if the xml should be 100% osm conform. In this 69 * case, not all information can be retrieved later (as example, modified state 70 * is lost and id's remain 0 instead of decrementing from -1) 71 */ 72 public All(DataSet ds, boolean osmConform) { 73 this.ds = ds; 74 this.osmConform = osmConform; 75 } 76 77 public void write(PrintWriter out) { 78 Visitor writer = new OsmWriter(out, osmConform, null); 79 for (Node n : ds.nodes) 80 if (shouldWrite(n)) 81 writer.visit(n); 82 for (Way w : ds.ways) 83 if (shouldWrite(w)) 84 writer.visit(w); 85 for (Relation e : ds.relations) 86 if (shouldWrite(e)) 87 writer.visit(e); 88 } 89 90 private boolean shouldWrite(OsmPrimitive osm) { 91 return osm.id != 0 || !osm.deleted; 92 } 93 94 @Override public void header(PrintWriter out) { 95 super.header(out); 96 for (DataSource s : ds.dataSources) { 97 out.println(" <bounds minlat='" 98 + s.bounds.min.lat()+"' minlon='" 99 + s.bounds.min.lon()+"' maxlat='" 100 + s.bounds.max.lat()+"' maxlon='" 101 + s.bounds.max.lon() 102 +"' origin='"+XmlWriter.encode(s.origin)+"' />"); 103 } 104 } 105 } 106 107 /** 108 * An output writer for functino output that writes only one specific primitive into 109 * the xml 110 */ 111 public static final class Single extends Osm { 112 private final OsmPrimitive osm; 113 private final boolean osmConform; 114 private final Changeset changeset; 115 116 public Single(OsmPrimitive osm, boolean osmConform, Changeset changeset) { 117 this.osm = osm; 118 this.osmConform = osmConform; 119 this.changeset = changeset; 120 } 121 122 public void write(PrintWriter out) { 123 osm.visit(new OsmWriter(out, osmConform, changeset)); 124 } 125 } 126 127 public OsmWriter(PrintWriter out, boolean osmConform, Changeset changeset) { 37 private boolean osmConform; 38 private boolean withBody = true; 39 private String version; 40 private Changeset changeset; 41 42 public OsmWriter(PrintWriter out, boolean osmConform, String version) { 128 43 super(out); 129 44 this.osmConform = osmConform; 130 this.changeset = changeset; 45 this.version = version; 46 } 47 48 public void setWithBody(boolean wb) { 49 this.withBody = wb; 50 } 51 public void setChangeset(Changeset cs) { 52 this.changeset = cs; 53 } 54 public void setVersion(String v) { 55 this.version = v; 56 } 57 58 public void header() { 59 out.println("<?xml version='1.0' encoding='UTF-8'?>"); 60 out.print("<osm version='"); 61 out.print(version); 62 out.println("' generator='JOSM'>"); 63 } 64 public void footer() { 65 out.println("</osm>"); 66 } 67 68 public void writeContent(DataSet ds) { 69 for (Node n : ds.nodes) 70 if (shouldWrite(n)) 71 visit(n); 72 for (Way w : ds.ways) 73 if (shouldWrite(w)) 74 visit(w); 75 for (Relation e : ds.relations) 76 if (shouldWrite(e)) 77 visit(e); 78 } 79 80 private boolean shouldWrite(OsmPrimitive osm) { 81 return osm.id != 0 || !osm.deleted; 82 } 83 84 public void writeDataSources(DataSet ds) { 85 for (DataSource s : ds.dataSources) { 86 out.println(" <bounds minlat='" 87 + s.bounds.min.lat()+"' minlon='" 88 + s.bounds.min.lon()+"' maxlat='" 89 + s.bounds.max.lat()+"' maxlon='" 90 + s.bounds.max.lon() 91 +"' origin='"+XmlWriter.encode(s.origin)+"' />"); 92 } 131 93 } 132 94 … … 135 97 addCommon(n, "node"); 136 98 out.print(" lat='"+n.coor.lat()+"' lon='"+n.coor.lon()+"'"); 137 addTags(n, "node", true); 99 if (!withBody) { 100 out.println("/>"); 101 } else { 102 addTags(n, "node", true); 103 } 138 104 } 139 105 … … 141 107 if (w.incomplete) return; 142 108 addCommon(w, "way"); 143 out.println(">"); 144 for (Node n : w.nodes) 145 out.println(" <nd ref='"+getUsedId(n)+"' />"); 146 addTags(w, "way", false); 109 if (!withBody) { 110 out.println("/>"); 111 } else { 112 out.println(">"); 113 for (Node n : w.nodes) 114 out.println(" <nd ref='"+getUsedId(n)+"' />"); 115 addTags(w, "way", false); 116 } 147 117 } 148 118 … … 150 120 if (e.incomplete) return; 151 121 addCommon(e, "relation"); 152 out.println(">"); 153 for (RelationMember em : e.members) { 154 out.print(" <member type='"); 155 em.member.visit(typeWriteVisitor); 156 out.println("' ref='"+getUsedId(em.member)+"' role='" + 157 XmlWriter.encode(em.role) + "' />"); 158 } 159 addTags(e, "relation", false); 160 } 161 122 if (!withBody) { 123 out.println("/>"); 124 } else { 125 out.println(">"); 126 for (RelationMember em : e.members) { 127 out.print(" <member type='"); 128 out.print(OsmApi.which(em.member)); 129 out.println("' ref='"+getUsedId(em.member)+"' role='" + 130 XmlWriter.encode(em.role) + "' />"); 131 } 132 addTags(e, "relation", false); 133 } 134 } 135 136 public void visit(Changeset cs) { 137 addCommon(cs, "changeset"); 138 out.println(">\n"); 139 addTags(cs, "changeset", false); 140 } 141 142 public final void footer(PrintWriter out) { 143 out.println("</osm>"); 144 } 162 145 163 146 /** … … 192 175 */ 193 176 private void addCommon(OsmPrimitive osm, String tagname) { 194 out.print(" <"+tagname+" id='"+getUsedId(osm)+"'"); 177 long id = getUsedId(osm); 178 out.print(" <"+tagname); 179 if (id != 0) { 180 out.print(" id='"+getUsedId(osm)+"'"); 181 } 195 182 if (!osmConform) { 196 183 String action = null; -
trunk/src/org/openstreetmap/josm/io/XmlWriter.java
r1169 r1523 2 2 package org.openstreetmap.josm.io; 3 3 4 import java.io.OutputStream;5 import java.io.OutputStreamWriter;6 4 import java.io.PrintWriter; 7 import java.io.UnsupportedEncodingException;8 5 import java.util.HashMap; 9 6 … … 15 12 public class XmlWriter { 16 13 17 /** 18 * The interface to write the data into an Osm stream 19 * @author immanuel.scholz 20 */ 21 public static interface OsmWriterInterface { 22 void header(PrintWriter out); 23 void write(PrintWriter out); 24 void footer(PrintWriter out); 25 } 26 27 28 protected XmlWriter(PrintWriter out) { 14 protected PrintWriter out; 15 16 public XmlWriter(PrintWriter out) { 29 17 this.out = out; 30 18 } 31 19 32 20 /** 33 21 * Encode the given string in XML1.0 format. … … 49 37 50 38 /** 51 * Write the header and start tag, then call the runnable to add all real tags and finally52 * "closes" the xml by writing the footer.53 */54 public static void output(OutputStream outStream, OsmWriterInterface outputWriter) {55 PrintWriter out;56 try {57 out = new PrintWriter(new OutputStreamWriter(outStream, "UTF-8"));58 } catch (UnsupportedEncodingException e) {59 throw new RuntimeException(e);60 }61 out.println("<?xml version='1.0' encoding='UTF-8'?>");62 outputWriter.header(out);63 outputWriter.write(out);64 outputWriter.footer(out);65 out.flush();66 out.close();67 }68 69 /**70 39 * The output writer to save the values to. 71 40 */ 72 protected final PrintWriter out;73 41 final private static HashMap<Character, String> encoding = new HashMap<Character, String>(); 74 42 static {
Note:
See TracChangeset
for help on using the changeset viewer.