Clover coverage report - JBind Project
Coverage timestamp: Fr Mai 28 2004 11:17:36 CEST
file stats: LOC: 494   Methods: 73
NCLOC: 388   Classes: 15
This license of Clover is provided to support the development of JBind only. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
ConstraintType.java 65,6% 74,3% 72,6% 72,4%
 1   
 /*
 2   
  * JBind
 3   
  *
 4   
  * Copyright (c) by Stefan Wachter. All rights reserved.
 5   
  *
 6   
  * Usage, modification, and redistribution is subject to license terms that are
 7   
  * available at 'http://www.jbind.org'. The JBind license is like the
 8   
  * 'Apache Software License V 1.1'.
 9   
  */
 10   
 package org.jbind.xml.core.constraint;
 11   
 
 12   
 import java.util.ArrayList;
 13   
 import java.util.HashMap;
 14   
 import java.util.Iterator;
 15   
 import java.util.List;
 16   
 import java.util.Map;
 17   
 
 18   
 import org.jbind.xml.base.IHasBindingAttributes;
 19   
 import org.jbind.xml.base.IHasLocation;
 20   
 import org.jbind.xml.base.ILocation;
 21   
 import org.jbind.xml.core.bridge.IRegEx;
 22   
 import org.jbind.xml.core.data.IAnySimpleTypeData;
 23   
 import org.jbind.xml.core.data.IAnyTypeData;
 24   
 import org.jbind.xml.core.data.IDataContext;
 25   
 import org.jbind.xml.core.data.IHasDigits;
 26   
 import org.jbind.xml.core.data.IHasLength;
 27   
 import org.jbind.xml.core.data.IHasOrder;
 28   
 import org.jbind.xml.msg.IConstraintViolations;
 29   
 import org.jbind.xml.msg.XmlMessages;
 30   
 
 31   
 public class ConstraintType {
 32   
 
 33   
   // The static counters must be declared first because otherwise they would
 34   
   // be set to 0 again after some ConstraintType instances have been created.
 35   
 
 36   
   /** Counts the number of constraint types that may occur only once at each type. */
 37   
   private static int ourSingleCounter = 0;
 38   
 
 39   
   /** Counts the number of constraint types that may occur several times at a type. */
 40   
   private static int ourMultipleCounter = 0;
 41   
 
 42   
   public static final ConstraintType BUILT_IN = new ConstraintType("builtIn", true);
 43   
 
 44   
   public static final org.jbind.xml.core.constraint.ConstraintType.Pattern PATTERN = new Pattern();
 45   
 
 46   
   public static final Enumeration ENUMERATION = new Enumeration();
 47   
 
 48   
   //
 49   
   // Order constraints
 50   
   //
 51   
 
 52   
   public static final Order MIN_EXCLUSIVE = new Order("minExclusive") {
 53   
 
 54  0
     public IConstraint createConstraint(IHasOrder aData) {
 55  0
       return new OrderConstraint(aData) {
 56  0
         public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
 57  0
           if (myThreshold.isGreaterOrEqual((IHasOrder)aData)) {
 58  0
             aViolations.add(XmlMessages.minExclusiveConstraint(myThreshold, aData, aData.getImpl_()));
 59   
           }
 60   
         }
 61  0
         public boolean isRestriction(IConstraint aConstraint) {
 62  0
           return myThreshold.isGreaterOrEqual(((OrderConstraint)aConstraint).getThreshold());
 63   
         }
 64   
       };
 65   
     }
 66   
   };
 67   
   public static final Order MAX_EXCLUSIVE = new Order("maxExclusive") {
 68   
 
 69  1
     public IConstraint createConstraint(IHasOrder aData) {
 70  1
       return new OrderConstraint(aData) {
 71  1
         public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
 72  1
           if (myThreshold.isLessOrEqual((IHasOrder)aData)) {
 73  0
             aViolations.add(XmlMessages.maxExclusiveConstraint(myThreshold, aData, aData.getImpl_()));
 74   
           }
 75   
         }
 76  0
         public boolean isRestriction(IConstraint aConstraint) {
 77  0
           return myThreshold.isLessOrEqual(((OrderConstraint)aConstraint).getThreshold());
 78   
         }
 79   
       };
 80   
     }
 81   
   };
 82   
   public static final Order MIN_INCLUSIVE = new Order("minInclusive") {
 83   
 
 84  12
     public IConstraint createConstraint(IHasOrder aData) {
 85  12
       return new OrderConstraint(aData) {
 86  94
         public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
 87  94
           if (myThreshold.isGreater((IHasOrder)aData)) {
 88  0
             aViolations.add(XmlMessages.minInclusiveConstraint(myThreshold, aData, aData.getImpl_()));
 89   
           }
 90   
         }
 91  0
         public boolean isRestriction(IConstraint aConstraint) {
 92  0
           return myThreshold.isGreaterOrEqual(((OrderConstraint)aConstraint).getThreshold());
 93   
         }
 94   
       };
 95   
     }
 96   
   };
 97   
   public static final Order MAX_INCLUSIVE = new Order("maxInclusive") {
 98   
 
 99  20
     public IConstraint createConstraint(IHasOrder aData) {
 100  20
       return new OrderConstraint(aData) {
 101  0
         public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
 102  0
           if (myThreshold.isLess((IHasOrder)aData)) {
 103  0
             aViolations.add(XmlMessages.maxInclusiveConstraint(myThreshold, aData, aData.getImpl_()));
 104   
           }
 105   
         }
 106  0
         public boolean isRestriction(IConstraint aConstraint) {
 107  0
           return myThreshold.isLessOrEqual(((OrderConstraint)aConstraint).getThreshold());
 108   
         }
 109   
       };
 110   
     }
 111   
   };
 112   
 
 113   
   //
 114   
   // Length constraints
 115   
   //
 116   
 
 117   
   public static final Length LENGTH = new Length("length") {
 118   
 
 119  10
     public IConstraint createConstraint(int aLength) {
 120  10
       return new LengthConstraint(aLength) {
 121  4
         public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
 122  4
           if (myLength != ((IHasLength)aData).getLength()) {
 123  1
             aViolations.add(XmlMessages.lengthConstraint(new Integer(myLength), new Integer(((IHasLength)aData).getLength()), aData.getImpl_()));
 124   
           }
 125   
         }
 126  0
         public boolean isRestriction(IConstraint aConstraint) {
 127  0
           return myLength == (((LengthConstraint)aConstraint).getLength());
 128   
         }
 129   
       };
 130   
     }
 131   
   };
 132   
   public static final Length MIN_LENGTH = new Length("minLength") {
 133   
 
 134  17
     public IConstraint createConstraint(int aLength) {
 135  17
       return new LengthConstraint(aLength) {
 136  72
         public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
 137  72
           if (myLength > ((IHasLength)aData).getLength()) {
 138  6
             aViolations.add(XmlMessages.minLengthConstraint(new Integer(myLength), new Integer(((IHasLength)aData).getLength()), aData.getImpl_()));
 139   
           }
 140   
         }
 141  1
         public boolean isRestriction(IConstraint aConstraint) {
 142  1
           return myLength >= (((LengthConstraint)aConstraint).getLength());
 143   
         }
 144   
       };
 145   
     }
 146   
   };
 147   
   public static final Length MAX_LENGTH = new Length("maxLength") {
 148   
 
 149  6
     public IConstraint createConstraint(int aLength) {
 150  6
       return new LengthConstraint(aLength) {
 151  59
         public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
 152  59
           if (myLength < ((IHasLength)aData).getLength()) {
 153  3
             aViolations.add(XmlMessages.maxLengthConstraint(new Integer(myLength), new Integer(((IHasLength)aData).getLength()), aData.getImpl_()));
 154   
           }
 155   
         }
 156  0
         public boolean isRestriction(IConstraint aConstraint) {
 157  0
           return myLength <= (((LengthConstraint)aConstraint).getLength());
 158   
         }
 159   
       };
 160   
     }
 161   
   };
 162   
 
 163   
   //
 164   
   // Digits constraints
 165   
   //
 166   
 
 167   
   public static final Digits TOTAL_DIGITS = new Digits("totalDigits") {
 168  0
     public IConstraint createConstraint(int aNbDigits) {
 169  0
       return new DigitsConstraint(aNbDigits) {
 170  0
         public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
 171  0
           if (myNbDigits < ((IHasDigits)aData).getNbDigits()) {
 172  0
             aViolations.add(XmlMessages.totalDigitsConstraint(new Integer(myNbDigits), aData, aData.getImpl_()));
 173   
           }
 174   
         }
 175   
       };
 176   
     }
 177   
   };
 178   
   public static final Digits FRACTION_DIGITS = new Digits("fractionDigits") {
 179  2
     public IConstraint createConstraint(int aNbDigits) {
 180  2
       return new DigitsConstraint(aNbDigits) {
 181  0
         public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
 182  0
           if (myNbDigits < ((IHasDigits)aData).getNbFractionDigits()) {
 183  0
             aViolations.add(XmlMessages.fractionDigitsConstraint(new Integer(myNbDigits), aData, aData.getImpl_()));
 184   
           }
 185   
         }
 186   
       };
 187   
     }
 188   
   };
 189   
 
 190   
   public static final ConstraintType ID = new ConstraintType("id", true);
 191   
   public static final ConstraintType IDS = new ConstraintType("ids", false);
 192   
   public static final ConstraintType ID_REF = new ConstraintType("idRef", false);
 193   
   public static final ConstraintType ID_REFS = new ConstraintType("idRefs", false);
 194   
 
 195   
   public static final ConstraintType TYPE_UNIQUE = new ConstraintType("typeUnique", false);
 196   
   public static final ConstraintType TYPE_KEY = new ConstraintType("typeKey", false);
 197   
   public static final ConstraintType TYPE_KEY_REF = new ConstraintType("typeKeyRef", false);
 198   
 
 199   
   public static final ConstraintType ELEMENT_UNIQUE = new ConstraintType("elementUnique", false);
 200   
   public static final ConstraintType ELEMENT_KEY = new ConstraintType("elementKey", false);
 201   
   public static final ConstraintType ELEMENT_KEY_REF = new ConstraintType("elementKeyRef", false);
 202   
 
 203   
   public static final ConstraintType XPATH_CONSTRAINT = new ConstraintType("xPath", false);
 204   
 
 205   
   public static final ConstraintType ATTRIBUTES = new ConstraintType("attributes", true);
 206   
 
 207   
   public static final WhiteSpace WHITE_SPACE = new WhiteSpace();
 208   
 
 209  1613
   public static final int getNbSingleConstraintTypes() {
 210  1613
     return ourSingleCounter;
 211   
   }
 212   
 
 213  1613
   public static final int getNbMultipleConstraintTypes() {
 214  1613
     return ourMultipleCounter;
 215   
   }
 216   
 
 217   
   private String myName = null;
 218   
   private boolean myIsSingleNotMultiple;
 219   
   private int myIndex;
 220   
 
 221  75
   protected ConstraintType(String aName, boolean anIsSingleNotMultiple) {
 222  75
     myName = aName;
 223  75
     myIsSingleNotMultiple = anIsSingleNotMultiple;
 224  75
     if (myIsSingleNotMultiple) {
 225  42
       myIndex = ourSingleCounter++;
 226   
     } else {
 227  33
       myIndex = ourMultipleCounter++;
 228   
     }
 229   
   }
 230   
 
 231  24793
   public boolean isSingleNotMultiple() {
 232  24793
     return myIsSingleNotMultiple;
 233   
   }
 234  14626
   public int getIndex() {
 235  14626
     return myIndex;
 236   
   }
 237  0
   public String toString() {
 238  0
     return myName;
 239   
   }
 240   
 
 241   
   private abstract static class FacetConstraintType extends ConstraintType {
 242  36
     protected FacetConstraintType(String aName, boolean aSingleNotMultiple) {
 243  36
       super(aName, aSingleNotMultiple);
 244   
     }
 245   
   }
 246   
 
 247   
   private abstract class SimpleConstraint implements ISimpleConstraint {
 248   
 
 249   
     private IHasLocation myHasLocation = null;
 250   
     private boolean myIsFinal = false;
 251  164
     public SimpleConstraint(IHasLocation aHasLocation) {
 252  164
       myHasLocation = aHasLocation;
 253   
     }
 254   
 
 255  55
     public final void globalCheck(IDataContext aContext, IAnyTypeData aData, IAnyTypeData anEnclosingData, IConstraintViolations aViolations) {}
 256  3
     public boolean isFinal() {
 257  3
       return myIsFinal;
 258   
     }
 259  0
     public void setIsFinal(boolean aBoolean) {
 260  0
       myIsFinal = aBoolean;
 261   
     }
 262  0
     public ILocation getLocation() {
 263  0
       return (null != myHasLocation) ? myHasLocation.getLocation() : null;
 264   
     }
 265   
   }
 266   
 
 267   
   //
 268   
   // Order constraint
 269   
   //
 270   
 
 271   
   public abstract static class Order extends FacetConstraintType {
 272   
 
 273  12
     private Order(String aName) {
 274  12
       super(aName, true);
 275   
     }
 276   
     public abstract IConstraint createConstraint(IHasOrder aHasOrder);
 277   
     public abstract class OrderConstraint extends SimpleConstraint {
 278   
 
 279  66
       public ConstraintType getConstraintType() {
 280  66
         return Order.this;
 281   
       }
 282   
       protected IHasOrder myThreshold = null;
 283  33
       public OrderConstraint(IHasOrder aThreshold) {
 284  33
         super(null);
 285  33
         myThreshold = aThreshold;
 286   
       }
 287  0
       public IHasOrder getThreshold() {
 288  0
         return myThreshold;
 289   
       }
 290   
     }
 291   
   }
 292   
 
 293   
   //
 294   
   // Digits constraint
 295   
   //
 296   
 
 297   
   public abstract static class Digits extends FacetConstraintType {
 298   
 
 299  6
     private Digits(String aName) {
 300  6
       super(aName, true);
 301   
     }
 302   
     public abstract IConstraint createConstraint(int aNbDigits);
 303   
     public abstract class DigitsConstraint extends SimpleConstraint {
 304   
 
 305   
       protected int myNbDigits;
 306  2
       public DigitsConstraint(int aNbDigits) {
 307  2
         super(null);
 308  2
         myNbDigits = aNbDigits;
 309   
       }
 310  4
       public ConstraintType getConstraintType() {
 311  4
         return Digits.this;
 312   
       }
 313  0
       public boolean isRestriction(IConstraint aConstraint) {
 314  0
         return myNbDigits <= ((DigitsConstraint)aConstraint).getNbDigits();
 315   
       }
 316  0
       public int getNbDigits() {
 317  0
         return myNbDigits;
 318   
       }
 319   
     }
 320   
   }
 321   
 
 322   
   //
 323   
   // Length constraint
 324   
   //
 325   
 
 326   
   public abstract static class Length extends FacetConstraintType {
 327   
 
 328  9
     private Length(String aName) {
 329  9
       super(aName, true);
 330   
     }
 331   
     public abstract IConstraint createConstraint(int aLength);
 332   
     public abstract class LengthConstraint extends SimpleConstraint {
 333   
 
 334   
       protected int myLength;
 335  33
       public LengthConstraint(int aLength) {
 336  33
         super(null);
 337  33
         myLength = aLength;
 338   
       }
 339  66
       public ConstraintType getConstraintType() {
 340  66
         return Length.this;
 341   
       }
 342  1
       public int getLength() {
 343  1
         return myLength;
 344   
       }
 345   
     }
 346   
   }
 347   
 
 348   
   //
 349   
   // Enumeration
 350   
   //
 351   
 
 352   
   public static class Enumeration extends FacetConstraintType {
 353   
 
 354  3
     private Enumeration() {
 355  3
       super("enumeration", true);
 356   
     }
 357  70
     public IEnumerationConstraint createConstraint() {
 358  70
       return new EnumerationConstraint();
 359   
     }
 360   
     public class EnumerationConstraint extends SimpleConstraint implements IEnumerationConstraint {
 361   
 
 362   
       private Map myOptions = new HashMap();
 363  70
       public EnumerationConstraint() {
 364  70
         super(null);
 365   
       }
 366  179
       public void addOption(IAnyTypeData aData, IHasBindingAttributes aHasBindingAttributes) {
 367  179
         myOptions.put(aData, aHasBindingAttributes);
 368   
       }
 369  4
       public boolean containsOption(IAnyTypeData aData) {
 370  4
         return myOptions.containsKey(aData);
 371   
       }
 372  260
       public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
 373  260
         if (!myOptions.containsKey(aData)) {
 374  9
           aViolations.add(XmlMessages.dataNotContainedInEnumeration(aData, aData.getImpl_()));
 375   
         }
 376   
       }
 377  140
       public ConstraintType getConstraintType() {
 378  140
         return Enumeration.this;
 379   
       }
 380  2
       public boolean isRestriction(IConstraint aConstraint) {
 381  2
         IConstraintViolations violations = XmlMessages.constraintViolations();
 382  2
         IEnumerationConstraint ec = (IEnumerationConstraint)aConstraint;
 383  2
         for (Iterator i = myOptions.keySet().iterator(); i.hasNext(); ) {
 384  4
           IAnyTypeData d = (IAnyTypeData)i.next();
 385  4
           if (!ec.containsOption(d)) {
 386  0
             violations.add(XmlMessages.dataNotContainedInEnumeration(d, d.getImpl_()));
 387   
           }
 388   
         }
 389  2
         return violations.isEmpty();
 390   
       }
 391  97
       public Iterator iterOptions() {
 392  97
         return myOptions.entrySet().iterator();
 393   
       }
 394   
     }
 395   
   }
 396   
 
 397   
   //
 398   
   // Pattern
 399   
   //
 400   
 
 401   
   public static class Pattern extends FacetConstraintType {
 402   
 
 403  3
     private Pattern() {
 404  3
       super("pattern", false);
 405   
     }
 406  26
     public IPatternConstraint createConstraint() {
 407  26
       return new PatternConstraint();
 408   
     }
 409   
     public class PatternConstraint extends SimpleConstraint implements IPatternConstraint {
 410   
 
 411   
       private List myRegularExpressions = new ArrayList();
 412  26
       public PatternConstraint() {
 413  26
         super(null);
 414   
       }
 415  28
       public void addPattern(IRegEx aRegEx, IConstraintViolations aViolations, IHasLocation aHasLocation) {
 416  28
         myRegularExpressions.add(aRegEx);
 417   
       }
 418  321
       public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
 419  321
         boolean ok = false;
 420  321
         String s = ((IAnySimpleTypeData)aData).getNormalizedValue_();
 421  321
         for (Iterator i = myRegularExpressions.iterator(); !ok && i.hasNext(); ) {
 422  322
           IRegEx regEx = (IRegEx)i.next();
 423  322
           ok = regEx.matches(s);
 424   
         }
 425  321
         if (!ok) {
 426  4
           StringBuffer patterns = new StringBuffer();
 427  4
           for (Iterator i = myRegularExpressions.iterator(); i.hasNext(); ) {
 428  5
             patterns.append("(").append(i.next()).append(")");
 429  5
             patterns.append(i.hasNext() ? "|" : "");
 430   
           }
 431  4
           aViolations.add(XmlMessages.patternConstraint(aData, patterns, aData.getImpl_()));
 432   
         }
 433   
       }
 434  26
       public ConstraintType getConstraintType() {
 435  26
         return Pattern.this;
 436   
       }
 437   
 
 438  0
       public boolean isRestriction(IConstraint aConstraint) {
 439   
         assert false :
 440   
                "pattern constraint can not be checked for being a restriction";
 441  0
         return false;
 442   
       }
 443   
     }
 444   
 
 445   
   }
 446   
 
 447   
   public static class WhiteSpace extends FacetConstraintType {
 448  3
     private WhiteSpace() {
 449  3
       super("whiteSpace", true);
 450   
     }
 451   
   }
 452   
 
 453   
   public static interface Checker {
 454   
     boolean isApplicable(ConstraintType aConstraintType);
 455   
   }
 456   
 
 457   
   public static final Checker CHECKER_LWEP = new Checker() {
 458  145
     public boolean isApplicable(ConstraintType aConstraintType) {
 459  145
       return (aConstraintType == LENGTH) || (aConstraintType == MIN_LENGTH) || (aConstraintType == MAX_LENGTH) || (aConstraintType == WHITE_SPACE) || (aConstraintType == ENUMERATION) || (aConstraintType == PATTERN);
 460   
     }
 461   
   };
 462   
 
 463   
   public static final Checker CHECKER_WEP = new Checker() {
 464  116
     public boolean isApplicable(ConstraintType aConstraintType) {
 465  116
       return (aConstraintType == WHITE_SPACE) || (aConstraintType == ENUMERATION) || (aConstraintType == PATTERN);
 466   
     }
 467   
   };
 468   
 
 469   
   public static final Checker CHECKER_OWEP = new Checker() {
 470  0
     public boolean isApplicable(ConstraintType aConstraintType) {
 471  0
       return (aConstraintType == MIN_EXCLUSIVE) || (aConstraintType == MIN_INCLUSIVE) || (aConstraintType == MAX_EXCLUSIVE) || (aConstraintType == MAX_INCLUSIVE) || (aConstraintType == WHITE_SPACE) || (aConstraintType == ENUMERATION) || (aConstraintType == PATTERN);
 472   
     }
 473   
   };
 474   
 
 475   
   public static final Checker CHECKER_ODWEP = new Checker() {
 476  48
     public boolean isApplicable(ConstraintType aConstraintType) {
 477  48
       return (aConstraintType == MIN_EXCLUSIVE) || (aConstraintType == MIN_INCLUSIVE) || (aConstraintType == MAX_EXCLUSIVE) || (aConstraintType == MAX_INCLUSIVE) || (aConstraintType == TOTAL_DIGITS) || (aConstraintType == FRACTION_DIGITS) || (aConstraintType == WHITE_SPACE) || (aConstraintType == ENUMERATION) || (aConstraintType == PATTERN);
 478   
     }
 479   
   };
 480   
 
 481   
   public static final Checker CHECKER_WP = new Checker() {
 482  4
     public boolean isApplicable(ConstraintType aConstraintType) {
 483  4
       return (aConstraintType == WHITE_SPACE) || (aConstraintType == PATTERN);
 484   
     }
 485   
   };
 486   
 
 487   
   public static final Checker CHECKER_EP = new Checker() {
 488  10
     public boolean isApplicable(ConstraintType aConstraintType) {
 489  10
       return (aConstraintType == ENUMERATION) || (aConstraintType == PATTERN);
 490   
     }
 491   
   };
 492   
 
 493   
 }
 494