1 16 package org.apache.cocoon.components.modules.input; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 21 import org.apache.commons.jxpath.ClassFunctions; 22 import org.apache.commons.jxpath.FunctionLibrary; 23 import org.apache.commons.jxpath.PackageFunctions; 24 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 33 public class JXPathHelperConfiguration { 34 35 40 private FunctionLibrary library; 41 42 47 private boolean lenient; 48 49 52 private Map namespaces; 53 54 55 58 public JXPathHelperConfiguration() { 59 this.lenient = true; 60 } 61 62 65 public JXPathHelperConfiguration(Configuration config) 66 throws ConfigurationException { 67 this.lenient = config.getChild("lenient").getValueAsBoolean(true); 68 this.library = new FunctionLibrary(); 69 setup(config); 70 71 this.library.addFunctions(new PackageFunctions("", null)); 74 } 75 76 79 public JXPathHelperConfiguration(JXPathHelperConfiguration global, Configuration config) 80 throws ConfigurationException { 81 this.lenient = global.lenient; 82 this.library = new FunctionLibrary(); 83 this.library.addFunctions(global.getLibrary()); 84 if (global.getNamespaces() != null) { 85 this.namespaces = new HashMap (global.getNamespaces()); 86 } 87 setup(config); 88 } 89 90 91 public boolean isLenient() { 92 return this.lenient; 93 } 94 95 public FunctionLibrary getLibrary() { 96 return this.library; 97 } 98 99 public Map getNamespaces() { 100 return this.namespaces; 101 } 102 103 104 private void setup(Configuration config) 105 throws ConfigurationException { 106 getFunctions(config); 107 getPackages(config); 108 getNamespaces(config); 109 } 110 111 118 private void getFunctions(Configuration conf) { 119 120 Configuration[] children = conf.getChildren("function"); 121 int i = children.length; 122 while (i-- > 0) { 123 String clazzName = children[i].getAttribute("name", null); 124 String prefix = children[i].getAttribute("prefix", null); 125 if (clazzName != null && prefix != null) { 126 try { 127 Class clazz = Class.forName(clazzName); 128 this.library.addFunctions(new ClassFunctions(clazz, prefix)); 129 } catch (ClassNotFoundException cnf) { 130 } 132 } 133 } 134 } 135 136 143 private void getPackages(Configuration conf) { 144 145 Configuration[] children = conf.getChildren("package"); 146 int i = children.length; 147 while (i-- > 0) { 148 String packageName = children[i].getAttribute("name", null); 149 String prefix = children[i].getAttribute("prefix", null); 150 if (packageName != null && prefix != null) { 151 this.library.addFunctions(new PackageFunctions(packageName, prefix)); 152 } 153 } 154 } 155 156 163 private void getNamespaces(Configuration conf) 164 throws ConfigurationException { 165 166 Configuration[] children = conf.getChildren("namespace"); 167 int i = children.length; 168 if (i > 0) { 169 this.namespaces = new HashMap (i + 2); 170 } 171 while (i-- > 0) { 172 String uri = children[i].getAttribute("uri"); 173 String prefix = children[i].getAttribute("prefix"); 174 if (uri != null && prefix != null) { 175 this.namespaces.put(prefix, uri); 176 } 177 } 178 } 179 } 180 | Popular Tags |