1 22 23 package org.xquark.schema; 24 25 import java.util.ArrayList ; 26 27 import org.xquark.schema.datatypes.PrimitiveType; 28 import org.xquark.schema.validation.DefaultValidationInfo; 29 import org.xquark.schema.validation.ValidationContextProvider; 30 import org.xquark.schema.validation.ValidationInfo; 31 32 52 public final class SimpleType extends Type { 53 private static final String RCSRevision = "$Revision: 1.2 $"; 54 private static final String RCSName = "$Name: $"; 55 public static final int VARIETY_ATOMIC = 1; 57 public static final int VARIETY_LIST = 2; 58 public static final int VARIETY_UNION = 3; 59 60 private PrimitiveType primitive = null; 61 private java.util.HashMap facets = new java.util.HashMap (10); 62 63 private int nVariety = VARIETY_ATOMIC; 65 66 private Type itemType = null; private ArrayList memberTypes = null; 69 76 public SimpleType(Schema schema, String name, Type baseType) { 77 super(schema, name, baseType); 78 } 79 80 public void accept(SchemaVisitor visitor) throws SchemaException { 81 visitor.visit(this); 82 } 83 84 public int getContentType() { 85 return TEXT_ONLY; 86 } 87 88 public SimpleType getValueType() { 89 return this; 90 } 91 92 public void setVariety(int nVariety) { 93 switch (nVariety) { 94 case VARIETY_ATOMIC : 95 case VARIETY_LIST : 96 this.nVariety = nVariety; 97 break; 98 case VARIETY_UNION : 99 this.nVariety = nVariety; 100 if (memberTypes == null) 101 memberTypes = new ArrayList (); 102 break; 103 default : 104 this.nVariety = VARIETY_ATOMIC; 106 } 107 } 108 109 114 public int getVariety() { 115 return nVariety; 116 } 117 118 public int getDerivationMethod() { 119 return RESTRICTION; 120 } 121 122 125 126 public String checkTypeDerivationOK(Type ancestor, int exclusions, boolean useBlock) { 127 if (SIMPLE_UR_TYPE.equals(ancestor.getName())) 128 return null; 129 if (!ancestor.isSimpleType()) { 130 if (ANY_TYPE.equals(ancestor.getName())) 131 return null; 132 else 133 return "cos-st-derived-ok.2.2"; 134 } 135 SimpleType simpleBase = (SimpleType) ancestor; 136 if (this == simpleBase) 137 return null; 138 java.util.Collection memberTypes = simpleBase.getMemberTypes(); 139 if (memberTypes != null) { 140 java.util.Iterator it = memberTypes.iterator(); 141 while (it.hasNext()) { 142 if (it.next() == this) 143 return null; 144 } 145 } 146 if ((getDerivationMethod() & exclusions) != 0) 147 return "cos-st-derived-ok.2.1"; 148 Type parent = getBaseType(); 149 if (parent == null) 150 return "cos-st-derived-ok.2.2"; 151 return parent.checkTypeDerivationOK(ancestor, exclusions, useBlock); 152 } 153 154 public String normalizedValue(String value) { 155 return primitive.normalizedValue(value); 156 } 157 158 public StringBuffer normalizedValue(StringBuffer value) { 159 return primitive.normalizedValue(value); 160 } 161 162 public StringBuffer normalizedValue(StringBuffer value, StringBuffer out) { 163 return primitive.normalizedValue(value, out); 164 } 165 166 public String toXMLString(Object value, ValidationContextProvider context) { 167 return primitive.toXMLString(value, context); 168 } 169 170 public ValidationInfo validate(String value, boolean normalize, ValidationContextProvider context) 171 throws SchemaException { 172 try { 173 if (nVariety == VARIETY_UNION) return validateUnion(value, normalize, context); 174 else { 175 DefaultValidationInfo info = new DefaultValidationInfo(); 176 info.setValidationType(this); 177 if (normalize) value = primitive.normalizedValue(value); 178 info.setNormalizedValue(value); 179 info.setActualValue(primitive.convert(value, false, context)); 180 return info; 181 } 182 } catch (SchemaException ex) { 183 throw new SchemaException("cvc-simple-type", this, ex); 184 } 185 } 186 187 public ValidationInfo validate(StringBuffer value, boolean normalize, ValidationContextProvider context) 188 throws SchemaException { 189 try { 190 if (nVariety == VARIETY_UNION) return validateUnion(value, normalize, context); 191 else { 192 DefaultValidationInfo info = new DefaultValidationInfo(); 193 info.setValidationType(this); 194 if (normalize) value = primitive.normalizedValue(value); 195 info.setNormalizedValueBuffer(value); 196 info.setActualValue(primitive.convert(value, false, context)); 197 return info; 198 } 199 } catch (SchemaException ex) { 200 throw new SchemaException("cvc-simple-type", this, ex); 201 } 202 } 203 204 private ValidationInfo validateUnion(String value, boolean normalize, ValidationContextProvider context) throws SchemaException { 205 ValidationInfo info = null; 206 ArrayList exceptions = null; 207 primitive.checkPattern(value); 208 for (int i = 0; i < memberTypes.size(); i++) { 209 try { 210 SimpleType member = (SimpleType) memberTypes.get(i); 211 info = member.validate(value, normalize, context); 212 break; 213 } catch (SchemaException ex) { 214 if (exceptions == null) exceptions = new ArrayList (); 215 exceptions.add(ex); 216 } 217 } 218 if (info != null) { 219 primitive.checkFacets(info.getActualValue()); 220 return info; 221 } else { 222 throw new SchemaException("cvc-datatype-valid.1.2.3", this, exceptions); 223 } 224 } 225 226 private ValidationInfo validateUnion(StringBuffer value, boolean normalize, ValidationContextProvider context) throws SchemaException { 227 ValidationInfo info = validateUnion(value.toString(), normalize, context); 228 value.setLength(0); 229 value.append(info.getNormalizedValue()); 230 return info; 231 } 232 233 public Object convert(String value, boolean normalize, ValidationContextProvider context) throws SchemaException { 234 try { 235 return primitive.convert(value, normalize, context); 236 } catch (SchemaException ex) { 237 throw new SchemaException("cvc-simple-type", this, ex); 238 } 239 } 240 241 public Object convert(StringBuffer value, boolean normalize, ValidationContextProvider context) 242 throws SchemaException { 243 try { 244 return primitive.convert(value, normalize, context); 245 } catch (SchemaException ex) { 246 throw new SchemaException("cvc-simple-type", this, ex); 247 } 248 } 249 250 public Object convertObject(Object data) throws SchemaException { 251 return primitive.convertObject(data); 252 } 253 254 public String toCanonicalForm(String value, ValidationContextProvider context) throws SchemaException { 255 ValidationInfo info = validate(value, true, context); 256 SimpleType realType = info.getValidationType(); 257 Object obj = info.getActualValue(); 258 return realType.toXMLString(obj, context); 259 } 260 261 public String validateDefault(String value, ValidationContextProvider context) throws SchemaException { 262 try { 263 return toCanonicalForm(value, context); 264 } catch (SchemaException ex) { 265 throw new SchemaException("cos-valid-default.1", this, ex); 266 } 267 } 268 269 274 public PrimitiveType getPrimitive() { 275 return primitive; 276 } 277 278 294 public java.util.Collection getAllFacets() { 295 return facets.values(); 296 } 297 298 public java.util.HashMap getFacets() { 299 return facets; 300 } 301 302 public void setFacet(Facet facet) throws SchemaException { 303 String facetName = facet.getName(); 304 305 if (ENUMERATION_TAG.equals(facetName)) { 307 Facet enumFacet = (Facet) facets.get(facetName); 308 if (enumFacet == null) { 309 enumFacet = facet; 310 facets.put(facetName, enumFacet); 311 } 312 enumFacet.addFacet(facet.getValue()); 313 } 314 315 else if (PATTERN_TAG.equals(facetName)) { 317 Facet pattternFacet = (Facet) facets.get(facetName); 318 if (pattternFacet == null) { 319 pattternFacet = facet; 320 facets.put(facetName, pattternFacet); 321 } else { 322 String pat = pattternFacet.getValue(); 323 String facetValue = facet.getValue(); 324 if (pat != null) 325 facetValue = pat + "|" + facetValue; 326 pattternFacet.setValue(facetValue); 327 } 328 } 329 330 else { 332 if (facets.put(facetName, facet) != null) { 333 throw new SchemaException("src-single-facet-value", this); 335 } 336 } 337 338 } 339 340 public void setPrimitive(PrimitiveType primitive) { 341 this.primitive = primitive; 342 } 343 344 350 public Type getItemType() { 351 return itemType; 352 } 353 354 public void setItemType(Type type) { 356 itemType = type; 357 } 358 359 365 public ArrayList getMemberTypes() { 366 return memberTypes; 367 } 368 369 public SimpleType getMemberType(int index) { 370 if (index < memberTypes.size()) 371 return (SimpleType) memberTypes.get(index); 372 else 373 return null; 374 } 375 376 public void addMemberType(Type type) { 378 if (memberTypes == null) 379 memberTypes = new ArrayList (); 380 memberTypes.add(type); 381 } 382 383 public void addMemberTypes(java.util.List memberTypes) { 385 if (this.memberTypes == null) 386 this.memberTypes = new ArrayList (); 387 this.memberTypes.addAll(memberTypes); 388 } 389 390 public boolean isSimpleType() { 392 return true; 393 } 394 395 } 396 | Popular Tags |