View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.xml.digest;
22  
23  import org.onemind.commons.java.lang.reflect.ReflectUtils;
24  import org.onemind.commons.java.util.StringUtils;
25  import org.xml.sax.Attributes;
26  import org.xml.sax.SAXException;
27  /***
28   * A ChainedDigester is a helper digester that chains the digestion
29   * of xml dynamically based on a dynamic digester configured as a 
30   * attribute name. 
31   * @author TiongHiang Lee (thlee@onemindsoft.org)
32   */
33  public class ChainedDigester extends AbstractElementCreatorDigester
34  {
35      /*** the attribute name that specify the digester class **/
36      private String _attrName;
37  
38      /*** the argument to pass the constructor of the dynamic digester **/
39      private Object[] _args;
40  
41      /***
42       * Constructor
43       * @param name the name of element
44       */
45      public ChainedDigester(String name)
46      {
47          this(name, "className", null);
48      }
49  
50      /***
51       * Constructor
52       * @param name the element name
53       * @param attrName the attr
54       */
55      public ChainedDigester(String name, String attrName)
56      {
57          this(name, attrName, null);
58      }
59  
60      /***
61       * Constructor
62       * @param name the element name
63       * @param attrName the attribute the specifies the dynamic digester
64       * @param args arguments to pass to constructor of the dynamic digester
65       */
66      public ChainedDigester(String name, String attrName, Object[] args)
67      {
68          super(name);
69          _attrName = attrName;
70          _args = args;
71      }
72  
73      /*** 
74       * {@inheritDoc}
75       */
76      public void startDigest(SaxDigesterHandler handler, Attributes attrs) throws SAXException
77      {
78          String className = attrs.getValue(_attrName);
79          if (StringUtils.isNullOrEmpty(className))
80          {
81              throw new SAXException("className attribute need to be present at " + handler.getCurrentPath());
82          } else
83          {
84              try
85              {
86                  ElementDigester dig = (ElementDigester) ReflectUtils.newInstance(ReflectUtils.getClass(className), _args);
87                  setCreatedElement(dig);
88                  handler.addSubDigester(dig);
89              } catch (Exception e)
90              {
91                  throw new SAXException("Cannot instantiate render context " + className, e);
92              }
93          }
94      }
95  }