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 { 12 if (log.isDebugEnabled()) { 13 log.debug("context.getValue() = " + context.getValue()); 14 } 15 16 String [] lines = context.getValue().split(";"); 17 for (int i = 0; i < lines.length; i++) { 18 String 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 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 right = line.substring(equalIndex + 1, line.length()); 36 if (log.isDebugEnabled()) { 37 log.debug("right = " + right); 38 } 39 Object o = new XPathExpressionParser(new StringReader(right)).parse(state); 40 41 stateSet(state, left, o); 42 } 43 } 44 45 private void stateSet(State state, String left, Object o) throws Exception { 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 name = ""; 54 String 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 ) { 76 element.setValue(path, (String ) 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 142 143 144 145 | Popular Tags |