KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > rules > groovy > GroovyRule


1 package org.sapia.validator.rules.groovy;
2
3 import groovy.lang.GroovyClassLoader;
4
5 import java.io.ByteArrayInputStream JavaDoc;
6 import java.util.Random JavaDoc;
7
8 import org.sapia.util.xml.confix.ConfigurationException;
9 import org.sapia.validator.Rule;
10 import org.sapia.validator.ValidationContext;
11
12 /**
13  * This class implements validation through a Groovy script. The script's source is provided
14  * as CDATA in the XML element to which an instance of this class corresponds.
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 GroovyRule extends Rule{
25   
26   private static final Random JavaDoc RAND = new Random JavaDoc();
27   
28   private String JavaDoc _name;
29   private String JavaDoc _src;
30   private Rule _inner;
31   private StringBuffer JavaDoc _imports = new StringBuffer JavaDoc();
32   
33   public void addImport(String JavaDoc imp) {
34     _imports.append("import ").append(imp).append(';');
35   }
36
37   public void setImport(String JavaDoc imp) {
38     addImport(imp);
39   }
40
41   /**
42    * @see org.sapia.util.xml.confix.ObjectCreationCallback#onCreate()
43    */

44   public Object JavaDoc onCreate() throws ConfigurationException {
45     super.onCreate();
46     try {
47       Class JavaDoc clazz = generate();
48       _inner = (Rule) clazz.newInstance();
49       _inner.setId(getId());
50       _inner.setErrorMessages(getErrorMessages());
51       return this;
52     } catch (RuntimeException JavaDoc e) {
53       throw e;
54     } catch (Exception JavaDoc e) {
55       throw new ConfigurationException("Could not generate Groovy code for: " +
56         _src, e);
57     }
58   }
59
60   public void setText(String JavaDoc src) throws Exception JavaDoc {
61     _src = src;
62   }
63
64   
65   /**
66    * @see org.sapia.validator.Rule#validate(org.sapia.validator.ValidationContext)
67    */

68   public void validate(ValidationContext context) {
69     if (_inner == null) {
70       throw new IllegalStateException JavaDoc("Source code not defined");
71     }
72
73     _inner.validate(context);
74   }
75
76   private synchronized Class JavaDoc generate() throws Exception JavaDoc {
77     if (_src == null) {
78       throw new IllegalStateException JavaDoc("Groovy source not specified");
79     }
80     
81     String JavaDoc src = _src;
82
83     long id = RAND.nextLong();
84     if(id < 0) id = -id;
85     src = _imports.toString() + " class Vlad_Groovy_Rule_Java_" +
86       id + " extends " + Rule.class.getName() + " {" +
87       " public void validate(" + ValidationContext.class.getName() + " context){" + _src + " }" +
88       /*" public String getId(){ return " + (getId() != null ? getId() : "\"groovyRule\"") + "; }" +*/
89       "}";
90     
91     ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(src.getBytes());
92     GroovyClassLoader loader = new GroovyClassLoader(Thread.currentThread()
93                                                               .getContextClassLoader());
94
95     return loader.parseClass(bis, getClass().getName());
96   }
97
98
99 }
100
101
102
Popular Tags