Class AbstractResource

java.lang.Object
org.onehippo.cms7.crisp.api.resource.AbstractResource
All Implemented Interfaces:
Serializable, Resource

public abstract class AbstractResource extends Object implements Resource
Abstract Resource representation.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    AbstractResource(String resourceType)
    Construct resource representation by using resource type name.
    AbstractResource(String resourceType, String name)
    Constructs resource representation by using resource type name and resource name.
    AbstractResource(Resource parent, String resourceType, String name)
    Constructs resource representation by using parent resoruce representation, resource type name and resource name.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static <T> T
    convertValueOfBasicType(Object value, Class<T> expectedType)
    Convert the value of a basic type to one of the expectedType.
    long
    Returns child resource count of this resource representation.
    Return a ResourceCollection of child resource representations.
    Resolves the default property value of this resource if available, or null if unavailable.
    <T> T
    Resolves the default property value of this resource if available and converts it into the given type, or null if unavailable.
    Returns the name of this resource representation if there's any.
    Return the underlying, internal node data that this Resource represents.
    Returns parent resource representation if there's any.
    Returns the path of this resource representation if there's any.
    Returns the resource type name of this resource representation if there's any.
    getValue(String relPath)
    Resolves a property value of this resource by the given relPath.
    <T> T
    getValue(String relPath, Class<T> type)
    Resolves a property value of this resource by the given relPath and converts it into the given type.
    boolean
    Returns true if this resource representation contains any child resource representation.
    boolean
    Returns true if this resource representation is purely for an array (e.g, JSON Array if underlying data is based on JSON).
    boolean
    isResourceType(String resourceType)
    Returns true if this resource representation is typed and of the specific resourceType name.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.onehippo.cms7.crisp.api.resource.Resource

    dump, getChildren, getMetadata, getValueMap
  • Constructor Details

    • AbstractResource

      public AbstractResource(String resourceType)
      Construct resource representation by using resource type name.
      Parameters:
      resourceType - resource type name
    • AbstractResource

      public AbstractResource(String resourceType, String name)
      Constructs resource representation by using resource type name and resource name.
      Parameters:
      resourceType - resource type name
      name - resource name
    • AbstractResource

      public AbstractResource(Resource parent, String resourceType, String name)
      Constructs resource representation by using parent resoruce representation, resource type name and resource name.
      Parameters:
      parent - parent resource representation
      resourceType - resource type name
      name - resource name
  • Method Details

    • convertValueOfBasicType

      protected static <T> T convertValueOfBasicType(Object value, Class<T> expectedType)
      Convert the value of a basic type to one of the expectedType.
      Parameters:
      value - the input value of a basic type
      expectedType - the expected type to convert to
      Returns:
      Converted value of the expectedType
    • getResourceType

      public String getResourceType()
      Returns the resource type name of this resource representation if there's any. For example, a JSON data based implementation may return '@type' property based on its domain rules, or a JCR based implementation may simply return javax.jcr.Node#getPrimaryNodeType().getName().
      Specified by:
      getResourceType in interface Resource
      Returns:
      the resource type name of this resource representation if there's any
    • isResourceType

      public boolean isResourceType(String resourceType)
      Returns true if this resource representation is typed and of the specific resourceType name.
      Specified by:
      isResourceType in interface Resource
      Parameters:
      resourceType - resource type name
      Returns:
      true if this resource representation is typed and of the specific resourceType name
    • getName

      public String getName()
      Returns the name of this resource representation if there's any. For example, a JSON data based implementation may return '@name' property based on its domain rules, or a JCR based implementation may simply return javax.jcr.Item#getName().
      Specified by:
      getName in interface Resource
      Returns:
      the name of this resource representation if there's any
    • getPath

      public String getPath()
      Returns the path of this resource representation if there's any. For example, a JSON data based implementation may construct an XPath-like path based on object hierarchy, or a JCR based implementation may simply return javax.jcr.Item#getPath().
      Specified by:
      getPath in interface Resource
      Returns:
      the path of this resource representation if there's any
    • getParent

      public Resource getParent()
      Returns parent resource representation if there's any.
      Specified by:
      getParent in interface Resource
      Returns:
      parent resource representation if there's any
    • isAnyChildContained

      public boolean isAnyChildContained()
      Returns true if this resource representation contains any child resource representation.
      Specified by:
      isAnyChildContained in interface Resource
      Returns:
      true if this resource representation contains any child resource representation
    • isArray

      public boolean isArray()
      Returns true if this resource representation is purely for an array (e.g, JSON Array if underlying data is based on JSON).
      Specified by:
      isArray in interface Resource
      Returns:
      true if this resource representation is purely for an array (e.g, JSON Array if underlying data is based on JSON)
    • getChildCount

      public long getChildCount()
      Returns child resource count of this resource representation.
      Specified by:
      getChildCount in interface Resource
      Returns:
      child resource count of this resource representation
    • getChildren

      public ResourceCollection getChildren()
      Return a ResourceCollection of child resource representations.
      Specified by:
      getChildren in interface Resource
      Returns:
      a ResourceCollection of child resource representations
    • getValue

      public Object getValue(String relPath)
      Resolves a property value of this resource by the given relPath. Or null if not resolved by the relPath.

      If relPath is a relative value path, like "content/title", then the return should be equivalent to the result of the call, ((Resource) getValueMap().get("content")).getValueMap().get("title") if existing.

      In addition, a path segment may contain an array index notation like "content/images[1]/title". In this case, the value at content/images must be an array type Resource object which returns true on Resource.isArray().

      Specified by:
      getValue in interface Resource
      Parameters:
      relPath - property or child resource relative path
      Returns:
      a resolved property value of this resource by the given relPath. Or null if not resolved by the relPath
    • getValue

      public <T> T getValue(String relPath, Class<T> type)
      Resolves a property value of this resource by the given relPath and converts it into the given type. Or null if not resolved by the relPath.

      If relPath is a relative value path, like "content/title", then the return should be equivalent to the result of the call, ((Resource) getValueMap().get("content")).getValueMap().get("title") if existing.

      In addition, a path segment may contain an array index notation like "content/images[1]/title". In this case, the value at content/images must be an array type Resource object which returns true on Resource.isArray().

      Specified by:
      getValue in interface Resource
      Parameters:
      relPath - property or child resource relative path
      Returns:
      a resolved property value of this resource by the given relPath. Or null if not resolved by the relPath
    • getDefaultValue

      public Object getDefaultValue()
      Resolves the default property value of this resource if available, or null if unavailable. The default value resolution totally depends on the implementations. For example, an XML element based implementation may choose to return the text content of the underlying element.

      This default implementation is equivalent to getValueMap().get("") if not overriden.

      Specified by:
      getDefaultValue in interface Resource
      Returns:
      the default property value of this resource if available, or null if unavailable
    • getDefaultValue

      public <T> T getDefaultValue(Class<T> type)
      Resolves the default property value of this resource if available and converts it into the given type, or null if unavailable. The default value resolution totally depends on the implementations. For example, an XML element based implementation may choose to return the text content of the underlying element.

      This default implementation is equivalent to getValueMap().get("", type) if not overriden.

      Specified by:
      getDefaultValue in interface Resource
      Returns:
      the default property value of this resource if available, or null if unavailable
    • getNodeData

      public Object getNodeData()
      Description copied from interface: Resource
      Return the underlying, internal node data that this Resource represents.

      WARNING: This method is for experts only who understands the risk of accessing the underlying internal, node data, and who is willing to change and upgrade their applications if the type of the underlying, internal data changes at any time in the future. The return object and its type of the underlying, internal data is not guaranteed in the future versions because this method is really implementation specific.

      NOTE: The return of this method is really implementation specific. If a underlying ResourceResolver maintains a Jackson node object for a Resource object, then it might support this method by returning the Jackson node or just return null when it doesn't support this method.

      Specified by:
      getNodeData in interface Resource
      Returns:
      the underlying node data if supported by the underlying ResourceResolver or null otherwise.