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 19:25:32 UTC

cvs commit: cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor SimpleParentProcessingNode.java NullNode.java CategoryNode.java ContainerNode.java NamedContainerNode.java AbstractProcessingNode.java AbstractParentProcessingNode.java

unico       2003/11/16 10:25:32

  Modified:    src/java/org/apache/cocoon/components/treeprocessor
                        SimpleParentProcessingNode.java NullNode.java
                        CategoryNode.java ContainerNode.java
                        NamedContainerNode.java AbstractProcessingNode.java
                        AbstractParentProcessingNode.java
  Log:
  merge ProcessingNodes with NodeBuilders
  
  Revision  Changes    Path
  1.2       +14 -13    cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/SimpleParentProcessingNode.java
  
  Index: SimpleParentProcessingNode.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/SimpleParentProcessingNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SimpleParentProcessingNode.java	9 Mar 2003 00:09:17 -0000	1.1
  +++ SimpleParentProcessingNode.java	16 Nov 2003 18:25:31 -0000	1.2
  @@ -50,8 +50,8 @@
   */
   package org.apache.cocoon.components.treeprocessor;
   
  -import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode;
  -import org.apache.cocoon.components.treeprocessor.ProcessingNode;
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
   
   import org.apache.cocoon.environment.Environment;
   
  @@ -60,16 +60,17 @@
    * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
    * @version CVS $Id$
    */
  -
   public abstract class SimpleParentProcessingNode extends AbstractParentProcessingNode {
  -
  -    /** The childrens of this matcher */
  -    protected ProcessingNode[] children;
  -
  -    public void setChildren(ProcessingNode[] children) {
  -        this.children = children;
  +    
  +    /** The child nodes belonging to this node */
  +    protected ProcessingNode[] m_children;
  +    
  +    
  +    public void configure(Configuration config) throws ConfigurationException {
  +        super.configure(config);
  +        m_children = getChildNodes(config);
       }
  -
  +    
       /**
        * Boolean method with returns true if this Node has children 
        * and false otherwise
  @@ -77,8 +78,9 @@
        * @return boolean 
        */
       public boolean hasChildren() {
  -        if ((this.children == null) || (this.children.length > 0))
  +        if ((m_children == null) || (m_children.length > 0)) {
               return true;
  +        }
           return false;
       }
   
  @@ -87,7 +89,6 @@
        * Define common invoke behavior here
        */
       public boolean invoke(Environment env, InvokeContext context) throws Exception {
  -
   
           // inform the pipeline (if available) that we have come across
           // a possible branch point
  
  
  
  1.2       +6 -2      cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/NullNode.java
  
  Index: NullNode.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/NullNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- NullNode.java	9 Mar 2003 00:09:16 -0000	1.1
  +++ NullNode.java	16 Nov 2003 18:25:31 -0000	1.2
  @@ -57,8 +57,12 @@
    *
    * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
    * @version CVS $Id$
  + * 
  + * @avalon.component
  + * @avalon.service type="ProcessingNode"
  + * @x-avalon.lifestyle type="singleton"
  + * @x-avalon.info name="null-node"
    */
  -
   public class NullNode extends AbstractProcessingNode {
   
       public final boolean invoke(Environment env, InvokeContext context) throws Exception {
  
  
  
  1.2       +7 -7      cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/CategoryNode.java
  
  Index: CategoryNode.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/CategoryNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CategoryNode.java	9 Mar 2003 00:09:15 -0000	1.1
  +++ CategoryNode.java	16 Nov 2003 18:25:31 -0000	1.2
  @@ -50,18 +50,18 @@
   */
   package org.apache.cocoon.components.treeprocessor;
   
  -import org.apache.cocoon.environment.Environment;
  -import org.apache.cocoon.ProcessingException;
  +import java.util.HashMap;
  +import java.util.Map;
   
  -import java.util.*;
  +import org.apache.cocoon.ProcessingException;
  +import org.apache.cocoon.environment.Environment;
   
   /**
  - * A generic container node that just invokes its children.
  - *
  + * TODO: do we still need this?
  + * 
    * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
    * @version CVS $Id$
    */
  -
   public final class CategoryNode extends AbstractParentProcessingNode {
   
       /** The name of this category */
  
  
  
  1.2       +26 -4     cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/ContainerNode.java
  
  Index: ContainerNode.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/ContainerNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ContainerNode.java	9 Mar 2003 00:09:15 -0000	1.1
  +++ ContainerNode.java	16 Nov 2003 18:25:31 -0000	1.2
  @@ -50,6 +50,8 @@
   */
   package org.apache.cocoon.components.treeprocessor;
   
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
   import org.apache.cocoon.environment.Environment;
   
   /**
  @@ -57,12 +59,32 @@
    *
    * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
    * @version CVS $Id$
  + * 
  + * @avalon.component
  + * @avalon.service type="ProcessingNode"
  + * @x-avalon.lifestyle type="singleton"
  + * @x-avalon.info name="container-node"
    */
  -
   public class ContainerNode extends SimpleParentProcessingNode {
   
  +    public ContainerNode() {
  +    }
  +    
  +    public void configure(Configuration config) throws ConfigurationException {
  +        super.configure(config);
  +        if (m_children.length == 0) {
  +            String msg = "There must be at least one child at " + config.getLocation();
  +            throw new ConfigurationException(msg);
  +        }
  +    }
  +    
       public final boolean invoke(Environment env, InvokeContext context) throws Exception {
  -
  -        return invokeNodes(this.children, env, context);
  +        return invokeNodes(m_children, env, context);
  +    }
  +    
  +    /** This builder has no parameters -- return <code>false</code> */
  +    protected boolean hasParameters() {
  +        return false;
       }
  +    
   }
  
  
  
  1.2       +17 -4     cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/NamedContainerNode.java
  
  Index: NamedContainerNode.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/NamedContainerNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- NamedContainerNode.java	9 Mar 2003 00:09:16 -0000	1.1
  +++ NamedContainerNode.java	16 Nov 2003 18:25:31 -0000	1.2
  @@ -50,19 +50,32 @@
   */
   package org.apache.cocoon.components.treeprocessor;
   
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +
   /**
    * A named container node that just invokes its children.
    *
    * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
    * @version CVS $Id$
  + * 
  + * @avalon.component
  + * @avalon.service type="ProcessingNode"
  + * @x-avalon.lifestyle type="singleton"
  + * @x-avalon.info name="named-container-node"
    */
  -
   public class NamedContainerNode extends ContainerNode implements NamedProcessingNode {
   
  +    private static final String NAME_ATTR = "name";
  +    
       private String name;
   
  -    public NamedContainerNode(String name) {
  -        this.name = name;
  +    public NamedContainerNode() {
  +    }
  +
  +    public void configure(Configuration config) throws ConfigurationException {
  +        super.configure(config);
  +        config.getAttribute(NAME_ATTR);
       }
   
       public String getName() {
  
  
  
  1.3       +17 -8     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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractProcessingNode.java	16 Nov 2003 14:30:01 -0000	1.2
  +++ AbstractProcessingNode.java	16 Nov 2003 18:25:31 -0000	1.3
  @@ -50,6 +50,9 @@
   */
   package org.apache.cocoon.components.treeprocessor;
   
  +import java.util.HashMap;
  +import java.util.Map;
  +
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  @@ -57,16 +60,11 @@
   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>
  @@ -76,7 +74,7 @@
   public abstract class AbstractProcessingNode extends AbstractLogEnabled 
   implements ProcessingNode, Serviceable, Configurable {
   
  -    private static final String PARAMETER_ELEMENT = "parameter";
  +    protected static final String PARAMETER_ELEMENT = "parameter";
       private static final String PARAMETER_NAME_ATTR = "name";
       private static final String PARAMETER_VALUE_ATTR = "value";
       
  @@ -105,7 +103,7 @@
       public final String getLocation() {
           return m_location;
       }
  -        
  +    
       /**
        * Parametrizable ProcessingNodes can overide this method to
        * have resolvable parameters set at configuration time.
  @@ -140,6 +138,17 @@
                   String msg = "Invalid pattern '" + value + "' at " + getConfigLocation(child);
                   throw new ConfigurationException(msg, pe);
               }
  +        }
  +    }
  +    
  +    /**
  +     * Check if the namespace URI of the given configuraition is the same as the
  +     * one given by the builder.
  +     */
  +    protected final void checkNamespace(Configuration config) throws ConfigurationException {
  +        if (TreeProcessor.SITEMAP_NS.equals(config.getNamespace())) {
  +            String msg = "Invalid namespace '" + config.getNamespace() + "' at " + getConfigLocation(config);
  +            throw new ConfigurationException(msg);
           }
       }
       
  
  
  
  1.2       +131 -6    cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/AbstractParentProcessingNode.java
  
  Index: AbstractParentProcessingNode.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/treeprocessor/AbstractParentProcessingNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractParentProcessingNode.java	9 Mar 2003 00:09:15 -0000	1.1
  +++ AbstractParentProcessingNode.java	16 Nov 2003 18:25:31 -0000	1.2
  @@ -50,25 +50,32 @@
   */
   package org.apache.cocoon.components.treeprocessor;
   
  -import org.apache.cocoon.environment.Environment;
  -
  +import java.util.ArrayList;
  +import java.util.Collection;
  +import java.util.List;
   import java.util.Map;
   
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.cocoon.environment.Environment;
  +
   /**
    *
    * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
    * @version CVS $Id$
    */
  -
   public abstract class AbstractParentProcessingNode extends AbstractProcessingNode {
  -
  +    
  +    
  +    public AbstractParentProcessingNode() {
  +    }
  +    
       /**
        * Invoke all nodes of a node array in order, until one succeeds.
        *
        * @param currentMap the <code>Map<code> of parameters produced by this node,
        *            which is added to <code>listOfMap</code>.
        */
  -
       protected final boolean invokeNodes(
           ProcessingNode[] nodes,
           Environment env,
  @@ -112,4 +119,122 @@
   
           return false;
       }
  +    
  +    /**
  +     * Subclasses should overide this method if they want to allow certain children.
  +     * 
  +     * @return  a Collection of node names that are allowed as children of this node.
  +     */
  +    protected Collection getAllowedChildren() {
  +        return null;
  +    }
  +    
  +    /**
  +     * Subclasses should overide this method if they want to forbid certain children.
  +     * 
  +     * @return  a Collection of node names that are forbidden as children of this node.
  +     */
  +    protected Collection getForbiddenChildren() {
  +        return null;
  +    }
  +    
  +    protected final ProcessingNode[] getChildNodes(Configuration config) throws ConfigurationException {
  +        return toNodeArray(getChildNodesList(config));
  +    }
  +    
  +    private final ProcessingNode[] toNodeArray(List list) {
  +        return (ProcessingNode[])list.toArray(new ProcessingNode[list.size()]);
  +    }
  +
  +    /**
  +     * Create the <code>ProcessingNode</code>s for the children of a given node.
  +     * Child nodes are controlled to be actually allowed in this node.
  +     */
  +    private final List getChildNodesList(Configuration config) throws ConfigurationException {
  +
  +        Configuration[] children = config.getChildren();
  +        List result = new ArrayList();
  +        
  +        for (int i = 0; i < children.length; i++) {
  +            
  +            Configuration child = children[i];
  +            try {
  +                 // TODO: is this check still neccesary?
  +                 if (isChild(child)) {
  +                    // OK : look it up from service manager
  +                    String id = child.getAttribute("id-ref");
  +                    result.add(m_manager.lookup(ProcessingNode.ROLE + "/" + id));
  +                 }
  +            } catch(ConfigurationException ce) {
  +                throw ce;
  +            } catch(Exception e) {
  +                String msg = "Error while creating node '" + child.getName() + "' at " + child.getLocation();
  +                throw new ConfigurationException(msg, e);
  +            }
  +        }
  +
  +        return result;
  +    }
  +    
  +    /**
  +     * Checks if a child element and is allowed, and if not throws a <code>ConfigurationException</code>.
  +     *
  +     * @param child the child configuration to check.
  +     * @return <code>true</code> if this child should be considered or <code>false</code>
  +     *         if it should be ignored.
  +     * @throws ConfigurationException if this child isn't allowed.
  +     */
  +    private final boolean isChild(Configuration child) throws ConfigurationException {
  +
  +        checkNamespace(child);
  +
  +        String name = child.getName();
  +
  +        // Is this a parameter of a parameterizable node builder ?
  +        if (isParameter(child)) {
  +            return false;
  +        }
  +
  +        // Is this element to be ignored ?
  +        // TODO: do we need this? It's not used in treeprocessor-builtins.xml
  +//        Collection ignoredChildren = getIgnoredChildren();
  +//        if (ignoredChildren != null && ignoredChildren.contains(name)) {
  +//            if (getLogger().isDebugEnabled()) {
  +//                getLogger().debug("Element '" + name + "' is ignored for building children of element '" +
  +//                                  child.getName() + "'");
  +//            }
  +//
  +//            return false;
  +//        }
  +
  +        // Is it allowed ?
  +        Collection allowedChildren = getAllowedChildren();
  +        Collection forbiddenChildren = getForbiddenChildren();
  +        if ( (allowedChildren != null && !allowedChildren.contains(name)) ||
  +             (forbiddenChildren != null && forbiddenChildren.contains(name)) ) {
  +            String msg = "Element '" + name + "' is not allowed at " + getConfigLocation(child);
  +            throw new ConfigurationException(msg);
  +        }
  +        return true;
  +    }
  +    
  +    /**
  +     * Check if the current config element is a parameter.
  +     * 
  +     * @throws ConfigurationException  if this config element is a parameter
  +     * and this node does not allow parameters.
  +     */
  +    private final boolean isParameter(Configuration config) throws ConfigurationException {
  +        String name = config.getName();
  +        if (name.equals(PARAMETER_ELEMENT)) {
  +            if (this.hasParameters()) {
  +                return true;
  +            } else {
  +                String msg = "Element '" + name + "' has no parameters at " + getConfigLocation(config);
  +                throw new ConfigurationException(msg);
  +            }
  +        }
  +        return false;
  +    }
  +
   }