KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.openinventions.webappfilter.processor;
2
3 import com.openinventions.metaframework.*;
4 import com.openinventions.webappfilter.processor.xpathexpression.*;
5 import java.io.*;
6 import org.apache.commons.logging.*;
7
8 public class XPathExpression implements Processor {
9     private static final Log log = LogFactory.getLog(XPathExpression.class);
10     
11     public void process(State state, Element context) throws Exception JavaDoc {
12         if (log.isDebugEnabled()) {
13             log.debug("context.getValue() = " + context.getValue());
14         }
15         
16         String JavaDoc[] lines = context.getValue().split(";");
17         for (int i = 0; i < lines.length; i++) {
18             String JavaDoc line = lines[i].trim();
19             if ((line == null) || (line.equals(""))) {
20                 continue;
21             }
22
23             int equalIndex = line.indexOf("=");
24             if (equalIndex < 0) {
25                 log.warn("expected equals " + line);
26                 continue;
27             }
28
29             String JavaDoc left = line.substring(0, equalIndex);
30             if (!left.startsWith("/")) {
31                 log.warn("expected left side of equals to start with slash " + line);
32                 continue;
33             }
34             
35             String JavaDoc right = line.substring(equalIndex + 1, line.length());
36             if (log.isDebugEnabled()) {
37                 log.debug("right = " + right);
38             }
39             Object JavaDoc o = new XPathExpressionParser(new StringReader(right)).parse(state);
40             
41             stateSet(state, left, o);
42         }
43     }
44
45     private void stateSet(State state, String JavaDoc left, Object JavaDoc o) throws Exception JavaDoc {
46         if (log.isDebugEnabled()) {
47             log.debug("left = " + left);
48             log.debug("o = " + o);
49             log.debug("o.getClass().getName() = " + o.getClass().getName());
50         }
51
52         int slash = left.indexOf("/", 1);
53         String JavaDoc name = "";
54         String JavaDoc path = "";
55
56         if (slash < 0) {
57             name = left.substring(1).trim();
58             if (log.isDebugEnabled()) {
59                 log.debug("name = " + name);
60                 log.debug("o = " + o);
61             }
62             state.set(name, o);
63         } else {
64             Element element = null;
65             name = left.substring(1, slash).trim();
66             path = left.substring(slash);
67             
68             if (!state.isExists(name)) {
69                 ElementFactory factory = (ElementFactory) state.get("com.openinventions.metaframework.ElementFactory");
70                 element = factory.createElement(name);
71             } else {
72                 element = (Element) state.get(name);
73             }
74
75             if (o instanceof String JavaDoc) {
76                 element.setValue(path, (String JavaDoc) o);
77             } else if (o instanceof Element) {
78                 element.setElement(path, (Element) o);
79             } else {
80                 log.error("not expecting = " + o);
81             }
82
83             if (log.isDebugEnabled()) {
84                 log.debug("name = " + name);
85                 log.debug("element = " + element);
86             }
87             state.set(name, element);
88         }
89     }
90 }
91 /* ====================================================================
92  * The webappfilter License, Version 1.1
93  *
94  * Copyright (c) 2002 Ivar Chan. All rights
95  * reserved.
96  *
97  * Redistribution and use in source and binary forms, with or without
98  * modification, are permitted provided that the following conditions
99  * are met:
100  *
101  * 1. Redistributions of source code must retain the above copyright
102  * notice, this list of conditions and the following disclaimer.
103  *
104  * 2. Redistributions in binary form must reproduce the above copyright
105  * notice, this list of conditions and the following disclaimer in
106  * the documentation and/or other materials provided with the
107  * distribution.
108  *
109  * 3. The end-user documentation included with the redistribution,
110  * if any, must include the following acknowledgment:
111  * "This product includes software developed by
112  * Ivar Chan (http://www.openinventions.com/webappfilter/)."
113  * Alternately, this acknowledgment may appear in the software itself,
114  * if and wherever such third-party acknowledgments normally appear.
115  *
116  * 4. The name "webappfilter" must not be used to endorse or promote products
117  * derived from this software without prior written permission. For
118  * written permission, please contact ivarchan@acm.org.
119  *
120  * 5. Products derived from this software may not be called "webappfilter",
121  * nor may "webappfilter" appear in their name, without
122  * prior written permission of Ivar Chan.
123  *
124  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
125  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
126  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
127  * DISCLAIMED. IN NO EVENT SHALL THE IVAR CHAN BE LIABLE FOR ANY
128  * DIRECT, INDIRECT, INCIDENTAL,
129  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
130  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
131  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
132  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
133  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
134  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
135  * SUCH DAMAGE.
136  * ====================================================================
137  *
138  * This software consists of voluntary contributions made by many
139  * individuals. For more information on webappfilter, please see
140  * <http://www.openinventions/webappfilter/>.
141  */

142
143
144
145
Popular Tags