1 package org.sapia.regis.loader; 2 3 import java.util.Collections ; 4 import java.util.HashMap ; 5 6 import org.dom4j.Element; 7 import org.dom4j.io.SAXReader; 8 import org.sapia.util.text.TemplateContextIF; 9 import org.sapia.util.xml.ProcessingException; 10 import org.sapia.util.xml.confix.ConfigurationException; 11 import org.sapia.util.xml.confix.Dom4jProcessor; 12 import org.sapia.util.xml.confix.ObjectFactoryIF; 13 import org.sapia.util.xml.confix.XMLConsumer; 14 import org.xml.sax.InputSource ; 15 16 public class Condition implements XMLConsumer { 17 private String _name; 18 private String _elemName; 19 private String [] _equals; 20 private int[][] _matches; 21 private TemplateContextIF _ctx; 22 private ObjectFactoryIF _fac; 23 private Element _elem; 24 25 public Condition(String elemName) { 26 _elemName = elemName; 27 } 28 public Condition(String elemName, TemplateContextIF ctx, ObjectFactoryIF fac) { 29 _elemName = elemName; 30 init(ctx, fac); 31 } 32 33 public void setParam(String name) { 34 _name = name; 35 } 36 37 public void setEquals(String eq) { 38 if(eq != null){ 39 _equals = eq.split(","); 40 for(int i = 0; i < _equals.length; i++){ 41 _equals[i] = _equals[i].trim(); 42 } 43 } 44 } 45 46 public void setMatches(String pattern) { 47 if(pattern != null){ 48 String [] patterns = pattern.split(","); 49 _matches = new int[patterns.length][]; 50 for(int i = 0; i < _matches.length; i++){ 51 _matches[i] = UriPatternHelper.compilePattern(patterns[i].trim()); 52 } 53 } 54 } 55 56 public void setValue(String eq) { 57 setEquals(eq); 58 } 59 60 public boolean isEqual() { 61 62 Object val = _ctx.getValue(_name); 63 64 if(val == null) { 65 return false; 66 } 67 68 if(_equals == null && _matches == null) { 69 return true; 70 } 71 72 if(_equals != null){ 73 val = val.toString(); 74 for(int i = 0; i < _equals.length; i++){ 75 if(val.equals(_equals[i])){ 76 return true; 77 } 78 } 79 return false; 80 } 81 else{ 82 val = val.toString(); 83 for(int i = 0; i < _matches.length; i++){ 84 if(UriPatternHelper.match(new HashMap (), (String )val, _matches[i])){ 85 return true; 86 } 87 } 88 return false; 89 } 90 91 } 92 93 public Object create() throws ConfigurationException { 94 if((_elem == null) || (_name == null)) { 95 return new NullObjectImpl(); 96 } 97 98 if(isEqual()) { 99 Dom4jProcessor proc = new Dom4jProcessor(_fac); 100 101 try { 102 return proc.process(null, _elem); 103 } catch(ProcessingException e) { 104 throw new ConfigurationException( 105 "Could not process xml nested in 'if' element", e); 106 } 107 } 108 109 return new NullObjectImpl(); 110 } 111 112 115 public void consume(InputSource is) throws Exception { 116 if(_elem != null) { 117 throw new ConfigurationException("'" + _elemName 118 + "' only takes a SINGLE nested xml element"); 119 } 120 SAXReader reader = new SAXReader(); 121 _elem = reader.read(is).getRootElement(); 122 } 123 124 protected void init(TemplateContextIF ctx, ObjectFactoryIF fac){ 125 _ctx = ctx; 126 _fac = fac; 127 } 128 129 130 } 131 | Popular Tags |