KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > jndi > GetAttributeTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.jndi;
18
19 import javax.servlet.jsp.*;
20 import javax.servlet.jsp.tagext.*;
21 import javax.naming.*;
22 import javax.naming.directory.*;
23
24 import java.io.*;
25
26 /**
27  *
28  * @author Danno Ferrin <shemnon@earthlink.net>
29  * @version $Revision: 1.4 $
30  */

31 public class GetAttributeTag extends BodyTagSupport {
32
33     private String JavaDoc attributeRef;
34     private Object JavaDoc attributeObject;
35     private int scope = PageContext.PAGE_SCOPE;
36     private String JavaDoc attribute;
37     private int multivalueMode = ONE_VALUE;;
38     private NamingEnumeration nameEnum;
39     private Attribute theAttribute;
40     
41     /** Only take one value (first value if ordered) */
42     public static final int ONE_VALUE = 1;
43     /** Body provides a separator for the values */
44     public static final int SEPARATE_VALUES = 2;
45     /** body should be iterated for each value */
46     public static final int ITERATE_VALUES = 3;
47
48     /** Creates new GetAttributeTag */
49     public GetAttributeTag() {
50     }
51
52     /**
53      * Setter for property scope.
54      * @param Scope New value of property scope.
55      */

56     public void setScope(String JavaDoc scope) {
57         this.scope = decodeScope(scope);
58     }
59
60     /**
61      * Getter for property object.
62      * @return Value of property object.
63      */

64     public Object JavaDoc getObject() {
65         return attributeObject;
66     }
67
68     /**
69      * Setter for property object.
70      * @param object New value of property object.
71      */

72     public void setObject(DirContext object) {
73         attributeObject = object;
74     }
75
76     /**
77      * Setter for property ref.
78      * @param ref New value of property ref.
79      */

80     public void setRef(String JavaDoc ref) {
81         attributeRef = ref;
82     }
83
84     /**
85      * Getter for property attribute.
86      * @return Value of property attribute.
87      */

88     public String JavaDoc getAttribute() {
89         return attribute;
90     }
91
92     /**
93      * Setter for property attribute.
94      * @param attribute New value of property attribute.
95      */

96     public void setAttribute(String JavaDoc attribute) {
97         this.attribute = attribute;
98     }
99
100     /**
101      * Getter for property multivalue
102      * @return Value of property multivalue.
103      */

104     public String JavaDoc getMultivalue() {
105         return decodeMultivalue(multivalueMode);
106     }
107
108     /**
109      * Setter for property multivalue.
110      * @param multivalue New value of property multivalue.
111      */

112     public void setMultivalue(String JavaDoc multivalue) {
113         multivalueMode = decodeMultivalue(multivalue);
114     }
115
116     public int doStartTag() throws JspException {
117         // Initialize invocation variables
118
nameEnum = null;
119         theAttribute = null;
120
121         if( attributeRef != null ) {
122             attributeObject = pageContext.findAttribute(attributeRef);
123         }
124
125         try {
126             if ((attributeObject instanceof DirContext)
127              && (attributeObject != null)) {
128                 attributeObject = ((DirContext)attributeObject).getAttributes("",
129                         new String JavaDoc[] {attribute});
130             }
131
132             if ((attributeObject instanceof SearchResult)
133              && (attributeObject != null)) {
134                 attributeObject = ((SearchResult)attributeObject)
135                         .getAttributes();
136             }
137
138             if ((attributeObject instanceof Attributes)
139              && (attributeObject != null)) {
140                 attributeObject = ((Attributes)attributeObject).get(attribute);
141             }
142
143             if (attributeObject instanceof Attribute) {
144                 // don't need to worry about nulls,
145
// it will behave properly since nulls can be cast
146
// to anything
147
theAttribute = (Attribute)attributeObject;
148             }
149         } catch (NamingException ne) {
150         }
151         
152         if (theAttribute == null) {
153             if (getId() != null) {
154                 pageContext.removeAttribute(getId(), scope);
155             }
156             return SKIP_BODY;
157         } else {
158             if (multivalueMode == ONE_VALUE) {
159                 try {
160                     pageContext.getOut().print(theAttribute.get());
161                     if (getId() != null) {
162                         pageContext.setAttribute(getId(),
163                                 theAttribute.get(), scope);
164                     }
165                 } catch (NamingException ne) {
166                     // if it throws this, we just fail to export it
167
// hence we do nothing
168
} catch (IOException ioe) {
169                     // however ioexceprtion is worse!
170
throw new JspException(ioe.toString());
171                 }
172                 return SKIP_BODY;
173             } else {
174                 try {
175                     nameEnum = theAttribute.getAll();
176                     if (nameEnum.hasMoreElements()) {
177                         if (multivalueMode == SEPARATE_VALUES) {
178                             // recycle attributeObject field
179
attributeObject = nameEnum.nextElement();
180                             if (nameEnum.hasMoreElements()) {
181                                 return EVAL_BODY_TAG;
182                             } else {
183                                 try {
184                                     pageContext.getOut().print(attributeObject);
185                                 } catch (IOException ioe) {
186                                     throw new JspException(ioe.toString());
187                                 }
188                                 return SKIP_BODY;
189                             }
190                         } else {
191                             pageContext.setAttribute(getId(),
192                                     nameEnum.nextElement(), scope);
193                             return EVAL_BODY_TAG;
194                         }
195                     } else {
196                         return SKIP_BODY;
197                     }
198                 } catch (NamingException ne) {
199                     // if there is no forAll, skip the body
200
return SKIP_BODY;
201                 }
202             }
203         }
204     }
205
206     public void doInitBody() throws JspException {
207         try {
208             if (multivalueMode == SEPARATE_VALUES) {
209                 bodyContent.print(attributeObject);
210             }
211         } catch (IOException ioe) {
212             throw new JspException(ioe.toString());
213         }
214     }
215         
216
217     public int doAfterBody() throws JspException {
218         if (multivalueMode == SEPARATE_VALUES) {
219             try {
220                 bodyContent.print(nameEnum.nextElement());
221             } catch (IOException ioe) {
222                 throw new JspException(ioe.toString());
223             }
224             if (nameEnum.hasMoreElements()) {
225                 return EVAL_BODY_TAG;
226             } else {
227                 return SKIP_BODY;
228             }
229         } else {
230             if (nameEnum.hasMoreElements()) {
231                 pageContext.setAttribute(getId(),
232                         nameEnum.nextElement(), scope);
233                 return EVAL_BODY_TAG;
234             } else {
235                 return SKIP_BODY;
236             }
237         }
238             
239     }
240     
241     public int doEndTag() throws JspException {
242         try {
243             if (bodyContent != null) {
244                 bodyContent.writeOut(pageContext.getOut());
245             }
246         } catch (IOException ioe) {
247             throw new JspException(ioe.toString());
248         }
249         if (nameEnum != null) {
250             try {
251                 nameEnum.close();
252             } catch (NamingException ne) {
253                 // we are trying to free the resource.
254
// if it throws fits, hope it gets GCed
255
nameEnum = null;
256             }
257         }
258         return EVAL_PAGE;
259     }
260     
261     public static String JavaDoc decodeMultivalue(int multivalue) {
262         switch (multivalue) {
263             case ONE_VALUE:
264                 return "one";
265             case SEPARATE_VALUES:
266                 return "separator";
267             case ITERATE_VALUES:
268                 return "iterate";
269             default:
270                 return null;
271         }
272     }
273     
274     public static int decodeMultivalue(String JavaDoc multivalue) {
275         if (multivalue.equalsIgnoreCase("one")) {
276             return ONE_VALUE;
277         } else if (multivalue.equalsIgnoreCase("separator")) {
278             return SEPARATE_VALUES;
279         } else if (multivalue.equalsIgnoreCase("iterate")) {
280             return ITERATE_VALUES;
281         } else {
282             return -1;
283         }
284     }
285
286     public static int decodeScope(String JavaDoc scope) {
287         if (scope.equalsIgnoreCase("request")) {
288             return PageContext.REQUEST_SCOPE;
289         } else if (scope.equalsIgnoreCase("session")) {
290             return PageContext.SESSION_SCOPE;
291         } else if (scope.equalsIgnoreCase("application")) {
292             return PageContext.APPLICATION_SCOPE;
293         } else {
294             return PageContext.PAGE_SCOPE;
295         }
296     }
297 }
298
Popular Tags