1 16 package org.apache.cocoon.components.treeprocessor; 17 18 import org.apache.avalon.framework.configuration.Configurable; 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.ConfigurationException; 21 import org.apache.cocoon.util.StringUtils; 22 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collection ; 26 import java.util.List ; 27 28 35 public abstract class AbstractParentProcessingNodeBuilder extends AbstractProcessingNodeBuilder 36 implements Configurable { 37 38 protected Collection allowedChildren; 39 40 protected Collection forbiddenChildren; 41 42 protected Collection ignoredChildren; 43 44 47 public void configure(Configuration config) throws ConfigurationException { 48 this.allowedChildren = getStringCollection(config.getChild("allowed-children")); 49 this.forbiddenChildren = getStringCollection(config.getChild("forbidden-children")); 50 this.ignoredChildren = getStringCollection(config.getChild("ignored-children")); 51 } 52 53 61 protected boolean isChild(Configuration child) throws ConfigurationException { 62 63 checkNamespace(child); 64 65 String name = child.getName(); 66 67 if (isParameter(child)) { 69 return false; 70 } 71 72 if (ignoredChildren != null && ignoredChildren.contains(name)) { 74 if (getLogger().isDebugEnabled()) { 75 getLogger().debug("Element '" + name + "' is ignored for building children of element '" + 76 child.getName() + "'"); 77 } 78 79 return false; 80 } 81 82 if ( (allowedChildren != null && !allowedChildren.contains(name)) || 84 (forbiddenChildren != null && forbiddenChildren.contains(name)) ) { 85 String msg = "Element '" + name + "' is not allowed at " + child.getLocation(); 86 throw new ConfigurationException(msg); 87 } 88 89 return true; 90 } 91 92 protected boolean isParameter(Configuration config) throws ConfigurationException { 93 String name = config.getName(); 94 if (name.equals(this.treeBuilder.getParameterName())) { 95 if (this.hasParameters()) { 96 return true; 97 } else { 98 String msg = "Element '" + name + "' has no parameters at " + config.getLocation(); 99 throw new ConfigurationException(msg); 100 } 101 } 102 return false; 103 } 104 105 109 protected List buildChildNodesList(Configuration config) throws Exception { 110 111 Configuration[] children = config.getChildren(); 112 List result = new ArrayList (); 113 114 for (int i = 0; i < children.length; i++) { 115 116 Configuration child = children[i]; 117 try { 118 if (isChild(child)) { 119 ProcessingNodeBuilder childBuilder = this.treeBuilder.createNodeBuilder(child); 121 result.add(childBuilder.buildNode(child)); 122 } 123 124 } catch(ConfigurationException ce) { 125 throw ce; 126 } catch(Exception e) { 127 String msg = "Error while creating node '" + child.getName() + "' at " + child.getLocation(); 128 throw new ConfigurationException(msg, e); 129 } 130 } 131 132 return result; 133 } 134 135 protected ProcessingNode[] buildChildNodes(Configuration config) throws Exception { 136 return toNodeArray(buildChildNodesList(config)); 137 } 138 139 143 public static ProcessingNode[] toNodeArray(List list) { 144 return (ProcessingNode[])list.toArray(new ProcessingNode[list.size()]); 145 } 146 147 153 private Collection getStringCollection(Configuration config) { 154 String s = config.getValue(null); 155 156 return (s == null) ? null : Arrays.asList(StringUtils.split(s, ", \t\n\r")); 157 } 158 } 159 | Popular Tags |