[Ejtools-cvs] CVS: applications/jndi.browser/src/main/net/sourceforge/ejtools/jndibrowser/web/taglib
Brought to you by:
letiemble
From: Laurent E. <let...@us...> - 2002-05-25 20:53:53
|
Update of /cvsroot/ejtools/applications/jndi.browser/src/main/net/sourceforge/ejtools/jndibrowser/web/taglib In directory usw-pr-cvs1:/tmp/cvs-serv4941/jndi.browser/src/main/net/sourceforge/ejtools/jndibrowser/web/taglib Added Files: AttributeEditorTag.java Log Message: Pretty print --- NEW FILE: AttributeEditorTag.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package net.sourceforge.ejtools.jndibrowser.web.taglib; import java.beans.PropertyDescriptor; import java.beans.PropertyEditor; import java.beans.PropertyEditorManager; import java.lang.reflect.Method; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; import net.sourceforge.ejtools.util.ClassTools; import org.apache.struts.util.RequestUtils; import org.apache.struts.util.ResponseUtils; /** * Description of the Class * * @author letiemble * @created 25 avril 2002 * @todo Javadoc to complete */ public class AttributeEditorTag extends TagSupport { /** Filter the rendered output for characters that are sensitive in HTML? */ protected boolean filter = true; /** Should we ignore missing beans and simply output nothing? */ protected boolean ignore = false; /** Name of the bean that contains the data we will be rendering. */ protected String name = null; /** Name of the property to be accessed on the specified bean. */ protected String property = null; /** The scope to be searched to retrieve the specified bean. */ protected String scope = null; /** * Description of the Method * * @return Description of the Returned Value * @exception JspException Description of Exception */ public int doStartTag() throws JspException { // Look up the requested bean (if necessary) Object bean = (Object) RequestUtils.lookup(pageContext, name, scope); if (ignore) { if (bean == null) { return (SKIP_BODY); } // Nothing to output } PropertyDescriptor descriptor = (PropertyDescriptor) RequestUtils.lookup(pageContext, property, scope); PropertyEditor propertyeditor = null; Class c = descriptor.getPropertyType(); if (c == null) { addUnsupportedProperty(descriptor); } else { if (c.isArray()) { Class componentType = c.getComponentType(); propertyeditor = PropertyEditorManager.findEditor(componentType); if (propertyeditor == null) { propertyeditor = PropertyEditorManager.findEditor(String.class); } addArrayProperty(bean, propertyeditor, descriptor); } else { Class customeditor = descriptor.getPropertyEditorClass(); if (customeditor == null) { propertyeditor = PropertyEditorManager.findEditor(c); } else { try { propertyeditor = (PropertyEditor) customeditor.newInstance(); } catch (Exception e) { } } if (propertyeditor == null) { propertyeditor = PropertyEditorManager.findEditor(String.class); } addProperty(bean, propertyeditor, descriptor); } } // Continue processing this page return (SKIP_BODY); } /** * Getter for the filter attribute * * @return The value of filter attribute */ public boolean getFilter() { return (this.filter); } /** * Getter for the ignore attribute * * @return The value of ignore attribute */ public boolean getIgnore() { return (this.ignore); } /** * Getter for the name attribute * * @return The value of name attribute */ public String getName() { return (this.name); } /** * Getter for the property attribute * * @return The value of property attribute */ public String getProperty() { return (this.property); } /** * Getter for the scope attribute * * @return The value of scope attribute */ public String getScope() { return (this.scope); } /** Release all allocated resources. */ public void release() { super.release(); filter = true; ignore = false; name = null; scope = null; } /** * Setter for the filter attribute * * @param filter The new value for filter attribute */ public void setFilter(boolean filter) { this.filter = filter; } /** * Setter for the ignore attribute * * @param ignore The new value for ignore attribute */ public void setIgnore(boolean ignore) { this.ignore = ignore; } /** * Setter for the name attribute * * @param name The new value for name attribute */ public void setName(String name) { this.name = name; } /** * Setter for the property attribute * * @param property The new value for property attribute */ public void setProperty(String property) { this.property = property; } /** * Setter for the scope attribute * * @param scope The new value for scope attribute */ public void setScope(String scope) { this.scope = scope; } /** * Adds a feature to the ArrayProperty attribute of the AttributeEditorTag object * * @param object The feature to be added to the ArrayProperty attribute * @param propertyeditor The feature to be added to the ArrayProperty attribute * @param descriptor The feature to be added to the ArrayProperty attribute * @exception JspException Description of the Exception */ protected void addArrayProperty(Object object, PropertyEditor propertyeditor, PropertyDescriptor descriptor) throws JspException { System.out.println("addProperty " + propertyeditor + " " + descriptor.getName()); String output = ""; Object obj = null; Object[] array = null; try { Method m = descriptor.getReadMethod(); if (m != null) { obj = m.invoke(object, new Object[0]); } if (obj != null) { array = (Object[]) obj; } } catch (Throwable _ex) { } for (int i = 0; i < array.length; i++) { try { PropertyEditor propertyeditor1 = (PropertyEditor) propertyeditor.getClass().newInstance(); propertyeditor1.setValue(array[i]); output = propertyeditor1.getAsText(); output = output + "<br/>"; if (filter) { ResponseUtils.write(pageContext, ResponseUtils.filter(output)); } else { ResponseUtils.write(pageContext, output); } } catch (Exception _ex) { } } } /** * Adds a feature to the Property attribute of the AttributeEditorTag object * * @param object The feature to be added to the Property attribute * @param propertyeditor The feature to be added to the Property attribute * @param descriptor The feature to be added to the Property attribute * @exception JspException Description of the Exception */ protected void addProperty(Object object, PropertyEditor propertyeditor, PropertyDescriptor descriptor) throws JspException { System.out.println("addProperty " + propertyeditor + " " + descriptor.getName()); String output = ""; try { Method m = descriptor.getReadMethod(); if (m != null) { propertyeditor.setValue(m.invoke(object, new Object[0])); } } catch (Throwable _ex) { } output = propertyeditor.getAsText(); if (filter) { ResponseUtils.write(pageContext, ResponseUtils.filter(output)); } else { ResponseUtils.write(pageContext, output); } } /** * Adds a feature to the UnsupportedProperty attribute of the AttributeEditorTag object * * @param descriptor The feature to be added to the UnsupportedProperty attribute * @exception JspException Description of the Exception */ protected void addUnsupportedProperty(PropertyDescriptor descriptor) throws JspException { System.out.println("Getting value for attribute " + descriptor.getName()); String output = "<i>Unsupported Class : " + descriptor.getPropertyType() + "</i>"; if (filter) { ResponseUtils.write(pageContext, ResponseUtils.filter(output)); } else { ResponseUtils.write(pageContext, output); } } static { // To change PropertyEditorManager.setEditorSearchPath(new String[]{"sun.beans.editors", "net.sourceforge.ejtools.awt.editors"}); } } |