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.specific;
8   
9   import org.mule.api.routing.filter.Filter;
10  import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser;
11  import org.mule.config.spring.parsers.delegate.ParentContextDefinitionParser;
12  import org.mule.config.spring.parsers.generic.MuleOrphanDefinitionParser;
13  import org.mule.config.spring.parsers.generic.OrphanDefinitionParser;
14  import org.mule.config.spring.parsers.generic.WrappingChildDefinitionParser;
15  import org.mule.routing.MessageFilter;
16  
17  import org.w3c.dom.Element;
18  
19  /**
20   * This allows a filter to be defined globally, or embedded within an endpoint. IF
21   * required the filter is wrapped in MessageFilter instance before being injected
22   * into the parent.
23   */
24  public class FilterDefinitionParser extends ParentContextDefinitionParser
25      implements WrappingChildDefinitionParser.WrappingController
26  {
27  
28      public static final String FILTER = "filter";
29      public static final String ATTRIBUTE_NAME = AbstractMuleBeanDefinitionParser.ATTRIBUTE_NAME;
30  
31      public FilterDefinitionParser(Class filter)
32      {
33          super(MuleOrphanDefinitionParser.ROOT_ELEMENT, new OrphanDefinitionParser(filter, false));
34          otherwise(new WrappingChildDefinitionParser("messageProcessor", filter, Filter.class, false,
35              MessageFilter.class, FILTER, FILTER, this));
36          addIgnored(ATTRIBUTE_NAME);
37      }
38  
39      public FilterDefinitionParser()
40      {
41          super(MuleOrphanDefinitionParser.ROOT_ELEMENT, new OrphanDefinitionParser(false));
42          otherwise(new WrappingChildDefinitionParser("messageProcessor", null, Filter.class, true,
43              MessageFilter.class, FILTER, FILTER, this));
44          addIgnored(ATTRIBUTE_NAME);
45      }
46  
47      public boolean shouldWrap(Element e)
48      {
49          String parentName = e.getParentNode().getLocalName().toLowerCase();
50          String grandParentName = e.getParentNode().getParentNode().getLocalName().toLowerCase();
51  
52          return !("message-filter".equals(parentName) || "and-filter".equals(parentName)
53                   || "or-filter".equals(parentName) || "not-filter".equals(parentName)
54                   || "outbound".equals(grandParentName) || "selective-consumer-router".equals(parentName)
55                   || "error-filter".equals(parentName) || "wire-tap".equals(parentName)
56                   || "wire-tap-router".equals(parentName) || "when".equals(parentName));
57      }
58  }