ImageResource.java
9.81 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.image.Layer
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* org.apache.commons.lang3.StringEscapeUtils
* org.apache.jackrabbit.util.Text
*/
package com.day.crx.packaging.gfx;
import com.day.crx.packaging.gfx.DownloadResource;
import com.day.crx.packaging.gfx.ImageHelper;
import com.day.image.Layer;
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.jackrabbit.util.Text;
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";
private String extFromType;
public ImageResource(Node node) {
super(node);
super.setExtension(".png");
try {
String suffix = "";
if (node != null) {
long lastMod = 0;
if (node.hasProperty("{http://www.jcp.org/jcr/1.0}lastModified")) {
lastMod = node.getProperty("{http://www.jcp.org/jcr/1.0}lastModified").getLong();
} else if (node.hasProperty("{http://www.jcp.org/jcr/1.0}created")) {
lastMod = node.getProperty("{http://www.jcp.org/jcr/1.0}created").getLong();
}
long fileLastMod = 0;
if (this.getFileReference().length() > 0) {
try {
Node refNode = this.getReferencedNode(this.getFileReference());
if (refNode != null) {
if (refNode.getNode("{http://www.jcp.org/jcr/1.0}content").hasProperty("{http://www.jcp.org/jcr/1.0}lastModified")) {
fileLastMod = refNode.getNode("{http://www.jcp.org/jcr/1.0}content").getProperty("{http://www.jcp.org/jcr/1.0}lastModified").getLong();
} else if (refNode.hasProperty("{http://www.jcp.org/jcr/1.0}created")) {
fileLastMod = refNode.getProperty("{http://www.jcp.org/jcr/1.0}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(Node resource, String imageName) {
this(ImageResource.getRelativeResource(resource, imageName));
}
protected static Node getRelativeResource(Node resource, String relPath) {
if (relPath == null) {
return resource;
}
try {
return resource.getNode(relPath);
}
catch (RepositoryException e) {
return null;
}
}
public String getAlt() {
String alt = this.get(this.getItemName("alt"));
if (alt.length() == 0) {
alt = this.getTitle();
}
return alt.length() == 0 ? this.getFileNodePath() : 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);
}
@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.length() > 0) {
out.printf("<a href=\"%s\">", this.completeHREF(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());
arrobject[1] = StringEscapeUtils.escapeHtml4((String)(value != null ? value.toString() : ""));
out.printf("%s=\"%s\" ", arrobject);
}
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) {
attributes.put("src", Text.escape((String)src, (char)'%', (boolean)true));
}
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;
}
}