01 /*
02 * DuplicateBeanDefinitionParser.java
03 *
04 * Copyright (c) 1995-2010, The University of Sheffield. See the file
05 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
06 *
07 * This file is part of GATE (see http://gate.ac.uk/), and is free
08 * software, licenced under the GNU Library General Public License,
09 * Version 2, June 1991 (in the distribution as file licence.html,
10 * and also available at http://gate.ac.uk/gate/licence.html).
11 *
12 * Ian Roberts, 10/Apr/2010
13 *
14 * $Id: DuplicateBeanDefinitionParser.java 12496 2010-04-15 16:20:57Z ian_roberts $
15 */
16 package gate.util.spring.xml;
17
18 import gate.util.spring.DuplicateResourceFactoryBean;
19
20 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
21 import org.springframework.beans.factory.xml.ParserContext;
22 import org.w3c.dom.Element;
23 import org.w3c.dom.Node;
24 import org.w3c.dom.NodeList;
25
26 public class DuplicateBeanDefinitionParser
27 extends
28 CustomisableBeanDefinitionParser {
29 @Override
30 protected void doParse(Element element, ParserContext parserContext,
31 BeanDefinitionBuilder builder) {
32 if(element.hasAttribute("scope")) {
33 if("prototype".equals(element.getAttribute("scope"))) {
34 parserContext.getReaderContext().warning(
35 "Prototype scope is not recommended for <gate:duplicate>", element);
36 }
37 builder.setScope(element.getAttribute("scope"));
38 }
39
40 if(element.hasAttribute("return-template")) {
41 builder.addPropertyValue("returnTemplate",
42 Boolean.valueOf(element.getAttribute("return-template")));
43 }
44
45 if(element.hasAttribute("template-ref")) {
46 // target-ref takes precedence
47 builder.addPropertyReference("template", element.getAttribute("template-ref"));
48 }
49 else {
50 // try and find a sub-element other than <gate:customisers>
51 Element targetElt = null;
52 NodeList children = element.getChildNodes();
53 for(int i = 0; i < children.getLength(); i++) {
54 Node n = children.item(i);
55 if(n instanceof Element &&
56 !("http://gate.ac.uk/ns/spring".equals(n.getNamespaceURI())
57 && "customisers".equals(n.getLocalName()))) {
58 if(targetElt != null) {
59 parserContext.getReaderContext().error(
60 "<gate:duplicate> element can only have one sub element " +
61 "(apart from customisers, source)", element);
62 }
63 targetElt = (Element)n;
64 }
65 }
66 if(targetElt != null) {
67 builder.addPropertyValue("template", parserContext.getDelegate()
68 .parsePropertySubElement(targetElt, builder.getRawBeanDefinition()));
69 }
70 }
71 // handle the customisers
72 extractCustomisers(element, parserContext, builder);
73 }
74
75 @Override
76 protected Class getBeanClass(Element element) {
77 return DuplicateResourceFactoryBean.class;
78 }
79
80 }
|