Source for gnu.xml.validation.datatype.TypeBuilder

   1: /* TypeBuilder.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.LinkedHashSet;
  41: import java.util.regex.Pattern;
  42: import javax.xml.namespace.QName;
  43: import org.relaxng.datatype.Datatype;
  44: import org.relaxng.datatype.DatatypeBuilder;
  45: import org.relaxng.datatype.DatatypeException;
  46: import org.relaxng.datatype.ValidationContext;
  47: 
  48: /**
  49:  * Datatype builder.
  50:  *
  51:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  52:  */
  53: public class TypeBuilder
  54:   implements DatatypeBuilder
  55: {
  56: 
  57:   final SimpleType type;
  58:   
  59:   TypeBuilder(SimpleType type)
  60:   {
  61:     this.type = type;
  62:     // TODO fundamental facets
  63:     type.facets = new LinkedHashSet();
  64:   }
  65: 
  66:   public void addParameter(String name, String value, ValidationContext context)
  67:     throws DatatypeException
  68:   {
  69:     // TODO fundamental facets
  70:     if ("length".equals(name))
  71:       type.facets.add(parseLengthFacet(value));
  72:     else if ("minLength".equals(name))
  73:       type.facets.add(parseMinLengthFacet(value));
  74:     else if ("maxLength".equals(name))
  75:       type.facets.add(parseMaxLengthFacet(value));
  76:     else if ("pattern".equals(name))
  77:       type.facets.add(parsePatternFacet(value));
  78:     else if ("enumeration".equals(name))
  79:       type.facets.add(parseEnumerationFacet(value));
  80:     else if ("whiteSpace".equals(name))
  81:       type.facets.add(parseWhiteSpaceFacet(value));
  82:     else if ("maxInclusive".equals(name))
  83:       type.facets.add(parseMaxInclusiveFacet(value, context));
  84:     else if ("maxExclusive".equals(name))
  85:       type.facets.add(parseMaxExclusiveFacet(value, context));
  86:     else if ("minExclusive".equals(name))
  87:       type.facets.add(parseMinExclusiveFacet(value, context));
  88:     else if ("minInclusive".equals(name))
  89:       type.facets.add(parseMinInclusiveFacet(value, context));
  90:     else if ("totalDigits".equals(name))
  91:       type.facets.add(parseTotalDigitsFacet(value));
  92:     else if ("fractionDigits".equals(name))
  93:       type.facets.add(parseFractionDigitsFacet(value));
  94:   }
  95: 
  96:   LengthFacet parseLengthFacet(String value)
  97:     throws DatatypeException
  98:   {
  99:     int si = value.indexOf(' ');
 100:     boolean fixed = false;
 101:     if (si != -1)
 102:       {
 103:         if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
 104:           throw new DatatypeException("second argument must be FIXED if present");
 105:         fixed = true;
 106:         value = value.substring(0, si);
 107:       }
 108:     return new LengthFacet(Integer.parseInt(value), fixed, null);
 109:   }
 110: 
 111:   MinLengthFacet parseMinLengthFacet(String value)
 112:     throws DatatypeException
 113:   {
 114:     int si = value.indexOf(' ');
 115:     boolean fixed = false;
 116:     if (si != -1)
 117:       {
 118:         if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
 119:           throw new DatatypeException("second argument must be FIXED if present");
 120:         fixed = true;
 121:         value = value.substring(0, si);
 122:       }
 123:     return new MinLengthFacet(Integer.parseInt(value), fixed, null);
 124:   }
 125: 
 126:   MaxLengthFacet parseMaxLengthFacet(String value)
 127:     throws DatatypeException
 128:   {
 129:     int si = value.indexOf(' ');
 130:     boolean fixed = false;
 131:     if (si != -1)
 132:       {
 133:         if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
 134:           throw new DatatypeException("second argument must be FIXED if present");
 135:         fixed = true;
 136:         value = value.substring(0, si);
 137:       }
 138:     return new MaxLengthFacet(Integer.parseInt(value), fixed, null);
 139:   }
 140:   
 141:   PatternFacet parsePatternFacet(String value)
 142:     throws DatatypeException
 143:   {
 144:     return new PatternFacet(Pattern.compile(value), null);
 145:   }
 146: 
 147:   EnumerationFacet parseEnumerationFacet(String value)
 148:     throws DatatypeException
 149:   {
 150:     return new EnumerationFacet(value, null);
 151:   }
 152: 
 153:   WhiteSpaceFacet parseWhiteSpaceFacet(String value)
 154:     throws DatatypeException
 155:   {
 156:     int si = value.indexOf(' ');
 157:     boolean fixed = false;
 158:     if (si != -1)
 159:       {
 160:         if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
 161:           throw new DatatypeException("second argument must be FIXED if present");
 162:         fixed = true;
 163:         value = value.substring(0, si);
 164:       }
 165:     if ("preserve".equals(value))
 166:       return new WhiteSpaceFacet(WhiteSpaceFacet.PRESERVE, fixed, null);
 167:     if ("replace".equals(value))
 168:       return new WhiteSpaceFacet(WhiteSpaceFacet.REPLACE, fixed, null);
 169:     if ("collapse".equals(value))
 170:       return new WhiteSpaceFacet(WhiteSpaceFacet.COLLAPSE, fixed, null);
 171:     throw new DatatypeException("argument must be preserve, replace, or collapse");
 172:   }
 173: 
 174:   MaxInclusiveFacet parseMaxInclusiveFacet(String value,
 175:                                            ValidationContext context)
 176:     throws DatatypeException
 177:   {
 178:     int si = value.indexOf(' ');
 179:     boolean fixed = false;
 180:     if (si != -1)
 181:       {
 182:         if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
 183:           throw new DatatypeException("second argument must be FIXED if present");
 184:         fixed = true;
 185:         value = value.substring(0, si);
 186:       }
 187:     return new MaxInclusiveFacet(type.createValue(value, context), fixed, null);
 188:   }
 189:   
 190:   MaxExclusiveFacet parseMaxExclusiveFacet(String value,
 191:                                            ValidationContext context)
 192:     throws DatatypeException
 193:   {
 194:     int si = value.indexOf(' ');
 195:     boolean fixed = false;
 196:     if (si != -1)
 197:       {
 198:         if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
 199:           throw new DatatypeException("second argument must be FIXED if present");
 200:         fixed = true;
 201:         value = value.substring(0, si);
 202:       }
 203:     return new MaxExclusiveFacet(type.createValue(value, context), fixed, null);
 204:   }
 205:   
 206:   MinExclusiveFacet parseMinExclusiveFacet(String value,
 207:                                            ValidationContext context)
 208:     throws DatatypeException
 209:   {
 210:     int si = value.indexOf(' ');
 211:     boolean fixed = false;
 212:     if (si != -1)
 213:       {
 214:         if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
 215:           throw new DatatypeException("second argument must be FIXED if present");
 216:         fixed = true;
 217:         value = value.substring(0, si);
 218:       }
 219:     return new MinExclusiveFacet(type.createValue(value, context), fixed, null);
 220:   }
 221:   
 222:   MinInclusiveFacet parseMinInclusiveFacet(String value,
 223:                                            ValidationContext context)
 224:     throws DatatypeException
 225:   {
 226:     int si = value.indexOf(' ');
 227:     boolean fixed = false;
 228:     if (si != -1)
 229:       {
 230:         if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
 231:           throw new DatatypeException("second argument must be FIXED if present");
 232:         fixed = true;
 233:         value = value.substring(0, si);
 234:       }
 235:     return new MinInclusiveFacet(type.createValue(value, context), fixed, null);
 236:   }
 237:   
 238:   TotalDigitsFacet parseTotalDigitsFacet(String value)
 239:     throws DatatypeException
 240:   {
 241:     int si = value.indexOf(' ');
 242:     boolean fixed = false;
 243:     if (si != -1)
 244:       {
 245:         if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
 246:           throw new DatatypeException("second argument must be FIXED if present");
 247:         fixed = true;
 248:         value = value.substring(0, si);
 249:       }
 250:     int val = Integer.parseInt(value);
 251:     if (val < 0)
 252:       throw new DatatypeException("value must be a positiveInteger");
 253:     return new TotalDigitsFacet(val, fixed, null);
 254:   }
 255:   
 256:   FractionDigitsFacet parseFractionDigitsFacet(String value)
 257:     throws DatatypeException
 258:   {
 259:     int si = value.indexOf(' ');
 260:     boolean fixed = false;
 261:     if (si != -1)
 262:       {
 263:         if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
 264:           throw new DatatypeException("second argument must be FIXED if present");
 265:         fixed = true;
 266:         value = value.substring(0, si);
 267:       }
 268:     int val = Integer.parseInt(value);
 269:     if (val < 0)
 270:       throw new DatatypeException("value must be a positiveInteger");
 271:     return new FractionDigitsFacet(val, fixed, null);
 272:   }
 273:   
 274:   public Datatype createDatatype()
 275:   {
 276:     return type;
 277:   }
 278:   
 279: }