001/*
002 * Syncany, www.syncany.org
003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package org.syncany.plugins.transfer.files;
019
020import java.util.regex.Matcher;
021import java.util.regex.Pattern;
022
023import org.syncany.plugins.transfer.RemoteTransaction;
024import org.syncany.plugins.transfer.StorageException;
025
026/**
027 * The transaction file represents a manifest of a transaction on the remote storage. 
028 * 
029 * <p><b>Name pattern:</b> The name pattern of a transaction file is
030 * <b>transaction-&lt;filehexhashcode&gt;</b>.
031 * 
032 * @author Pim Otte
033 */
034public class TransactionRemoteFile extends RemoteFile {
035        private static final Pattern NAME_PATTERN = Pattern.compile("transaction-([a-f0-9]+)");
036        private static final String NAME_FORMAT = "transaction-%s";
037
038        /**
039         * Initializes a new transaction file, given a name. 
040         * 
041         * @param name transaction file name; <b>must</b> always match the {@link #NAME_PATTERN} 
042         * @throws StorageException If the name is not match the name pattern
043         */
044        public TransactionRemoteFile(String name) throws StorageException {
045                super(name);
046        }
047
048        /**
049         * Initializes a new transaction file, given the transaction itself.
050         * 
051         * @param remoteTransaction the remoteTransaction for which a file is needed
052         * @throws StorageException If the name is not match the name pattern
053         */
054        public TransactionRemoteFile(RemoteTransaction remoteTransaction) throws StorageException {
055                super(String.format(NAME_FORMAT, Integer.toHexString(remoteTransaction.hashCode())));
056        }
057
058        @Override
059        protected String validateName(String name) throws StorageException {
060                Matcher matcher = NAME_PATTERN.matcher(name);
061
062                if (!matcher.matches()) {
063                        throw new StorageException(name + ": remote filename pattern does not match: " + NAME_PATTERN.pattern() + " expected.");
064                }
065
066                return name;
067        }
068}