KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openinventions > webappfilter > processor > CommandTraverser


1 package com.openinventions.webappfilter.processor;
2
3 import com.openinventions.metaframework.*;
4 import org.apache.commons.logging.*;
5
6 public class CommandTraverser implements Processor {
7     private static final Log log = LogFactory.getLog(CommandTraverser.class);
8     private static final int MAX_ACTIONS = 50;
9     
10     public void process(State state, Element context) throws Exception JavaDoc {
11         ElementFactory factory = new JXPathElementFileFactory();
12         String JavaDoc filterDirection = (String JavaDoc) state.get("filterDirection");
13         String JavaDoc singleCommand = "";
14        
15         if (state.isExists("singleCommand")) {
16             singleCommand = (String JavaDoc) state.get("singleCommand");
17         }
18         int i = 0;
19         while (filterDirection.equals("")) {
20             String JavaDoc path = (String JavaDoc) state.get("nextCommand");
21             Element config = (Element) state.get("com.openinventions.metaframework.Config");
22
23             if (log.isDebugEnabled()) {
24                 log.debug("path = " + path);
25                 log.debug("config.isExists = " + config.isExists(path));
26             }
27         
28             path = normalizePath(path);
29
30             if (log.isDebugEnabled()) {
31                 log.debug("path = " + path);
32                 log.debug("config.isExists = " + config.isExists(path));
33             }
34             
35             if (!config.isExists(path)) {
36                 if (state.isExists("lastCommand")) {
37                     path = (String JavaDoc) state.get("lastCommand");
38                     if ((path == null) || path.equals("")) {
39                         log.error("bad lastCommand. path not found = " + path);
40                     }
41                 } else {
42                     log.error("nothing to traverse. path not found = " + path);
43                 }
44             }
45             Element commandContext = (Element) config.getElement(path);
46             path = commandContext.getPath();
47             state.set("nextCommand", path);
48             
49             Processor command = null;
50             String JavaDoc commandName = config.getValue("name(" + path + ")");
51
52             if (log.isDebugEnabled()) {
53                 log.debug("path = " + path);
54                 log.debug("commandName = " + commandName);
55             }
56
57             String JavaDoc relative = "";
58             if (state.isExists("relativeCommand")) {
59                 relative = (String JavaDoc) state.get("relativeCommand");
60             }
61             
62             // command exists
63
if (config.isExists("commands/command[@name='" + commandName + "']")) {
64                 String JavaDoc refid = config.getValue("commands/command[@name='" + commandName + "']/@refid");
65                 Element commandElement = null;
66                 if (!refid.equals("")) {
67                     commandElement = config.getElement("commands/command[@id='" + refid + "']");
68                 } else {
69                     commandElement = config.getElement("commands/command[@name='" + commandName + "']");
70                 }
71                 commandName = config.getValue("name(" + commandElement.getPath() + "/child::*[1])");
72                 if (log.isDebugEnabled()) {
73                     log.debug("refid = " + refid);
74                     log.debug("commandElement.getPath() = " + commandElement.getPath());
75                     log.debug("commandName = " + commandName);
76                 }
77                 command = (Processor) state.get(commandName);
78                 command.process(state, commandElement.getElement("/child::*[1]"));
79             } else {
80                 command = (Processor) state.get(commandName);
81                 command.process(state, commandContext);
82             }
83
84             if (!relative.equals("")) {
85                 String JavaDoc nextCommand = (String JavaDoc) state.get("nextCommand");
86                 // If next command has been changed by processor, don't adjust
87
if (nextCommand.equals(path)) {
88                     state.set("nextCommand", path + relative);
89                 }
90             }
91             
92             filterDirection = (String JavaDoc) state.get("filterDirection");
93             i++;
94             // catches infinite loops
95
if (i > MAX_ACTIONS) {
96                 log.error(MAX_ACTIONS + " actions is maximum supported. possible infinite loop encountered");
97                 break;
98             }
99
100             if (singleCommand.equals("true")) {
101                 break;
102             }
103         }
104     }
105
106     private String JavaDoc normalizePath(String JavaDoc path) {
107         // For some reason, following-sibling::*/preceding-sibling::* doesn't seem to evaluate properly with XPath
108
while (path.indexOf("/following-sibling::*[1]/preceding-sibling::*[1]") >= 0) {
109             path = path.replaceAll("/following-sibling::\\*\\[1\\]/preceding-sibling::\\*\\[1\\]", "");
110         }
111
112         while (path.indexOf("/preceding-sibling::*[1]/following-sibling::*[1]") >= 0) {
113             path = path.replaceAll("/preceding-sibling::\\*\\[1\\]/following-sibling::\\*\\[1\\]", "");
114         }
115         return path;
116     }
117 }
118
119 /* ====================================================================
120  * The webappfilter License, Version 1.1
121  *
122  * Copyright (c) 2002 Ivar Chan. All rights
123  * reserved.
124  *
125  * Redistribution and use in source and binary forms, with or without
126  * modification, are permitted provided that the following conditions
127  * are met:
128  *
129  * 1. Redistributions of source code must retain the above copyright
130  * notice, this list of conditions and the following disclaimer.
131  *
132  * 2. Redistributions in binary form must reproduce the above copyright
133  * notice, this list of conditions and the following disclaimer in
134  * the documentation and/or other materials provided with the
135  * distribution.
136  *
137  * 3. The end-user documentation included with the redistribution,
138  * if any, must include the following acknowledgment:
139  * "This product includes software developed by
140  * Ivar Chan (http://www.openinventions.com/webappfilter/)."
141  * Alternately, this acknowledgment may appear in the software itself,
142  * if and wherever such third-party acknowledgments normally appear.
143  *
144  * 4. The name "webappfilter" must not be used to endorse or promote products
145  * derived from this software without prior written permission. For
146  * written permission, please contact ivarchan@acm.org.
147  *
148  * 5. Products derived from this software may not be called "webappfilter",
149  * nor may "webappfilter" appear in their name, without
150  * prior written permission of Ivar Chan.
151  *
152  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
153  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
154  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
155  * DISCLAIMED. IN NO EVENT SHALL THE IVAR CHAN BE LIABLE FOR ANY
156  * DIRECT, INDIRECT, INCIDENTAL,
157  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
158  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
159  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
160  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
161  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
162  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
163  * SUCH DAMAGE.
164  * ====================================================================
165  *
166  * This software consists of voluntary contributions made by many
167  * individuals. For more information on webappfilter, please see
168  * <http://www.openinventions/webappfilter/>.
169  */

170
Popular Tags