1 package org.sapia.regis.loader; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import org.dom4j.io.SAXReader; 7 import org.sapia.util.text.TemplateContextIF; 8 import org.sapia.util.xml.ProcessingException; 9 import org.sapia.util.xml.confix.ConfigurationException; 10 import org.sapia.util.xml.confix.Dom4jProcessor; 11 import org.sapia.util.xml.confix.ObjectCreationCallback; 12 import org.sapia.util.xml.confix.ObjectFactoryIF; 13 import org.sapia.util.xml.confix.XMLConsumer; 14 import org.xml.sax.InputSource ; 15 16 17 public class ChooseTag implements ObjectCreationCallback, TagFactory{ 18 private List _whens = new ArrayList (); 19 private Otherwise _otherwise; 20 private TemplateContextIF _ctx; 21 private ObjectFactoryIF _fac; 22 23 public ChooseTag() { 24 } 25 26 public When createWhen() { 27 When whenObj = new When(_ctx, _fac); 28 _whens.add(whenObj); 29 30 return whenObj; 31 } 32 33 public Otherwise createOtherwise() { 34 if(_otherwise != null) { 35 throw new IllegalArgumentException ( 36 "'otherwise' element already specified"); 37 } 38 39 return _otherwise = new Otherwise(_fac); 40 } 41 42 public Object onCreate() throws ConfigurationException { 43 When current; 44 45 for(int i = 0; i < _whens.size(); i++) { 46 current = (When) _whens.get(i); 47 48 if(current.isEqual()) { 49 return current.create(); 50 } 51 } 52 53 if(_otherwise != null) { 54 return _otherwise.create(); 55 } 56 57 return new NullObjectImpl(); 58 } 59 60 public static class When extends Condition { 61 When(TemplateContextIF ctx, ObjectFactoryIF fac) { 62 super("when", ctx, fac); 63 } 64 } 65 66 public static final class Otherwise implements XMLConsumer { 67 private ObjectFactoryIF _fac; 68 private org.dom4j.Element _elem; 69 70 Otherwise(ObjectFactoryIF fac) { 71 _fac = fac; 72 } 73 74 public Object create() throws ConfigurationException { 75 if(_elem == null) { 76 return new NullObjectImpl(); 77 } 78 79 Dom4jProcessor proc = new Dom4jProcessor(_fac); 80 81 try { 82 return proc.process(null, _elem); 83 } catch(ProcessingException e) { 84 throw new ConfigurationException( 85 "Could not process xml nested in 'if' element", e); 86 } 87 } 88 89 92 public void consume(InputSource is) throws Exception { 93 if(_elem != null) { 94 throw new ConfigurationException( 95 "'choose' only takes a SINGLE nested xml element"); 96 } 97 SAXReader reader = new SAXReader(); 98 _elem = reader.read(is).getRootElement(); 99 } 100 } 101 102 public Object create(TemplateContextIF context, ObjectFactoryIF fac) throws Exception { 103 _ctx = context; 104 _fac = fac; 105 return this; 106 } 107 } 108 | Popular Tags |