KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > simya > ldap > taglib > GetAttribute


1 package net.simya.ldap.taglib;
2
3 import javax.servlet.jsp.tagext.*;
4 import javax.servlet.jsp.*;
5 import javax.servlet.http.*;
6 import javax.naming.NamingException JavaDoc;
7 import javax.naming.directory.Attribute JavaDoc;
8 import net.simya.ldap.beans.*;
9 import net.simya.ldap.servlet.*;
10
11 public class GetAttribute
12        extends TagSupport
13 {
14     private static final String JavaDoc DEFAULT_DELIMITER = ", ";
15     private static final String JavaDoc DEFAULT_MIMETYPE = "text/plain";
16     private static final String JavaDoc DEFAULT_MIMETEXT = "text";
17     private static final int DEFAULT_INDEX = -1;
18     private static final String JavaDoc DEFAULT_VALUE = "";
19
20     private String JavaDoc name = null;
21     private String JavaDoc delimiter = DEFAULT_DELIMITER;
22     private String JavaDoc mimeType = DEFAULT_MIMETYPE;
23     private int index = DEFAULT_INDEX;
24     private String JavaDoc def = DEFAULT_VALUE;
25
26     public int doStartTag()
27     throws JspException
28     {
29         Query queryTag = null;
30         try
31         {
32             queryTag = (Query) getParent();
33         }
34         catch ( Exception JavaDoc ex )
35         {
36             throw new JspTagException( "<getAttribute> tag should be child node of <query> tag." );
37         }
38
39         JspWriter out = pageContext.getOut();
40         try
41         {
42             Attribute JavaDoc attr = queryTag.getAttribute(name);
43             if ( attr == null )
44             {
45                 out.print( def );
46                 return EVAL_BODY_INCLUDE;
47             }
48             if ( index != DEFAULT_INDEX )
49             {
50                 output( out, attr.get( index ) );
51                 return EVAL_BODY_INCLUDE;
52             }
53             output( out, attr.get() );
54             for ( int i=1; i<attr.size(); i++ )
55             {
56                 out.print( delimiter );
57                 output( out, attr.get(i) );
58             }
59         } catch ( java.io.IOException JavaDoc ex )
60         {
61             throw new JspTagException( "IO Error" );
62         } catch (NamingException JavaDoc ex)
63         {
64             throw new LdapTaglibException( ex );
65         }
66         return EVAL_BODY_INCLUDE;
67     }
68
69     public void release()
70     {
71         name = null;
72         delimiter = DEFAULT_DELIMITER;
73         index = DEFAULT_INDEX;
74         mimeType = DEFAULT_MIMETYPE;
75     }
76
77     private void output( JspWriter out, Object JavaDoc attr )
78     throws java.io.IOException JavaDoc
79     {
80         if ( mimeType.startsWith( DEFAULT_MIMETEXT ) )
81             out.print( attr );
82         else
83         {
84             String JavaDoc id = LdapGlobalCache.addCache( attr, mimeType );
85             out.print( ((HttpServletRequest)pageContext.getRequest()).getContextPath() );
86             out.print( '/' );
87             out.print( "LdapGlobalCache?id=" );
88             out.print( id );
89         }
90     }
91
92     public String JavaDoc getname() { return this.name; }
93     public void setName(String JavaDoc name) { this.name = name; }
94
95     public String JavaDoc getDelimiter() { return this.delimiter; }
96     public void setDelimiter(String JavaDoc delimiter) { this.delimiter = delimiter; }
97
98     public String JavaDoc getIndex() {
99         return new Integer JavaDoc(this.index).toString();
100     }
101     public void setIndex(String JavaDoc index){
102         this.index = new Integer JavaDoc(index).intValue();
103     }
104
105     public String JavaDoc getMimeType() { return this.mimeType; }
106     public void setMimeType(String JavaDoc mimeType) { this.mimeType = mimeType; }
107
108     public String JavaDoc getDefault() { return this.def; }
109     public void setDefault(String JavaDoc def) { this.def = def; }
110 }
Popular Tags