1 package net.sf.saxon.value; 2 3 import net.sf.saxon.ConversionContext; 4 import net.sf.saxon.om.FastStringBuffer; 5 import net.sf.saxon.om.XMLChar; 6 import net.sf.saxon.type.*; 7 8 9 14 15 public final class RestrictedStringValue extends StringValue { 16 17 private int type; 18 19 26 27 public static AtomicValue makeRestrictedString(CharSequence value, int type, boolean validate) { 28 RestrictedStringValue rsv = new RestrictedStringValue(); 29 rsv.type = type; 30 if (value == null) { 31 rsv.value = ""; 32 } else if (type == Type.NORMALIZED_STRING) { 33 rsv.value = normalizeWhitespace(value); 34 } else if (type == Type.TOKEN) { 35 rsv.value = collapseWhitespace(value); 36 } else { 37 rsv.value = trimWhitespace(value); 38 if (validate) { 39 ValidationException err = rsv.validate(); 40 if (err == null) { 41 return rsv; 42 } else { 43 return new ValidationErrorValue(err); 44 } 45 } else { 46 return rsv; 47 } 48 } 49 return rsv; 50 } 51 52 56 57 private ValidationException validate() { 58 switch (type) { 59 60 case Type.TOKEN: 61 return null; 62 case Type.NORMALIZED_STRING: 63 return null; 64 case Type.LANGUAGE: 65 String regex = 66 "(([a-z]|[A-Z])([a-z]|[A-Z])|" + "([iI]-([a-z]|[A-Z])+)|" + "([xX]-([a-z]|[A-Z])+))" + "(-([a-z]|[A-Z])+)*"; if (!java.util.regex.Pattern.matches(regex, value.toString())) { 71 ValidationException err = new ValidationException("The value '" + value + "' is not a valid xs:language"); 72 err.setErrorCode("FORG0001"); 73 return err; 74 } 75 return null; 76 case Type.NAME: 77 FastStringBuffer buff = new FastStringBuffer(value.length()); 79 buff.append(value.toString()); 80 for (int i = 0; i < buff.length(); i++) { 81 if (buff.charAt(i) == ':') { 82 buff.setCharAt(i, '_'); 83 } 84 } 85 if (!XMLChar.isValidNCName(buff.toString())) { 86 ValidationException err = new ValidationException("The value '" + value + "' is not a valid Name"); 87 err.setErrorCode("FORG0001"); 88 return err; 89 } 90 return null; 91 case Type.NCNAME: 92 case Type.ID: 93 case Type.IDREF: 94 case Type.ENTITY: 95 if (!XMLChar.isValidNCName(value.toString())) { 96 ValidationException err = new ValidationException("The value '" + value + "' is not a valid NCName"); 97 err.setErrorCode("FORG0001"); 98 return err; 99 } 100 return null; 101 case Type.NMTOKEN: 102 if (!XMLChar.isValidNmtoken(value.toString())) { 103 ValidationException err = new ValidationException("The value '" + value + "' is not a valid NMTOKEN"); 104 err.setErrorCode("FORG0001"); 105 return err; 106 } 107 return null; 108 default: 109 throw new IllegalArgumentException ("Unknown string value type " + type); 110 } 111 } 112 113 114 117 118 public ItemType getItemType() { 119 return (AtomicType) BuiltInSchemaFactory.getSchemaType(type); 120 } 121 122 128 129 public AtomicValue convertPrimitive(BuiltInAtomicType requiredType, boolean validate, ConversionContext conversion) { 130 int req = requiredType.getPrimitiveType(); 131 if (req == Type.STRING) { 132 return StringValue.makeStringValue(value); 133 } else if (req == Type.UNTYPED_ATOMIC) { 134 return new UntypedAtomicValue(value); 135 } else { 136 return super.convertPrimitive(requiredType, validate, conversion); 137 } 138 } 139 140 public String toString() { 141 return getItemType().toString() + '(' + super.toString() + ')'; 142 } 143 144 145 } 146 147 165 | Popular Tags |