| 122 | | ImageWrapper iw = getIfAvailableImpl(dirs, id, subdir, name, archive); |
| 123 | | if (iw == null) |
| 124 | | return null; |
| 125 | | if (sanitize && !iw.sanitized) { |
| 126 | | iw.img = sanitize(iw.img); |
| 127 | | iw.sanitized = true; |
| | 130 | ImageIcon icon = new ImageIcon(); |
| | 131 | if (name.matches("^wiki(:[0-9]+)?://.*")) { |
| | 132 | getIfAvailableWikiImpl(icon, dirs, id, name, sanitize); |
| | 133 | } else { |
| | 134 | ImageWrapper iw = getIfAvailableImpl(dirs, id, subdir, name, archive); |
| | 135 | if (iw == null) |
| | 136 | return null; |
| | 137 | if (sanitize && !iw.sanitized) { |
| | 138 | iw.img = sanitize(iw.img); |
| | 139 | iw.sanitized = true; |
| | 140 | } |
| | 141 | icon.setImage(iw.img); |
| | 146 | private static void getIfAvailableWikiImpl(final ImageIcon icon, Collection<String> dirs, String id, final String name, final boolean sanitize) { |
| | 147 | ImageWrapper iw = cache.get(name); |
| | 148 | if (iw != null) { |
| | 149 | icon.setImage(iw.img); |
| | 150 | } else { |
| | 151 | // do not return null, since we will loose control over the icon object else |
| | 152 | icon.setImage(getIfAvailable(dirs, id, null, "misc/no_icon.png", null, sanitize).getImage()); |
| | 153 | |
| | 154 | Main.worker.execute(new Runnable(){ |
| | 155 | public void run() { |
| | 156 | try { |
| | 157 | final String base = Main.pref.get("url.openstreetmap-wiki", "http://wiki.openstreetmap.org/wiki/File:"); |
| | 158 | final String fn = name.substring(name.lastIndexOf('/') + 1); |
| | 159 | |
| | 160 | final XMLReader parser = XMLReaderFactory.createXMLReader(); |
| | 161 | parser.setContentHandler(new DefaultHandler() { |
| | 162 | @Override |
| | 163 | public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { |
| | 164 | System.out.println(); |
| | 165 | if (localName.equalsIgnoreCase("img")) { |
| | 166 | String val = atts.getValue("src"); |
| | 167 | if (val.endsWith(fn)) { |
| | 168 | try { |
| | 169 | // mirror just the unscaled image.. |
| | 170 | MirroredInputStream is = new MirroredInputStream(val, new File(Main.pref.getPreferencesDir(), |
| | 171 | "images").toString()); |
| | 172 | |
| | 173 | Image img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); |
| | 174 | int width = 0; |
| | 175 | try { |
| | 176 | // this allows definition of custom sizes via the icon-image url |
| | 177 | width = Integer.parseInt(name.replaceFirst("^wiki:([0-9]+)?.*", "$1")); |
| | 178 | } catch (Exception e) { |
| | 179 | // use a default if left empty (e.g. wiki://Signet_Cycleroute.png) |
| | 180 | width = 20; |
| | 181 | } |
| | 182 | img = img.getScaledInstance(width, -1, Image.SCALE_SMOOTH); |
| | 183 | |
| | 184 | if (sanitize) { |
| | 185 | img = sanitize(img); |
| | 186 | } |
| | 187 | |
| | 188 | // ..but put the scaled image into the cache |
| | 189 | cache.put(name, new ImageWrapper(img, sanitize)); |
| | 190 | |
| | 191 | // finally, update the icon image that up to now displayed "no_icon" |
| | 192 | icon.setImage(img); |
| | 193 | |
| | 194 | throw new SAXException(); // done parsing, quit early |
| | 195 | } catch (IOException e) { |
| | 196 | System.out.println("INFO: fetching " + base + fn + "failed"); |
| | 197 | } |
| | 198 | } |
| | 199 | } |
| | 200 | } |
| | 201 | }); |
| | 202 | |
| | 203 | parser.setEntityResolver(new EntityResolver() { |
| | 204 | public InputSource resolveEntity (String publicId, String systemId) { |
| | 205 | return new InputSource(new ByteArrayInputStream(new byte[0])); |
| | 206 | } |
| | 207 | }); |
| | 208 | parser.parse(new InputSource(new MirroredInputStream( |
| | 209 | base + fn, |
| | 210 | new File(Main.pref.getPreferencesDir(), "images").toString() |
| | 211 | ))); |
| | 212 | } catch (Exception e) { |
| | 213 | if (!(e instanceof SAXException)) { |
| | 214 | e.printStackTrace(); |
| | 215 | } |
| | 216 | } |
| | 217 | } |
| | 218 | }); |
| | 219 | } |
| | 220 | } |
| | 221 | |