KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > config > Choose


1 package org.sapia.soto.config;
2
3 import org.dom4j.io.SAXReader;
4
5 import org.sapia.util.text.TemplateContextIF;
6 import org.sapia.util.xml.ProcessingException;
7 import org.sapia.util.xml.confix.ConfigurationException;
8 import org.sapia.util.xml.confix.XMLConsumer;
9 import org.sapia.util.xml.confix.Dom4jProcessor;
10 import org.sapia.util.xml.confix.ObjectCreationCallback;
11 import org.sapia.util.xml.confix.ObjectFactoryIF;
12 import org.xml.sax.InputSource JavaDoc;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17
18 /**
19  * @author Yanick Duchesne
20  *
21  * <dl>
22  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
23  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
24  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
25  * </dl>
26  */

27 public class Choose implements ObjectCreationCallback {
28   private List JavaDoc _whens = new ArrayList JavaDoc();
29   private Otherwise _otherwise;
30   private TemplateContextIF _ctx;
31   private ObjectFactoryIF _fac;
32
33   public Choose(TemplateContextIF ctx, ObjectFactoryIF fac) {
34     _ctx = ctx;
35     _fac = fac;
36   }
37
38   public When createWhen() {
39     When whenObj = new When(_ctx, _fac);
40     _whens.add(whenObj);
41
42     return whenObj;
43   }
44
45   public Otherwise createOtherwise() {
46     if (_otherwise != null) {
47       throw new IllegalArgumentException JavaDoc(
48         "'otherwise' element already specified");
49     }
50
51     return _otherwise = new Otherwise(_ctx, _fac);
52   }
53
54   public Object JavaDoc onCreate() throws ConfigurationException {
55     When current;
56
57     for (int i = 0; i < _whens.size(); i++) {
58       current = (When) _whens.get(i);
59
60       if (current.isEqual()) {
61         return current.create();
62       }
63     }
64
65     if (_otherwise != null) {
66       return _otherwise.create();
67     }
68
69     return new NullObjectImpl();
70   }
71
72   public static class When extends Condition {
73     When(TemplateContextIF ctx, ObjectFactoryIF fac) {
74       super("when", ctx, fac);
75     }
76   }
77
78   public static final class Otherwise implements XMLConsumer{
79     private TemplateContextIF _ctx;
80     private ObjectFactoryIF _fac;
81     private org.dom4j.Element _elem;
82
83     Otherwise(TemplateContextIF ctx, ObjectFactoryIF fac) {
84       _ctx = ctx;
85       _fac = fac;
86     }
87
88     public Object JavaDoc create() throws ConfigurationException {
89       if (_elem == null) {
90         return new NullObjectImpl();
91       }
92
93       Dom4jProcessor proc = new Dom4jProcessor(_fac);
94
95       try {
96         return proc.process(null, _elem);
97       } catch (ProcessingException e) {
98         throw new ConfigurationException("Could not process xml nested in 'if' element",
99           e);
100       }
101     }
102
103         /**
104          * @see org.sapia.util.xml.confix.XMLConsumer#consume(org.xml.sax.InputSource)
105          */

106         public void consume(InputSource JavaDoc is) throws Exception JavaDoc {
107             if (_elem != null) {
108                 throw new ConfigurationException("'choose' only takes a SINGLE nested xml element");
109             }
110             SAXReader reader = new SAXReader();
111             _elem = reader.read(is).getRootElement();
112         }
113   }
114 }
115
116
Popular Tags