@@ -179,28 +179,39 @@ public String createGPX( String trackName, long startTimeMillis )
179
179
private void createWayPointBlock ( StringBuilder output , Instruction instruction )
180
180
{
181
181
output .append ("\n <wpt " );
182
- output .append ("lat='" ).append (Helper .round6 (instruction .getFirstLat ()));
183
- output .append ("' lon='" ).append (Helper .round6 (instruction .getFirstLon ())).append ("'>" );
182
+ output .append ("lat=\" " ).append (Helper .round6 (instruction .getFirstLat ()));
183
+ output .append ("\" lon=\" " ).append (Helper .round6 (instruction .getFirstLon ())).append ("\" >" );
184
+ String name ;
184
185
if (instruction .getName ().isEmpty ())
185
- output . append ( " < name>" ). append ( instruction .getTurnDescription (tr )). append ( "</name>" );
186
+ name = instruction .getTurnDescription (tr );
186
187
else
187
- output .append (" <name>" ).append (instruction .getName ()).append ("</name>" );
188
+ name = instruction .getName ();
189
+
190
+ output .append (" <name>" ).append (simpleXMLEscape (name )).append ("</name>" );
188
191
output .append ("</wpt>" );
189
192
}
190
193
194
+ static String simpleXMLEscape ( String str )
195
+ {
196
+ // We could even use the 'more flexible' CDATA section but for now do the following. The 'and' could be important sometimes:
197
+ return str .replaceAll ("&" , "&" ).
198
+ // but do not care for:
199
+ replaceAll ("[\\ <\\ >]" , "_" );
200
+ }
201
+
191
202
public String createGPX ( String trackName , long startTimeMillis , boolean includeElevation , boolean withRoute , boolean withTrack , boolean withWayPoints )
192
203
{
193
204
DateFormat formatter = Helper .createFormatter ();
194
-
195
- String header = "<?xml version=' 1.0' encoding=' UTF-8' standalone='no' ?>"
196
- + "<gpx xmlns=' http://www.topografix.com/GPX/1/1' xmlns:xsi=' http://www.w3.org/2001/XMLSchema-instance' "
197
- + " creator=' Graphhopper version " + Constants .VERSION + "' version=' 1.1' "
205
+
206
+ String header = "<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" no \" ?>"
207
+ + "<gpx xmlns=\" http://www.topografix.com/GPX/1/1\" xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\" "
208
+ + " creator=\" Graphhopper version " + Constants .VERSION + "\" version=\" 1.1\" "
198
209
// This xmlns:gh acts only as ID, no valid URL necessary.
199
210
// Use a separate namespace for custom extensions to make basecamp happy.
200
- + " xmlns:gh=' https://graphhopper.com/public/schema/gpx/1.1' >"
211
+ + " xmlns:gh=\" https://graphhopper.com/public/schema/gpx/1.1\" >"
201
212
+ "\n <metadata>"
202
213
+ "<copyright author=\" OpenStreetMap contributors\" />"
203
- + "<link href=' http://graphhopper.com' >"
214
+ + "<link href=\" http://graphhopper.com\" >"
204
215
+ "<text>GraphHopper GPX</text>"
205
216
+ "</link>"
206
217
+ "<time>" + formatter .format (startTimeMillis ) + "</time>"
@@ -242,8 +253,8 @@ public String createGPX( String trackName, long startTimeMillis, boolean include
242
253
gpxOutput .append ("<trkseg>" );
243
254
for (GPXEntry entry : createGPXList ())
244
255
{
245
- gpxOutput .append ("\n <trkpt lat=' " ).append (Helper .round6 (entry .getLat ()));
246
- gpxOutput .append ("' lon='" ).append (Helper .round6 (entry .getLon ())).append ("' >" );
256
+ gpxOutput .append ("\n <trkpt lat=\" " ).append (Helper .round6 (entry .getLat ()));
257
+ gpxOutput .append ("\" lon=\" " ).append (Helper .round6 (entry .getLon ())).append ("\" >" );
247
258
if (includeElevation )
248
259
gpxOutput .append ("<ele>" ).append (Helper .round2 (entry .getEle ())).append ("</ele>" );
249
260
gpxOutput .append ("<time>" ).append (formatter .format (startTimeMillis + entry .getTime ())).append ("</time>" );
@@ -255,7 +266,7 @@ public String createGPX( String trackName, long startTimeMillis, boolean include
255
266
256
267
// we could now use 'wpt' for via points
257
268
gpxOutput .append ("\n </gpx>" );
258
- return gpxOutput .toString (). replaceAll ( " \\ '" , " \" " ) ;
269
+ return gpxOutput .toString ();
259
270
}
260
271
261
272
public void createRteptBlock ( StringBuilder output , Instruction instruction , Instruction nextI )
@@ -264,7 +275,7 @@ public void createRteptBlock( StringBuilder output, Instruction instruction, Ins
264
275
append ("\" lon=\" " ).append (Helper .round6 (instruction .getFirstLon ())).append ("\" >" );
265
276
266
277
if (!instruction .getName ().isEmpty ())
267
- output .append ("<desc>" ).append (instruction .getTurnDescription (tr )).append ("</desc>" );
278
+ output .append ("<desc>" ).append (simpleXMLEscape ( instruction .getTurnDescription (tr ) )).append ("</desc>" );
268
279
269
280
output .append ("<extensions>" );
270
281
output .append ("<gh:distance>" ).append (Helper .round (instruction .getDistance (), 1 )).append ("</gh:distance>" );
0 commit comments