1 package org.sapia.soto.config; 2 3 import org.dom4j.Element; 4 import org.dom4j.io.SAXReader; 5 6 import org.sapia.util.text.TemplateContextIF; 7 import org.sapia.util.xml.ProcessingException; 8 import org.sapia.util.xml.confix.ConfigurationException; 9 import org.sapia.util.xml.confix.Dom4jProcessor; 10 import org.sapia.util.xml.confix.ObjectFactoryIF; 11 import org.sapia.util.xml.confix.XMLConsumer; 12 import org.xml.sax.InputSource ; 13 14 15 24 public class Condition implements XMLConsumer { 25 private String _name; 26 private String _elemName; 27 private String _equals; 28 private TemplateContextIF _ctx; 29 private ObjectFactoryIF _fac; 30 private Element _elem; 31 32 public Condition(String elemName, TemplateContextIF ctx, ObjectFactoryIF fac) { 33 _ctx = ctx; 34 _fac = fac; 35 _elemName = elemName; 36 } 37 38 public void setParam(String name) { 39 _name = name; 40 } 41 42 public void setEquals(String eq) { 43 _equals = eq; 44 } 45 46 public boolean isEqual() { 47 Object val = _ctx.getValue(_name); 48 49 if (val == null) { 50 return false; 51 } 52 53 if (_equals == null) { 54 return true; 55 } 56 57 return val.toString().equals(_equals); 58 } 59 60 public Object create() throws ConfigurationException { 61 if ((_elem == null) || (_name == null)) { 62 return new NullObjectImpl(); 63 } 64 65 if (isEqual()) { 66 Dom4jProcessor proc = new Dom4jProcessor(_fac); 67 68 try { 69 return proc.process(null, _elem); 70 } catch (ProcessingException e) { 71 throw new ConfigurationException("Could not process xml nested in 'if' element", 72 e); 73 } 74 } 75 76 return new NullObjectImpl(); 77 } 78 79 82 public void consume(InputSource is) throws Exception { 83 if (_elem != null) { 84 throw new ConfigurationException("'" + _elemName + 85 "' only takes a SINGLE nested xml element"); 86 } 87 SAXReader reader = new SAXReader(); 88 _elem = reader.read(is).getRootElement(); 89 } 90 } 91 | Popular Tags |