ImageResource.java 13.8 KB
/*
 * 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;
    }
}