01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.components.util;
18:
19: import javax.jbi.messaging.MessageExchange;
20: import javax.jbi.messaging.MessagingException;
21: import javax.jbi.messaging.NormalizedMessage;
22:
23: import org.apache.servicemix.expression.PropertyExpression;
24:
25: /**
26: * Expression that returns the 'org.apache.servicemix.file.name' property on the
27: * message added by a file extensions. Existing file extensions are by default
28: * removed
29: *
30: * @author Mayrbaeurl
31: * @since 3.2
32: */
33: public class FileExtensionPropertyExpression extends PropertyExpression {
34:
35: private final String extension;
36:
37: private boolean deleteExistingExtension = true;
38:
39: public FileExtensionPropertyExpression(String fileExtension) {
40: super (DefaultFileMarshaler.FILE_NAME_PROPERTY);
41:
42: this .extension = fileExtension;
43: }
44:
45: public FileExtensionPropertyExpression(String extension,
46: boolean deleteExistingExtension) {
47: super (DefaultFileMarshaler.FILE_NAME_PROPERTY);
48:
49: this .extension = extension;
50: this .deleteExistingExtension = deleteExistingExtension;
51: }
52:
53: // Implementation methods
54: // -------------------------------------------------------------------------
55: public Object evaluate(MessageExchange exchange,
56: NormalizedMessage message) throws MessagingException {
57: Object result = super .evaluate(exchange, message);
58: if ((result != null) && (result instanceof String)) {
59: return this .removeExtension((String) result)
60: + this .extension;
61: } else {
62: return result;
63: }
64: }
65:
66: private String removeExtension(String fileName) {
67: String result = fileName;
68: if (this .deleteExistingExtension && fileName != null
69: && fileName.length() > 1) {
70: int index = fileName.lastIndexOf('.');
71: if (index != -1) {
72: result = fileName.substring(0, index);
73: }
74: }
75: return result;
76: }
77: }
|