org.apache.sling.api.resource
Class ResourceUtil

java.lang.Object
  extended by org.apache.sling.api.resource.ResourceUtil

public class ResourceUtil
extends Object

The ResourceUtil class provides helper methods dealing with resources.


Constructor Summary
ResourceUtil()
           
 
Method Summary
static
<T> Iterator<T>
adaptTo(Iterator<Resource> iterator, Class<T> type)
          Return an iterator for objecs of the specified type.
static String findResourceSuperType(Resource resource)
          Returns the super type of the given resource.
static String getName(Resource rsrc)
          Deprecated. since 2.1.0, use Resource.getName() instead
static String getName(String path)
          Utility method returns the name of the given path, which is normalized by normalize(String) before resolving the name.
static Resource getParent(Resource rsrc)
          Deprecated. since 2.1.0, use Resource.getParent() instead
static String getParent(String path)
          Utility method returns the parent path of the given path, which is normalized by normalize(String) before resolving the parent.
static String getResourceSuperType(ResourceResolver resourceResolver, String resourceType)
          Returns the super type of the given resource type.
static ValueMap getValueMap(Resource res)
          Returns an ValueMap object for the given Resource.
static boolean isA(Resource resource, String resourceType)
          Check if the resource is of the given type.
static boolean isNonExistingResource(Resource res)
          Returns true if the resource res is a non-existing resource.
static boolean isStarResource(Resource res)
          Returns true if the resource res is a "star resource".
static boolean isSyntheticResource(Resource res)
          Returns true if the resource res is a synthetic resource.
static Iterator<Resource> listChildren(Resource parent)
          Deprecated. since 2.1.0, use Resource.listChildren() instead
static String normalize(String path)
          Resolves relative path segments '.' and '..' in the absolute path.
static String resourceTypeToPath(String type)
          Helper method, which returns the given resource type as returned from the Resource.getResourceType() as a relative path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceUtil

public ResourceUtil()
Method Detail

normalize

public static String normalize(String path)
Resolves relative path segments '.' and '..' in the absolute path. Returns null if not possible (.. points above root) or if path is not absolute.


getParent

public static String getParent(String path)
Utility method returns the parent path of the given path, which is normalized by normalize(String) before resolving the parent.

Parameters:
path - The path whose parent is to be returned.
Returns:
null if path is the root path ( /) or if path is a single name containing no slash (/) characters.
Throws:
IllegalArgumentException - If the path cannot be normalized by the normalize(String) method.
NullPointerException - If path is null.

getParent

public static Resource getParent(Resource rsrc)
Deprecated. since 2.1.0, use Resource.getParent() instead

Utility method returns the parent resource of the resource.

Returns:
The parent resource or null if the rsrc is the root.
Throws:
NullPointerException - If rsrc is null.

getName

public static String getName(Resource rsrc)
Deprecated. since 2.1.0, use Resource.getName() instead

Utility method returns the name of the resource.

Throws:
NullPointerException - If rsrc is null.

getName

public static String getName(String path)
Utility method returns the name of the given path, which is normalized by normalize(String) before resolving the name.

Parameters:
path - The path whose name (the last path element) is to be returned.
Returns:
The empty string if path is the root path ( /) or if path is a single name containing no slash (/) characters.
Throws:
IllegalArgumentException - If the path cannot be normalized by the normalize(String) method.
NullPointerException - If path is null.

isSyntheticResource

public static boolean isSyntheticResource(Resource res)
Returns true if the resource res is a synthetic resource.

This method checks whether the resource is an instance of the org.apache.sling.resource.SyntheticResource class.

Parameters:
res - The Resource to check whether it is a synthetic resource.
Returns:
true if res is a synthetic resource. false is returned if res is null or not an instance of the org.apache.sling.resource.SyntheticResource class.

isStarResource

public static boolean isStarResource(Resource res)
Returns true if the resource res is a "star resource". A star resource is a resource returned from the ResourceResolver.resolve(HttpServletRequest) whose path terminates in a /*. Generally such resource result from requests to something like /some/path/* or /some/path/*.html which may be used web applications to uniformly handle resources to be created.

This method checks whether the resource path ends with a /* indicating such a star resource.

Parameters:
res - The Resource to check whether it is a star resource.
Returns:
true if res is to be considered a star resource.
Throws:
NullPointerException - if res is null.

isNonExistingResource

public static boolean isNonExistingResource(Resource res)
Returns true if the resource res is a non-existing resource.

This method checks the resource type of the resource to match the well-known resource type sling:nonexisting of the NonExistingResource class defined in the Sling API.

Parameters:
res - The Resource to check whether it is a non-existing resource.
Returns:
true if res is to be considered a non-existing resource.
Throws:
NullPointerException - if res is null.

listChildren

public static Iterator<Resource> listChildren(Resource parent)
Deprecated. since 2.1.0, use Resource.listChildren() instead

Returns an Iterator of Resource objects loaded from the children of the given Resource.

This is a convenience method for ResourceResolver.listChildren(Resource).

Parameters:
parent - The Resource whose children are requested.
Returns:
An Iterator of Resource objects.
Throws:
NullPointerException - If parent is null.
SlingException - If any error occurs acquiring the child resource iterator.
See Also:
ResourceResolver.listChildren(Resource)

getValueMap

public static ValueMap getValueMap(Resource res)
Returns an ValueMap object for the given Resource. This method calls Adaptable.adaptTo(Class) with the ValueMap class as an argument. If the adaptTo method returns a map, this map is returned. If the resource is not adaptable to a value map, next an adaption to Map is tried and if this is successful the map is wrapped as a value map. If the adaptions are not successful an empty value map is returned. If null is provided as the resource an empty map is returned as well.

Parameters:
res - The Resource to adapt to the value map.
Returns:
A value map.

resourceTypeToPath

public static String resourceTypeToPath(String type)
Helper method, which returns the given resource type as returned from the Resource.getResourceType() as a relative path.

Parameters:
type - The resource type to be converted into a path
Returns:
The resource type as a path.
Since:
2.0.6

getResourceSuperType

public static String getResourceSuperType(ResourceResolver resourceResolver,
                                          String resourceType)
Returns the super type of the given resource type. This method converts the resource type to a resource path by calling resourceTypeToPath(String) and uses the resourceResolver to get the corresponding resource. If the resource exists, the Resource.getResourceSuperType() metod is called.

Parameters:
resourceResolver - The ResourceResolver used to access the resource whose path (relative or absolute) is given by the resourceType parameter.
resourceType - The resource type whose super type is to be returned. This type is turned into a path by calling the resourceTypeToPath(String) method before trying to get the resource through the resourceResolver.
Returns:
the super type of the resourceType or null if the resource type does not exists or returns null for its super type.
Since:
2.0.6

findResourceSuperType

public static String findResourceSuperType(Resource resource)
Returns the super type of the given resource. This method checks first if the resource itself knows its super type by calling Resource.getResourceSuperType(). If that returns null getResourceSuperType(ResourceResolver, String) is invoked with the resource type of the resource.

Parameters:
resource - The resource to return the resource super type for.
Returns:
the super type of the resource or null if no super type could be computed.
Since:
2.0.6

isA

public static boolean isA(Resource resource,
                          String resourceType)
Check if the resource is of the given type. This method first checks the resource type of the resource, then its super resource type and continues to go up the resource super type hierarchy.

Parameters:
resource - the resource to check
resourceType - the resource type to check the resource against
Returns:
false if resource is null. Otherwise returns the result of calling Resource.isResourceType(String) with the given resourceType.
Since:
2.0.6

adaptTo

public static <T> Iterator<T> adaptTo(Iterator<Resource> iterator,
                                      Class<T> type)
Return an iterator for objecs of the specified type. A new iterator is returned which tries to adapt the provided resources to the given type (using Adaptable.adaptTo(Class). If a resource in the original iterator is not adaptable to the given class, this object is skipped. This implies that the number of objects returned by the new iterator might be less than the number of resource objects.

Type Parameters:
T - The adapted type
Parameters:
iterator - A resource iterator.
Since:
2.0.6


Copyright © 2007-2011 The Apache Software Foundation. All Rights Reserved.