java-fmpp-remove-imageinfo.patch
| 1 | See https://github.com/freemarker/fmpp/pull/18 |
| 2 | |
| 3 | In order to leverage the package dependency we removed |
| 4 | `org.devlib.schmidt.imageinfo.ImageInfo` class and replaced it with more |
| 5 | standard one javax.imageio.ImageIO in Fedora. We suggest you do the same here. |
| 6 | |
| 7 | The old class comes from a package whose latest release is in 2008 and whose |
| 8 | homepage doesn't exist anymore. There doesn't seem to be any corresponding |
| 9 | sources anymore. |
| 10 | |
| 11 | From 14dae73c5e5f162e6ddb59430cbfc7c555429140 Mon Sep 17 00:00:00 2001 |
| 12 | From: Tomas Repik <trepik@redhat.com> |
| 13 | Date: Mon, 5 Dec 2016 13:17:54 +0100 |
| 14 | Subject: [PATCH] removed org.devlib.schmidt.imageinfo.ImageInfo dependency |
| 15 | from sources |
| 16 | |
| 17 | --- |
| 18 | .../java/fmpp/dataloaders/HtmlUtilsDataLoader.java | 399 ++++++++++----------- |
| 19 | 1 file changed, 196 insertions(+), 203 deletions(-) |
| 20 | |
| 21 | diff --git a/src/main/java/fmpp/dataloaders/HtmlUtilsDataLoader.java b/src/main/java/fmpp/dataloaders/HtmlUtilsDataLoader.java |
| 22 | index 3dcb613..6c32065 100644 |
| 23 | --- a/src/main/java/fmpp/dataloaders/HtmlUtilsDataLoader.java |
| 24 | +++ b/src/main/java/fmpp/dataloaders/HtmlUtilsDataLoader.java |
| 25 | @@ -1,12 +1,12 @@ |
| 26 | /* |
| 27 | * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky |
| 28 | - * |
| 29 | + * |
| 30 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 31 | * you may not use this file except in compliance with the License. |
| 32 | * You may obtain a copy of the License at |
| 33 | - * |
| 34 | + * |
| 35 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 36 | - * |
| 37 | + * |
| 38 | * Unless required by applicable law or agreed to in writing, software |
| 39 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 40 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 41 | @@ -14,8 +14,8 @@ |
| 42 | * limitations under the License. |
| 43 | */ |
| 44 | |
| 45 | -package fmpp.dataloaders; |
| 46 | - |
| 47 | +package fmpp.dataloaders; |
| 48 | + |
| 49 | import java.io.File; |
| 50 | import java.io.FileNotFoundException; |
| 51 | import java.io.IOException; |
| 52 | @@ -25,8 +25,7 @@ import java.util.HashMap; |
| 53 | import java.util.Iterator; |
| 54 | import java.util.List; |
| 55 | import java.util.Map; |
| 56 | - |
| 57 | -import org.devlib.schmidt.imageinfo.ImageInfo; |
| 58 | +import javax.imageio.ImageIO; |
| 59 | |
| 60 | import fmpp.Engine; |
| 61 | import fmpp.tdd.DataLoader; |
| 62 | @@ -36,199 +35,193 @@ import freemarker.template.TemplateModelException; |
| 63 | import freemarker.template.TemplateNumberModel; |
| 64 | import freemarker.template.TemplateScalarModel; |
| 65 | import freemarker.template.TemplateTransformModel; |
| 66 | - |
| 67 | -/** |
| 68 | - * Returns a hash that contains useful directives for HTML generation. |
| 69 | - * <ul> |
| 70 | - * <li>img: Same as HTML img, but automatically calculates the width and/or |
| 71 | - * height attributes if they are missing. |
| 72 | - * </ul> |
| 73 | - */ |
| 74 | -public class HtmlUtilsDataLoader implements DataLoader {
|
| 75 | - private boolean xHtml = false; |
| 76 | - private String eTagClose; |
| 77 | - |
| 78 | - private Engine engine; |
| 79 | - |
| 80 | - private static final int MAX_CACHE_SIZE = 100; |
| 81 | - private Map imageInfoCache = new HashMap(); |
| 82 | - private CachedImageInfo first; |
| 83 | - private CachedImageInfo last; |
| 84 | - private ImageInfo imageInfo = new ImageInfo(); |
| 85 | - |
| 86 | - public Object load(Engine e, List args) throws Exception {
|
| 87 | - if (args.size() != 0) {
|
| 88 | - throw new IllegalArgumentException( |
| 89 | - "data loader does not have arguments"); |
| 90 | - } |
| 91 | - engine = e; |
| 92 | - if (xHtml) {
|
| 93 | - eTagClose = " />"; |
| 94 | - } else {
|
| 95 | - eTagClose = ">"; |
| 96 | - } |
| 97 | - |
| 98 | - Map map = new HashMap(); |
| 99 | - |
| 100 | - map.put("img", new ImgTransform());
|
| 101 | - |
| 102 | - return map; |
| 103 | - } |
| 104 | - |
| 105 | - public void setXHtml(boolean xHtml) {
|
| 106 | - this.xHtml = xHtml; |
| 107 | - } |
| 108 | - |
| 109 | - private CachedImageInfo getImageInfo(File f) |
| 110 | - throws IOException, TemplateModelException {
|
| 111 | - String cacheKey = f.getCanonicalPath(); |
| 112 | - |
| 113 | - CachedImageInfo inf = (CachedImageInfo) imageInfoCache.get(cacheKey); |
| 114 | - if (inf != null) {
|
| 115 | - long lmd = new File(cacheKey).lastModified(); |
| 116 | - if (inf.lmd == lmd && lmd != 0L && inf.lmd != 0L) {
|
| 117 | - if (inf != last) {
|
| 118 | - if (inf.prev != null) {
|
| 119 | - inf.prev.next = inf.next; |
| 120 | - } else {
|
| 121 | - first = inf.next; |
| 122 | - } |
| 123 | - if (inf.next != null) {
|
| 124 | - inf.next.prev = inf.prev; |
| 125 | - } else {
|
| 126 | - last = inf.prev; |
| 127 | - } |
| 128 | - |
| 129 | - inf.prev = last; |
| 130 | - inf.next = null; |
| 131 | - last = inf; |
| 132 | - inf.prev.next = last; |
| 133 | - } |
| 134 | - return inf; //! |
| 135 | - } else {
|
| 136 | - imageInfoCache.remove(cacheKey); |
| 137 | - if (inf.prev != null) {
|
| 138 | - inf.prev.next = inf.next; |
| 139 | - } else {
|
| 140 | - first = inf.next; |
| 141 | - } |
| 142 | - if (inf.next != null) {
|
| 143 | - inf.next.prev = inf.prev; |
| 144 | - } else {
|
| 145 | - last = inf.prev; |
| 146 | - } |
| 147 | - } |
| 148 | - } |
| 149 | - |
| 150 | - RandomAccessFile raf; |
| 151 | - try {
|
| 152 | - raf = new RandomAccessFile(f, "r"); |
| 153 | - } catch (FileNotFoundException e) { |
| 154 | - throw new TemplateModelException("Image file not found: " + f.getAbsolutePath(), e); |
| 155 | - } |
| 156 | - try {
|
| 157 | - imageInfo.setCollectComments(false); |
| 158 | - imageInfo.setInput(raf); |
| 159 | - if (!imageInfo.check()) {
|
| 160 | - throw new TemplateModelException("Failed to analyse image file: " + cacheKey);
|
| 161 | - } |
| 162 | - } finally {
|
| 163 | - raf.close(); |
| 164 | - } |
| 165 | - inf = new CachedImageInfo(); |
| 166 | - inf.lmd = f.lastModified(); |
| 167 | - inf.width = imageInfo.getWidth(); |
| 168 | - inf.height = imageInfo.getHeight(); |
| 169 | - inf.path = cacheKey; |
| 170 | - if (last != null) {
|
| 171 | - last.next = inf; |
| 172 | - } |
| 173 | - inf.prev = last; |
| 174 | - inf.next = null; |
| 175 | - last = inf; |
| 176 | - if (inf.prev == null) {
|
| 177 | - first = inf; |
| 178 | - } |
| 179 | - imageInfoCache.put(cacheKey, inf); |
| 180 | - if (imageInfoCache.size() > MAX_CACHE_SIZE) {
|
| 181 | - imageInfoCache.remove(first.path); |
| 182 | - first.next.prev = null; |
| 183 | - first = first.next; |
| 184 | - } |
| 185 | - |
| 186 | - return inf; |
| 187 | - } |
| 188 | - |
| 189 | - private class CachedImageInfo {
|
| 190 | - private CachedImageInfo prev; |
| 191 | - private CachedImageInfo next; |
| 192 | - private String path; |
| 193 | - private long lmd; |
| 194 | - private int width; |
| 195 | - private int height; |
| 196 | - } |
| 197 | - |
| 198 | - private class ImgTransform implements TemplateTransformModel {
|
| 199 | - public Writer getWriter(Writer out, Map args) |
| 200 | - throws TemplateModelException, IOException {
|
| 201 | - boolean detectHeight = true; |
| 202 | - boolean detectWidth = true; |
| 203 | - String src = null; |
| 204 | - |
| 205 | - out.write("<img");
|
| 206 | - |
| 207 | - Iterator it = args.entrySet().iterator(); |
| 208 | - while (it.hasNext()) {
|
| 209 | - Map.Entry e = (Map.Entry) it.next(); |
| 210 | - String pname = (String) e.getKey(); |
| 211 | - Object obj = e.getValue(); |
| 212 | - String pvalue; |
| 213 | - if (obj instanceof TemplateScalarModel) {
|
| 214 | - pvalue = ((TemplateScalarModel) obj).getAsString(); |
| 215 | - } else if (obj instanceof TemplateNumberModel) {
|
| 216 | - pvalue = ((TemplateNumberModel) obj).getAsNumber() |
| 217 | - .toString(); |
| 218 | - } else if (obj instanceof TemplateBooleanModel) {
|
| 219 | - pvalue = null; |
| 220 | - if (((TemplateBooleanModel) obj).getAsBoolean()) {
|
| 221 | - out.write(" " + pname);
|
| 222 | - } |
| 223 | - } else {
|
| 224 | - throw new TemplateModelException( |
| 225 | - "Argument to img must be string, " + "number or boolean"); |
| 226 | - } |
| 227 | - if (pvalue != null) {
|
| 228 | - pname = pname.toLowerCase(); |
| 229 | - out.write(" " + pname + "=\""
|
| 230 | - + StringUtil.htmlEnc(pvalue) + "\""); |
| 231 | - if (pname.equals("src")) {
|
| 232 | - src = pvalue; |
| 233 | - } else if (pname.equals("width")) {
|
| 234 | - detectWidth = false; |
| 235 | - } else if (pname.equals("height")) {
|
| 236 | - detectHeight = false; |
| 237 | - } |
| 238 | - } |
| 239 | - } |
| 240 | - if (detectWidth || detectHeight) {
|
| 241 | - if (src == null) {
|
| 242 | - throw new TemplateModelException( |
| 243 | - "The src attribute of img is missing"); |
| 244 | - } |
| 245 | - CachedImageInfo inf; |
| 246 | - inf = getImageInfo(engine.getTemplateEnvironment() |
| 247 | - .resolveSourcePath(src)); |
| 248 | - if (detectWidth) {
|
| 249 | - out.write(" width=\"" + inf.width + "\"");
|
| 250 | - } |
| 251 | - if (detectHeight) {
|
| 252 | - out.write(" height=\"" + inf.height + "\"");
|
| 253 | - } |
| 254 | - } |
| 255 | - |
| 256 | - out.write(eTagClose); |
| 257 | - |
| 258 | - return null; |
| 259 | - } |
| 260 | - } |
| 261 | -} |
| 262 | + |
| 263 | +/** |
| 264 | + * Returns a hash that contains useful directives for HTML generation. |
| 265 | + * <ul> |
| 266 | + * <li>img: Same as HTML img, but automatically calculates the width and/or |
| 267 | + * height attributes if they are missing. |
| 268 | + * </ul> |
| 269 | + */ |
| 270 | +public class HtmlUtilsDataLoader implements DataLoader { |
| 271 | + private boolean xHtml = false; |
| 272 | + private String eTagClose; |
| 273 | + |
| 274 | + private Engine engine; |
| 275 | + |
| 276 | + private static final int MAX_CACHE_SIZE = 100; |
| 277 | + private Map imageInfoCache = new HashMap(); |
| 278 | + private CachedImageInfo first; |
| 279 | + private CachedImageInfo last; |
| 280 | + |
| 281 | + public Object load(Engine e, List args) throws Exception { |
| 282 | + if (args.size() != 0) { |
| 283 | + throw new IllegalArgumentException( |
| 284 | + "data loader does not have arguments"); |
| 285 | + } |
| 286 | + engine = e; |
| 287 | + if (xHtml) { |
| 288 | + eTagClose = " />"; |
| 289 | + } else { |
| 290 | + eTagClose = ">"; |
| 291 | + } |
| 292 | + |
| 293 | + Map map = new HashMap(); |
| 294 | + |
| 295 | + map.put("img", new ImgTransform()); |
| 296 | + |
| 297 | + return map; |
| 298 | + } |
| 299 | + |
| 300 | + public void setXHtml(boolean xHtml) { |
| 301 | + this.xHtml = xHtml; |
| 302 | + } |
| 303 | + |
| 304 | + private CachedImageInfo getImageInfo(File f) |
| 305 | + throws IOException, TemplateModelException { |
| 306 | + String cacheKey = f.getCanonicalPath(); |
| 307 | + |
| 308 | + CachedImageInfo inf = (CachedImageInfo) imageInfoCache.get(cacheKey); |
| 309 | + if (inf != null) { |
| 310 | + long lmd = new File(cacheKey).lastModified(); |
| 311 | + if (inf.lmd == lmd && lmd != 0L && inf.lmd != 0L) { |
| 312 | + if (inf != last) { |
| 313 | + if (inf.prev != null) { |
| 314 | + inf.prev.next = inf.next; |
| 315 | + } else { |
| 316 | + first = inf.next; |
| 317 | + } |
| 318 | + if (inf.next != null) { |
| 319 | + inf.next.prev = inf.prev; |
| 320 | + } else { |
| 321 | + last = inf.prev; |
| 322 | + } |
| 323 | + |
| 324 | + inf.prev = last; |
| 325 | + inf.next = null; |
| 326 | + last = inf; |
| 327 | + inf.prev.next = last; |
| 328 | + } |
| 329 | + return inf; //! |
| 330 | + } else { |
| 331 | + imageInfoCache.remove(cacheKey); |
| 332 | + if (inf.prev != null) { |
| 333 | + inf.prev.next = inf.next; |
| 334 | + } else { |
| 335 | + first = inf.next; |
| 336 | + } |
| 337 | + if (inf.next != null) { |
| 338 | + inf.next.prev = inf.prev; |
| 339 | + } else { |
| 340 | + last = inf.prev; |
| 341 | + } |
| 342 | + } |
| 343 | + } |
| 344 | + |
| 345 | + int width = 0; |
| 346 | + int height = 0; |
| 347 | + |
| 348 | + try { |
| 349 | + java.awt.image.BufferedImage img = ImageIO.read(f); |
| 350 | + width = img.getWidth(); |
| 351 | + height = img.getHeight(); |
| 352 | + } catch(Exception e) { |
| 353 | + throw new TemplateModelException("Failed to analyse image file:" + cacheKey); |
| 354 | + } |
| 355 | + inf = new CachedImageInfo(); |
| 356 | + inf.lmd = f.lastModified(); |
| 357 | + inf.width = width; |
| 358 | + inf.height = height; |
| 359 | + inf.path = cacheKey; |
| 360 | + if (last != null) { |
| 361 | + last.next = inf; |
| 362 | + } |
| 363 | + inf.prev = last; |
| 364 | + inf.next = null; |
| 365 | + last = inf; |
| 366 | + if (inf.prev == null) { |
| 367 | + first = inf; |
| 368 | + } |
| 369 | + imageInfoCache.put(cacheKey, inf); |
| 370 | + if (imageInfoCache.size() > MAX_CACHE_SIZE) { |
| 371 | + imageInfoCache.remove(first.path); |
| 372 | + first.next.prev = null; |
| 373 | + first = first.next; |
| 374 | + } |
| 375 | + |
| 376 | + return inf; |
| 377 | + } |
| 378 | + |
| 379 | + private class CachedImageInfo { |
| 380 | + private CachedImageInfo prev; |
| 381 | + private CachedImageInfo next; |
| 382 | + private String path; |
| 383 | + private long lmd; |
| 384 | + private int width; |
| 385 | + private int height; |
| 386 | + } |
| 387 | + |
| 388 | + private class ImgTransform implements TemplateTransformModel { |
| 389 | + public Writer getWriter(Writer out, Map args) |
| 390 | + throws TemplateModelException, IOException { |
| 391 | + boolean detectHeight = true; |
| 392 | + boolean detectWidth = true; |
| 393 | + String src = null; |
| 394 | + |
| 395 | + out.write("<img"); |
| 396 | + |
| 397 | + Iterator it = args.entrySet().iterator(); |
| 398 | + while (it.hasNext()) { |
| 399 | + Map.Entry e = (Map.Entry) it.next(); |
| 400 | + String pname = (String) e.getKey(); |
| 401 | + Object obj = e.getValue(); |
| 402 | + String pvalue; |
| 403 | + if (obj instanceof TemplateScalarModel) { |
| 404 | + pvalue = ((TemplateScalarModel) obj).getAsString(); |
| 405 | + } else if (obj instanceof TemplateNumberModel) { |
| 406 | + pvalue = ((TemplateNumberModel) obj).getAsNumber() |
| 407 | + .toString(); |
| 408 | + } else if (obj instanceof TemplateBooleanModel) { |
| 409 | + pvalue = null; |
| 410 | + if (((TemplateBooleanModel) obj).getAsBoolean()) { |
| 411 | + out.write(" " + pname); |
| 412 | + } |
| 413 | + } else { |
| 414 | + throw new TemplateModelException( |
| 415 | + "Argument to img must be string, " + "number or boolean"); |
| 416 | + } |
| 417 | + if (pvalue != null) { |
| 418 | + pname = pname.toLowerCase(); |
| 419 | + out.write(" " + pname + "=\"" |
| 420 | + + StringUtil.htmlEnc(pvalue) + "\""); |
| 421 | + if (pname.equals("src")) { |
| 422 | + src = pvalue; |
| 423 | + } else if (pname.equals("width")) { |
| 424 | + detectWidth = false; |
| 425 | + } else if (pname.equals("height")) { |
| 426 | + detectHeight = false; |
| 427 | + } |
| 428 | + } |
| 429 | + } |
| 430 | + if (detectWidth || detectHeight) { |
| 431 | + if (src == null) { |
| 432 | + throw new TemplateModelException( |
| 433 | + "The src attribute of img is missing"); |
| 434 | + } |
| 435 | + CachedImageInfo inf; |
| 436 | + inf = getImageInfo(engine.getTemplateEnvironment() |
| 437 | + .resolveSourcePath(src)); |
| 438 | + if (detectWidth) { |
| 439 | + out.write(" width=\"" + inf.width + "\""); |
| 440 | + } |
| 441 | + if (detectHeight) { |
| 442 | + out.write(" height=\"" + inf.height + "\""); |
| 443 | + } |
| 444 | + } |
| 445 | + |
| 446 | + out.write(eTagClose); |
| 447 | + |
| 448 | + return null; |
| 449 | + } |
| 450 | + } |
| 451 | +} |
| 452 | -- |
| 453 | 2.11.0 |
| 454 | |
| 455 |