KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > xml > BeanDefinitionParserDelegate


1 /*
2  * Copyright 2002-2007 the original author or authors.
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
17 package org.springframework.beans.factory.xml;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.NamedNodeMap JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34
35 import org.springframework.beans.PropertyValue;
36 import org.springframework.beans.factory.BeanDefinitionStoreException;
37 import org.springframework.beans.factory.config.BeanDefinition;
38 import org.springframework.beans.factory.config.BeanDefinitionHolder;
39 import org.springframework.beans.factory.config.ConstructorArgumentValues;
40 import org.springframework.beans.factory.config.RuntimeBeanNameReference;
41 import org.springframework.beans.factory.config.RuntimeBeanReference;
42 import org.springframework.beans.factory.config.TypedStringValue;
43 import org.springframework.beans.factory.parsing.BeanEntry;
44 import org.springframework.beans.factory.parsing.ConstructorArgumentEntry;
45 import org.springframework.beans.factory.parsing.ParseState;
46 import org.springframework.beans.factory.parsing.PropertyEntry;
47 import org.springframework.beans.factory.support.AbstractBeanDefinition;
48 import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
49 import org.springframework.beans.factory.support.LookupOverride;
50 import org.springframework.beans.factory.support.ManagedList;
51 import org.springframework.beans.factory.support.ManagedMap;
52 import org.springframework.beans.factory.support.ManagedProperties;
53 import org.springframework.beans.factory.support.ManagedSet;
54 import org.springframework.beans.factory.support.MethodOverrides;
55 import org.springframework.beans.factory.support.ReplaceOverride;
56 import org.springframework.core.AttributeAccessor;
57 import org.springframework.util.Assert;
58 import org.springframework.util.ClassUtils;
59 import org.springframework.util.CollectionUtils;
60 import org.springframework.util.ObjectUtils;
61 import org.springframework.util.StringUtils;
62 import org.springframework.util.xml.DomUtils;
63
64 /**
65  * Stateful delegate class used to parse XML bean definitions.
66  * Intended for use by both the main parser and any extension
67  * {@link BeanDefinitionParser BeanDefinitionParsers}
68  * or {@link BeanDefinitionDecorator BeanDefinitionDecorators}.
69  *
70  * @author Rob Harrop
71  * @author Juergen Hoeller
72  * @author Rod Johnson
73  * @since 2.0
74  * @see ParserContext
75  * @see DefaultBeanDefinitionDocumentReader
76  */

77 public class BeanDefinitionParserDelegate {
78
79     public static final String JavaDoc BEANS_NAMESPACE_URI = "http://www.springframework.org/schema/beans";
80
81     public static final String JavaDoc BEAN_NAME_DELIMITERS = ",; ";
82
83     /**
84      * Value of a T/F attribute that represents true.
85      * Anything else represents false. Case seNsItive.
86      */

87     public static final String JavaDoc TRUE_VALUE = "true";
88
89     public static final String JavaDoc DEFAULT_VALUE = "default";
90
91     public static final String JavaDoc DESCRIPTION_ELEMENT = "description";
92
93     public static final String JavaDoc AUTOWIRE_BY_NAME_VALUE = "byName";
94
95     public static final String JavaDoc AUTOWIRE_BY_TYPE_VALUE = "byType";
96
97     public static final String JavaDoc AUTOWIRE_CONSTRUCTOR_VALUE = "constructor";
98
99     public static final String JavaDoc AUTOWIRE_AUTODETECT_VALUE = "autodetect";
100
101     public static final String JavaDoc DEPENDENCY_CHECK_ALL_ATTRIBUTE_VALUE = "all";
102
103     public static final String JavaDoc DEPENDENCY_CHECK_SIMPLE_ATTRIBUTE_VALUE = "simple";
104
105     public static final String JavaDoc DEPENDENCY_CHECK_OBJECTS_ATTRIBUTE_VALUE = "objects";
106
107     public static final String JavaDoc NAME_ATTRIBUTE = "name";
108
109     public static final String JavaDoc BEAN_ELEMENT = "bean";
110
111     public static final String JavaDoc META_ELEMENT = "meta";
112
113     public static final String JavaDoc ID_ATTRIBUTE = "id";
114
115     public static final String JavaDoc PARENT_ATTRIBUTE = "parent";
116
117     public static final String JavaDoc CLASS_ATTRIBUTE = "class";
118
119     public static final String JavaDoc ABSTRACT_ATTRIBUTE = "abstract";
120
121     public static final String JavaDoc SCOPE_ATTRIBUTE = "scope";
122
123     public static final String JavaDoc SINGLETON_ATTRIBUTE = "singleton";
124
125     public static final String JavaDoc LAZY_INIT_ATTRIBUTE = "lazy-init";
126
127     public static final String JavaDoc AUTOWIRE_ATTRIBUTE = "autowire";
128
129     public static final String JavaDoc AUTOWIRE_CANDIDATE_ATTRIBUTE = "autowire-candidate";
130
131     public static final String JavaDoc DEPENDENCY_CHECK_ATTRIBUTE = "dependency-check";
132
133     public static final String JavaDoc DEPENDS_ON_ATTRIBUTE = "depends-on";
134
135     public static final String JavaDoc INIT_METHOD_ATTRIBUTE = "init-method";
136
137     public static final String JavaDoc DESTROY_METHOD_ATTRIBUTE = "destroy-method";
138
139     public static final String JavaDoc FACTORY_METHOD_ATTRIBUTE = "factory-method";
140
141     public static final String JavaDoc FACTORY_BEAN_ATTRIBUTE = "factory-bean";
142
143     public static final String JavaDoc CONSTRUCTOR_ARG_ELEMENT = "constructor-arg";
144
145     public static final String JavaDoc INDEX_ATTRIBUTE = "index";
146
147     public static final String JavaDoc TYPE_ATTRIBUTE = "type";
148
149     public static final String JavaDoc VALUE_TYPE_ATTRIBUTE = "value-type";
150
151     public static final String JavaDoc KEY_TYPE_ATTRIBUTE = "key-type";
152
153     public static final String JavaDoc PROPERTY_ELEMENT = "property";
154
155     public static final String JavaDoc REF_ATTRIBUTE = "ref";
156
157     public static final String JavaDoc VALUE_ATTRIBUTE = "value";
158
159     public static final String JavaDoc LOOKUP_METHOD_ELEMENT = "lookup-method";
160
161     public static final String JavaDoc REPLACED_METHOD_ELEMENT = "replaced-method";
162
163     public static final String JavaDoc REPLACER_ATTRIBUTE = "replacer";
164
165     public static final String JavaDoc ARG_TYPE_ELEMENT = "arg-type";
166
167     public static final String JavaDoc ARG_TYPE_MATCH_ATTRIBUTE = "match";
168
169     public static final String JavaDoc REF_ELEMENT = "ref";
170
171     public static final String JavaDoc IDREF_ELEMENT = "idref";
172
173     public static final String JavaDoc BEAN_REF_ATTRIBUTE = "bean";
174
175     public static final String JavaDoc LOCAL_REF_ATTRIBUTE = "local";
176
177     public static final String JavaDoc PARENT_REF_ATTRIBUTE = "parent";
178
179     public static final String JavaDoc VALUE_ELEMENT = "value";
180
181     public static final String JavaDoc NULL_ELEMENT = "null";
182
183     public static final String JavaDoc LIST_ELEMENT = "list";
184
185     public static final String JavaDoc SET_ELEMENT = "set";
186
187     public static final String JavaDoc MAP_ELEMENT = "map";
188
189     public static final String JavaDoc ENTRY_ELEMENT = "entry";
190
191     public static final String JavaDoc KEY_ELEMENT = "key";
192
193     public static final String JavaDoc KEY_ATTRIBUTE = "key";
194
195     public static final String JavaDoc KEY_REF_ATTRIBUTE = "key-ref";
196
197     public static final String JavaDoc VALUE_REF_ATTRIBUTE = "value-ref";
198
199     public static final String JavaDoc PROPS_ELEMENT = "props";
200
201     public static final String JavaDoc PROP_ELEMENT = "prop";
202
203     public static final String JavaDoc MERGE_ATTRIBUTE = "merge";
204
205     public static final String JavaDoc DEFAULT_LAZY_INIT_ATTRIBUTE = "default-lazy-init";
206
207     public static final String JavaDoc DEFAULT_AUTOWIRE_ATTRIBUTE = "default-autowire";
208
209     public static final String JavaDoc DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE = "default-dependency-check";
210
211     public static final String JavaDoc DEFAULT_INIT_METHOD_ATTRIBUTE = "default-init-method";
212
213     public static final String JavaDoc DEFAULT_DESTROY_METHOD_ATTRIBUTE = "default-destroy-method";
214
215     public static final String JavaDoc DEFAULT_MERGE_ATTRIBUTE = "default-merge";
216
217
218     protected final Log logger = LogFactory.getLog(getClass());
219
220     private final XmlReaderContext readerContext;
221
222     private DocumentDefaultsDefinition defaults;
223
224     private ParseState parseState = new ParseState();
225
226     /**
227      * Stores all used bean names so we can enforce uniqueness on a per file basis.
228      */

229     private final Set JavaDoc usedNames = new HashSet JavaDoc();
230
231
232     /**
233      * Create a new BeanDefinitionParserDelegate associated with the
234      * supplied {@link XmlReaderContext}.
235      */

236     public BeanDefinitionParserDelegate(XmlReaderContext readerContext) {
237         Assert.notNull(readerContext, "XmlReaderContext must not be null");
238         this.readerContext = readerContext;
239     }
240
241     /**
242      * Get the {@link XmlReaderContext} associated with this helper instance.
243      */

244     public final XmlReaderContext getReaderContext() {
245         return this.readerContext;
246     }
247
248
249     /**
250      * Invoke the {@link org.springframework.beans.factory.parsing.SourceExtractor} to pull the
251      * source metadata from the supplied {@link Element}.
252      */

253     protected Object JavaDoc extractSource(Element JavaDoc ele) {
254         return this.readerContext.extractSource(ele);
255     }
256
257     /**
258      * Report an error with the given message for the given source element.
259      */

260     protected void error(String JavaDoc message, Element JavaDoc source) {
261         this.readerContext.error(message, source, this.parseState.snapshot());
262     }
263
264     /**
265      * Report an error with the given message for the given source element.
266      */

267     protected void error(String JavaDoc message, Element JavaDoc source, Throwable JavaDoc cause) {
268         this.readerContext.error(message, source, this.parseState.snapshot(), cause);
269     }
270
271
272     /**
273      * Initialize the default lazy-init, autowire, dependency check settings,
274      * init-method, destroy-method and merge settings.
275      * @see #getDefaults()
276      */

277     public void initDefaults(Element JavaDoc root) {
278         DocumentDefaultsDefinition defaults = new DocumentDefaultsDefinition();
279         defaults.setLazyInit(root.getAttribute(DEFAULT_LAZY_INIT_ATTRIBUTE));
280         defaults.setAutowire(root.getAttribute(DEFAULT_AUTOWIRE_ATTRIBUTE));
281         defaults.setDependencyCheck(root.getAttribute(DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE));
282         if (root.hasAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE)) {
283             defaults.setInitMethod(root.getAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE));
284         }
285         if (root.hasAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE)) {
286             defaults.setDestroyMethod(root.getAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE));
287         }
288         defaults.setMerge(root.getAttribute(DEFAULT_MERGE_ATTRIBUTE));
289         defaults.setSource(this.readerContext.extractSource(root));
290
291         this.defaults = defaults;
292         this.readerContext.fireDefaultsRegistered(defaults);
293     }
294
295     /**
296      * Return the defaults definition object, or <code>null</code> if the
297      * defaults have been initialized yet.
298      */

299     public DocumentDefaultsDefinition getDefaults() {
300         return this.defaults;
301     }
302
303
304     /**
305      * Parses the supplied <code>&lt;bean&gt;</code> element. May return <code>null</code>
306      * if there were errors during parse. Errors are reported to the
307      * {@link org.springframework.beans.factory.parsing.ProblemReporter}.
308      */

309     public BeanDefinitionHolder parseBeanDefinitionElement(Element JavaDoc ele) {
310         return parseBeanDefinitionElement(ele, null);
311     }
312
313     /**
314      * Parses the supplied <code>&lt;bean&gt;</code> element. May return <code>null</code>
315      * if there were errors during parse. Errors are reported to the
316      * {@link org.springframework.beans.factory.parsing.ProblemReporter}.
317      */

318     public BeanDefinitionHolder parseBeanDefinitionElement(Element JavaDoc ele, BeanDefinition containingBean) {
319         String JavaDoc id = ele.getAttribute(ID_ATTRIBUTE);
320         String JavaDoc nameAttr = ele.getAttribute(NAME_ATTRIBUTE);
321
322         List JavaDoc aliases = new ArrayList JavaDoc();
323         if (StringUtils.hasLength(nameAttr)) {
324             String JavaDoc[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, BEAN_NAME_DELIMITERS);
325             aliases.addAll(Arrays.asList(nameArr));
326         }
327
328         String JavaDoc beanName = id;
329         if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) {
330             beanName = (String JavaDoc) aliases.remove(0);
331             if (logger.isDebugEnabled()) {
332                 logger.debug("No XML 'id' specified - using '" + beanName +
333                         "' as bean name and " + aliases + " as aliases");
334             }
335         }
336
337         if (containingBean == null) {
338             checkNameUniqueness(beanName, aliases, ele);
339         }
340
341         AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);
342         if (beanDefinition != null) {
343             if (!StringUtils.hasText(beanName)) {
344                 try {
345                     if (containingBean != null) {
346                         beanName = BeanDefinitionReaderUtils.generateBeanName(
347                                 beanDefinition, this.readerContext.getRegistry(), true);
348                     }
349                     else {
350                         beanName = this.readerContext.generateBeanName(beanDefinition);
351                     }
352                     if (logger.isDebugEnabled()) {
353                         logger.debug("Neither XML 'id' nor 'name' specified - " +
354                                 "using generated bean name [" + beanName + "]");
355                     }
356                 }
357                 catch (BeanDefinitionStoreException ex) {
358                     error(ex.getMessage(), ele);
359                     return null;
360                 }
361             }
362             String JavaDoc[] aliasesArray = StringUtils.toStringArray(aliases);
363             return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray);
364         }
365
366         return null;
367     }
368
369     private void checkNameUniqueness(String JavaDoc beanName, List JavaDoc aliases, Element JavaDoc beanElement) {
370         String JavaDoc foundName = null;
371
372         if (StringUtils.hasText(beanName) && this.usedNames.contains(beanName)) {
373             foundName = beanName;
374         }
375         if (foundName == null) {
376             foundName = (String JavaDoc) CollectionUtils.findFirstMatch(this.usedNames, aliases);
377         }
378         if (foundName != null) {
379             error("Bean name '" + foundName + "' is already used in this file.", beanElement);
380         }
381
382         this.usedNames.add(beanName);
383         this.usedNames.addAll(aliases);
384     }
385
386     /**
387      * Parse the bean definition itself, without regard to name or aliases. May return
388      * <code>null</code> if problems occured during the parse of the bean definition.
389      */

390     public AbstractBeanDefinition parseBeanDefinitionElement(
391             Element JavaDoc ele, String JavaDoc beanName, BeanDefinition containingBean) {
392
393         String JavaDoc className = null;
394         if (ele.hasAttribute(CLASS_ATTRIBUTE)) {
395             className = ele.getAttribute(CLASS_ATTRIBUTE).trim();
396         }
397         String JavaDoc parent = null;
398         if (ele.hasAttribute(PARENT_ATTRIBUTE)) {
399             parent = ele.getAttribute(PARENT_ATTRIBUTE);
400         }
401
402         try {
403             this.parseState.push(new BeanEntry(beanName));
404
405             AbstractBeanDefinition bd = BeanDefinitionReaderUtils.createBeanDefinition(
406                     parent, className, this.readerContext.getReader().getBeanClassLoader());
407
408             if (ele.hasAttribute(SCOPE_ATTRIBUTE)) {
409                 // Spring 2.0 "scope" attribute
410
bd.setScope(ele.getAttribute(SCOPE_ATTRIBUTE));
411                 if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) {
412                     error("Specify either 'scope' or 'singleton', not both", ele);
413                 }
414             }
415             else if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) {
416                 // Spring 1.x "singleton" attribute
417
bd.setSingleton(TRUE_VALUE.equals(ele.getAttribute(SINGLETON_ATTRIBUTE)));
418             }
419             else if (containingBean != null) {
420                 // Take default from containing bean in case of an inner bean definition.
421
bd.setSingleton(containingBean.isSingleton());
422             }
423
424             if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) {
425                 bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE)));
426             }
427
428             String JavaDoc lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE);
429             if (DEFAULT_VALUE.equals(lazyInit) && bd.isSingleton()) {
430                 // Just apply default to singletons, as lazy-init has no meaning for prototypes.
431
lazyInit = this.defaults.getLazyInit();
432             }
433             bd.setLazyInit(TRUE_VALUE.equals(lazyInit));
434
435             if (ele.hasAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE)) {
436                 bd.setAutowireCandidate(TRUE_VALUE.equals(ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE)));
437             }
438
439             String JavaDoc autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE);
440             if (DEFAULT_VALUE.equals(autowire)) {
441                 autowire = this.defaults.getAutowire();
442             }
443             bd.setAutowireMode(getAutowireMode(autowire));
444
445             String JavaDoc dependencyCheck = ele.getAttribute(DEPENDENCY_CHECK_ATTRIBUTE);
446             if (DEFAULT_VALUE.equals(dependencyCheck)) {
447                 dependencyCheck = this.defaults.getDependencyCheck();
448             }
449             bd.setDependencyCheck(getDependencyCheck(dependencyCheck));
450
451             if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
452                 String JavaDoc dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE);
453                 bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, BEAN_NAME_DELIMITERS));
454             }
455
456             if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) {
457                 bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE));
458             }
459             if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) {
460                 bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE));
461             }
462
463             if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) {
464                 String JavaDoc initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE);
465                 if (!"".equals(initMethodName)) {
466                     bd.setInitMethodName(initMethodName);
467                 }
468             }
469             else {
470                 if (this.defaults.getInitMethod() != null) {
471                     bd.setInitMethodName(this.defaults.getInitMethod());
472                     bd.setEnforceInitMethod(false);
473                 }
474             }
475
476             if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
477                 String JavaDoc destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE);
478                 if (!"".equals(destroyMethodName)) {
479                     bd.setDestroyMethodName(destroyMethodName);
480                 }
481             }
482             else {
483                 if (this.defaults.getDestroyMethod() != null) {
484                     bd.setDestroyMethodName(this.defaults.getDestroyMethod());
485                     bd.setEnforceDestroyMethod(false);
486                 }
487             }
488
489             parseMetaElements(ele, bd);
490             parseLookupOverrideSubElements(ele, bd.getMethodOverrides());
491             parseReplacedMethodSubElements(ele, bd.getMethodOverrides());
492
493             parseConstructorArgElements(ele, bd);
494             parsePropertyElements(ele, bd);
495
496             bd.setResourceDescription(this.readerContext.getResource().getDescription());
497             bd.setSource(extractSource(ele));
498
499             return bd;
500         }
501         catch (ClassNotFoundException JavaDoc ex) {
502             error("Bean class [" + className + "] not found", ele, ex);
503         }
504         catch (NoClassDefFoundError JavaDoc err) {
505             error("Class that bean class [" + className + "] depends on not found", ele, err);
506         }
507         catch (Throwable JavaDoc ex) {
508             error("Unexpected failure during bean definition parsing", ele, ex);
509         }
510         finally {
511             this.parseState.pop();
512         }
513
514         return null;
515     }
516
517     public void parseMetaElements(Element JavaDoc ele, AttributeAccessor attributeAccessor) {
518         NodeList JavaDoc nl = ele.getChildNodes();
519         for (int i = 0; i < nl.getLength(); i++) {
520             Node JavaDoc node = nl.item(i);
521             if (node instanceof Element JavaDoc && DomUtils.nodeNameEquals(node, META_ELEMENT)) {
522                 Element JavaDoc metaElement = (Element JavaDoc) node;
523                 String JavaDoc key = metaElement.getAttribute(KEY_ATTRIBUTE);
524                 String JavaDoc value = metaElement.getAttribute(VALUE_ATTRIBUTE);
525                 attributeAccessor.setAttribute(key, value);
526             }
527         }
528     }
529
530     public int getDependencyCheck(String JavaDoc att) {
531         int dependencyCheckCode = AbstractBeanDefinition.DEPENDENCY_CHECK_NONE;
532         if (DEPENDENCY_CHECK_ALL_ATTRIBUTE_VALUE.equals(att)) {
533             dependencyCheckCode = AbstractBeanDefinition.DEPENDENCY_CHECK_ALL;
534         }
535         else if (DEPENDENCY_CHECK_SIMPLE_ATTRIBUTE_VALUE.equals(att)) {
536             dependencyCheckCode = AbstractBeanDefinition.DEPENDENCY_CHECK_SIMPLE;
537         }
538         else if (DEPENDENCY_CHECK_OBJECTS_ATTRIBUTE_VALUE.equals(att)) {
539             dependencyCheckCode = AbstractBeanDefinition.DEPENDENCY_CHECK_OBJECTS;
540         }
541         // Else leave default value.
542
return dependencyCheckCode;
543     }
544
545     public int getAutowireMode(String JavaDoc att) {
546         int autowire = AbstractBeanDefinition.AUTOWIRE_NO;
547         if (AUTOWIRE_BY_NAME_VALUE.equals(att)) {
548             autowire = AbstractBeanDefinition.AUTOWIRE_BY_NAME;
549         }
550         else if (AUTOWIRE_BY_TYPE_VALUE.equals(att)) {
551             autowire = AbstractBeanDefinition.AUTOWIRE_BY_TYPE;
552         }
553         else if (AUTOWIRE_CONSTRUCTOR_VALUE.equals(att)) {
554             autowire = AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR;
555         }
556         else if (AUTOWIRE_AUTODETECT_VALUE.equals(att)) {
557             autowire = AbstractBeanDefinition.AUTOWIRE_AUTODETECT;
558         }
559         // Else leave default value.
560
return autowire;
561     }
562
563     /**
564      * Parse constructor-arg sub-elements of the given bean element.
565      */

566     public void parseConstructorArgElements(Element JavaDoc beanEle, BeanDefinition bd) {
567         NodeList JavaDoc nl = beanEle.getChildNodes();
568         for (int i = 0; i < nl.getLength(); i++) {
569             Node JavaDoc node = nl.item(i);
570             if (node instanceof Element JavaDoc && DomUtils.nodeNameEquals(node, CONSTRUCTOR_ARG_ELEMENT)) {
571                 parseConstructorArgElement((Element JavaDoc) node, bd);
572             }
573         }
574     }
575
576     /**
577      * Parse property sub-elements of the given bean element.
578      */

579     public void parsePropertyElements(Element JavaDoc beanEle, BeanDefinition bd) {
580         NodeList JavaDoc nl = beanEle.getChildNodes();
581         for (int i = 0; i < nl.getLength(); i++) {
582             Node JavaDoc node = nl.item(i);
583             if (node instanceof Element JavaDoc && DomUtils.nodeNameEquals(node, PROPERTY_ELEMENT)) {
584                 parsePropertyElement((Element JavaDoc) node, bd);
585             }
586         }
587     }
588
589     /**
590      * Parse lookup-override sub-elements of the given bean element.
591      */

592     public void parseLookupOverrideSubElements(Element JavaDoc beanEle, MethodOverrides overrides) {
593         NodeList JavaDoc nl = beanEle.getChildNodes();
594         for (int i = 0; i < nl.getLength(); i++) {
595             Node JavaDoc node = nl.item(i);
596             if (node instanceof Element JavaDoc && DomUtils.nodeNameEquals(node, LOOKUP_METHOD_ELEMENT)) {
597                 Element JavaDoc ele = (Element JavaDoc) node;
598                 String JavaDoc methodName = ele.getAttribute(NAME_ATTRIBUTE);
599                 String JavaDoc beanRef = ele.getAttribute(BEAN_ELEMENT);
600                 LookupOverride override = new LookupOverride(methodName, beanRef);
601                 override.setSource(extractSource(ele));
602                 overrides.addOverride(override);
603             }
604         }
605     }
606
607     /**
608      * Parse replaced-method sub-elements of the given bean element.
609      */

610     public void parseReplacedMethodSubElements(Element JavaDoc beanEle, MethodOverrides overrides) {
611         NodeList JavaDoc nl = beanEle.getChildNodes();
612         for (int i = 0; i < nl.getLength(); i++) {
613             Node JavaDoc node = nl.item(i);
614             if (node instanceof Element JavaDoc && DomUtils.nodeNameEquals(node, REPLACED_METHOD_ELEMENT)) {
615                 Element JavaDoc replacedMethodEle = (Element JavaDoc) node;
616                 String JavaDoc name = replacedMethodEle.getAttribute(NAME_ATTRIBUTE);
617                 String JavaDoc callback = replacedMethodEle.getAttribute(REPLACER_ATTRIBUTE);
618                 ReplaceOverride replaceOverride = new ReplaceOverride(name, callback);
619                 // Look for arg-type match elements.
620
List JavaDoc argTypeEles = DomUtils.getChildElementsByTagName(replacedMethodEle, ARG_TYPE_ELEMENT);
621                 for (Iterator JavaDoc it = argTypeEles.iterator(); it.hasNext();) {
622                     Element JavaDoc argTypeEle = (Element JavaDoc) it.next();
623                     replaceOverride.addTypeIdentifier(argTypeEle.getAttribute(ARG_TYPE_MATCH_ATTRIBUTE));
624                 }
625                 replaceOverride.setSource(extractSource(replacedMethodEle));
626                 overrides.addOverride(replaceOverride);
627             }
628         }
629     }
630
631     /**
632      * Parse a constructor-arg element.
633      */

634     public void parseConstructorArgElement(Element JavaDoc ele, BeanDefinition bd) {
635         String JavaDoc indexAttr = ele.getAttribute(INDEX_ATTRIBUTE);
636         String JavaDoc typeAttr = ele.getAttribute(TYPE_ATTRIBUTE);
637         if (StringUtils.hasLength(indexAttr)) {
638             try {
639                 int index = Integer.parseInt(indexAttr);
640                 if (index < 0) {
641                     error("'index' cannot be lower than 0", ele);
642                 }
643                 else {
644                     try {
645                         this.parseState.push(new ConstructorArgumentEntry(index));
646                         Object JavaDoc value = parsePropertyValue(ele, bd, null);
647                         ConstructorArgumentValues.ValueHolder valueHolder = new ConstructorArgumentValues.ValueHolder(value);
648                         if (StringUtils.hasLength(typeAttr)) {
649                             valueHolder.setType(typeAttr);
650                         }
651                         valueHolder.setSource(extractSource(ele));
652                         bd.getConstructorArgumentValues().addIndexedArgumentValue(index, valueHolder);
653                     }
654                     finally {
655                         this.parseState.pop();
656                     }
657                 }
658             }
659             catch (NumberFormatException JavaDoc ex) {
660                 error("Attribute 'index' of tag 'constructor-arg' must be an integer", ele);
661             }
662         }
663         else {
664             try {
665                 this.parseState.push(new ConstructorArgumentEntry());
666                 Object JavaDoc value = parsePropertyValue(ele, bd, null);
667                 ConstructorArgumentValues.ValueHolder valueHolder = new ConstructorArgumentValues.ValueHolder(value);
668                 if (StringUtils.hasLength(typeAttr)) {
669                     valueHolder.setType(typeAttr);
670                 }
671                 valueHolder.setSource(extractSource(ele));
672                 bd.getConstructorArgumentValues().addGenericArgumentValue(valueHolder);
673             }
674             finally {
675                 this.parseState.pop();
676             }
677         }
678     }
679
680     /**
681      * Parse a property element.
682      */

683     public void parsePropertyElement(Element JavaDoc ele, BeanDefinition bd) {
684         String JavaDoc propertyName = ele.getAttribute(NAME_ATTRIBUTE);
685         if (!StringUtils.hasLength(propertyName)) {
686             error("Tag 'property' must have a 'name' attribute", ele);
687             return;
688         }
689         this.parseState.push(new PropertyEntry(propertyName));
690         try {
691             if (bd.getPropertyValues().contains(propertyName)) {
692                 error("Multiple 'property' definitions for property '" + propertyName + "'", ele);
693                 return;
694             }
695             Object JavaDoc val = parsePropertyValue(ele, bd, propertyName);
696             PropertyValue pv = new PropertyValue(propertyName, val);
697             parseMetaElements(ele, pv);
698             pv.setSource(extractSource(ele));
699             bd.getPropertyValues().addPropertyValue(pv);
700         }
701         finally {
702             this.parseState.pop();
703         }
704     }
705
706     /**
707      * Get the value of a property element. May be a list etc.
708      * Also used for constructor arguments, "propertyName" being null in this case.
709      */

710     public Object JavaDoc parsePropertyValue(Element JavaDoc ele, BeanDefinition bd, String JavaDoc propertyName) {
711         String JavaDoc elementName = (propertyName != null) ?
712                         "<property> element for property '" + propertyName + "'" :
713                         "<constructor-arg> element";
714
715         // Should only have one child element: ref, value, list, etc.
716
NodeList JavaDoc nl = ele.getChildNodes();
717         Element JavaDoc subElement = null;
718         for (int i = 0; i < nl.getLength(); i++) {
719             if (nl.item(i) instanceof Element JavaDoc) {
720                 Element JavaDoc candidateEle = (Element JavaDoc) nl.item(i);
721                 if (DESCRIPTION_ELEMENT.equals(candidateEle.getTagName())) {
722                     // Keep going: we don't use this value for now.
723
}
724                 else {
725                     // Child element is what we're looking for.
726
if (subElement != null && !META_ELEMENT.equals(subElement.getTagName())) {
727                         error(elementName + " must not contain more than one sub-element", ele);
728                     }
729                     else {
730                         subElement = candidateEle;
731                     }
732                 }
733             }
734         }
735
736         boolean hasRefAttribute = ele.hasAttribute(REF_ATTRIBUTE);
737         boolean hasValueAttribute = ele.hasAttribute(VALUE_ATTRIBUTE);
738         if ((hasRefAttribute && hasValueAttribute) ||
739                 ((hasRefAttribute || hasValueAttribute)) && subElement != null) {
740             error(elementName +
741                     " is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element", ele);
742         }
743
744         if (hasRefAttribute) {
745             String JavaDoc refName = ele.getAttribute(REF_ATTRIBUTE);
746             if (!StringUtils.hasText(refName)) {
747                 error(elementName + " contains empty 'ref' attribute", ele);
748             }
749             RuntimeBeanReference ref = new RuntimeBeanReference(refName);
750             ref.setSource(extractSource(ele));
751             return ref;
752         }
753         else if (hasValueAttribute) {
754             TypedStringValue valueHolder = new TypedStringValue(ele.getAttribute(VALUE_ATTRIBUTE));
755             valueHolder.setSource(extractSource(ele));
756             return valueHolder;
757         }
758         else if (subElement != null) {
759             return parsePropertySubElement(subElement, bd);
760         }
761         else {
762             // Neither child element nor "ref" or "value" attribute found.
763
error(elementName + " must specify a ref or value", ele);
764             return null;
765         }
766     }
767
768     public Object JavaDoc parsePropertySubElement(Element JavaDoc ele, BeanDefinition bd) {
769         return parsePropertySubElement(ele, bd, null);
770     }
771
772     /**
773      * Parse a value, ref or collection sub-element of a property or
774      * constructor-arg element.
775      * @param ele subelement of property element; we don't know which yet
776      * @param defaultTypeClassName the default type (class name) for any
777      * <code>&lt;value&gt;</code> tag that might be created
778      */

779     public Object JavaDoc parsePropertySubElement(Element JavaDoc ele, BeanDefinition bd, String JavaDoc defaultTypeClassName) {
780         if (!isDefaultNamespace(ele.getNamespaceURI())) {
781             return parseNestedCustomElement(ele, bd);
782         }
783         else if (DomUtils.nodeNameEquals(ele, BEAN_ELEMENT)) {
784             BeanDefinitionHolder bdHolder = parseBeanDefinitionElement(ele, bd);
785             if (bdHolder != null) {
786                 bdHolder = decorateBeanDefinitionIfRequired(ele, bdHolder);
787             }
788             return bdHolder;
789         }
790         else if (DomUtils.nodeNameEquals(ele, REF_ELEMENT)) {
791             // A generic reference to any name of any bean.
792
String JavaDoc refName = ele.getAttribute(BEAN_REF_ATTRIBUTE);
793             boolean toParent = false;
794             if (!StringUtils.hasLength(refName)) {
795                 // A reference to the id of another bean in the same XML file.
796
refName = ele.getAttribute(LOCAL_REF_ATTRIBUTE);
797                 if (!StringUtils.hasLength(refName)) {
798                     // A reference to the id of another bean in a parent context.
799
refName = ele.getAttribute(PARENT_REF_ATTRIBUTE);
800                     toParent = true;
801                     if (!StringUtils.hasLength(refName)) {
802                         error("'bean', 'local' or 'parent' is required for <ref> element", ele);
803                         return null;
804                     }
805                 }
806             }
807             if (!StringUtils.hasText(refName)) {
808                 error("<ref> element contains empty target attribute", ele);
809                 return null;
810             }
811             RuntimeBeanReference ref = new RuntimeBeanReference(refName, toParent);
812             ref.setSource(extractSource(ele));
813             return ref;
814         }
815         else if (DomUtils.nodeNameEquals(ele, IDREF_ELEMENT)) {
816             // A generic reference to any name of any bean.
817
String JavaDoc refName = ele.getAttribute(BEAN_REF_ATTRIBUTE);
818             if (!StringUtils.hasLength(refName)) {
819                 // A reference to the id of another bean in the same XML file.
820
refName = ele.getAttribute(LOCAL_REF_ATTRIBUTE);
821                 if (!StringUtils.hasLength(refName)) {
822                     error("Either 'bean' or 'local' is required for <idref> element", ele);
823                     return null;
824                 }
825             }
826             if (!StringUtils.hasText(refName)) {
827                 error("<idref> element contains empty target attribute", ele);
828                 return null;
829             }
830             RuntimeBeanNameReference ref = new RuntimeBeanNameReference(refName);
831             ref.setSource(extractSource(ele));
832             return ref;
833         }
834         else if (DomUtils.nodeNameEquals(ele, VALUE_ELEMENT)) {
835             // It's a literal value.
836
String JavaDoc value = DomUtils.getTextValue(ele);
837             String JavaDoc typeClassName = ele.getAttribute(TYPE_ATTRIBUTE);
838             if (!StringUtils.hasText(typeClassName)) {
839                 typeClassName = defaultTypeClassName;
840             }
841             try {
842                 return buildTypedStringValue(value, typeClassName, ele);
843             }
844             catch (ClassNotFoundException JavaDoc ex) {
845                 error("Type class [" + typeClassName + "] not found for <value> element", ele, ex);
846                 return value;
847             }
848         }
849         else if (DomUtils.nodeNameEquals(ele, NULL_ELEMENT)) {
850             // It's a distinguished null value. Let's wrap it in a TypedStringValue
851
// object in order to preserve the source location.
852
TypedStringValue nullHolder = new TypedStringValue(null);
853             nullHolder.setSource(extractSource(ele));
854             return nullHolder;
855         }
856         else if (DomUtils.nodeNameEquals(ele, LIST_ELEMENT)) {
857             return parseListElement(ele, bd);
858         }
859         else if (DomUtils.nodeNameEquals(ele, SET_ELEMENT)) {
860             return parseSetElement(ele, bd);
861         }
862         else if (DomUtils.nodeNameEquals(ele, MAP_ELEMENT)) {
863             return parseMapElement(ele, bd);
864         }
865         else if (DomUtils.nodeNameEquals(ele, PROPS_ELEMENT)) {
866             return parsePropsElement(ele);
867         }
868         error("Unknown property sub-element: [" + ele.getTagName() + "]", ele);
869         return null;
870     }
871
872     private Object JavaDoc buildTypedStringValue(String JavaDoc value, String JavaDoc targetTypeName, Element JavaDoc ele)
873             throws ClassNotFoundException JavaDoc {
874
875         ClassLoader JavaDoc classLoader = this.readerContext.getReader().getBeanClassLoader();
876         TypedStringValue typedValue = null;
877         if (!StringUtils.hasText(targetTypeName)) {
878             typedValue = new TypedStringValue(value);
879         }
880         else if (classLoader != null) {
881             Class JavaDoc targetType = ClassUtils.forName(targetTypeName, classLoader);
882             typedValue = new TypedStringValue(value, targetType);
883         }
884         else {
885             typedValue = new TypedStringValue(value, targetTypeName);
886         }
887         typedValue.setSource(extractSource(ele));
888         return typedValue;
889     }
890
891     /**
892      * Parse a list element.
893      */

894     public List JavaDoc parseListElement(Element JavaDoc collectionEle, BeanDefinition bd) {
895         String JavaDoc defaultTypeClassName = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
896         NodeList JavaDoc nl = collectionEle.getChildNodes();
897         ManagedList list = new ManagedList(nl.getLength());
898         list.setSource(extractSource(collectionEle));
899         list.setMergeEnabled(parseMergeAttribute(collectionEle));
900         for (int i = 0; i < nl.getLength(); i++) {
901             if (nl.item(i) instanceof Element JavaDoc) {
902                 Element JavaDoc ele = (Element JavaDoc) nl.item(i);
903                 list.add(parsePropertySubElement(ele, bd, defaultTypeClassName));
904             }
905         }
906         return list;
907     }
908
909     /**
910      * Parse a set element.
911      */

912     public Set JavaDoc parseSetElement(Element JavaDoc collectionEle, BeanDefinition bd) {
913         String JavaDoc defaultTypeClassName = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
914         NodeList JavaDoc nl = collectionEle.getChildNodes();
915         ManagedSet set = new ManagedSet(nl.getLength());
916         set.setSource(extractSource(collectionEle));
917         set.setMergeEnabled(parseMergeAttribute(collectionEle));
918         for (int i = 0; i < nl.getLength(); i++) {
919             if (nl.item(i) instanceof Element JavaDoc) {
920                 Element JavaDoc ele = (Element JavaDoc) nl.item(i);
921                 set.add(parsePropertySubElement(ele, bd, defaultTypeClassName));
922             }
923         }
924         return set;
925     }
926
927     /**
928      * Parse a map element.
929      */

930     public Map JavaDoc parseMapElement(Element JavaDoc mapEle, BeanDefinition bd) {
931         String JavaDoc defaultKeyTypeClassName = mapEle.getAttribute(KEY_TYPE_ATTRIBUTE);
932         String JavaDoc defaultValueTypeClassName = mapEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
933
934         List JavaDoc entryEles = DomUtils.getChildElementsByTagName(mapEle, ENTRY_ELEMENT);
935         ManagedMap map = new ManagedMap(entryEles.size());
936         map.setMergeEnabled(parseMergeAttribute(mapEle));
937         map.setSource(extractSource(mapEle));
938
939         for (Iterator JavaDoc it = entryEles.iterator(); it.hasNext();) {
940             Element JavaDoc entryEle = (Element JavaDoc) it.next();
941             // Should only have one value child element: ref, value, list, etc.
942
// Optionally, there might be a key child element.
943
NodeList JavaDoc entrySubNodes = entryEle.getChildNodes();
944
945             Element JavaDoc keyEle = null;
946             Element JavaDoc valueEle = null;
947             for (int j = 0; j < entrySubNodes.getLength(); j++) {
948                 if (entrySubNodes.item(j) instanceof Element JavaDoc) {
949                     Element JavaDoc candidateEle = (Element JavaDoc) entrySubNodes.item(j);
950                     if (DomUtils.nodeNameEquals(candidateEle, KEY_ELEMENT)) {
951                         if (keyEle != null) {
952                             error("<entry> element is only allowed to contain one <key> sub-element", entryEle);
953                         }
954                         else {
955                             keyEle = candidateEle;
956                         }
957                     }
958                     else {
959                         // Child element is what we're looking for.
960
if (valueEle != null) {
961                             error("<entry> element must not contain more than one value sub-element", entryEle);
962                         }
963                         else {
964                             valueEle = candidateEle;
965                         }
966                     }
967                 }
968             }
969
970             // Extract key from attribute or sub-element.
971
Object JavaDoc key = null;
972             boolean hasKeyAttribute = entryEle.hasAttribute(KEY_ATTRIBUTE);
973             boolean hasKeyRefAttribute = entryEle.hasAttribute(KEY_REF_ATTRIBUTE);
974             if ((hasKeyAttribute && hasKeyRefAttribute) ||
975                     ((hasKeyAttribute || hasKeyRefAttribute)) && keyEle != null) {
976                 error("<entry> element is only allowed to contain either " +
977                                 "a 'key' attribute OR a 'key-ref' attribute OR a <key> sub-element", entryEle);
978             }
979             if (hasKeyAttribute) {
980                 key = buildTypedStringValueForMap(
981                         entryEle.getAttribute(KEY_ATTRIBUTE), defaultKeyTypeClassName, entryEle);
982             }
983             else if (hasKeyRefAttribute) {
984                 String JavaDoc refName = entryEle.getAttribute(KEY_REF_ATTRIBUTE);
985                 if (!StringUtils.hasText(refName)) {
986                     error("<entry> element contains empty 'key-ref' attribute", entryEle);
987                 }
988                 RuntimeBeanReference ref = new RuntimeBeanReference(refName);
989                 ref.setSource(extractSource(keyEle));
990                 key = ref;
991             }
992             else if (keyEle != null) {
993                 key = parseKeyElement(keyEle, bd, defaultKeyTypeClassName);
994             }
995             else {
996                 error("<entry> element must specify a key", entryEle);
997             }
998
999             // Extract value from attribute or sub-element.
1000
Object JavaDoc value = null;
1001            boolean hasValueAttribute = entryEle.hasAttribute(VALUE_ATTRIBUTE);
1002            boolean hasValueRefAttribute = entryEle.hasAttribute(VALUE_REF_ATTRIBUTE);
1003            if ((hasValueAttribute && hasValueRefAttribute) ||
1004                    ((hasValueAttribute || hasValueRefAttribute)) && valueEle != null) {
1005                error("<entry> element is only allowed to contain either " +
1006                                "'value' attribute OR 'value-ref' attribute OR <value> sub-element", entryEle);
1007            }
1008            if (hasValueAttribute) {
1009                value = buildTypedStringValueForMap(
1010                        entryEle.getAttribute(VALUE_ATTRIBUTE), defaultValueTypeClassName, entryEle);
1011            }
1012            else if (hasValueRefAttribute) {
1013                String JavaDoc refName = entryEle.getAttribute(VALUE_REF_ATTRIBUTE);
1014                if (!StringUtils.hasText(refName)) {
1015                    error("<entry> element contains empty 'value-ref' attribute", entryEle);
1016                }
1017                RuntimeBeanReference ref = new RuntimeBeanReference(refName);
1018                ref.setSource(extractSource(valueEle));
1019                value = ref;
1020            }
1021            else if (valueEle != null) {
1022                value = parsePropertySubElement(valueEle, bd, defaultValueTypeClassName);
1023            }
1024            else {
1025                error("<entry> element must specify a value", entryEle);
1026            }
1027
1028            // Add final key and value to the Map.
1029
map.put(key, value);
1030        }
1031
1032        return map;
1033    }
1034
1035    private Object JavaDoc buildTypedStringValueForMap(String JavaDoc value, String JavaDoc defaultTypeClassName, Element JavaDoc entryEle) {
1036        try {
1037            return buildTypedStringValue(value, defaultTypeClassName, entryEle);
1038        }
1039        catch (ClassNotFoundException JavaDoc ex) {
1040            error("Type class [" + defaultTypeClassName + "] not found for Map key/value type", entryEle, ex);
1041            return value;
1042        }
1043    }
1044
1045    /**
1046     * Parse a key sub-element of a map element.
1047     */

1048    public Object JavaDoc parseKeyElement(Element JavaDoc keyEle, BeanDefinition bd, String JavaDoc defaultKeyTypeClassName) {
1049        NodeList JavaDoc nl = keyEle.getChildNodes();
1050        Element JavaDoc subElement = null;
1051        for (int i = 0; i < nl.getLength(); i++) {
1052            if (nl.item(i) instanceof Element JavaDoc) {
1053                Element JavaDoc candidateEle = (Element JavaDoc) nl.item(i);
1054                // Child element is what we're looking for.
1055
if (subElement != null) {
1056                    error("<key> element must not contain more than one value sub-element", keyEle);
1057                }
1058                else {
1059                    subElement = candidateEle;
1060                }
1061            }
1062        }
1063        return parsePropertySubElement(subElement, bd, defaultKeyTypeClassName);
1064    }
1065
1066    /**
1067     * Parse a props element.
1068     */

1069    public Properties JavaDoc parsePropsElement(Element JavaDoc propsEle) {
1070        ManagedProperties props = new ManagedProperties();
1071        props.setSource(extractSource(propsEle));
1072        props.setMergeEnabled(parseMergeAttribute(propsEle));
1073
1074        List JavaDoc propEles = DomUtils.getChildElementsByTagName(propsEle, PROP_ELEMENT);
1075        for (Iterator JavaDoc it = propEles.iterator(); it.hasNext();) {
1076            Element JavaDoc propEle = (Element JavaDoc) it.next();
1077            String JavaDoc key = propEle.getAttribute(KEY_ATTRIBUTE);
1078            // Trim the text value to avoid unwanted whitespace
1079
// caused by typical XML formatting.
1080
String JavaDoc value = DomUtils.getTextValue(propEle).trim();
1081
1082            TypedStringValue keyHolder = new TypedStringValue(key);
1083            keyHolder.setSource(extractSource(propEle));
1084            TypedStringValue valueHolder = new TypedStringValue(value);
1085            valueHolder.setSource(extractSource(propEle));
1086            props.put(keyHolder, valueHolder);
1087        }
1088
1089        return props;
1090    }
1091
1092    /**
1093     * Parse the merge attribute of a collection element, if any.
1094     */

1095    public boolean parseMergeAttribute(Element JavaDoc collectionElement) {
1096        String JavaDoc value = collectionElement.getAttribute(MERGE_ATTRIBUTE);
1097        if (DEFAULT_VALUE.equals(value)) {
1098            value = this.defaults.getMerge();
1099        }
1100        return TRUE_VALUE.equals(value);
1101    }
1102
1103    public BeanDefinition parseCustomElement(Element JavaDoc ele) {
1104        return parseCustomElement(ele, null);
1105    }
1106
1107    public BeanDefinition parseCustomElement(Element JavaDoc ele, BeanDefinition containingBd) {
1108        String JavaDoc namespaceUri = ele.getNamespaceURI();
1109        NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
1110        if (handler == null) {
1111            error("Unable to locate NamespaceHandler for namespace [" + namespaceUri + "]", ele);
1112            return null;
1113        }
1114        return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
1115    }
1116
1117    public BeanDefinitionHolder decorateBeanDefinitionIfRequired(Element JavaDoc ele, BeanDefinitionHolder definitionHolder) {
1118        BeanDefinitionHolder finalDefinition = definitionHolder;
1119
1120        // Decorate based on custom attributes first.
1121
NamedNodeMap JavaDoc attributes = ele.getAttributes();
1122        for (int i = 0; i < attributes.getLength(); i++) {
1123            Node JavaDoc node = attributes.item(i);
1124            finalDefinition = decorateIfRequired(node, finalDefinition);
1125        }
1126
1127        // Decorate based on custom nested elements.
1128
NodeList JavaDoc children = ele.getChildNodes();
1129        for (int i = 0; i < children.getLength(); i++) {
1130            Node JavaDoc node = children.item(i);
1131            if (node.getNodeType() == Node.ELEMENT_NODE) {
1132                finalDefinition = decorateIfRequired(node, finalDefinition);
1133            }
1134        }
1135        return finalDefinition;
1136    }
1137
1138    private BeanDefinitionHolder decorateIfRequired(Node JavaDoc node, BeanDefinitionHolder finalDefinition) {
1139        String JavaDoc uri = node.getNamespaceURI();
1140        if (!isDefaultNamespace(uri)) {
1141            NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(uri);
1142            finalDefinition = handler.decorate(node, finalDefinition, new ParserContext(this.readerContext, this));
1143        }
1144        return finalDefinition;
1145    }
1146
1147    public boolean isDefaultNamespace(String JavaDoc namespaceUri) {
1148        return (!StringUtils.hasLength(namespaceUri) || BEANS_NAMESPACE_URI.equals(namespaceUri));
1149    }
1150
1151    private BeanDefinitionHolder parseNestedCustomElement(Element JavaDoc ele, BeanDefinition containingBd) {
1152        BeanDefinition innerDefinition = parseCustomElement(ele, containingBd);
1153        if (innerDefinition == null) {
1154            error("Incorrect usage of element '" + ele.getNodeName() + "' in a nested manner. " +
1155                    "This tag cannot be used nested inside <property>.", ele);
1156            return null;
1157        }
1158        String JavaDoc id = ele.getNodeName() + BeanDefinitionReaderUtils.GENERATED_BEAN_NAME_SEPARATOR +
1159                ObjectUtils.getIdentityHexString(innerDefinition);
1160        if (logger.isDebugEnabled()) {
1161            logger.debug("Using generated bean name [" + id +
1162                    "] for nested custom element '" + ele.getNodeName() + "'");
1163        }
1164        return new BeanDefinitionHolder(innerDefinition, id);
1165    }
1166
1167}
1168
Popular Tags