KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > digester > SetPropertiesRule


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

18
19
20 package org.apache.tomcat.util.digester;
21
22
23 import org.apache.tomcat.util.IntrospectionUtils;
24 import org.xml.sax.Attributes JavaDoc;
25
26
27 /**
28  * <p>Rule implementation that sets properties on the object at the top of the
29  * stack, based on attributes with corresponding names.</p>
30  *
31  * <p>This rule supports custom mapping of attribute names to property names.
32  * The default mapping for particular attributes can be overridden by using
33  * {@link #SetPropertiesRule(String[] attributeNames, String[] propertyNames)}.
34  * This allows attributes to be mapped to properties with different names.
35  * Certain attributes can also be marked to be ignored.</p>
36  */

37
38 public class SetPropertiesRule extends Rule {
39
40
41     // ----------------------------------------------------------- Constructors
42

43
44     /**
45      * Default constructor sets only the the associated Digester.
46      *
47      * @param digester The digester with which this rule is associated
48      *
49      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
50      * Use {@link #SetPropertiesRule()} instead.
51      */

52     public SetPropertiesRule(Digester digester) {
53
54         this();
55
56     }
57     
58
59     /**
60      * Base constructor.
61      */

62     public SetPropertiesRule() {
63
64         // nothing to set up
65

66     }
67     
68     /**
69      * <p>Convenience constructor overrides the mapping for just one property.</p>
70      *
71      * <p>For details about how this works, see
72      * {@link #SetPropertiesRule(String[] attributeNames, String[] propertyNames)}.</p>
73      *
74      * @param attributeName map this attribute
75      * @param propertyName to a property with this name
76      */

77     public SetPropertiesRule(String JavaDoc attributeName, String JavaDoc propertyName) {
78         
79         attributeNames = new String JavaDoc[1];
80         attributeNames[0] = attributeName;
81         propertyNames = new String JavaDoc[1];
82         propertyNames[0] = propertyName;
83     }
84     
85     /**
86      * <p>Constructor allows attribute->property mapping to be overriden.</p>
87      *
88      * <p>Two arrays are passed in.
89      * One contains the attribute names and the other the property names.
90      * The attribute name / property name pairs are match by position
91      * In order words, the first string in the attribute name list matches
92      * to the first string in the property name list and so on.</p>
93      *
94      * <p>If a property name is null or the attribute name has no matching
95      * property name, then this indicates that the attibute should be ignored.</p>
96      *
97      * <h5>Example One</h5>
98      * <p> The following constructs a rule that maps the <code>alt-city</code>
99      * attribute to the <code>city</code> property and the <code>alt-state</code>
100      * to the <code>state</code> property.
101      * All other attributes are mapped as usual using exact name matching.
102      * <code><pre>
103      * SetPropertiesRule(
104      * new String[] {"alt-city", "alt-state"},
105      * new String[] {"city", "state"});
106      * </pre></code>
107      *
108      * <h5>Example Two</h5>
109      * <p> The following constructs a rule that maps the <code>class</code>
110      * attribute to the <code>className</code> property.
111      * The attribute <code>ignore-me</code> is not mapped.
112      * All other attributes are mapped as usual using exact name matching.
113      * <code><pre>
114      * SetPropertiesRule(
115      * new String[] {"class", "ignore-me"},
116      * new String[] {"className"});
117      * </pre></code>
118      *
119      * @param attributeNames names of attributes to map
120      * @param propertyNames names of properties mapped to
121      */

122     public SetPropertiesRule(String JavaDoc[] attributeNames, String JavaDoc[] propertyNames) {
123         // create local copies
124
this.attributeNames = new String JavaDoc[attributeNames.length];
125         for (int i=0, size=attributeNames.length; i<size; i++) {
126             this.attributeNames[i] = attributeNames[i];
127         }
128         
129         this.propertyNames = new String JavaDoc[propertyNames.length];
130         for (int i=0, size=propertyNames.length; i<size; i++) {
131             this.propertyNames[i] = propertyNames[i];
132         }
133     }
134         
135     // ----------------------------------------------------- Instance Variables
136

137     /**
138      * Attribute names used to override natural attribute->property mapping
139      */

140     private String JavaDoc [] attributeNames;
141     /**
142      * Property names used to override natural attribute->property mapping
143      */

144     private String JavaDoc [] propertyNames;
145
146
147     // --------------------------------------------------------- Public Methods
148

149
150     /**
151      * Process the beginning of this element.
152      *
153      * @param attributes The attribute list of this element
154      */

155     public void begin(Attributes JavaDoc attributes) throws Exception JavaDoc {
156         
157         // Populate the corresponding properties of the top object
158
Object JavaDoc top = digester.peek();
159         if (digester.log.isDebugEnabled()) {
160             if (top != null) {
161                 digester.log.debug("[SetPropertiesRule]{" + digester.match +
162                                    "} Set " + top.getClass().getName() +
163                                    " properties");
164             } else {
165                 digester.log.debug("[SetPropertiesRule]{" + digester.match +
166                                    "} Set NULL properties");
167             }
168         }
169         
170         // set up variables for custom names mappings
171
int attNamesLength = 0;
172         if (attributeNames != null) {
173             attNamesLength = attributeNames.length;
174         }
175         int propNamesLength = 0;
176         if (propertyNames != null) {
177             propNamesLength = propertyNames.length;
178         }
179         
180         for (int i = 0; i < attributes.getLength(); i++) {
181             String JavaDoc name = attributes.getLocalName(i);
182             if ("".equals(name)) {
183                 name = attributes.getQName(i);
184             }
185             String JavaDoc value = attributes.getValue(i);
186             
187             // we'll now check for custom mappings
188
for (int n = 0; n<attNamesLength; n++) {
189                 if (name.equals(attributeNames[n])) {
190                     if (n < propNamesLength) {
191                         // set this to value from list
192
name = propertyNames[n];
193                     
194                     } else {
195                         // set name to null
196
// we'll check for this later
197
name = null;
198                     }
199                     break;
200                 }
201             }
202             
203             if (digester.log.isDebugEnabled()) {
204                 digester.log.debug("[SetPropertiesRule]{" + digester.match +
205                         "} Setting property '" + name + "' to '" +
206                         value + "'");
207             }
208             IntrospectionUtils.setProperty(top, name, value);
209         }
210
211     }
212
213
214     /**
215      * <p>Add an additional attribute name to property name mapping.
216      * This is intended to be used from the xml rules.
217      */

218     public void addAlias(String JavaDoc attributeName, String JavaDoc propertyName) {
219         
220         // this is a bit tricky.
221
// we'll need to resize the array.
222
// probably should be synchronized but digester's not thread safe anyway
223
if (attributeNames == null) {
224             
225             attributeNames = new String JavaDoc[1];
226             attributeNames[0] = attributeName;
227             propertyNames = new String JavaDoc[1];
228             propertyNames[0] = propertyName;
229             
230         } else {
231             int length = attributeNames.length;
232             String JavaDoc [] tempAttributes = new String JavaDoc[length + 1];
233             for (int i=0; i<length; i++) {
234                 tempAttributes[i] = attributeNames[i];
235             }
236             tempAttributes[length] = attributeName;
237             
238             String JavaDoc [] tempProperties = new String JavaDoc[length + 1];
239             for (int i=0; i<length && i< propertyNames.length; i++) {
240                 tempProperties[i] = propertyNames[i];
241             }
242             tempProperties[length] = propertyName;
243             
244             propertyNames = tempProperties;
245             attributeNames = tempAttributes;
246         }
247     }
248   
249
250     /**
251      * Render a printable version of this Rule.
252      */

253     public String JavaDoc toString() {
254
255         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("SetPropertiesRule[");
256         sb.append("]");
257         return (sb.toString());
258
259     }
260
261
262 }
263
Popular Tags