1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      // TagLibrary interface
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      // Implementation methods
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          // is the name bound to a specific class
62          Class beanType = getBeanType(name, attributes);
63          if (beanType != null) {
64              return new BeanTag(beanType, name);
65          }
66  
67          // its a property tag
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  }