WCMInheritanceValueMap.java 3.68 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.ValueMapWrapper
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.cq.sightly.internal;

import com.day.cq.commons.ValueMapWrapper;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;

public class WCMInheritanceValueMap
extends ValueMapWrapper {
    private Resource resource;

    public WCMInheritanceValueMap(Resource resource) {
        super(ResourceUtil.getValueMap((Resource)resource));
        this.resource = resource;
    }

    public Object get(Object key) {
        Object o = super.get(key);
        if (o == null) {
            o = this.getInherited(key.toString(), null);
        }
        return o;
    }

    public <T> T get(String name, Class<T> type) {
        Object inherited;
        Object o = super.get(name, type);
        if (o == null && (inherited = this.getInherited(name, type)) != null) {
            return (T)inherited;
        }
        return (T)o;
    }

    public <T> T get(String name, T defaultValue) {
        if (defaultValue == null) {
            return (T)this.get(name);
        }
        Class type = defaultValue.getClass();
        Class value = this.get(name, (T)type);
        if (value == null) {
            return defaultValue;
        }
        return (T)value;
    }

    private <T> Object getInherited(String key, Class<T> type) {
        return this.getParentPageValue(WCMInheritanceValueMap.getInnerPath(this.resource), key, type);
    }

    private <T> Object getParentPageValue(String innerPath, String key, Class<T> type) {
        ResourceResolver resolver;
        Resource content;
        if (this.resource == null) {
            return null;
        }
        Resource parent = this.resource.getParent();
        boolean isContentNode = this.resource.getName().equals("jcr:content");
        if (parent == null) {
            parent = WCMInheritanceValueMap.getNextExistingParent(this.resource);
            if (parent == null) {
                return null;
            }
            isContentNode = false;
        }
        if (!isContentNode && (content = (resolver = parent.getResourceResolver()).getResource(parent, "jcr:content")) != null) {
            Resource innerResource = resolver.getResource(content, innerPath);
            ValueMap innerResourceProperties = (ValueMap)innerResource.adaptTo(ValueMap.class);
            Object value = type != null ? innerResourceProperties.get(key, type) : innerResourceProperties.get((Object)key);
            if (value != null) {
                return value;
            }
        }
        return new WCMInheritanceValueMap(parent).getParentPageValue(innerPath, key, type);
    }

    private static String getInnerPath(Resource resource) {
        if (resource == null) {
            return ".";
        }
        String resPath = resource.getPath();
        int pos = resPath.indexOf("jcr:content/");
        if (pos <= 0) {
            return ".";
        }
        return resPath.substring(pos + "jcr:content".length() + 1);
    }

    private static Resource getNextExistingParent(Resource resource) {
        ResourceResolver resolver = resource.getResourceResolver();
        String path = resource.getPath();
        do {
            if ((path = ResourceUtil.getParent((String)path)) != null) continue;
            return null;
        } while ((resource = resolver.getResource(path)) == null);
        return resource;
    }
}