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 ; 7 import javax.naming.directory.Attribute ; 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 DEFAULT_DELIMITER = ", "; 15 private static final String DEFAULT_MIMETYPE = "text/plain"; 16 private static final String DEFAULT_MIMETEXT = "text"; 17 private static final int DEFAULT_INDEX = -1; 18 private static final String DEFAULT_VALUE = ""; 19 20 private String name = null; 21 private String delimiter = DEFAULT_DELIMITER; 22 private String mimeType = DEFAULT_MIMETYPE; 23 private int index = DEFAULT_INDEX; 24 private String 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 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 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 ex ) 60 { 61 throw new JspTagException( "IO Error" ); 62 } catch (NamingException 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 attr ) 78 throws java.io.IOException 79 { 80 if ( mimeType.startsWith( DEFAULT_MIMETEXT ) ) 81 out.print( attr ); 82 else 83 { 84 String 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 getname() { return this.name; } 93 public void setName(String name) { this.name = name; } 94 95 public String getDelimiter() { return this.delimiter; } 96 public void setDelimiter(String delimiter) { this.delimiter = delimiter; } 97 98 public String getIndex() { 99 return new Integer (this.index).toString(); 100 } 101 public void setIndex(String index){ 102 this.index = new Integer (index).intValue(); 103 } 104 105 public String getMimeType() { return this.mimeType; } 106 public void setMimeType(String mimeType) { this.mimeType = mimeType; } 107 108 public String getDefault() { return this.def; } 109 public void setDefault(String def) { this.def = def; } 110 } | Popular Tags |