ImageResource.java
13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.widget.Doctype
* com.day.image.Layer
* com.day.text.Text
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* org.apache.commons.lang3.StringEscapeUtils
* org.apache.sling.api.resource.NonExistingResource
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
*/
package com.day.cq.commons;
import com.day.cq.commons.DiffInfo;
import com.day.cq.commons.Doctype;
import com.day.cq.commons.DownloadResource;
import com.day.cq.commons.ImageHelper;
import com.day.cq.commons.impl.DiffedResourceWrapper;
import com.day.image.Layer;
import com.day.text.Text;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
public class ImageResource
extends DownloadResource {
public static final String PN_HTML_WIDTH = "htmlWidth";
public static final String PN_HTML_HEIGHT = "htmlHeight";
public static final String PN_WIDTH = "width";
public static final String PN_HEIGHT = "height";
public static final String PN_MIN_WIDTH = "minWidth";
public static final String PN_MIN_HEIGHT = "minHeight";
public static final String PN_MAX_WIDTH = "maxWidth";
public static final String PN_MAX_HEIGHT = "maxHeight";
public static final String PN_ALT = "alt";
public static final String PN_IMAGE_CROP = "imageCrop";
public static final String PN_IMAGE_ROTATE = "imageRotate";
public static final String PN_LINK_URL = "linkURL";
public static final String PN_DEFAULT_IMAGE_PATH = "defaultImagePath";
public static final String DEFAULT_IMAGE_PATH = "etc/designs/default/0.gif";
private com.day.cq.widget.Doctype doctype = com.day.cq.widget.Doctype.HTML_401_STRICT;
private String extFromType;
public ImageResource(Resource resource) {
super(resource);
super.setExtension(".png");
try {
String suffix = "";
long lastMod = 0;
if (this.node != null) {
if (this.node.hasProperty("jcr:lastModified")) {
lastMod = this.node.getProperty("jcr:lastModified").getLong();
} else if (this.node.hasProperty("jcr:created")) {
lastMod = this.node.getProperty("jcr:created").getLong();
}
} else {
ValueMap values = (ValueMap)this.getResource().adaptTo(ValueMap.class);
if (values != null) {
Long value = (Long)values.get("jcr:lastModified", Long.class);
if (value == null) {
value = (Long)values.get("jcr:created", Long.class);
}
if (value != null) {
lastMod = value;
}
}
}
long fileLastMod = 0;
if (this.getFileReference().length() > 0) {
try {
Node refNode = (Node)resource.getResourceResolver().getResource(this.getFileReference()).adaptTo(Node.class);
if (refNode.getNode("jcr:content").hasProperty("jcr:lastModified")) {
fileLastMod = refNode.getNode("jcr:content").getProperty("jcr:lastModified").getLong();
} else if (refNode.hasProperty("jcr:created")) {
fileLastMod = refNode.getProperty("jcr:created").getLong();
}
}
catch (Exception e) {
// empty catch block
}
}
if (fileLastMod > lastMod) {
lastMod = fileLastMod;
}
if (lastMod != 0) {
suffix = suffix + lastMod;
suffix = suffix + this.getExtension();
}
this.setSuffix(suffix);
}
catch (RepositoryException re) {
// empty catch block
}
}
public ImageResource(Resource resource, String imageName) {
this(ImageResource.getRelativeResource(resource, imageName));
}
protected static Resource getRelativeResource(Resource resource, String relPath) {
DiffInfo info;
if (relPath == null) {
return resource;
}
Object res = resource.getResourceResolver().getResource(resource, relPath);
if (res == null) {
res = new NonExistingResource(resource.getResourceResolver(), resource.getPath() + "/" + relPath);
}
if ((info = (DiffInfo)resource.adaptTo(DiffInfo.class)) != null) {
Resource content = info.getContent();
if (content != null) {
if ((content = content.getResourceResolver().getResource(content, relPath)) == null) {
content = new NonExistingResource(resource.getResourceResolver(), info.getContent().getPath() + "/" + relPath);
}
info = new DiffInfo(content, info.getType());
}
res = new DiffedResourceWrapper((Resource)res, info);
}
return res;
}
@Override
public String getTitle(boolean escape) {
String refPath;
String title = this.get(this.getItemName("jcr:title"));
if (title.equals("") && !(refPath = this.getFileReference()).equals("")) {
try {
Node node = (Node)this.getResourceResolver().getResource(refPath).adaptTo(Node.class);
title = node.getNode("jcr:content").getNode("metadata").getProperty("dc:title").getString();
this.setTitle(title);
}
catch (Exception e) {
title = refPath.lastIndexOf(".") > refPath.lastIndexOf("/") ? refPath.substring(refPath.lastIndexOf("/") + 1, refPath.lastIndexOf(".")) : refPath.substring(refPath.lastIndexOf("/") + 1);
this.setTitle(title);
}
}
return escape ? StringEscapeUtils.escapeHtml4((String)title) : title;
}
public String getAlt() {
String alt = this.get(this.getItemName("alt"));
if (alt.length() == 0) {
alt = this.getTitle();
}
return alt.length() == 0 ? this.getFileNodePath().substring(this.getFileNodePath().lastIndexOf("/") + 1) : alt;
}
public void setAlt(String alt) {
this.set("alt", alt);
}
public String getSrc() {
return this.getHref();
}
@Override
public String getExtension() {
if (this.extFromType == null) {
try {
this.extFromType = ImageHelper.getExtensionFromType(this.getMimeType());
}
catch (RepositoryException e) {
// empty catch block
}
if (this.extFromType == null) {
this.extFromType = super.getExtension();
} else if (!this.extFromType.startsWith(".")) {
this.extFromType = "." + this.extFromType;
}
}
return this.extFromType;
}
@Override
public void setExtension(String extension) {
this.extFromType = extension;
super.setExtension(extension);
}
public void setSrc(String src) {
super.setHref(src);
}
@Deprecated
public Doctype getDoctype() {
return Doctype.valueOf(this.doctype.name());
}
public com.day.cq.widget.Doctype getImageDoctype() {
return this.doctype;
}
@Deprecated
public void setDoctype(Doctype doctype) {
if (doctype != null) {
this.doctype = com.day.cq.widget.Doctype.valueOf((String)doctype.name());
}
}
public void setImageDoctype(com.day.cq.widget.Doctype doctype) {
if (doctype != null) {
this.doctype = doctype;
}
}
@Override
public void draw(Writer w) throws IOException {
if (this.canDraw()) {
this.doDraw(new PrintWriter(w));
}
}
protected void doDraw(PrintWriter out) {
Map<String, String> attributes = this.getImageTagAttributes();
String linkURL = this.get("linkURL");
if (linkURL != null && linkURL.length() > 0) {
linkURL = this.completeHREF(linkURL).replace("<", "%3C").replace(">", "%3E").replace("\"", "%22");
out.printf("<a href=\"%s\">", linkURL);
}
out.print("<img ");
for (Map.Entry<String, String> e : attributes.entrySet()) {
String value = e.getValue();
Object[] arrobject = new Object[2];
arrobject[0] = StringEscapeUtils.escapeHtml4((String)e.getKey().toString());
arrobject[1] = StringEscapeUtils.escapeHtml4((String)(value != null ? value.toString() : ""));
out.printf("%s=\"%s\" ", arrobject);
}
if (this.doctype.isXHTML()) {
out.print("/>");
} else {
out.print(">");
}
if (linkURL.length() > 0) {
out.print("</a>");
}
}
protected boolean canDraw() {
return this.hasContent();
}
protected Map<String, String> getImageTagAttributes() {
String src;
HashMap<String, String> attributes = new HashMap<String, String>();
if (this.get(this.getItemName("htmlWidth")).length() > 0) {
attributes.put("width", this.get(this.getItemName("htmlWidth")));
}
if (this.get(this.getItemName("htmlHeight")).length() > 0) {
attributes.put("height", this.get(this.getItemName("htmlHeight")));
}
if ((src = this.getSrc()) != null) {
String q = this.getQuery();
if (q == null) {
q = "";
}
attributes.put("src", Text.escape((String)src, (char)'%', (boolean)true) + q);
}
attributes.put("alt", this.getAlt());
attributes.put("title", this.getTitle());
if (this.attrs != null) {
attributes.putAll(this.attrs);
}
return attributes;
}
private String completeHREF(String href) {
if (href != null && href.length() > 0 && (href.charAt(0) == '/' || href.charAt(0) == '#')) {
int anchorPos = href.indexOf("#");
if (anchorPos == 0) {
return href;
}
String anchor = "";
if (anchorPos > 0) {
anchor = href.substring(anchorPos, href.length());
href = href.substring(0, anchorPos);
}
int extSepPos = href.lastIndexOf(".");
int slashPos = href.lastIndexOf("/");
if (extSepPos <= 0 || extSepPos < slashPos) {
href = Text.escape((String)href, (char)'%', (boolean)true) + ".html" + anchor;
}
}
return href;
}
public Rectangle getCropRect() {
String cropData = this.get(this.getItemName("imageCrop"));
if (cropData.length() > 0) {
return ImageHelper.getCropRect(cropData, this.getPath());
}
return null;
}
public int getRotation() {
String rotation = this.get(this.getItemName("imageRotate"));
if (rotation.length() > 0) {
try {
return Integer.parseInt(rotation);
}
catch (NumberFormatException nfe) {
// empty catch block
}
}
return 0;
}
public Layer resize(Layer layer) {
Dimension d = new Dimension(this.get(this.getItemName("width"), 0), this.get(this.getItemName("height"), 0));
Dimension min = new Dimension(this.get(this.getItemName("minWidth"), 0), this.get(this.getItemName("minHeight"), 0));
Dimension max = new Dimension(this.get(this.getItemName("maxWidth"), 0), this.get(this.getItemName("maxHeight"), 0));
return ImageHelper.resize(layer, d, min, max);
}
public Layer crop(Layer layer) {
Rectangle rect = this.getCropRect();
if (rect != null) {
layer.crop((Rectangle2D)rect);
return layer;
}
return null;
}
public Layer rotate(Layer layer) {
int rotation = this.getRotation();
if (rotation != 0) {
layer.rotate((double)rotation);
return layer;
}
return null;
}
public Layer getLayer(boolean cropped, boolean resized, boolean rotated) throws IOException, RepositoryException {
Layer layer = null;
Property data = this.getData();
if (data != null) {
layer = ImageHelper.createLayer((Item)data);
if (layer != null && cropped) {
this.crop(layer);
}
if (layer != null && resized) {
this.resize(layer);
}
if (layer != null && rotated) {
this.rotate(layer);
}
}
return layer;
}
@Override
public Property getData() throws RepositoryException {
Property data = super.getData();
if (data != null) {
return data;
}
if (this.node == null) {
return null;
}
if ("/etc/designs/default/0.gif".equals(this.node.getPath())) {
Node fileNode = this.node;
if (fileNode.hasNode("jcr:content")) {
fileNode = fileNode.getNode("jcr:content");
}
if (fileNode.hasProperty("jcr:data")) {
return fileNode.getProperty("jcr:data");
}
}
return null;
}
}