private void writeWaypoints(final cgCache cache) throws IOException {
   List<cgWaypoint> waypoints = cache.getWaypoints();
   List<cgWaypoint> ownWaypoints = new ArrayList<cgWaypoint>(waypoints.size());
   List<cgWaypoint> originWaypoints = new ArrayList<cgWaypoint>(waypoints.size());
   for (cgWaypoint wp : cache.getWaypoints()) {
     if (wp.isUserDefined()) {
       ownWaypoints.add(wp);
     } else {
       originWaypoints.add(wp);
     }
   }
   int maxPrefix = 0;
   for (cgWaypoint wp : originWaypoints) {
     String prefix = wp.getPrefix();
     try {
       maxPrefix = Math.max(Integer.parseInt(prefix), maxPrefix);
     } catch (NumberFormatException ex) {
       Log.e("Unexpected origin waypoint prefix='" + prefix + "'", ex);
     }
     writeCacheWaypoint(wp, prefix);
   }
   for (cgWaypoint wp : ownWaypoints) {
     maxPrefix++;
     String prefix = StringUtils.leftPad(String.valueOf(maxPrefix), 2, '0');
     writeCacheWaypoint(wp, prefix);
   }
 }
  /**
   * Return the first of the cache coordinates, the waypoint coordinates or the extra coordinates.
   * <code>null</code> entities are skipped.
   *
   * @param cache a cache
   * @param waypoint a waypoint
   * @param coords extra coordinates
   * @return the first non-null coordinates, or null if none are set
   */
  private static Geopoint getCoordinates(
      final cgCache cache, final cgWaypoint waypoint, final Geopoint coords) {
    if (cache != null && cache.getCoords() != null) {
      return cache.getCoords();
    }

    if (waypoint != null && waypoint.getCoords() != null) {
      return waypoint.getCoords();
    }

    return coords;
  }
    /**
     * Writes one waypoint entry for cache waypoint.
     *
     * @param cache The
     * @param wp
     * @param prefix
     * @throws IOException
     */
    private void writeCacheWaypoint(final cgWaypoint wp, final String prefix) throws IOException {
      gpx.write("<wpt lat=\"");
      final Geopoint coords = wp.getCoords();
      gpx.write(
          coords != null
              ? Double.toString(coords.getLatitude())
              : ""); // TODO: check whether is the best way to handle unknown waypoint coordinates
      gpx.write("\" lon=\"");
      gpx.write(coords != null ? Double.toString(coords.getLongitude()) : "");
      gpx.write("\">");

      gpx.write("<name>");
      gpx.write(StringEscapeUtils.escapeXml(prefix));
      gpx.write(StringEscapeUtils.escapeXml(wp.getGeocode().substring(2)));
      gpx.write("</name>");

      gpx.write("<cmt>");
      gpx.write(StringEscapeUtils.escapeXml(wp.getNote()));
      gpx.write("</cmt>");

      gpx.write("<desc>");
      gpx.write(StringEscapeUtils.escapeXml(wp.getName()));
      gpx.write("</desc>");

      gpx.write("<sym>");
      gpx.write(
          StringEscapeUtils.escapeXml(
              wp.getWaypointType().toString())); // TODO: Correct identifier string
      gpx.write("</sym>");

      gpx.write("<type>Waypoint|");
      gpx.write(
          StringEscapeUtils.escapeXml(
              wp.getWaypointType().toString())); // TODO: Correct identifier string
      gpx.write("</type>");

      gpx.write("</wpt>");
    }