Source for gnu.xml.validation.datatype.SimpleType

   1: /* SimpleType.java -- 
   2:    Copyright (C) 2006  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.xml.validation.datatype;
  39: 
  40: import java.util.Iterator;
  41: import java.util.Set;
  42: import java.util.regex.Matcher;
  43: import javax.xml.XMLConstants;
  44: import javax.xml.namespace.QName;
  45: import org.relaxng.datatype.Datatype;
  46: import org.relaxng.datatype.DatatypeException;
  47: import org.relaxng.datatype.DatatypeStreamingValidator;
  48: import org.relaxng.datatype.ValidationContext;
  49: 
  50: /**
  51:  * An XML Schema simple type.
  52:  *
  53:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  54:  */
  55: public class SimpleType
  56:   extends Type
  57:   implements Datatype
  58: {
  59: 
  60:   /**
  61:    * The variety of the <code>anySimpleType</code> datatype.
  62:    */
  63:   public static final int ANY = 0;
  64:   
  65:   /**
  66:    * The atomic variety.
  67:    */
  68:   public static final int ATOMIC = 1;
  69:   
  70:   /**
  71:    * The list variety.
  72:    */
  73:   public static final int LIST = 2;
  74: 
  75:   /**
  76:    * The union variety.
  77:    */
  78:   public static final int UNION = 3;
  79: 
  80:   public static final int ID_TYPE_NULL = 0;
  81:   public static final int ID_TYPE_ID = 1;
  82:   public static final int ID_TYPE_IDREF = 2;
  83:   public static final int ID_TYPE_IDREFS = 3;
  84: 
  85:   /**
  86:    * The variety of this simple type.
  87:    */
  88:   public final int variety;
  89: 
  90:   /**
  91:    * The facets of this simple type.
  92:    */
  93:   public Set facets;
  94: 
  95:   /**
  96:    * The fundamental facets of this simple type.
  97:    */
  98:   public int fundamentalFacets;
  99: 
 100:   /**
 101:    * If this datatype has been derived by restriction, then the component
 102:    * from which it was derived.
 103:    */
 104:   public final SimpleType baseType;
 105: 
 106:   /**
 107:    * Optional annotation.
 108:    */
 109:   public final Annotation annotation;
 110: 
 111:   public SimpleType(QName name, int variety, Set facets,
 112:                     int fundamentalFacets, SimpleType baseType,
 113:                     Annotation annotation)
 114:   {
 115:     super(name);
 116:     this.variety = variety;
 117:     this.facets = facets;
 118:     this.fundamentalFacets = fundamentalFacets;
 119:     this.baseType = baseType;
 120:     this.annotation = annotation;
 121:   }
 122: 
 123:   /**
 124:    * Indicates whether this type permits the specified value.
 125:    */
 126:   public boolean isValid(String value, ValidationContext context)
 127:   {
 128:     try
 129:       {
 130:         checkValid(value, context);
 131:         return true;
 132:       }
 133:     catch (DatatypeException e)
 134:       {
 135:         return false;
 136:       }
 137:   }
 138: 
 139:   public void checkValid(String value, ValidationContext context)
 140:     throws DatatypeException
 141:   {
 142:     if (facets != null && !facets.isEmpty())
 143:       {
 144:         Object parsedValue = createValue(value, context);
 145:         for (Iterator i = facets.iterator(); i.hasNext(); )
 146:           {
 147:             Facet facet = (Facet) i.next();
 148:             switch (facet.type)
 149:               {
 150:               case Facet.LENGTH:
 151:                 LengthFacet lf = (LengthFacet) facet;
 152:                 if (value.length() != lf.value)
 153:                   throw new DatatypeException("invalid length");
 154:                 break;
 155:               case Facet.MIN_LENGTH:
 156:                 MinLengthFacet nlf = (MinLengthFacet) facet;
 157:                 if (value.length() < nlf.value)
 158:                   throw new DatatypeException("invalid minimum length");
 159:                 break;
 160:               case Facet.MAX_LENGTH:
 161:                 MaxLengthFacet xlf = (MaxLengthFacet) facet;
 162:                 if (value.length() > xlf.value)
 163:                   throw new DatatypeException("invalid maximum length");
 164:                 break;
 165:               case Facet.PATTERN:
 166:                 PatternFacet pf = (PatternFacet) facet;
 167:                 Matcher matcher = pf.value.matcher(value);
 168:                 if (!matcher.find())
 169:                   throw new DatatypeException("invalid match for pattern");
 170:                 break;
 171:               case Facet.ENUMERATION:
 172:                 // TODO
 173:                 break;
 174:               case Facet.WHITESPACE:
 175:                 // TODO
 176:                 break;
 177:               case Facet.MAX_INCLUSIVE:
 178:                 MaxInclusiveFacet xif = (MaxInclusiveFacet) facet;
 179:                 if (!xif.matches(parsedValue))
 180:                   throw new DatatypeException("beyond upper bound");
 181:                 break;
 182:               case Facet.MAX_EXCLUSIVE:
 183:                 MaxExclusiveFacet xef = (MaxExclusiveFacet) facet;
 184:                 if (!xef.matches(parsedValue))
 185:                   throw new DatatypeException("beyond upper bound");
 186:                 break;
 187:               case Facet.MIN_EXCLUSIVE:
 188:                 MinExclusiveFacet nef = (MinExclusiveFacet) facet;
 189:                 if (!nef.matches(parsedValue))
 190:                   throw new DatatypeException("beyond lower bound");
 191:                 break;
 192:               case Facet.MIN_INCLUSIVE:
 193:                 MinInclusiveFacet nif = (MinInclusiveFacet) facet;
 194:                 if (!nif.matches(parsedValue))
 195:                   throw new DatatypeException("beyond lower bound");
 196:                 break;
 197:               case Facet.TOTAL_DIGITS:
 198:                 TotalDigitsFacet tdf = (TotalDigitsFacet) facet;
 199:                 if (countDigits(value, true) > tdf.value)
 200:                   throw new DatatypeException("too many digits");
 201:                 break;
 202:               case Facet.FRACTION_DIGITS:
 203:                 FractionDigitsFacet fdf = (FractionDigitsFacet) facet;
 204:                 if (countDigits(value, false) > fdf.value)
 205:                   throw new DatatypeException("too many fraction digits");
 206:                 break;
 207:               }
 208:           }
 209:       }
 210:   }
 211: 
 212:   private static int countDigits(String value, boolean any)
 213:   {
 214:     int count = 0;
 215:     int len = value.length();
 216:     boolean seenDecimal = false;
 217:     for (int i = 0; i < len; i++)
 218:       {
 219:         char c = value.charAt(i);
 220:         if (c == 0x2e)
 221:           seenDecimal = true;
 222:         else if (c >= 0x30 && c <= 0x39 && (any || seenDecimal))
 223:           count++;
 224:       }
 225:     return count;
 226:   }
 227: 
 228:   // TODO createStreamingValidator
 229:   public DatatypeStreamingValidator createStreamingValidator(ValidationContext context)
 230:   {
 231:     throw new UnsupportedOperationException();
 232:   }
 233:   
 234:   public Object createValue(String literal, ValidationContext context) {
 235:     return literal;
 236:   }
 237:   
 238:   public boolean sameValue(Object value1, Object value2) {
 239:     return value1.equals(value2);
 240:   }
 241:   
 242:   public int valueHashCode(Object value) {
 243:     return value.hashCode();
 244:   }
 245: 
 246:   public int getIdType()
 247:   {
 248:     return ID_TYPE_NULL;
 249:   }
 250: 
 251:   public boolean isContextDependent()
 252:   {
 253:     return false;
 254:   }
 255: 
 256: }