1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }