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.util;
019
020import java.nio.file.attribute.DosFileAttributes;
021import java.nio.file.attribute.FileTime;
022
023/**
024 * @author Philipp C. Heckel <philipp.heckel@gmail.com>
025 */
026public class LimitedDosFileAttributes implements DosFileAttributes {
027        private String dosAttributes;
028        
029        public LimitedDosFileAttributes(String dosAttributes) {
030                if (dosAttributes == null || dosAttributes.length() != 4) {
031                        throw new IllegalArgumentException("Given DOS attribute string is invalid: " + dosAttributes);
032                }
033                
034                this.dosAttributes = dosAttributes.toLowerCase();               
035        }
036        
037        @Override
038        public boolean isReadOnly() {
039                return dosAttributes.charAt(0) == 'r';
040        }
041
042        @Override
043        public boolean isHidden() {
044                return dosAttributes.charAt(1) == 'h';
045        }
046
047        @Override
048        public boolean isArchive() {
049                return dosAttributes.charAt(2) == 'a';
050        }
051
052        @Override
053        public boolean isSystem() {
054                return dosAttributes.charAt(3) == 's';
055        }
056        
057        @Override 
058        public String toString() {
059                return toString(this);
060        }
061        
062        public static String toString(DosFileAttributes dosFileAttributes) {
063                StringBuilder sb = new StringBuilder();
064
065                sb.append(dosFileAttributes.isReadOnly() ? "r" : "-");
066                sb.append(dosFileAttributes.isHidden() ? "h" : "-");
067                sb.append(dosFileAttributes.isArchive() ? "a" : "-");
068                sb.append(dosFileAttributes.isSystem() ? "s" : "-");
069
070                return sb.toString();           
071        }
072
073        @Override
074        public long size() {
075                throw new RuntimeException("Not supported.");
076        }
077
078        @Override
079        public FileTime lastModifiedTime() {
080                throw new RuntimeException("Not supported.");
081        }
082
083        @Override
084        public FileTime lastAccessTime() {
085                throw new RuntimeException("Not supported.");
086        }
087
088        @Override
089        public boolean isSymbolicLink() {
090                throw new RuntimeException("Not supported.");
091        }
092
093        @Override
094        public boolean isRegularFile() {
095                throw new RuntimeException("Not supported.");
096        }
097
098        @Override
099        public boolean isOther() {
100                throw new RuntimeException("Not supported.");
101        }
102
103        @Override
104        public boolean isDirectory() {
105                throw new RuntimeException("Not supported.");
106        }
107
108        @Override
109        public Object fileKey() {
110                throw new RuntimeException("Not supported.");
111        }
112
113        @Override
114        public FileTime creationTime() {
115                throw new RuntimeException("Not supported.");
116        }
117}