KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > selection > XPathExceptionSelector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.selection;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
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 /**
31  * Additional to the inherited functionality from its superclass ExceptionSelector,
32  * this selector allows to define xpath expressions to evaluate supplemental information
33  * given in the thrown exception.
34  * The configuration of this selector allows to map not only exceptions but also
35  * xpath expressions to symbolic names that are used in the <map:when> alternatives.
36  * <p>
37  * Example configuration :
38  * <pre>
39  * &lt;map:selector type="error" SRC="....XPathExceptionSelector">
40  * &lt;exception name="denied" class="my.comp.auth.AuthenticationFailure">
41  * &lt;xpath name="PasswordWrong" test="authCode=10"/>
42  * &lt;xpath name="PasswordExpired" test="errorCode=11"/>
43  * &lt;xpath name="AccessForbidden" test="errorCode&gt;11"/>
44  * &lt;/exception>
45  * &lt;/map:selector>
46  * </pre>
47  * This example shows several features :
48  * <li>the test is the xpath expression that will be evaluated against the exception ,</li>
49  * <li>an xpath expression can be given a name, which is used in the &lt;map:when> tests,</li>
50  *
51  * @author <a HREF="mailto:juergen.seitz@basf-it-services.com">J&uuml;rgen Seitz</a>
52  * @author <a HREF="mailto:bluetkemeier@s-und-n.de">Bj&ouml;rn L&uuml;tkemeier</a>
53  * @since 2.1
54  * @version CVS $Id: XPathExceptionSelector.java 30932 2004-07-29 17:35:38Z vgritsenko $
55  */

56 public class XPathExceptionSelector extends ExceptionSelector
57   implements Configurable {
58
59     private Map JavaDoc exception2XPath = new HashMap JavaDoc();
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             // Check if there are XPath-Expressions configured
70
xPathChildren = children[i].getChildren("xpath");
71             Map JavaDoc xPathMap = new LinkedMap(11);
72
73             for (int j = 0; j < xPathChildren.length; j++) {
74                 Configuration xPathChild = xPathChildren[j];
75
76                 String JavaDoc 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                 // store xpath - config if there is some
83
exception2XPath.put(children[i].getAttribute("name", null),
84                                     xPathMap);
85             }
86         }
87     }
88
89     /**
90      * Compute the exception type, given the configuration and the exception stored in the object model.
91      */

92     public Object JavaDoc getSelectorContext(Map JavaDoc objectModel, Parameters parameters) {
93
94         // get exception from super class
95
FindResult selectorContext = (FindResult) super.getSelectorContext(objectModel,
96                                          parameters);
97
98         if (selectorContext != null) {
99             String JavaDoc exceptionName = selectorContext.getName();
100             Throwable JavaDoc t = selectorContext.getThrowable();
101
102             Map JavaDoc xPathMap = (Map JavaDoc) exception2XPath.get(exceptionName);
103
104             if (xPathMap != null) {
105                 // create a context for the thrown exception
106
JXPathContext context = JXPathContext.newContext(t);
107
108                 for (Iterator JavaDoc iterator = xPathMap.entrySet().iterator(); iterator.hasNext(); ) {
109                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
110
111                     if (((CompiledExpression) entry.getValue()).getValue(context).equals(Boolean.TRUE)) {
112                         // set the configured name if the expression is succesfull
113
selectorContext.setName((String JavaDoc) entry.getKey());
114                         return selectorContext;
115                     }
116                 }
117             }
118         }
119
120         return selectorContext;
121     }
122 }
123
Popular Tags