1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.bean;
17
18 import org.apache.commons.jelly.JellyException;
19 import org.apache.commons.jelly.Tag;
20 import org.apache.commons.jelly.TagLibrary;
21 import org.apache.commons.jelly.impl.TagFactory;
22 import org.apache.commons.jelly.impl.TagScript;
23
24 import org.xml.sax.Attributes;
25
26 /***
27 * A normal tag library which will use a BeanTag to create beans but this tag
28 * library does not derive from BeanTagLibrary and so does not have a <
29 * beandef> tag
30 *
31 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
32 * @version $Revision: 155420 $
33 */
34 public class MyTagLibrary extends TagLibrary {
35
36 public MyTagLibrary() {
37 }
38
39
40
41
42 public TagScript createTagScript(String name, Attributes attributes) throws JellyException {
43
44 TagFactory factory = new TagFactory() {
45 public Tag createTag(String name, Attributes attributes) throws JellyException {
46 return createBeanTag(name, attributes);
47 }
48 };
49 return new TagScript( factory );
50 }
51
52
53
54
55 /***
56 * Factory method to create a Tag for the given tag and attributes. If this
57 * tag matches a root bean, then a BeanTag will be created, otherwise a
58 * BeanPropertyTag is created to make a nested property.
59 */
60 protected Tag createBeanTag(String name, Attributes attributes) throws JellyException {
61
62 Class beanType = getBeanType(name, attributes);
63 if (beanType != null) {
64 return new BeanTag(beanType, name);
65 }
66
67
68 return new BeanPropertyTag(name);
69 }
70
71 /***
72 * Return the bean class that we should use for the given element name
73 *
74 * @param name is the XML element name
75 * @param attributes the XML attributes
76 * @return Class the bean class to use for this element or null if the tag
77 * is a nested property
78 */
79 protected Class getBeanType(String name, Attributes attributes) {
80 if (name.equals( "customer")) {
81 return Customer.class;
82 }
83 return null;
84 }
85 }