Changeset 6148 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2013-08-14T13:12:50+02:00 (11 years ago)
Author:
bastiK
Message:

fixed #8686 - Allow ZIP files whose paths are relative to the ZIP root in JOSM's map styling

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapImage.java

    r6105 r6148  
    5858                .setId("mappaint."+source.getPrefName())
    5959                .setArchive(source.zipIcons)
     60                .setInArchiveDir(source.getZipEntryDirName())
    6061                .setWidth(width)
    6162                .setHeight(height)
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r6104 r6148  
    66import java.io.File;
    77import java.io.IOException;
    8 import java.io.InputStream;
    98import java.io.InputStreamReader;
    109import java.util.ArrayList;
     
    9796                .setId("mappaint."+namespace)
    9897                .setArchive(ref.source.zipIcons)
     98                .setInArchiveDir(ref.source.getZipEntryDirName())
    9999                .setWidth(width)
    100100                .setHeight(height)
     
    121121                .setId("mappaint."+source.getPrefName())
    122122                .setArchive(source.zipIcons)
     123                .setInArchiveDir(source.getZipEntryDirName())
    123124                .setOptional(true).get();
    124125    }
     
    211212        try {
    212213            in = new MirroredInputStream(entry.url);
    213             InputStream zip = in.getZipEntry("mapcss", "style");
    214             if (zip != null)
     214            String zipEntryPath = in.findZipEntryPath("mapcss", "style");
     215            if (zipEntryPath != null) {
     216                entry.isZip = true;
     217                entry.zipEntryPath = zipEntryPath;
    215218                return new MapCSSStyleSource(entry);
    216             zip = in.getZipEntry("xml", "style");
    217             if (zip != null)
     219            }
     220            zipEntryPath = in.findZipEntryPath("xml", "style");
     221            if (zipEntryPath != null)
    218222                return new XmlStyleSource(entry);
    219223            if (entry.url.toLowerCase().endsWith(".mapcss"))
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java

    r6090 r6148  
    4040
    4141    public StyleSource(SourceEntry entry) {
    42         super(entry.url, entry.name, entry.title, entry.active);
     42        super(entry);
    4343    }
    4444
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r6070 r6148  
    66import java.awt.Color;
    77import java.io.ByteArrayInputStream;
     8import java.io.File;
    89import java.io.IOException;
    910import java.io.InputStream;
     
    1112import java.util.List;
    1213import java.util.Map.Entry;
     14import java.util.zip.ZipEntry;
     15import java.util.zip.ZipFile;
    1316
    1417import org.openstreetmap.josm.data.osm.Node;
     
    8891
    8992        MirroredInputStream in = new MirroredInputStream(url);
    90         InputStream zip = in.getZipEntry("mapcss", "style");
    91         if (zip != null) {
    92             zipIcons = in.getFile();
    93             return zip;
     93        if (isZip) {
     94            File file = in.getFile();
     95            Utils.close(in);
     96            ZipFile zipFile = new ZipFile(file);
     97            zipIcons = file;
     98            ZipEntry zipEntry = zipFile.getEntry(zipEntryPath);
     99            return zipFile.getInputStream(zipEntry);
    94100        } else {
    95101            zipIcons = null;
  • trunk/src/org/openstreetmap/josm/gui/mappaint/xml/XmlStyleSource.java

    r6143 r6148  
    9494    public InputStream getSourceInputStream() throws IOException {
    9595        MirroredInputStream in = new MirroredInputStream(url);
    96         InputStream zip = in.getZipEntry("xml", "style");
     96        InputStream zip = in.findZipEntryInputStream("xml", "style");
    9797        if (zip != null) {
    9898            zipIcons = in.getFile();
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEntry.java

    r6070 r6148  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.preferences;
     3
     4import static org.openstreetmap.josm.tools.Utils.equal;
    35
    46import java.io.File;
    57import java.util.regex.Matcher;
    68import java.util.regex.Pattern;
    7 
    8 import static org.openstreetmap.josm.tools.Utils.equal;
    99
    1010/**
     
    1919     */
    2020    public String url;
     21
     22    /**
     23     * Indicates, that {@link #url} is a zip file and the resource is
     24     * inside the zip file.
     25     */
     26    public boolean isZip;
     27
     28    /**
     29     * If {@link #isZip} is true, denotes the path inside the zip file.
     30     */
     31    public String zipEntryPath;
    2132
    2233    /**
     
    4253    public boolean active;
    4354
    44     public SourceEntry(String url, String name, String title, Boolean active) {
     55    public SourceEntry(String url, boolean isZip, String zipEntryPath, String name, String title, boolean active) {
    4556        this.url = url;
     57        this.isZip = isZip;
     58        this.zipEntryPath = equal(zipEntryPath, "") ? null : zipEntryPath;
    4659        this.name = equal(name, "") ? null : name;
    4760        this.title = equal(title, "") ? null : title;
     
    4962    }
    5063
     64    public SourceEntry(String url, String name, String title, Boolean active) {
     65        this(url, false, null, name, title, active);
     66    }
     67
    5168    public SourceEntry(SourceEntry e) {
    5269        this.url = e.url;
     70        this.isZip = e.isZip;
     71        this.zipEntryPath = e.zipEntryPath;
    5372        this.name = e.name;
    5473        this.title = e.title;
     
    6281        final SourceEntry other = (SourceEntry) obj;
    6382        return equal(other.url, url) &&
     83                other.isZip == isZip &&
     84                equal(other.zipEntryPath, zipEntryPath) &&
    6485                equal(other.name, name) &&
    6586                equal(other.title, title) &&
     
    7192        int hash = 5;
    7293        hash = 89 * hash + (this.url != null ? this.url.hashCode() : 0);
     94        hash = 89 * hash + (this.isZip ? 1 : 0);
     95        hash = 89 * hash + (this.zipEntryPath != null ? this.zipEntryPath.hashCode() : 0);
    7396        hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0);
    7497        hash = 89 * hash + (this.title != null ? this.title.hashCode() : 0);
     
    133156        return dir.getPath();
    134157    }
     158
     159    /**
     160     * Returns the parent directory of the resource inside the zip file.
     161     * @return null, if zipEntryPath is null, otherwise the parent directory of
     162     * the resource inside the zip file
     163     */
     164    public String getZipEntryDirName() {
     165        if (zipEntryPath == null) return null;
     166        return new File(zipEntryPath).getParent().toString();
     167    }
    135168}
  • trunk/src/org/openstreetmap/josm/gui/preferences/map/MapPaintPreference.java

    r6143 r6148  
    4141    private static final List<SourceProvider> styleSourceProviders = new ArrayList<SourceProvider>();
    4242
    43     public static final boolean registerSourceProvider(SourceProvider provider) {
     43    public static boolean registerSourceProvider(SourceProvider provider) {
    4444        if (provider != null)
    4545            return styleSourceProviders.add(provider);
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetReader.java

    r6145 r6148  
    159159        MirroredInputStream s = new MirroredInputStream(source);
    160160        try {
    161             InputStream zip = s.getZipEntry("xml","preset");
     161            InputStream zip = s.findZipEntryInputStream("xml","preset");
    162162            if(zip != null) {
    163163                zipIcons = s.getFile();
  • trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java

    r6090 r6148  
    2222
    2323import org.openstreetmap.josm.Main;
     24import org.openstreetmap.josm.tools.Pair;
    2425import org.openstreetmap.josm.tools.Utils;
    2526
     
    9192
    9293    /**
    93      * Replies an input stream for a file in a ZIP-file. Replies a file in the top
    94      * level directory of the ZIP file which has an extension <code>extension</code>. If more
    95      * than one files have this extension, the last file whose name includes <code>namepart</code>
     94     * Looks for a certain entry inside a zip file and returns the entry path.
     95     *
     96     * Replies a file in the top level directory of the ZIP file which has an
     97     * extension <code>extension</code>. If more than one files have this
     98     * extension, the last file whose name includes <code>namepart</code>
    9699     * is opened.
    97100     *
    98101     * @param extension  the extension of the file we're looking for
    99102     * @param namepart the name part
    100      * @return an input stream. Null if this mirrored input stream doesn't represent a zip file or if
    101      * there was no matching file in the ZIP file
    102      */
     103     * @return The zip entry path of the matching file. Null if this mirrored
     104     * input stream doesn't represent a zip file or if there was no matching
     105     * file in the ZIP file.
     106     */
     107    public String findZipEntryPath(String extension, String namepart) {
     108        Pair<String, InputStream> ze = findZipEntryImpl(extension, namepart);
     109        if (ze == null) return null;
     110        return ze.a;
     111    }
     112
     113    /**
     114     * Like {@link #findZipEntryPath}, but returns the corresponding InputStream.
     115     */
     116    public InputStream findZipEntryInputStream(String extension, String namepart) {
     117        Pair<String, InputStream> ze = findZipEntryImpl(extension, namepart);
     118        if (ze == null) return null;
     119        return ze.b;
     120    }
     121
     122    @Deprecated // use findZipEntryInputStream
    103123    public InputStream getZipEntry(String extension, String namepart) {
     124        return findZipEntryInputStream(extension, namepart);
     125    }
     126
     127    private Pair<String, InputStream> findZipEntryImpl(String extension, String namepart) {
    104128        if (file == null)
    105129            return null;
    106         InputStream res = null;
     130        Pair<String, InputStream> res = null;
    107131        try {
    108132            ZipFile zipFile = new ZipFile(file);
     
    120144            }
    121145            if (resentry != null) {
    122                 res = zipFile.getInputStream(resentry);
     146                InputStream is = zipFile.getInputStream(resentry);
     147                res = Pair.create(resentry.getName(), is);
    123148            } else {
    124149                Utils.close(zipFile);
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r6093 r6148  
    9393    protected String name;
    9494    protected File archive;
     95    protected String inArchiveDir;
    9596    protected int width = -1;
    9697    protected int height = -1;
     
    153154    public ImageProvider setArchive(File archive) {
    154155        this.archive = archive;
     156        return this;
     157    }
     158
     159    /**
     160     * Specify a base path inside the zip file.
     161     *
     162     * The subdir and name will be relative to this path.
     163     *
     164     * (optional)
     165     */
     166    public ImageProvider setInArchiveDir(String inArchiveDir) {
     167        this.inArchiveDir = inArchiveDir;
    155168        return this;
    156169    }
     
    438451                    case ARCHIVE:
    439452                        if (archive != null) {
    440                             ir = getIfAvailableZip(full_name, archive, type);
     453                            ir = getIfAvailableZip(full_name, archive, inArchiveDir, type);
    441454                            if (ir != null) {
    442455                                cache.put(cache_name, ir);
     
    521534    }
    522535
    523     private static ImageResource getIfAvailableZip(String full_name, File archive, ImageType type) {
     536    private static ImageResource getIfAvailableZip(String full_name, File archive, String inArchiveDir, ImageType type) {
    524537        ZipFile zipFile = null;
    525538        try
    526539        {
    527540            zipFile = new ZipFile(archive);
    528             ZipEntry entry = zipFile.getEntry(full_name);
     541            String entry_name;
     542            if (inArchiveDir != null) {
     543                File dir = new File(inArchiveDir);
     544                entry_name = new File(dir, full_name).getPath();
     545            } else {
     546                entry_name = full_name;
     547            }
     548            ZipEntry entry = zipFile.getEntry(entry_name);
    529549            if(entry != null)
    530550            {
     
    537557                    switch (type) {
    538558                    case SVG:
    539                         URI uri = getSvgUniverse().loadSVG(is, full_name);
     559                        URI uri = getSvgUniverse().loadSVG(is, entry_name);
    540560                        SVGDiagram svg = getSvgUniverse().getDiagram(uri);
    541561                        return svg == null ? null : new ImageResource(svg);
Note: See TracChangeset for help on using the changeset viewer.