View Javadoc

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