From 8ea24913b5cabae06ff067c36d0d90f59ca707d7 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 26 Sep 2008 20:47:42 +0000 Subject: [PATCH] Added SimpleHeaderEnricherParser. --- .../config/SimpleHeaderEnricherParser.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 org.springframework.integration/src/main/java/org/springframework/integration/transformer/config/SimpleHeaderEnricherParser.java diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/transformer/config/SimpleHeaderEnricherParser.java b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/config/SimpleHeaderEnricherParser.java new file mode 100644 index 0000000000..1b066df6d6 --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/config/SimpleHeaderEnricherParser.java @@ -0,0 +1,85 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.transformer.config; + +import java.util.HashMap; +import java.util.Map; + +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.transformer.HeaderEnricher; +import org.springframework.integration.transformer.Transformer; +import org.springframework.util.ObjectUtils; + +/** + * @author Mark Fisher + */ +public class SimpleHeaderEnricherParser extends AbstractTransformerParser { + + private static String[] ineligibleHeaderNames = new String[] { + "id", "input-channel", "output-channel", "overwrite" + }; + + + private final String prefix; + + + public SimpleHeaderEnricherParser() { + this(null); + } + + public SimpleHeaderEnricherParser(String prefix) { + this.prefix = prefix; + } + + + @Override + protected Class getTransformerClass() { + return HeaderEnricher.class; + } + + protected boolean isEligibleHeaderName(String headerName) { + return !(ObjectUtils.containsElement(ineligibleHeaderNames, headerName)); + } + + protected boolean shouldOverwrite(Element element) { + return "true".equals(element.getAttribute("overwrite").toLowerCase()); + } + + @Override + protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + Map headersToAdd = new HashMap(); + NamedNodeMap nodeMap = element.getAttributes(); + for (int i = 0; i < nodeMap.getLength(); i++) { + Node node = nodeMap.item(i); + String name = node.getNodeName(); + if (this.isEligibleHeaderName(name)) { + headersToAdd.put(name, node.getNodeValue()); + } + } + builder.addConstructorArgValue(headersToAdd); + builder.addPropertyValue("overwrite", this.shouldOverwrite(element)); + if (this.prefix != null) { + builder.addPropertyValue("prefix", this.prefix); + } + } + +}