All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.granite.ui.components.ds.ValueMapResource Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2013 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.adobe.granite.ui.components.ds;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.SyntheticResource;
import org.apache.sling.api.resource.ValueMap;

import java.util.Collection;
import java.util.Iterator;

/**
 * An implementation of resource that will return the ValueMap passed, when this
 * resource is adapted to ValueMap.
 */
public class ValueMapResource extends SyntheticResource {
    private ValueMap vm;
    private Collection children;

    /**
     * Creates a ValueMapResource based on provided parameters
     * @param resourceResolver The resource resolver
     * @param path The resource path
     * @param resourceType The resource type
     * @param vm The properties of the resource
     */
    public ValueMapResource(ResourceResolver resourceResolver, String path, String resourceType, ValueMap vm) {
        super(resourceResolver, path, resourceType);
        this.vm = vm;
    }

    /**
     * Creates a ValueMapResource based on provided parameters, with the ability to define the children
     * @param resourceResolver The resource resolver
     * @param path The resource path
     * @param resourceType The resource type
     * @param vm The properties of the resource
     * @param children The children of the resource
     */
    public ValueMapResource(ResourceResolver resourceResolver, String path, String resourceType, ValueMap vm, Collection children) {
        this(resourceResolver, path, resourceType, vm);
        this.children = children;
    }

    /**
     * Creates a ValueMapResource based on provided parameters
     * @param resourceResolver The resource resolver
     * @param rm The resource meta data
     * @param resourceType The resource type
     * @param vm The properties of the resource
     */
    public ValueMapResource(ResourceResolver resourceResolver, ResourceMetadata rm, String resourceType, ValueMap vm) {
        super(resourceResolver, rm, resourceType);
        this.vm = vm;
    }

    /**
     * Creates a ValueMapResource based on provided parameters, with the ability to define the children
     * @param resourceResolver The resource resolver
     * @param rm The resource meta data
     * @param resourceType The resource type
     * @param vm The properties of the resource
     * @param children The children of the resource
     */
    public ValueMapResource(ResourceResolver resourceResolver, ResourceMetadata rm, String resourceType, ValueMap vm, Collection children) {
        this(resourceResolver, rm, resourceType, vm);
        this.children = children;
    }

    @SuppressWarnings("unchecked")
    @Override
    public  Type adaptTo(Class type) {
        if (ValueMap.class.equals(type)) {
            return (Type) vm;
        } else {
            return super.adaptTo(type);
        }
    }

    @Override
    public Resource getChild(String relPath) {
        if (children == null) {
            return super.getChild(relPath);
        }
        for (Resource child : children) {
            if (child.getPath() != null && child.getPath().equals(getPath() + "/" + relPath)) {
                return child;
            }
        }
        return null;
    }

    @Override
    public Iterator listChildren() {
        if (children == null) {
            return super.listChildren();
        }
        return children.iterator();
    }

    @Override
    public Iterable getChildren() {
        return new Iterable() {
            @Override
            public Iterator iterator() {
                return listChildren();
            }
        };
    }

    @Override
    public boolean hasChildren() {
        if (children == null) {
            return super.hasChildren();
        }
        return !children.isEmpty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy