View Javadoc
1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "PredicatePrimitiveTypeRule.java".  Description: 
10  "PrimitiveTypeRule that validates using predicates" 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2012.  All Rights Reserved. 
14  
15  Contributor(s): ______________________________________. 
16  
17  Alternatively, the contents of this file may be used under the terms of the 
18  GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
19  applicable instead of those above.  If you wish to allow use of your version of this 
20  file only under the terms of the GPL and not to allow others to use your version 
21  of this file under the MPL, indicate your decision by deleting  the provisions above 
22  and replace  them with the notice and other provisions required by the GPL License.  
23  If you do not delete the provisions above, a recipient may use your version of 
24  this file under either the MPL or the GPL. 
25   */
26  package ca.uhn.hl7v2.validation.builder;
27  
28  import java.util.regex.Pattern;
29  
30  import ca.uhn.hl7v2.validation.ValidationException;
31  import ca.uhn.hl7v2.validation.impl.AbstractPrimitiveTypeRule;
32  
33  /**
34   * Abstract base class for PrimitiveTypeRules that validate using predicates
35   * 
36   * @author Christian Ohr
37   */
38  @SuppressWarnings("serial")
39  public class PredicatePrimitiveTypeRule extends AbstractPrimitiveTypeRule implements
40  		PredicateRuleSupport<String> {
41   
42      private static final Pattern LEADING_WHITESPACE = Pattern.compile("^\\s+");
43      private static final Pattern TRAILING_WHITESPACE = Pattern.compile("\\s+$");
44  
45      
46      public enum Trimmer {
47  
48          LEFT {
49              @Override public String trim(String source) { return trimPattern(LEADING_WHITESPACE, source); }            
50          },
51          RIGHT {
52              @Override public String trim(String source) { return trimPattern(TRAILING_WHITESPACE, source); }            
53          },                
54          NONE {
55              @Override public String trim(String source) { return source; }            
56          },
57          ALL {
58              @Override public String trim(String source) { return source == null ? null : source.trim(); }                        
59          };
60  
61          /**
62           * Trims a string
63           * @param source the string to be trimmed
64           * @return the trimmed string
65           */
66          public abstract String trim(String source);
67          
68          protected String trimPattern(Pattern pattern, String source) {
69              if (source == null || pattern == null) 
70                  return source;
71              return pattern.matcher(source).replaceAll("");
72          }
73      }
74  
75  	private final Predicate predicate;
76  	private final Trimmer trimmer;
77  
78      /**
79       * Creates a new primitive rule without trimming
80       * @param predicate predicate to be used
81       */
82  	public PredicatePrimitiveTypeRule(Predicate predicate) {
83  		this(predicate, Trimmer.NONE);
84  	}
85  
86      /**
87       * Creates a new primitive rule
88       * @param predicate predicate to be used
89       * @param trimmer trimming behavior
90       */
91  	public PredicatePrimitiveTypeRule(Predicate predicate, Trimmer trimmer) {
92  		this.predicate = predicate;
93  		this.trimmer = trimmer;
94          setDescription("Primitive value '%s' requires to be " + predicate.getDescription());
95  	}
96  
97  	public Predicate getPredicate() {
98  		return predicate;
99  	}
100 
101 	public String correct(String value) {
102 	    return trimmer.trim(value);
103 	}
104 
105 	public boolean test(String value) {
106 		try {
107 			return getPredicate().evaluate(value);
108 		} catch (ValidationException e) {
109 			return false;
110 		}
111 	}
112 
113 	public ValidationException[] apply(String value) {
114 		try {
115 			return result(getPredicate().evaluate(correct(value)), value);
116 		} catch (ValidationException e) {
117 			return failed(e);
118 		}
119 	}
120 
121 	@Override
122 	public String toString() {
123 		return getDescription();
124 	}
125 
126 }