View Javadoc

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