1 19 20 package org.netbeans.modules.j2ee.sun.validation.constraints; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.text.MessageFormat ; 25 26 import org.netbeans.modules.j2ee.sun.validation.Constants; 27 import org.netbeans.modules.j2ee.sun.validation.constraints.ConstraintFailure; 28 import org.netbeans.modules.j2ee.sun.validation.util.BundleReader; 29 30 44 public class CardinalConstraint extends ConstraintUtils 45 implements Constraint{ 46 47 50 private int cardinal = Constants.MANDATORY_ELEMENT; 51 52 53 54 public CardinalConstraint() { 55 cardinal = Constants.MANDATORY_ELEMENT; 56 } 57 58 59 60 public CardinalConstraint(int cardinal) { 61 this.cardinal = cardinal; 62 } 63 64 65 66 public CardinalConstraint(String cardinal) { 67 try { 68 this.cardinal = Integer.parseInt(cardinal); 69 } catch(NumberFormatException e) { 70 String format = 71 BundleReader.getValue("Error_failed_to_create"); Object [] arguments = 73 new Object []{"CardinalConstraint"}; System.out.println(MessageFormat.format(format, arguments)); 75 } 76 } 77 78 79 96 public Collection match(String value, String name){ 97 return match((Object )value, name); 98 } 99 100 101 117 public Collection match(Object value, String name) { 118 Collection failed_constrained_list = 119 new ArrayList (); 120 121 if(Constants.MANDATORY_ARRAY == cardinal) { 122 Object [] values = null; 123 try { 124 values = (Object [])value; 125 } catch(ClassCastException exception){ 126 System.out.println(BundleReader.getValue( 127 "Error_expects_argument_one_to_be_an_array_of_Objects")); } 129 130 if((null == values) || (values.length < 1)){ 131 String failureMessage = formatFailureMessage(toString(), name); 132 failed_constrained_list.add(new ConstraintFailure(toString(), 133 null, name, failureMessage, BundleReader.getValue( 134 "MSG_CardinalConstraint_Failure"))); } 136 } else { 137 if(Constants.MANDATORY_ELEMENT == cardinal){ 138 if (null == value){ 139 String failureMessage = 140 formatFailureMessage(toString(), name); 141 failed_constrained_list.add( 142 new ConstraintFailure(toString(), null, name, 143 failureMessage, BundleReader.getValue( 144 "MSG_CardinalConstraint_Failure"))); } 146 } 147 } 148 return failed_constrained_list; 149 } 150 151 152 159 public void setCardinal(int cardinal){ 160 this.cardinal = cardinal; 161 } 162 163 164 171 public void setCardinal(String cardinal){ 172 try { 173 this.cardinal = Integer.parseInt(cardinal); 174 } catch(NumberFormatException e) { 175 String format = 176 BundleReader.getValue("Error_failed_to_set"); Object [] arguments = 178 new Object []{this.toString(), "Cardinal"}; 180 System.out.println(MessageFormat.format(format, arguments)); 181 } 182 } 183 184 185 188 public void print() { 189 super.print(); 190 String format = BundleReader.getValue("Name_Value_Pair_Format"); Object [] arguments = 192 new Object []{"Cardinal", String.valueOf(cardinal)}; System.out.println(MessageFormat.format(format, arguments)); 194 } 195 } 196 | Popular Tags |