KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
13
14
15 /**
16  * @author Yanick Duchesne
17  *
18  * <dl>
19  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
20  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
21  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
22  * </dl>
23  */

24 public class Condition implements XMLConsumer {
25   private String JavaDoc _name;
26   private String JavaDoc _elemName;
27   private String JavaDoc _equals;
28   private TemplateContextIF _ctx;
29   private ObjectFactoryIF _fac;
30   private Element _elem;
31
32   public Condition(String JavaDoc elemName, TemplateContextIF ctx, ObjectFactoryIF fac) {
33     _ctx = ctx;
34     _fac = fac;
35     _elemName = elemName;
36   }
37
38   public void setParam(String JavaDoc name) {
39     _name = name;
40   }
41
42   public void setEquals(String JavaDoc eq) {
43     _equals = eq;
44   }
45
46   public boolean isEqual() {
47     Object JavaDoc 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 JavaDoc 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   /**
80    * @see org.sapia.util.xml.confix.XMLConsumer#consume(org.xml.sax.InputSource)
81    */

82   public void consume(InputSource JavaDoc is) throws Exception JavaDoc {
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