1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.tomcat.util.digester;
20
21
22 import org.xml.sax.Attributes;
23
24
25 /**
26  * Rule implementation that creates a new object and pushes it
27  * onto the object stack.  When the element is complete, the
28  * object will be popped
29  */

30
31 public class ObjectCreateRule extends Rule {
32
33
34     // ----------------------------------------------------------- Constructors
35
36
37     /**
38      * Construct an object create rule with the specified class name.
39      *
40      * @param className Java class name of the object to be created
41      */

42     public ObjectCreateRule(String className) {
43
44         this(className, null);
45
46     }
47
48
49     /**
50      * Construct an object create rule with the specified class name and an
51      * optional attribute name containing an override.
52      *
53      * @param className Java class name of the object to be created
54      * @param attributeName Attribute name which, if present, contains an
55      *  override of the class name to create
56      */

57     public ObjectCreateRule(String className,
58                             String attributeName) {
59
60         this.className = className;
61         this.attributeName = attributeName;
62
63     }
64
65
66     // ----------------------------------------------------- Instance Variables
67
68     /**
69      * The attribute containing an override class name if it is present.
70      */

71     protected String attributeName = null;
72
73
74     /**
75      * The Java class name of the object to be created.
76      */

77     protected String className = null;
78
79
80     // --------------------------------------------------------- Public Methods
81
82
83     /**
84      * Process the beginning of this element.
85      *
86      * @param namespace the namespace URI of the matching element, or an
87      *   empty string if the parser is not namespace aware or the element has
88      *   no namespace
89      * @param name the local name if the parser is namespace aware, or just
90      *   the element name otherwise
91      * @param attributes The attribute list for this element
92      */

93     @Override
94     public void begin(String namespace, String name, Attributes attributes)
95             throws Exception {
96
97         String realClassName = getRealClassName(attributes);
98
99         if (realClassName == null) {
100             throw new NullPointerException(sm.getString("rule.noClassName", namespace, name));
101         }
102
103         // Instantiate the new object and push it on the context stack
104         Class<?> clazz = digester.getClassLoader().loadClass(realClassName);
105         Object instance = clazz.getConstructor().newInstance();
106         digester.push(instance);
107     }
108
109
110     /**
111      * Return the actual class name of the class to be instantiated.
112      * @param attributes The attribute list for this element
113      * @return the class name
114      */

115     protected String getRealClassName(Attributes attributes) {
116         // Identify the name of the class to instantiate
117         String realClassName = className;
118         if (attributeName != null) {
119             String value = attributes.getValue(attributeName);
120             if (value != null) {
121                 realClassName = value;
122             }
123         }
124         return realClassName;
125     }
126
127
128     /**
129      * Process the end of this element.
130      *
131      * @param namespace the namespace URI of the matching element, or an
132      *   empty string if the parser is not namespace aware or the element has
133      *   no namespace
134      * @param name the local name if the parser is namespace aware, or just
135      *   the element name otherwise
136      */

137     @Override
138     public void end(String namespace, String name) throws Exception {
139
140         Object top = digester.pop();
141         if (digester.log.isDebugEnabled()) {
142             digester.log.debug("[ObjectCreateRule]{" + digester.match +
143                     "} Pop " + top.getClass().getName());
144         }
145
146     }
147
148
149     /**
150      * Render a printable version of this Rule.
151      */

152     @Override
153     public String toString() {
154         StringBuilder sb = new StringBuilder("ObjectCreateRule[");
155         sb.append("className=");
156         sb.append(className);
157         sb.append(", attributeName=");
158         sb.append(attributeName);
159         sb.append("]");
160         return sb.toString();
161     }
162
163
164 }
165