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; 019 020import java.io.File; 021import java.lang.reflect.Field; 022import java.lang.reflect.Type; 023 024import org.syncany.util.ReflectionUtil; 025 026/** 027 * A plugin option represents a single setting of a transfer plugin 028 * within the corresponding {@link TransferSettings} class. A plugin option 029 * is created during the initialization from the {@link Setup} annotation 030 * to aid the guided repository setup (init and connect). 031 * 032 * @author Christian Roth (christian.roth@port17.de) 033 */ 034public class TransferPluginOption { 035 public enum ValidationResult { 036 VALID, INVALID_TYPE, INVALID_NOT_SET 037 } 038 039 private final Field field; 040 private final String name; 041 private final String description; 042 private final Type type; 043 private final FileType fileType; 044 private final boolean encrypted; 045 private final boolean sensitive; 046 private final boolean singular; 047 private final boolean visible; 048 private final boolean required; 049 private final Class<? extends TransferPluginOptionCallback> callback; 050 private final Class<? extends TransferPluginOptionConverter> converter; 051 052 public TransferPluginOption(Field field, String name, String description, Type type, FileType fileType, boolean encrypted, boolean sensitive, 053 boolean singular, boolean visible, boolean required, Class<? extends TransferPluginOptionCallback> callback, 054 Class<? extends TransferPluginOptionConverter> converter) { 055 056 this.field = field; 057 this.name = name; 058 this.description = description; 059 this.type = type; 060 this.fileType = fileType; 061 this.encrypted = encrypted; 062 this.sensitive = sensitive; 063 this.singular = singular; 064 this.visible = visible; 065 this.required = required; 066 this.callback = callback; 067 this.converter = converter; 068 } 069 070 public Field getField() { 071 return field; 072 } 073 074 public String getName() { 075 return name; 076 } 077 078 public String getDescription() { 079 return description; 080 } 081 082 public Type getType() { 083 return type; 084 } 085 086 public FileType getFileType() { 087 return fileType; 088 } 089 090 public boolean isEncrypted() { 091 return encrypted; 092 } 093 094 public boolean isSensitive() { 095 return sensitive; 096 } 097 098 public boolean isSingular() { 099 return singular; 100 } 101 102 public boolean isVisible() { 103 return visible; 104 } 105 106 public boolean isRequired() { 107 return required; 108 } 109 110 public Class<? extends TransferPluginOptionCallback> getCallback() { 111 return callback; 112 } 113 114 public Class<? extends TransferPluginOptionConverter> getConverter() { 115 return converter; 116 } 117 118 public ValidationResult isValid(String value) { 119 if (!validateInputMandatory(value)) { 120 return ValidationResult.INVALID_NOT_SET; 121 } 122 123 if (!validateInputType(value)) { 124 return ValidationResult.INVALID_TYPE; 125 } 126 127 return ValidationResult.VALID; 128 } 129 130 private boolean validateInputMandatory(String value) { 131 return !isRequired() || (value != null && !value.equals("")); 132 } 133 134 private boolean validateInputType(String value) { 135 if (type == String.class) { 136 return true; 137 } 138 else if (type == Integer.TYPE) { 139 try { 140 Integer.toString(Integer.parseInt(value)); 141 return true; 142 } 143 catch (NumberFormatException e) { 144 return false; 145 } 146 } 147 else if (type == Boolean.TYPE) { 148 return true; 149 } 150 else if (ReflectionUtil.getClassFromType(type).isEnum()) { 151 return ReflectionUtil.isValidEnum(value.toUpperCase(), ReflectionUtil.getClassFromType(type)); 152 } 153 else if (type == File.class) { 154 if (isRequired()) { 155 if (value != null) { 156 return true; 157 } 158 return false; 159 } 160 else { 161 return true; 162 } 163 } 164 else { 165 throw new RuntimeException("Unknown type: " + type); 166 } 167 } 168}