Ignore:
Timestamp:
2015-05-01T21:47:18+02:00 (10 years ago)
Author:
Don-vip
Message:

simplify URL encoding/decoding

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r8286 r8304  
    2727import java.io.InputStream;
    2828import java.io.StringReader;
    29 import java.io.UnsupportedEncodingException;
    3029import java.net.URI;
    3130import java.net.URL;
    32 import java.net.URLDecoder;
    33 import java.net.URLEncoder;
    3431import java.nio.charset.StandardCharsets;
    3532import java.util.ArrayList;
     
    287284     *
    288285     * (optional)
     286     * @param archive zip file where the image is located
    289287     * @return the current object, for convenience
    290288     */
     
    816814     */
    817815    private static ImageResource getIfAvailableDataUrl(String url) {
    818         try {
    819             Matcher m = dataUrlPattern.matcher(url);
    820             if (m.matches()) {
    821                 String mediatype = m.group(1);
    822                 String base64 = m.group(2);
    823                 String data = m.group(3);
    824                 byte[] bytes;
    825                 if (";base64".equals(base64)) {
    826                     bytes = DatatypeConverter.parseBase64Binary(data);
    827                 } else {
    828                     try {
    829                         bytes = URLDecoder.decode(data, "UTF-8").getBytes(StandardCharsets.UTF_8);
    830                     } catch (IllegalArgumentException ex) {
    831                         Main.warn("Unable to decode URL data part: "+ex.getMessage() + " (" + data + ")");
    832                         return null;
    833                     }
    834                 }
    835                 if ("image/svg+xml".equals(mediatype)) {
    836                     String s = new String(bytes, StandardCharsets.UTF_8);
    837                     SVGDiagram svg = null;
    838                     synchronized (getSvgUniverse()) {
    839                         URI uri = getSvgUniverse().loadSVG(new StringReader(s), URLEncoder.encode(s, "UTF-8"));
    840                         svg = getSvgUniverse().getDiagram(uri);
    841                     }
    842                     if (svg == null) {
    843                         Main.warn("Unable to process svg: "+s);
    844                         return null;
    845                     }
    846                     return new ImageResource(svg);
    847                 } else {
    848                     try {
    849                         // See #10479: for PNG files, always enforce transparency to be sure tNRS chunk is used even not in paletted mode
    850                         // This can be removed if someday Oracle fixes https://bugs.openjdk.java.net/browse/JDK-6788458
    851                         // hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/828c4fedd29f/src/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java#l656
    852                         Image img = read(new ByteArrayInputStream(bytes), false, true);
    853                         return img == null ? null : new ImageResource(img);
    854                     } catch (IOException e) {
    855                         Main.warn("IOException while reading image: "+e.getMessage());
    856                     }
    857                 }
    858             }
    859             return null;
    860         } catch (UnsupportedEncodingException ex) {
    861             throw new RuntimeException(ex.getMessage(), ex);
    862         }
     816        Matcher m = dataUrlPattern.matcher(url);
     817        if (m.matches()) {
     818            String mediatype = m.group(1);
     819            String base64 = m.group(2);
     820            String data = m.group(3);
     821            byte[] bytes;
     822            if (";base64".equals(base64)) {
     823                bytes = DatatypeConverter.parseBase64Binary(data);
     824            } else {
     825                try {
     826                    bytes = Utils.decodeUrl(data).getBytes(StandardCharsets.UTF_8);
     827                } catch (IllegalArgumentException ex) {
     828                    Main.warn("Unable to decode URL data part: "+ex.getMessage() + " (" + data + ")");
     829                    return null;
     830                }
     831            }
     832            if ("image/svg+xml".equals(mediatype)) {
     833                String s = new String(bytes, StandardCharsets.UTF_8);
     834                SVGDiagram svg = null;
     835                synchronized (getSvgUniverse()) {
     836                    URI uri = getSvgUniverse().loadSVG(new StringReader(s), Utils.encodeUrl(s));
     837                    svg = getSvgUniverse().getDiagram(uri);
     838                }
     839                if (svg == null) {
     840                    Main.warn("Unable to process svg: "+s);
     841                    return null;
     842                }
     843                return new ImageResource(svg);
     844            } else {
     845                try {
     846                    // See #10479: for PNG files, always enforce transparency to be sure tNRS chunk is used even not in paletted mode
     847                    // This can be removed if someday Oracle fixes https://bugs.openjdk.java.net/browse/JDK-6788458
     848                    // hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/828c4fedd29f/src/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java#l656
     849                    Image img = read(new ByteArrayInputStream(bytes), false, true);
     850                    return img == null ? null : new ImageResource(img);
     851                } catch (IOException e) {
     852                    Main.warn("IOException while reading image: "+e.getMessage());
     853                }
     854            }
     855        }
     856        return null;
    863857    }
    864858
  • trunk/src/org/openstreetmap/josm/tools/OsmUrlToBounds.java

    r8291 r8304  
    66import java.awt.HeadlessException;
    77import java.awt.Toolkit;
    8 import java.io.UnsupportedEncodingException;
    9 import java.net.URLDecoder;
    108import java.util.HashMap;
    119import java.util.Map;
     
    2624            // a percent sign indicates an encoded URL (RFC 1738).
    2725            if (url.contains("%")) {
    28                 url = URLDecoder.decode(url, "UTF-8");
    29             }
    30         } catch (UnsupportedEncodingException | IllegalArgumentException x) {
     26                url = Utils.decodeUrl(url);
     27            }
     28        } catch (IllegalArgumentException x) {
    3129            Main.error(x);
    3230        }
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r8291 r8304  
    2525import java.net.URL;
    2626import java.net.URLConnection;
     27import java.net.URLDecoder;
    2728import java.net.URLEncoder;
    2829import java.nio.charset.StandardCharsets;
     
    11161117                sb.append(c);
    11171118            } else {
    1118                 try {
    1119                     sb.append(URLEncoder.encode(c, "UTF-8"));
    1120                 } catch (UnsupportedEncodingException ex) {
    1121                     throw new RuntimeException(ex);
    1122                 }
     1119                sb.append(encodeUrl(c));
    11231120            }
    11241121        }
    11251122        return sb.toString();
     1123    }
     1124
     1125    /**
     1126     * Translates a string into <code>application/x-www-form-urlencoded</code>
     1127     * format. This method uses UTF-8 encoding scheme to obtain the bytes for unsafe
     1128     * characters.
     1129     *
     1130     * @param   s <code>String</code> to be translated.
     1131     * @return  the translated <code>String</code>.
     1132     * @see #decodeUrl(String)
     1133     * @since 8304
     1134     */
     1135    public static String encodeUrl(String s) {
     1136        final String enc = StandardCharsets.UTF_8.name();
     1137        try {
     1138            return URLEncoder.encode(s, enc);
     1139        } catch (UnsupportedEncodingException e) {
     1140            Main.error(e);
     1141            return null;
     1142        }
     1143    }
     1144
     1145    /**
     1146     * Decodes a <code>application/x-www-form-urlencoded</code> string.
     1147     * UTF-8 encoding is used to determine
     1148     * what characters are represented by any consecutive sequences of the
     1149     * form "<code>%<i>xy</i></code>".
     1150     *
     1151     * @param s the <code>String</code> to decode
     1152     * @return the newly decoded <code>String</code>
     1153     * @see #encodeUrl(String)
     1154     * @since 8304
     1155     */
     1156    public static String decodeUrl(String s) {
     1157        final String enc = StandardCharsets.UTF_8.name();
     1158        try {
     1159            return URLDecoder.decode(s, enc);
     1160        } catch (UnsupportedEncodingException e) {
     1161            Main.error(e);
     1162            return null;
     1163        }
    11261164    }
    11271165
Note: See TracChangeset for help on using the changeset viewer.