KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > core > CustomAttribute


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.core;
54
55 import freemarker.template.Template;
56
57 /**
58  * A class that allows one to associate custom data with a configuration,
59  * a template, or environment. It works pretty much like {@link ThreadLocal}, a
60  * class that allows one to associate custom data with a thread.
61  * @author Attila Szegedi
62  * @version $Id: CustomAttribute.java,v 1.3 2003/11/10 14:05:22 ddekany Exp $
63  */

64 public class CustomAttribute
65 {
66     /**
67      * Constant used in the constructor specifying that this attribute is
68      * scoped by the environment.
69      */

70     public static final int SCOPE_ENVIRONMENT = 0;
71         
72     /**
73      * Constant used in the constructor specifying that this attribute is
74      * scoped by the template.
75      */

76     public static final int SCOPE_TEMPLATE = 1;
77         
78     /**
79      * Constant used in the constructor specifying that this attribute is
80      * scoped by the configuration.
81      */

82     public static final int SCOPE_CONFIGURATION = 2;
83
84     // We use an internal key instead of 'this' so that malicious subclasses
85
// overriding equals() and hashCode() can't gain access to other attribute
86
// values. That's also the reason why get() and set() are marked final.
87
private final Object JavaDoc key = new Object JavaDoc();
88     private final int scope;
89     
90     /**
91      * Creates a new custom attribute with the specified scope
92      * @param scope one of <tt>SCOPE_</tt> constants.
93      */

94     public CustomAttribute(int scope) {
95         if(scope != SCOPE_ENVIRONMENT &&
96            scope != SCOPE_TEMPLATE &&
97            scope != SCOPE_CONFIGURATION) {
98                 throw new IllegalArgumentException JavaDoc();
99             }
100         this.scope = scope;
101     }
102     
103     /**
104      * This method is invoked when {@link #get()} is invoked without
105      * {@link #set(Object)} being invoked before it to define the value in the
106      * current scope. Override it to create the attribute value on-demand.
107      * @return the initial value for the custom attribute. By default returns null.
108      */

109     protected Object JavaDoc create() {
110         return null;
111     }
112     
113     /**
114      * @return the value of the attribute in the context of the current environment.
115      * @throws IllegalStateException if there is no current environment (and
116      * hence also no current template and configuration), therefore the
117      * attribute's current scope object can't be resolved.
118      */

119     public final Object JavaDoc get() {
120         return getScopeConfigurable().getCustomAttribute(key, this);
121     }
122     
123     /**
124      * @return the value of a template-scope attribute in the context of a
125      * given template.
126      * @throws UnsupportedOperationException if this custom attribute is not a
127      * template-scope attribute
128      * @throws NullPointerException if t is null
129      */

130     public final Object JavaDoc get(Template t) {
131         if(scope != SCOPE_TEMPLATE) {
132             throw new UnsupportedOperationException JavaDoc("This is not a template-scope attribute");
133         }
134         return ((Configurable)t).getCustomAttribute(key, this);
135     }
136     
137     /**
138      * Sets the value of the attribute in the context of the current environment.
139      * @param value the new value of the attribute
140      * @throws IllegalStateException if there is no current environment (and
141      * hence also no current template and configuration), therefore the
142      * attribute's current scope object can't be resolved.
143      */

144     public final void set(Object JavaDoc value) {
145         getScopeConfigurable().setCustomAttribute(key, value);
146     }
147
148     /**
149      * Sets the value of a template-scope attribute in the context of the given
150      * template.
151      * @param value the new value of the attribute
152      * @param t the template
153      * @throws UnsupportedOperationException if this custom attribute is not a
154      * template-scope attribute
155      * @throws NullPointerException if t is null
156      */

157     public final void set(Object JavaDoc value, Template t) {
158         if(scope != SCOPE_TEMPLATE) {
159             throw new UnsupportedOperationException JavaDoc("This is not a template-scope attribute");
160         }
161         ((Configurable)t).setCustomAttribute(key, value);
162     }
163
164     private Configurable getScopeConfigurable() {
165         Configurable c = Environment.getCurrentEnvironment();
166         if(c == null) {
167             throw new IllegalStateException JavaDoc("No current environment");
168         }
169         switch(scope) {
170             case SCOPE_ENVIRONMENT: {
171                 return c;
172             }
173             case SCOPE_TEMPLATE: {
174                 return c.getParent();
175             }
176             case SCOPE_CONFIGURATION: {
177                 return c.getParent().getParent();
178             }
179             default: {
180                 throw new Error JavaDoc();
181             }
182         }
183     }
184 }
185
Popular Tags