1 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 31 public class GetAttributeTag extends BodyTagSupport { 32 33 private String attributeRef; 34 private Object attributeObject; 35 private int scope = PageContext.PAGE_SCOPE; 36 private String attribute; 37 private int multivalueMode = ONE_VALUE;; 38 private NamingEnumeration nameEnum; 39 private Attribute theAttribute; 40 41 42 public static final int ONE_VALUE = 1; 43 44 public static final int SEPARATE_VALUES = 2; 45 46 public static final int ITERATE_VALUES = 3; 47 48 49 public GetAttributeTag() { 50 } 51 52 56 public void setScope(String scope) { 57 this.scope = decodeScope(scope); 58 } 59 60 64 public Object getObject() { 65 return attributeObject; 66 } 67 68 72 public void setObject(DirContext object) { 73 attributeObject = object; 74 } 75 76 80 public void setRef(String ref) { 81 attributeRef = ref; 82 } 83 84 88 public String getAttribute() { 89 return attribute; 90 } 91 92 96 public void setAttribute(String attribute) { 97 this.attribute = attribute; 98 } 99 100 104 public String getMultivalue() { 105 return decodeMultivalue(multivalueMode); 106 } 107 108 112 public void setMultivalue(String multivalue) { 113 multivalueMode = decodeMultivalue(multivalue); 114 } 115 116 public int doStartTag() throws JspException { 117 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 [] {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 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 } catch (IOException ioe) { 169 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 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 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 nameEnum = null; 256 } 257 } 258 return EVAL_PAGE; 259 } 260 261 public static String 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 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 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 |