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 ForEachAttributeTag extends BodyTagSupport { 32 33 private String attributeRef; 34 private Object attributeObject; 35 private NamingEnumeration nameEnum; 36 private Attribute currentAttribute; 37 private int scope = PageContext.PAGE_SCOPE; 38 39 40 public ForEachAttributeTag() { 41 } 42 43 47 public void setScope(String scope) { 48 this.scope = decodeScope(scope); 49 } 50 51 55 public Object getObject() { 56 return attributeObject; 57 } 58 59 63 public void setObject(DirContext object) { 64 attributeObject = object; 65 } 66 67 71 public void setRef(String ref) { 72 this.attributeRef = ref; 73 } 74 75 public int doStartTag() throws JspException { 76 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 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 |