KickJava   Java API By Example, From Geeks To Geeks.

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


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.3 $
30  */

31 public class ForEachAttributeTag extends BodyTagSupport {
32
33     private String JavaDoc attributeRef;
34     private Object JavaDoc attributeObject;
35     private NamingEnumeration nameEnum;
36     private Attribute currentAttribute;
37     private int scope = PageContext.PAGE_SCOPE;
38
39     /** Creates new ForEachAttributeTag */
40     public ForEachAttributeTag() {
41     }
42
43     /**
44      * Setter for property scope.
45      * @param Scope New value of property scope.
46      */

47     public void setScope(String JavaDoc scope) {
48         this.scope = decodeScope(scope);
49     }
50
51     /**
52      * Getter for property object.
53      * @return Value of property object.
54      */

55     public Object JavaDoc getObject() {
56         return attributeObject;
57     }
58
59     /**
60      * Setter for property object.
61      * @param object New value of property object.
62      */

63     public void setObject(DirContext object) {
64         attributeObject = object;
65     }
66
67     /**
68      * Setter for property ref.
69      * @param ref New value of property ref.
70      */

71     public void setRef(String JavaDoc ref) {
72         this.attributeRef = ref;
73     }
74     
75     public int doStartTag() throws JspException {
76         // Initialize invocation variables
77
nameEnum = null;
78
79         if( attributeRef != null ) {
80             attributeObject = pageContext.findAttribute(attributeRef);
81         }
82         try {
83             if ((attributeObject instanceof DirContext)
84              && (attributeObject != null)) {
85                 attributeObject = ((DirContext)attributeObject).getAttributes("");
86             }
87
88             if ((attributeObject instanceof SearchResult)
89              && (attributeObject != null)) {
90                 attributeObject = ((SearchResult)attributeObject)
91                         .getAttributes();
92             }
93
94             if ((attributeObject instanceof Attributes)
95              && (attributeObject != null)) {
96                 nameEnum = ((Attributes)attributeObject).getAll();
97             }
98         } catch (NamingException ne) {
99         }
100         
101         if ((nameEnum != null) && nameEnum.hasMoreElements()) {
102             return EVAL_BODY_TAG;
103         } else {
104             return SKIP_BODY;
105         }
106     }
107     
108     public void doInitBody() {
109         currentAttribute = (Attribute) nameEnum.nextElement();
110         pageContext.setAttribute(getId(), currentAttribute, scope);
111     }
112
113     public int doAfterBody() {
114         if (!nameEnum.hasMoreElements()) {
115             return SKIP_BODY;
116         } else {
117             currentAttribute = (Attribute) nameEnum.nextElement();
118             pageContext.setAttribute(getId(), currentAttribute, scope);
119             return EVAL_BODY_TAG;
120         }
121     }
122     
123     public int doEndTag() throws JspException {
124         try {
125             if (bodyContent != null) {
126                 bodyContent.writeOut(pageContext.getOut());
127             }
128         } catch (IOException ioe) {
129             throw new JspException(ioe.toString());
130         }
131         return EVAL_PAGE;
132     }
133     
134     public static int decodeScope(String JavaDoc scope) {
135         if (scope.equalsIgnoreCase("request")) {
136             return PageContext.REQUEST_SCOPE;
137         } else if (scope.equalsIgnoreCase("session")) {
138             return PageContext.SESSION_SCOPE;
139         } else if (scope.equalsIgnoreCase("application")) {
140             return PageContext.APPLICATION_SCOPE;
141         } else {
142             return PageContext.PAGE_SCOPE;
143         }
144     }
145 }
146
Popular Tags