1 // 2 // This software is now distributed according to 3 // the Lesser Gnu Public License. Please see 4 // http://www.gnu.org/copyleft/lesser.txt for 5 // the details. 6 // -- Happy Computing! 7 // 8 package com.stevesoft.pat; 9 10 /** 11 * This class makes it easy to create your own patterns and integrate them into 12 * Regex. For more detail, see the example file <a 13 * href="http://javaregex.com/code/deriv2.java.html">deriv2.java</a> or <a 14 * href="http://javaregex.com/code/deriv3.java.html">deriv3.java</a>. 15 */ 16 17 public class Validator 18 { 19 String argsave = null; 20 21 String pattern = "."; 22 23 /** 24 * This method does extra checking on a matched section of a String beginning 25 * at position start and ending at end. The idea is that you can do extra 26 * checking with this that you don't know how to do with a standard Regex. 27 * 28 * If this method is successful, it returns the location of the end of this 29 * pattern element -- that may be the value end provided or some other value. 30 * A negative value signifies that a match failure. 31 * 32 * By default, this method just returns end and thus does nothing. 33 * 34 * @see com.stevesoft.pat.Regex#define(java.lang.String,java.lang.String,com.stevesoft.pat.Validator) 35 */ validate(StringLike src, int start, int end)36 public int validate(StringLike src, int start, int end) 37 { 38 return end; 39 } 40 41 /* 42 * This method allows you to modify the behavior of this validator by making a 43 * new Validator object. If a Validator named "foo" is defined, then the 44 * pattern "{??foo:bar}" will cause Regex to first get the Validator given to 45 * Regex.define and then to call its arg method with the string "bar". If this 46 * method returns a null (the default) you get the same behavior as the 47 * pattern "{??foo}" would supply. 48 */ arg(String s)49 public Validator arg(String s) 50 { 51 return null; 52 } 53 54 /** 55 * For optimization it is helpful, but not necessary, that you define the 56 * minimum number of characters this validator will allow to match. To do this 57 * return new patInt(number) where number is the smallest number of characters 58 * that can match. 59 */ minChars()60 public patInt minChars() 61 { 62 return new patInt(0); 63 } 64 65 /** 66 * For optimization it is helpful, but not necessary, that you define the 67 * maximum number of characters this validator will allow to match. To do this 68 * either return new patInt(number), or new patInf() if an infinite number of 69 * characters may match. 70 */ maxChars()71 public patInt maxChars() 72 { 73 return new patInf(); 74 } 75 } 76