You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by un...@apache.org on 2003/11/16 15:30:01 UTC

cvs commit: cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor AbstractProcessingNode.java

unico       2003/11/16 06:30:01

  Modified:    src/java/org/apache/cocoon/components/treeprocessor
                        AbstractProcessingNode.java
  Log:
  merge with AbstractProcessingNodeBuilder
  
  Revision  Changes    Path
  1.2       +93 -16    cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/AbstractProcessingNode.java
  
  Index: AbstractProcessingNode.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/AbstractProcessingNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractProcessingNode.java	9 Mar 2003 00:09:15 -0000	1.1
  +++ AbstractProcessingNode.java	16 Nov 2003 14:30:01 -0000	1.2
  @@ -50,39 +50,116 @@
   */
   package org.apache.cocoon.components.treeprocessor;
   
  +import org.apache.avalon.framework.configuration.Configurable;
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
  +import org.apache.avalon.framework.service.ServiceException;
  +import org.apache.avalon.framework.service.ServiceManager;
  +import org.apache.avalon.framework.service.Serviceable;
  +import org.apache.cocoon.Constants;
  +import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
   import org.apache.cocoon.environment.SourceResolver;
  +import org.apache.cocoon.sitemap.PatternException;
  +import org.apache.cocoon.xml.LocationAugmentationPipe;
   
  +import java.util.Collections;
  +import java.util.HashMap;
   import java.util.Map;
   
   /**
    *
    * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
  + * @author <a href="mailto:unico@apache.org">Unico Hommes</a>
    * @version CVS $Id$
    */
  +public abstract class AbstractProcessingNode extends AbstractLogEnabled 
  +implements ProcessingNode, Serviceable, Configurable {
   
  -public abstract class AbstractProcessingNode extends AbstractLogEnabled implements ProcessingNode {
  -
  -    protected String location = "unknown location";
  -
  +    private static final String PARAMETER_ELEMENT = "parameter";
  +    private static final String PARAMETER_NAME_ATTR = "name";
  +    private static final String PARAMETER_VALUE_ATTR = "value";
  +    
  +    private String m_location;
  +    protected Map m_parameters;
  +    
  +    protected ServiceManager m_manager;
  +    
  +    public AbstractProcessingNode() {
  +    }
  +    
  +    public void configure(final Configuration config) throws ConfigurationException {
  +        m_location = getConfigLocation(config);
  +        if (hasParameters()) {
  +            setParameters(config);
  +        }
  +    }
  +    
  +    public void service(final ServiceManager manager) throws ServiceException {
  +        m_manager = manager;
  +    }
  +    
       /**
  -     * Get the <code>SourceResolver</code> in an object model.
  +     * Get the location of this node.
        */
  -    protected static final SourceResolver getSourceResolver(Map objectModel) {
  -        return (SourceResolver)objectModel.get(OBJECT_SOURCE_RESOLVER);
  +    public final String getLocation() {
  +        return m_location;
       }
  -
  +        
       /**
  -     * Get the location of this node.
  +     * Parametrizable ProcessingNodes can overide this method to
  +     * have resolvable parameters set at configuration time.
  +     * 
  +     * @return  whether this processing node is parametrizable.
        */
  -    public String getLocation() {
  -        return this.location;
  +    protected boolean hasParameters() {
  +        return false;
       }
  -
  +    
  +    /**
  +     * Set &lt;xxx:parameter&gt; elements as a <code>Map</code> of </code>ListOfMapResolver</code>s,
  +     * that can be turned into parameters using <code>ListOfMapResolver.buildParameters()</code>.
  +     *
  +     * @return the Map of ListOfMapResolver, or <code>null</code> if there are no parameters.
  +     */
  +    private final void setParameters(Configuration config) throws ConfigurationException {
  +        final Configuration[] children = config.getChildren(PARAMETER_ELEMENT);
  +        if (children.length == 0) {
  +            return;
  +        }
  +        m_parameters = new HashMap();
  +        for (int i = 0; i < children.length; i++) {
  +            Configuration child = children[i];
  +            String name = child.getAttribute(PARAMETER_NAME_ATTR);
  +            String value = child.getAttribute(PARAMETER_VALUE_ATTR);
  +            try {
  +                m_parameters.put(
  +                    VariableResolverFactory.getResolver(name, m_manager),
  +                    VariableResolverFactory.getResolver(value, m_manager));
  +            } catch(PatternException pe) {
  +                String msg = "Invalid pattern '" + value + "' at " + getConfigLocation(child);
  +                throw new ConfigurationException(msg, pe);
  +            }
  +        }
  +    }
  +    
  +    /**
  +     * Get the location information that is encoded as a location attribute
  +     * on the current configuration element.
  +     * 
  +     * @param config  the configuration element to read the location from.
  +     * @return  the location if the location attribute exists, else <code>Unknown</code>.
  +     */
  +    protected final String getConfigLocation(Configuration config) {
  +        return config.getAttribute(LocationAugmentationPipe.LOCATION_ATTR,
  +            LocationAugmentationPipe.UNKNOWN_LOCATION);
  +    }
  +    
       /**
  -     * Set the location of this node.
  +     * Get the <code>SourceResolver</code> in an object model.
        */
  -    public void setLocation(String location) {
  -        this.location = location;
  +    protected static final SourceResolver getSourceResolver(Map objectModel) {
  +        return (SourceResolver) objectModel.get(OBJECT_SOURCE_RESOLVER);
       }
  +
   }