import java.util.regex.Pattern; import javax.swing.JOptionPane; import java.io.File; public class pmValidData { public static String removeAllHTMLTags (String str) { String strReturn = ""; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (!(c == '<' || c == '>')) { strReturn += c; } } return strReturn; } public static String getFileExt (String strFile) { int lastDot = strFile.lastIndexOf('.'); if (lastDot != -1) { return strFile.substring(lastDot + 1); } return ""; } public static int getValidAge (String strAge, int youngest, int oldest) { try { int age = Integer.parseInt(strAge); //If youngest/oldest has been specified (not -1) then make sure it is in the range if (age < 0 || (youngest != -1 && age < youngest) || (oldest != -1 && age > oldest)) { return -1; } return age; } catch (NumberFormatException ex) { return -1; } } public static boolean containsValidChars (String str, boolean allLetters, boolean allDigits, String strOthers) { String strValid = strOthers; if (allLetters) { strValid += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; } if (allDigits) { strValid += "0123456789"; } for (int i = 0; i < str.length(); i++) { //Tests each char of str can be found in the collection of valid chars if (strValid.indexOf(str.charAt(i)) == -1) { return false; } } return true; } public static boolean isValidStandardPassword (String strPass) { return containsValidChars(strPass, true, true, ""); } public static boolean isValidAge (String strAge, int youngest, int oldest) { return getValidAge(strAge, youngest, oldest) != -1; } public static boolean isValidEmail (String strEmail) { //All credits for this e-mail pattern goes to //http://gregs.tcias.co.uk/javascript/checking_for_a_valid_e-mail_address.cfm String strPattern = "^[-._&0-9a-zA-Z]+[@][-._&0-9a-zA-Z]+[.][._0-9a-zA-Z]+[a-zA-Z]$"; return Pattern.matches(strPattern, strEmail); } public static boolean isValidName (String strName) { return ((strName.length() >= 2 && strName.length() <= 20) && containsValidChars(strName, true, false, " ")); } public static byte[] getValidImageBytes (File file, long fileSizeLimit) throws dialogueException { try { if (!file.exists()) { throw new dialogueException("Could not open the file because it does not exist\n" + file.getAbsolutePath(), "File does not exist", JOptionPane.WARNING_MESSAGE); } else if (file.isDirectory()) { throw new dialogueException("The selected item is a directory. Only can upload files\n" + file.getAbsolutePath(), "Not a file", JOptionPane.WARNING_MESSAGE); } else if (!file.canRead()) { throw new dialogueException("The file can not be read, check file permissions\n" + file.getAbsolutePath(), "Read access error", JOptionPane.WARNING_MESSAGE); } } catch (SecurityException ex) { throw new dialogueException("The O/S is blocking this application from accessing the file\n" + file.getAbsolutePath(), "Security policy exception", JOptionPane.WARNING_MESSAGE); } String strExt = getFileExt(file.getName()); if (!(strExt.equalsIgnoreCase("jpg") || strExt.equalsIgnoreCase("jpeg") || strExt.equalsIgnoreCase("gif"))) { throw new dialogueException("Can only upload image files\n" + file.getAbsolutePath(), "Invalid file format", JOptionPane.WARNING_MESSAGE); } byte[] imageData = null; try { imageData = dataManager.getFileByteArray(file); } catch (Exception ex) { throw new dialogueException(ex.getMessage()); } if ((fileSizeLimit != -1) && (imageData.length >= fileSizeLimit)) { throw new dialogueException("The file is too big, the upload limit is " + fileSizeLimit + " bytes\n" + file.getAbsolutePath(), "Too big", JOptionPane.WARNING_MESSAGE); } return imageData; } }