org.apache.cocoon.components.modules.input
Class LocateResource

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.cocoon.components.modules.input.AbstractInputModule
          extended by org.apache.cocoon.components.modules.input.LocateResource
All Implemented Interfaces:
Disposable, Component, Composable, Configurable, LogEnabled, ThreadSafe, InputModule

public class LocateResource
extends AbstractInputModule
implements Composable, ThreadSafe

Locate a resource in a resource tree. Any attribute name is interpreted as a URI with the last part being the resource name unless it ends with a slash. The URI is checked if the resource exists and the URI is returned. If the resource does not exist, the URI is shortened until the resource name is found and the new URI is returned. If no resource with the given name exists, null is returned.

A use case is to locate the closest menu file or when moving a site from a filesystem path == URL system from a httpd to Cocoon and provide similar functions to .htaccess files.

Example: for context:/some/path/to/a/file.xml the following URIs are tested: context:/some/path/to/a/file.xml, context:/some/path/to/file.xml, context:/some/path/file.xml, context:/some/file.xml, and context:/file.xml. For the attribute name context:/some/path/foo/ tests context:/some/path/foo/, context:/some/path/, context:/some/, and context:/ are tested.

The getAttribute() method will return the URI for the first match while getAttributeValues() will return an array of all existing paths. getAttributeNames() will return an Iterator to an empty collection.

Version:
$Id: LocateResource.html 1304258 2012-03-23 10:09:27Z ilgrosso $
Author:
Christian Haul

Field Summary
protected static Collection col
           
protected  ComponentManager manager
           
 
Fields inherited from class org.apache.cocoon.components.modules.input.AbstractInputModule
settings
 
Fields inherited from interface org.apache.cocoon.components.modules.input.InputModule
ROLE
 
Constructor Summary
LocateResource()
           
 
Method Summary
protected  int calculateMinLen(String name)
          Calculate the minimal length of the URL, that is the position of the first ":" if a protocol is provided or otherwise 0.
 void compose(ComponentManager manager)
          Set the current ComponentManager instance used by this Composable.
protected  String extractFilename(String urlstring)
          if the url does not end with a "/", keep the last part in order to add it later again after traversing up
 Object getAttribute(String name, Configuration modeConf, Map objectModel)
          Standard access to an attribute's value.
 Iterator getAttributeNames(Configuration modeConf, Map objectModel)
          Returns an Iterator of String objects containing the names of the attributes available.
 Object[] getAttributeValues(String name, Configuration modeConf, Map objectModel)
          Returns an array of String objects containing all of the values the given attribute has, or null if the attribute does not exist.
protected  String locateResource(String urlstring, String filename, int minLen)
          Locate a resource with the given URL consisting of urlstring + filename.
protected  String shortenURI(String urlstring, int minLen)
          Remove one path element from the URL unless minimum length has been reached.
 
Methods inherited from class org.apache.cocoon.components.modules.input.AbstractInputModule
configure, dispose
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

col

protected static final Collection col

manager

protected ComponentManager manager
Constructor Detail

LocateResource

public LocateResource()
Method Detail

calculateMinLen

protected int calculateMinLen(String name)
Calculate the minimal length of the URL, that is the position of the first ":" if a protocol is provided or otherwise 0.

Parameters:
name -
Returns:
minimal length

shortenURI

protected String shortenURI(String urlstring,
                            int minLen)
Remove one path element from the URL unless minimum length has been reached.

Parameters:
urlstring -
minLen -
Returns:
shortened URI

extractFilename

protected String extractFilename(String urlstring)
if the url does not end with a "/", keep the last part in order to add it later again after traversing up


locateResource

protected String locateResource(String urlstring,
                                String filename,
                                int minLen)
Locate a resource with the given URL consisting of urlstring + filename. The filename is appended each time the path is shortened. Returns the first existing occurance.

Parameters:
urlstring -
filename -
minLen -
Returns:
urlstring if resource was found, null otherwise

compose

public void compose(ComponentManager manager)
             throws ComponentException
Set the current ComponentManager instance used by this Composable.

Specified by:
compose in interface Composable
Throws:
ComponentException

getAttribute

public Object getAttribute(String name,
                           Configuration modeConf,
                           Map objectModel)
                    throws ConfigurationException
Description copied from interface: InputModule
Standard access to an attribute's value. If more than one value exists, the first is returned. If the value does not exist, null is returned. To get all values, use InputModule.getAttributeValues(String, Configuration, Map) or InputModule.getAttributeNames(Configuration, Map) and InputModule.getAttribute(String, Configuration, Map) to get them one by one.

Specified by:
getAttribute in interface InputModule
Overrides:
getAttribute in class AbstractInputModule
Parameters:
name - a String that specifies what the caller thinks would identify an attribute. This is mainly a fallback if no modeConf is present.
modeConf - column's mode configuration from resource description. This argument is optional.
Throws:
ConfigurationException

getAttributeNames

public Iterator getAttributeNames(Configuration modeConf,
                                  Map objectModel)
                           throws ConfigurationException
Description copied from interface: InputModule
Returns an Iterator of String objects containing the names of the attributes available. If no attributes are available, the method returns an empty Iterator.

Specified by:
getAttributeNames in interface InputModule
Overrides:
getAttributeNames in class AbstractInputModule
Parameters:
modeConf - column's mode configuration from resource description. This argument is optional.
Throws:
ConfigurationException

getAttributeValues

public Object[] getAttributeValues(String name,
                                   Configuration modeConf,
                                   Map objectModel)
                            throws ConfigurationException
Description copied from interface: InputModule
Returns an array of String objects containing all of the values the given attribute has, or null if the attribute does not exist. As an alternative, InputModule.getAttributeNames(Configuration, Map) together with InputModule.getAttribute(String, Configuration, Map) can be used to get the values one by one.

Specified by:
getAttributeValues in interface InputModule
Overrides:
getAttributeValues in class AbstractInputModule
Parameters:
name - a String that specifies what the caller thinks would identify an attributes. This is mainly a fallback if no modeConf is present.
modeConf - column's mode configuration from resource description. This argument is optional.
Throws:
ConfigurationException


Copyright © 1999-2010 The Apache Software Foundation. All Rights Reserved.