[15024] | 1 | /*
|
---|
| 2 | Launch4j (http://launch4j.sourceforge.net/)
|
---|
| 3 | Cross-platform Java application wrapper for creating Windows native executables.
|
---|
| 4 |
|
---|
| 5 | Copyright (C) 2004, 2006 Grzegorz Kowal
|
---|
| 6 |
|
---|
| 7 | This program is free software; you can redistribute it and/or modify
|
---|
| 8 | it under the terms of the GNU General Public License as published by
|
---|
| 9 | the Free Software Foundation; either version 2 of the License, or
|
---|
| 10 | (at your option) any later version.
|
---|
| 11 |
|
---|
| 12 | This program is distributed in the hope that it will be useful,
|
---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | GNU General Public License for more details.
|
---|
| 16 |
|
---|
| 17 | You should have received a copy of the GNU General Public License
|
---|
| 18 | along with this program; if not, write to the Free Software
|
---|
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | /*
|
---|
| 23 | * Created on 2004-01-30
|
---|
| 24 | */
|
---|
| 25 | package net.sf.launch4j.binding;
|
---|
| 26 |
|
---|
| 27 | import java.io.File;
|
---|
| 28 | import java.util.Collection;
|
---|
| 29 | import java.util.HashSet;
|
---|
| 30 |
|
---|
| 31 | import net.sf.launch4j.Util;
|
---|
| 32 | import net.sf.launch4j.config.ConfigPersister;
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * @author Copyright (C) 2004 Grzegorz Kowal
|
---|
| 36 | */
|
---|
| 37 | public class Validator {
|
---|
| 38 | public static final String ALPHANUMERIC_PATTERN = "[\\w]*?"; //$NON-NLS-1$
|
---|
| 39 | public static final String ALPHA_PATTERN = "[\\w&&\\D]*?"; //$NON-NLS-1$
|
---|
| 40 | public static final String NUMERIC_PATTERN = "[\\d]*?"; //$NON-NLS-1$
|
---|
| 41 | public static final String PATH_PATTERN = "[\\w|[ .,:\\-/\\\\]]*?"; //$NON-NLS-1$
|
---|
| 42 |
|
---|
| 43 | public static final int MAX_PATH = 260;
|
---|
| 44 |
|
---|
| 45 | private Validator() {}
|
---|
| 46 |
|
---|
| 47 | public static boolean isEmpty(String s) {
|
---|
| 48 | return s == null || s.equals(""); //$NON-NLS-1$
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public static void checkNotNull(Object o, String property, String name) {
|
---|
| 52 | if (o == null) {
|
---|
| 53 | signalViolation(property,
|
---|
| 54 | Messages.getString("Validator.empty.field", name)); //$NON-NLS-1$
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public static void checkString(String s, int maxLength, String property, String name) {
|
---|
| 59 | if (s == null || s.length() == 0) {
|
---|
| 60 | signalViolation(property,
|
---|
| 61 | Messages.getString("Validator.empty.field", name)); //$NON-NLS-1$
|
---|
| 62 | }
|
---|
| 63 | if (s.length() > maxLength) {
|
---|
| 64 | signalLengthViolation(property, name, maxLength);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public static void checkString(String s, int maxLength, String pattern,
|
---|
| 69 | String property, String name) {
|
---|
| 70 | checkString(s, maxLength, property, name);
|
---|
| 71 | if (!s.matches(pattern)) {
|
---|
| 72 | signalViolation(property,
|
---|
| 73 | Messages.getString("Validator.invalid.data", name)); //$NON-NLS-1$
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public static void checkOptString(String s, int maxLength, String property, String name) {
|
---|
| 78 | if (s == null || s.length() == 0) {
|
---|
| 79 | return;
|
---|
| 80 | }
|
---|
| 81 | if (s.length() > maxLength) {
|
---|
| 82 | signalLengthViolation(property, name, maxLength);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | public static void checkOptString(String s, int maxLength, String pattern,
|
---|
| 87 | String property, String name) {
|
---|
| 88 | if (s == null || s.length() == 0) {
|
---|
| 89 | return;
|
---|
| 90 | }
|
---|
| 91 | if (s.length() > maxLength) {
|
---|
| 92 | signalLengthViolation(property, name, maxLength);
|
---|
| 93 | }
|
---|
| 94 | if (!s.matches(pattern)) {
|
---|
| 95 | signalViolation(property,
|
---|
| 96 | Messages.getString("Validator.invalid.data", name)); //$NON-NLS-1$
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public static void checkRange(int value, int min, int max,
|
---|
| 101 | String property, String name) {
|
---|
| 102 | if (value < min || value > max) {
|
---|
| 103 | signalViolation(property,
|
---|
| 104 | Messages.getString("ValidatocheckFiler.must.be.in.range", //$NON-NLS-1$
|
---|
| 105 | name, String.valueOf(min), String.valueOf(max)));
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public static void checkRange(char value, char min, char max,
|
---|
| 110 | String property, String name) {
|
---|
| 111 | if (value < min || value > max) {
|
---|
| 112 | signalViolation(property,
|
---|
| 113 | Messages.getString("Validator.must.be.in.range", //$NON-NLS-1$
|
---|
| 114 | name, String.valueOf(min), String.valueOf(max)));
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | public static void checkMin(int value, int min, String property, String name) {
|
---|
| 119 | if (value < min) {
|
---|
| 120 | signalViolation(property,
|
---|
| 121 | Messages.getString("Validator.must.be.at.least", //$NON-NLS-1$
|
---|
| 122 | name, String.valueOf(min)));
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | public static void checkTrue(boolean condition, String property, String msg) {
|
---|
| 127 | if (!condition) {
|
---|
| 128 | signalViolation(property, msg);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public static void checkFalse(boolean condition, String property, String msg) {
|
---|
| 133 | if (condition) {
|
---|
| 134 | signalViolation(property, msg);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | public static void checkElementsNotNullUnique(Collection c, String property, String msg) {
|
---|
| 139 | if (c.contains(null)
|
---|
| 140 | || new HashSet(c).size() != c.size()) {
|
---|
| 141 | signalViolation(property,
|
---|
| 142 | Messages.getString("Validator.already.exists", msg)); //$NON-NLS-1$
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | public static void checkElementsUnique(Collection c, String property, String msg) {
|
---|
| 147 | if (new HashSet(c).size() != c.size()) {
|
---|
| 148 | signalViolation(property,
|
---|
| 149 | Messages.getString("Validator.already.exists", msg)); //$NON-NLS-1$
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | public static void checkFile(File f, String property, String fileDescription) {
|
---|
| 154 | File cfgPath = ConfigPersister.getInstance().getConfigPath();
|
---|
| 155 | if (f == null || (!f.exists() && !Util.getAbsoluteFile(cfgPath, f).exists())) {
|
---|
| 156 | signalViolation(property,
|
---|
| 157 | Messages.getString("Validator.doesnt.exist", fileDescription)); //$NON-NLS-1$
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | public static void checkOptFile(File f, String property, String fileDescription) {
|
---|
| 162 | if (f != null && f.getPath().length() > 0) {
|
---|
| 163 | checkFile(f, property, fileDescription);
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | public static void checkRelativeWinPath(String path, String property, String msg) {
|
---|
| 168 | if (path == null
|
---|
| 169 | || path.equals("")
|
---|
| 170 | || path.startsWith("/") //$NON-NLS-1$
|
---|
| 171 | || path.startsWith("\\") //$NON-NLS-1$
|
---|
| 172 | || path.indexOf(':') != -1) {
|
---|
| 173 | signalViolation(property, msg);
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | public static void signalLengthViolation(String property, String name, int maxLength) {
|
---|
| 178 | signalViolation(property,
|
---|
| 179 | Messages.getString("Validator.exceeds.max.length", //$NON-NLS-1$
|
---|
| 180 | name, String.valueOf(maxLength)));
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | public static void signalViolation(String property, String msg) {
|
---|
| 184 | throw new InvariantViolationException(property, msg);
|
---|
| 185 | }
|
---|
| 186 | }
|
---|