1 package org.drools.tags.rule;
2
3 /*
4 $Id: DeclarationTag.java,v 1.6 2003/03/25 19:47:32 tdiesler Exp $
5
6 Copyright 2002 (C) The Werken Company. All Rights Reserved.
7
8 Redistribution and use of this software and associated documentation
9 ("Software"), with or without modification, are permitted provided
10 that the following conditions are met:
11
12 1. Redistributions of source code must retain copyright
13 statements and notices. Redistributions must also contain a
14 copy of this document.
15
16 2. Redistributions in binary form must reproduce the
17 above copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
20
21 3. The name "drools" must not be used to endorse or promote
22 products derived from this Software without prior written
23 permission of The Werken Company. For written permission,
24 please contact bob@werken.com.
25
26 4. Products derived from this Software may not be called "drools"
27 nor may "drools" appear in their names without prior written
28 permission of The Werken Company. "drools" is a registered
29 trademark of The Werken Company.
30
31 5. Due credit should be given to The Werken Company.
32 (http://drools.werken.com/).
33
34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS
35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
45 OF THE POSSIBILITY OF SUCH DAMAGE.
46
47 */
48
49 import org.apache.commons.jelly.JellyTagException;
50 import org.apache.commons.jelly.MissingAttributeException;
51 import org.apache.commons.jelly.XMLOutput;
52 import org.drools.rule.Declaration;
53 import org.drools.rule.Rule;
54 import org.drools.spi.ObjectType;
55
56 /*** Construct a <code>Declaration</code> for a <code>Rule</code>.
57 *
58 * @see Declaration
59 *
60 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
61 *
62 * @version $Id: DeclarationTag.java,v 1.6 2003/03/25 19:47:32 tdiesler Exp $
63 */
64 public class DeclarationTag extends RuleTagSupport implements ObjectTypeReceptor
65 {
66 // ------------------------------------------------------------
67 // Instance members
68 // ------------------------------------------------------------
69
70 /*** The identifier. */
71 private String identifier;
72
73 /*** The semantic type. */
74 private ObjectType objectType;
75
76 /*** The variable. */
77 private String var;
78
79 // ------------------------------------------------------------
80 // Constructors
81 // ------------------------------------------------------------
82
83 /*** Construct.
84 */
85 public DeclarationTag()
86 {
87 this.identifier = null;
88 }
89
90 /*** Set the identifier.
91 *
92 * @param identifier The identifier.
93 */
94 public void setIdentifier(String identifier)
95 {
96 this.identifier = identifier;
97 }
98
99 /*** Retrieve the identifier.
100 *
101 * @return The identifier.
102 */
103 public String getIdentifier()
104 {
105 return this.identifier;
106 }
107
108 /*** Set the <code>ObjectType</code>.
109 *
110 * @param objectType The object type.
111 */
112 public void setObjectType(ObjectType objectType)
113 {
114 this.objectType = objectType;
115 }
116
117 /*** Set the variable in which to store the <code>Declaration</code>.
118 *
119 * @param var The variable name.
120 */
121 public void setVar(String var)
122 {
123 this.var = var;
124 }
125
126 /*** Retrieve the variable in which to store the <code>Declaration</code>.
127 *
128 * @return The variable name.
129 */
130 public String getVar()
131 {
132 return this.var;
133 }
134
135 /*** Verify required attributes.
136 *
137 * @throws MissingAttributeException If a required
138 * attribute is not present.
139 */
140 protected void verifyAttributes() throws MissingAttributeException
141 {
142 requiredAttribute( "identifier",
143 this.identifier );
144 }
145
146 /*** Create the <code>Declaration</code>.
147 *
148 * @param output The output sink.
149 *
150 * @return The configured declaration.
151 *
152 * @throws JellyTagException If an error occurs while attempting to
153 * configure the declaration.
154 */
155 protected Declaration createDeclaration(XMLOutput output) throws JellyTagException
156 {
157 invokeBody( output );
158
159 if ( this.objectType == null )
160 {
161 throw new JellyTagException( "No object type specified" );
162 }
163
164 Declaration decl = new Declaration( this.objectType,
165 getIdentifier() );
166
167 return decl;
168 }
169
170 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
171 // org.drools.tags.rule.ObjectTypeReceptor
172 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
173
174 /*** Receive the <code>ObjectType</code>.
175 *
176 * @param objectType The object type.
177 */
178 public void receiveObjectType(ObjectType objectType)
179 {
180 setObjectType( objectType );
181 }
182
183 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
184 // org.apache.commons.jelly.Tag
185 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
186
187 /*** Perform this tag.
188 *
189 * @param output The output sink.
190 *
191 * @throws JellyTagException If an error occurs while attempting
192 * to perform this tag.
193 */
194 public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException
195 {
196 verifyAttributes();
197
198 Declaration decl = createDeclaration( output );
199
200
201 if ( this.var != null )
202 {
203 getContext().setVariable( this.var,
204 decl );
205 }
206
207 getContext().setVariable( "org.drools.declaration",
208 decl );
209
210 Rule rule = getRule();
211
212 if ( rule != null )
213 {
214 rule.addDeclaration( decl );
215 }
216 }
217 }
This page was automatically generated by Maven