Ticket #20793: 20793.gpx-memory.diff

File 20793.gpx-memory.diff, 14.0 KB (added by Bjoeni, 5 years ago)
  • src/org/openstreetmap/josm/data/gpx/GpxExtensionCollection.java

     
    2222
    2323    private static final long serialVersionUID = 1L;
    2424
    25     private final Stack<GpxExtension> childStack = new Stack<>();
     25    private Stack<GpxExtension> childStack;
    2626    private IWithAttributes parent;
    2727
    2828    /**
     
    4646     * @param atts the attributes
    4747     */
    4848    public void openChild(String namespaceURI, String qName, Attributes atts) {
     49        if (childStack == null) {
     50            childStack = new Stack<>();
     51        }
    4952        GpxExtension child = new GpxExtension(namespaceURI, qName, atts);
    5053        if (!childStack.isEmpty()) {
    5154            childStack.lastElement().getExtensions().add(child);
     
    6265     * @param value the value
    6366     */
    6467    public void closeChild(String qName, String value) {
    65         if (childStack.isEmpty())
     68        if (childStack == null || childStack.isEmpty())
    6669            throw new InvalidArgumentException("Can't close child " + qName + ", no element in stack.");
    6770
    6871        GpxExtension child = childStack.pop();
     
    258261
    259262    @Override
    260263    public void clear() {
    261         childStack.clear();
     264        if (childStack != null) {
     265            childStack.clear();
     266            childStack = null;
     267        }
    262268        super.clear();
    263269    }
    264270
  • src/org/openstreetmap/josm/data/gpx/GpxTrack.java

     
    104104    }
    105105
    106106    private Color getColorFromExtension() {
    107         GpxExtension gpxd = getExtensions().find("gpxd", "color");
    108         if (gpxd != null) {
    109             colorFormat = ColorFormat.GPXD;
    110             String cs = gpxd.getValue();
    111             try {
    112                 return Color.decode(cs);
    113             } catch (NumberFormatException ex) {
    114                 Logging.warn("Could not read gpxd color: " + cs);
    115             }
    116         } else {
    117             GpxExtension gpxx = getExtensions().find("gpxx", "DisplayColor");
    118             if (gpxx != null) {
    119                 colorFormat = ColorFormat.GPXX;
    120                 String cs = gpxx.getValue();
    121                 if (cs != null) {
    122                     Color cc = GARMIN_COLORS.get(cs);
    123                     if (cc != null) {
    124                         return cc;
     107        if (hasExtensions()) {
     108            GpxExtension gpxd = getExtensions().find("gpxd", "color");
     109            if (gpxd != null) {
     110                colorFormat = ColorFormat.GPXD;
     111                String cs = gpxd.getValue();
     112                try {
     113                    return Color.decode(cs);
     114                } catch (NumberFormatException ex) {
     115                    Logging.warn("Could not read gpxd color: " + cs);
     116                }
     117            } else {
     118                GpxExtension gpxx = getExtensions().find("gpxx", "DisplayColor");
     119                if (gpxx != null) {
     120                    colorFormat = ColorFormat.GPXX;
     121                    String cs = gpxx.getValue();
     122                    if (cs != null) {
     123                        Color cc = GARMIN_COLORS.get(cs);
     124                        if (cc != null) {
     125                            return cc;
     126                        }
    125127                    }
     128                    Logging.warn("Could not read garmin color: " + cs);
    126129                }
    127                 Logging.warn("Could not read garmin color: " + cs);
    128130            }
    129131        }
    130132        return null;
  • src/org/openstreetmap/josm/data/gpx/IWithAttributes.java

     
    5757    Map<String, Object> getAttributes();
    5858
    5959    /**
     60     * Returns whether the {@link GpxExtensionCollection} instance has been created yet, should be overridden.
     61     * The instance will usually be created on first call of {@link #getExtensions()}.
     62     * @return whether the {@link GpxExtensionCollection} instance has been created yet
     63     */
     64    default boolean hasExtensions() {
     65        return getExtensions() != null;
     66    }
     67
     68    /**
    6069     * Returns the extensions
    6170     * @return the extensions
    6271     */
  • src/org/openstreetmap/josm/data/gpx/WithAttributes.java

     
    2424    /**
    2525     * The "exts" collection contains all extensions.
    2626     */
    27     private final GpxExtensionCollection exts = new GpxExtensionCollection(this);
     27    private GpxExtensionCollection exts;
    2828
    2929    /**
    3030     * Returns the Object value to which the specified key is mapped,
     
    8686    }
    8787
    8888    @Override
     89    public boolean hasExtensions() {
     90        return exts != null;
     91    }
     92
     93    @Override
    8994    public GpxExtensionCollection getExtensions() {
     95        if (exts == null) {
     96            exts = new GpxExtensionCollection(this);
     97        }
    9098        return exts;
    9199    }
    92100
  • src/org/openstreetmap/josm/io/GpxReader.java

     
    506506                    currentState = states.pop();
    507507                    if (!currentTrackSeg.isEmpty()) {
    508508                        GpxTrackSegment seg = new GpxTrackSegment(currentTrackSeg);
    509                         seg.getExtensions().addAll(currentExtensionCollection);
     509                        if (!currentExtensionCollection.isEmpty()) {
     510                            seg.getExtensions().addAll(currentExtensionCollection);
     511                        }
    510512                        currentTrack.add(seg);
    511513                    }
    512514                    currentExtensionCollection.clear();
     
    518520                    currentState = states.pop();
    519521                    convertUrlToLink(currentTrackAttr);
    520522                    GpxTrack trk = new GpxTrack(new ArrayList<>(currentTrack), currentTrackAttr);
    521                     trk.getExtensions().addAll(currentTrackExtensionCollection);
     523                    if (!currentTrackExtensionCollection.isEmpty()) {
     524                        trk.getExtensions().addAll(currentTrackExtensionCollection);
     525                    }
    522526                    data.addTrack(trk);
    523527                    currentTrackExtensionCollection.clear();
    524528                    break;