1 12 package org.displaytag.decorator; 13 14 import javax.servlet.jsp.PageContext ; 15 16 import org.apache.commons.lang.StringUtils; 17 import org.displaytag.exception.DecoratorException; 18 import org.displaytag.exception.DecoratorInstantiationException; 19 import org.displaytag.properties.MediaTypeEnum; 20 import org.displaytag.util.ReflectHelper; 21 22 23 28 public class DefaultDecoratorFactory implements DecoratorFactory 29 { 30 31 49 public TableDecorator loadTableDecorator(PageContext pageContext, String decoratorName) 50 throws DecoratorInstantiationException 51 { 52 if (StringUtils.isBlank(decoratorName)) 53 { 54 return null; 55 } 56 57 Object decorator = pageContext.findAttribute(decoratorName); 59 60 if (decorator == null) 63 { 64 try 65 { 66 decorator = ReflectHelper.classForName(decoratorName).newInstance(); 67 } 68 catch (ClassNotFoundException e) 69 { 70 throw new DecoratorInstantiationException(DefaultDecoratorFactory.class, decoratorName, e); 71 } 72 catch (InstantiationException e) 73 { 74 throw new DecoratorInstantiationException(DefaultDecoratorFactory.class, decoratorName, e); 75 } 76 catch (IllegalAccessException e) 77 { 78 throw new DecoratorInstantiationException(DefaultDecoratorFactory.class, decoratorName, e); 79 } 80 } 81 82 if (decorator instanceof TableDecorator) 83 { 84 return (TableDecorator) decorator; 85 } 86 else 87 { 88 throw new DecoratorInstantiationException( 89 DefaultDecoratorFactory.class, 90 decoratorName, 91 new ClassCastException (decorator.getClass().getName())); 92 } 93 94 } 95 96 114 public DisplaytagColumnDecorator loadColumnDecorator(PageContext pageContext, String decoratorName) 115 throws DecoratorInstantiationException 116 { 117 if (StringUtils.isBlank(decoratorName)) 118 { 119 return null; 120 } 121 122 Object decorator = pageContext.findAttribute(decoratorName); 124 125 if (decorator == null) 128 { 129 try 130 { 131 decorator = ReflectHelper.classForName(decoratorName).newInstance(); 132 } 133 catch (ClassNotFoundException e) 134 { 135 throw new DecoratorInstantiationException(DefaultDecoratorFactory.class, decoratorName, e); 136 } 137 catch (InstantiationException e) 138 { 139 throw new DecoratorInstantiationException(DefaultDecoratorFactory.class, decoratorName, e); 140 } 141 catch (IllegalAccessException e) 142 { 143 throw new DecoratorInstantiationException(DefaultDecoratorFactory.class, decoratorName, e); 144 } 145 } 146 147 if (decorator instanceof DisplaytagColumnDecorator) 148 { 149 return (DisplaytagColumnDecorator) decorator; 150 } 151 else if (decorator instanceof ColumnDecorator) 152 { 153 return new DeprecatedDecoratorWrapper((ColumnDecorator) decorator); 154 } 155 else 156 { 157 throw new DecoratorInstantiationException( 158 DefaultDecoratorFactory.class, 159 decoratorName, 160 new ClassCastException (decorator.getClass().getName())); 161 } 162 } 163 164 168 private static class DeprecatedDecoratorWrapper implements DisplaytagColumnDecorator 169 { 170 171 174 private ColumnDecorator decorator; 175 176 180 public DeprecatedDecoratorWrapper(ColumnDecorator decorator) 181 { 182 this.decorator = decorator; 183 } 184 185 189 public Object decorate(Object columnValue, PageContext pageContext, MediaTypeEnum media) 190 throws DecoratorException 191 { 192 return decorator.decorate(columnValue); 193 } 194 195 } 196 } | Popular Tags |