1 16 17 package org.apache.commons.jelly; 18 19 import java.io.File ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.apache.commons.beanutils.ConvertUtils; 24 import org.apache.commons.beanutils.Converter; 25 26 import org.apache.commons.jelly.expression.CompositeExpression; 27 import org.apache.commons.jelly.expression.ConstantExpression; 28 import org.apache.commons.jelly.expression.Expression; 29 import org.apache.commons.jelly.expression.ExpressionFactory; 30 import org.apache.commons.jelly.impl.TagFactory; 31 import org.apache.commons.jelly.impl.TagScript; 32 33 import org.xml.sax.Attributes ; 34 35 40 41 public abstract class TagLibrary { 42 43 private Map tags = new HashMap (); 44 45 static { 46 47 49 ConvertUtils.register( 50 new Converter() { 51 public Object convert(Class type, Object value) { 52 if ( value instanceof File ) { 53 return (File ) value; 54 } 55 else if ( value != null ) { 56 String text = value.toString(); 57 return new File ( text ); 58 } 59 return null; 60 } 61 }, 62 File .class 63 ); 64 } 65 66 public TagLibrary() { 67 } 68 69 70 public TagScript createTagScript(String name, Attributes attributes) 71 throws JellyException { 72 73 Object value = tags.get(name); 74 if (value instanceof Class ) { 75 Class type = (Class ) value; 76 return TagScript.newInstance(type); 77 } 78 else if (value instanceof TagFactory) { 79 return new TagScript( (TagFactory) value ); 80 } 81 return null; 82 83 } 84 85 86 public Tag createTag(String name, Attributes attributes) 87 throws JellyException { 88 89 Object value = tags.get(name); 90 if (value instanceof Class ) { 91 Class type = (Class ) value; 92 try { 93 return (Tag) type.newInstance(); 94 } catch (InstantiationException e) { 95 throw new JellyException(e.toString()); 96 } catch (IllegalAccessException e) { 97 throw new JellyException(e.toString()); 98 } 99 } 100 else if (value instanceof TagFactory) { 101 TagFactory factory = (TagFactory) value; 102 return factory.createTag(name, attributes); 103 } 104 return null; 105 } 106 107 108 public Expression createExpression( 109 ExpressionFactory factory, 110 TagScript tagScript, 111 String attributeName, 112 String attributeValue) 113 throws JellyException { 114 115 ExpressionFactory myFactory = getExpressionFactory(); 116 if (myFactory == null) { 117 myFactory = factory; 118 } 119 if (myFactory != null) { 120 return CompositeExpression.parse(attributeValue, myFactory); 121 } 122 123 return new ConstantExpression(attributeValue); 125 } 126 127 128 131 134 protected void registerTag(String name, Class type) { 135 tags.put(name, type); 136 } 137 138 141 protected void registerTagFactory(String name, TagFactory tagFactory) { 142 tags.put(name, tagFactory); 143 } 144 145 146 protected ExpressionFactory getExpressionFactory() { 147 return null; 148 } 149 150 protected Map getTagClasses() { 151 return tags; 152 } 153 154 } 155 | Popular Tags |