View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config.spring.parsers.delegate;
8   
9   import org.mule.config.spring.parsers.MuleDefinitionParser;
10  
11  import org.springframework.beans.factory.xml.ParserContext;
12  import org.w3c.dom.Element;
13  
14  public class BooleanAttributeSelectionDefinitionParser extends AbstractParallelDelegatingDefinitionParser
15  {
16  
17      private String attribute;
18      private boolean dflt;
19      private MuleDefinitionParser whenTrue;
20      private MuleDefinitionParser whenFalse;
21  
22      public BooleanAttributeSelectionDefinitionParser(String attribute, boolean dflt, MuleDefinitionParser whenTrue, MuleDefinitionParser whenFalse)
23      {
24          super(new MuleDefinitionParser[]{whenTrue, whenFalse});
25          this.attribute = attribute;
26          this.dflt = dflt;
27          this.whenTrue = whenTrue;
28          this.whenFalse = whenFalse;
29          addIgnored(attribute);
30      }
31  
32      protected MuleDefinitionParser getDelegate(Element element, ParserContext parserContext)
33      {
34          boolean value = dflt;
35          if (null != element && element.hasAttribute(attribute))
36          {
37              value = Boolean.valueOf(element.getAttribute(attribute)).booleanValue();
38          }
39          if (value)
40          {
41              return whenTrue;
42          }
43          else
44          {
45              return whenFalse;
46          }
47      }
48  
49  }