1 16 package org.apache.cocoon.selection; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.commons.collections.map.LinkedMap; 27 import org.apache.commons.jxpath.CompiledExpression; 28 import org.apache.commons.jxpath.JXPathContext; 29 30 56 public class XPathExceptionSelector extends ExceptionSelector 57 implements Configurable { 58 59 private Map exception2XPath = new HashMap (); 60 61 public void configure(Configuration conf) throws ConfigurationException { 62 63 super.configure(conf); 64 65 Configuration[] children = conf.getChildren("exception"); 66 Configuration[] xPathChildren; 67 68 for (int i = 0; i < children.length; i++) { 69 xPathChildren = children[i].getChildren("xpath"); 71 Map xPathMap = new LinkedMap(11); 72 73 for (int j = 0; j < xPathChildren.length; j++) { 74 Configuration xPathChild = xPathChildren[j]; 75 76 String xPathName = xPathChild.getAttribute("name"); 77 CompiledExpression xPath = JXPathContext.compile(xPathChild.getAttribute("test")); 78 79 xPathMap.put(xPathName, xPath); 80 } 81 if (xPathMap.size() > 0) { 82 exception2XPath.put(children[i].getAttribute("name", null), 84 xPathMap); 85 } 86 } 87 } 88 89 92 public Object getSelectorContext(Map objectModel, Parameters parameters) { 93 94 FindResult selectorContext = (FindResult) super.getSelectorContext(objectModel, 96 parameters); 97 98 if (selectorContext != null) { 99 String exceptionName = selectorContext.getName(); 100 Throwable t = selectorContext.getThrowable(); 101 102 Map xPathMap = (Map ) exception2XPath.get(exceptionName); 103 104 if (xPathMap != null) { 105 JXPathContext context = JXPathContext.newContext(t); 107 108 for (Iterator iterator = xPathMap.entrySet().iterator(); iterator.hasNext(); ) { 109 Map.Entry entry = (Map.Entry ) iterator.next(); 110 111 if (((CompiledExpression) entry.getValue()).getValue(context).equals(Boolean.TRUE)) { 112 selectorContext.setName((String ) entry.getKey()); 114 return selectorContext; 115 } 116 } 117 } 118 } 119 120 return selectorContext; 121 } 122 } 123 | Popular Tags |