001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.file.strategy;
018    
019    import java.io.File;
020    
021    /**
022     * @version $Revision: 640438 $
023     */
024    public class DefaultFileRenamer implements FileRenamer {
025    
026        private static final boolean ON_WINDOWS = System.getProperty("os.name").startsWith("Windows");
027    
028        private String namePrefix;
029        private String namePostfix;
030    
031        public DefaultFileRenamer() {
032        }
033    
034        public DefaultFileRenamer(String namePrefix, String namePostfix) {
035            this.namePrefix = namePrefix;
036            this.namePostfix = namePostfix;
037        }
038    
039        public File renameFile(File file) {
040            File parent = file.getParentFile();
041            String name = renameFileName(file);
042    
043            if (ON_WINDOWS && (name.indexOf(":") >= 0 || name.startsWith("//"))) {
044                return new File(name);
045            }
046            return new File(parent, name);
047        }
048    
049        public String getNamePostfix() {
050            return namePostfix;
051        }
052    
053        /**
054         * Sets the name postfix appended to moved files. For example
055         * to rename all the files from * to *.done set this value to ".done"
056         */
057        public void setNamePostfix(String namePostfix) {
058            this.namePostfix = namePostfix;
059        }
060    
061        public String getNamePrefix() {
062            return namePrefix;
063        }
064    
065        /**
066         * Sets the name prefix appended to moved files. For example
067         * to move processed files into a hidden directory called ".camel"
068         * set this value to ".camel/"
069         */
070        public void setNamePrefix(String namePrefix) {
071            this.namePrefix = namePrefix;
072        }
073    
074    
075        protected String renameFileName(File file) {
076            StringBuffer buffer = new StringBuffer();
077            if (namePrefix != null) {
078                buffer.append(namePrefix);
079            }
080            buffer.append(file.getName());
081            if (namePostfix != null) {
082                buffer.append(namePostfix);
083            }
084            return buffer.toString();
085        }
086    }