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

com.day.cq.commons.inherit.ComponentInheritanceValueMap Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2011 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.day.cq.commons.inherit;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;

import com.day.cq.commons.ValueMapWrapper;
import com.day.cq.commons.jcr.JcrConstants;

/**
 * An {@link InheritanceValueMap} for a given {@link Resource} that will inherit values from
 * ancestors up to its ancestral component, but no higher.
 *
 * 

* For example, given: /content/parent/page/jcr:content/footer/image/@width, * the ComponentInheritanceValueMap will search for a width property in: *

    *
  • /content/parent/page/jcr:content/footer/image/@width
  • *
  • /content/parent/page/jcr:content/footer/@width
  • *
  • /content/parent/page/jcr:content/@width
  • *
* Having not found it in any of those locations, it will then return null. * *

* Note that ComponentInheritanceValueMap searches only the component * hierarchy. It will not (for instance), look in: *

    *
  • /content/parent/jcr:content/footer/image/@width
  • *
* See {@link com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap} for that * functionality. */ public class ComponentInheritanceValueMap extends ValueMapWrapper implements InheritanceValueMap { protected Resource resource; /** * Will wrap the {@link ValueMap} returned by * resource.adaptTo(ValueMap.class), or use an empty map if * resource is null or if it doesn't adapt to a ValueMap. The * inheritance is internally resolved by fetching the parent resource and * wrapping it into an {@link ComponentInheritanceValueMap} as well. * * @param resource * the resource to start from */ public ComponentInheritanceValueMap(Resource resource) { super(ResourceUtil.getValueMap(resource)); this.resource = resource; } /** * Use this if the underlying {@link Resource} for a {@link ValueMap} is not * available and no inheritance is needed. Using the inheritance-enabled * {@link #getInherited(String, Class) getter} * {@link #getInherited(String, Object) methods} will behave exactly like * the normal ValueMap getters. * * @param map * a ValueMap to wrap */ public ComponentInheritanceValueMap(ValueMap map) { super(map); this.resource = null; } @SuppressWarnings("unchecked") public T get(String name, Class type) { // overwritten to fix NPE if (type == null) { return (T) get(name); } return super.get(name, type); } public T getInherited(String name, Class type) { T value = get(name, type); if (value == null) { value = getParentComponentValue(name, type); } return value; } @SuppressWarnings("unchecked") public T getInherited(String name, T defaultValue) { Class type; if (defaultValue == null) { type = null; } else { // special handling in case the default value implements one // of the interface types supported by the convertToType method type = (Class) defaultValue.getClass(); } T value = getInherited(name, type); if (value == null) { value = defaultValue; } return value; } protected T getParentComponentValue(final String name, final Class type) { // stop inheritance in case resource is null or a page's content resource if (resource == null || resource.getName().equals(JcrConstants.JCR_CONTENT)) { return null; } Resource parent = resource.getParent(); if(parent == null) { return null; } return new ComponentInheritanceValueMap(parent).getInherited(name, type); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy