Added namespace support for FileToStringTransformer and FileToByteArrayTransformer.

This commit is contained in:
Mark Fisher
2008-09-26 18:27:22 +00:00
parent 959b906f4e
commit f2b7353165
12 changed files with 173 additions and 27 deletions

View File

@@ -0,0 +1,45 @@
/*
* 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.file.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.transformer.config.AbstractTransformerParser;
/**
* Base class for File payload transformer parsers.
*
* @author Mark Fisher
*/
public abstract class AbstractFilePayloadTransformerParser extends AbstractTransformerParser {
@Override
protected final void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
boolean deleteFiles = "true".equals(element.getAttribute("delete-files").toLowerCase());
builder.addPropertyValue("deleteFiles", deleteFiles);
this.postProcessTransformer(element, parserContext, builder);
}
/**
* Subclasses may override this method to provide additional configuration.
*/
protected void postProcessTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
}
}

View File

@@ -22,12 +22,15 @@ import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
* Namespace handler for Spring Integration's 'file' namespace.
*
* @author Iwein Fuld
* @author Mark Fisher
*/
public class FileNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("inbound-channel-adapter", new FileInboundChannelAdapterParser());
registerBeanDefinitionParser("outbound-channel-adapter", new FileOutboundChannelAdapterParser());
registerBeanDefinitionParser("file-to-string-transformer", new FileToStringTransformerParser());
registerBeanDefinitionParser("file-to-bytes-transformer", new FileToByteArrayTransformerParser());
}
}

View File

@@ -0,0 +1,34 @@
/*
* 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.file.config;
import org.springframework.integration.file.transformer.FileToByteArrayTransformer;
import org.springframework.integration.transformer.Transformer;
/**
* Parser for the <file-to-bytes-transformer> element.
*
* @author Mark Fisher
*/
public class FileToByteArrayTransformerParser extends AbstractFilePayloadTransformerParser {
@Override
protected Class<? extends Transformer> getTransformerClass() {
return FileToByteArrayTransformer.class;
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.file.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.IntegrationNamespaceUtils;
import org.springframework.integration.file.transformer.FileToStringTransformer;
import org.springframework.integration.transformer.Transformer;
/**
* Parser for the &lt;file-to-string-transformer&gt; element.
*
* @author Mark Fisher
*/
public class FileToStringTransformerParser extends AbstractFilePayloadTransformerParser {
@Override
protected Class<? extends Transformer> getTransformerClass() {
return FileToStringTransformer.class;
}
@Override
protected void postProcessTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "charset");
}
}

View File

@@ -53,4 +53,39 @@
</xsd:complexType>
</xsd:element>
<xsd:element name="file-to-string-transformer">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Creates a Transformer that converts a File payload to a String.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="transformerType">
<xsd:attribute name="charset" type="xsd:string"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="file-to-bytes-transformer">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Creates a Transformer that converts a File payload to an array of bytes.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="transformerType"/>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="transformerType">
<xsd:attribute name="id" type="xsd:string"/>
<xsd:attribute name="input-channel" type="xsd:string" use="required"/>
<xsd:attribute name="output-channel" type="xsd:string" use="required"/>
<xsd:attribute name="delete-files" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>

View File

@@ -37,14 +37,15 @@ public abstract class AbstractFilePayloadTransformer<T> implements Transformer {
private final Log logger = LogFactory.getLog(this.getClass());
private volatile boolean deleteFileAfterTransformation;
private volatile boolean deleteFiles;
/**
* Specify whether to delete the File after transformation.
* Default is <em>false</em>.
*/
public void setDeleteFileAfterTransformation(boolean deleteFileAfterTransformation) {
this.deleteFileAfterTransformation = deleteFileAfterTransformation;
public void setDeleteFiles(boolean deleteFiles) {
this.deleteFiles = deleteFiles;
}
public final Message<?> transform(Message<?> message) {
@@ -59,7 +60,7 @@ public abstract class AbstractFilePayloadTransformer<T> implements Transformer {
.copyHeaders(message.getHeaders())
.setHeaderIfAbsent(FileNameGenerator.FILENAME_PROPERTY_KEY, file.getName())
.build();
if (this.deleteFileAfterTransformation) {
if (this.deleteFiles) {
if (!file.delete() && this.logger.isWarnEnabled()) {
this.logger.warn("failed to delete File '" + file + "'");
}