1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 2001, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.impl.dv.xs;
  58. import java.util.StringTokenizer;
  59. import java.util.Vector;
  60. import com.sun.org.apache.xerces.internal.impl.Constants;
  61. import com.sun.org.apache.xerces.internal.impl.dv.DatatypeException;
  62. import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeFacetException;
  63. import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  64. import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
  65. import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  66. import com.sun.org.apache.xerces.internal.impl.dv.XSFacets;
  67. import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;
  68. import com.sun.org.apache.xerces.internal.impl.xpath.regex.RegularExpression;
  69. import com.sun.org.apache.xerces.internal.xs.StringList;
  70. import com.sun.org.apache.xerces.internal.xs.XSAnnotation;
  71. import com.sun.org.apache.xerces.internal.xs.XSConstants;
  72. import com.sun.org.apache.xerces.internal.xs.XSFacet;
  73. import com.sun.org.apache.xerces.internal.xs.XSMultiValueFacet;
  74. import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
  75. import com.sun.org.apache.xerces.internal.xs.XSObjectList;
  76. import com.sun.org.apache.xerces.internal.xs.XSSimpleTypeDefinition;
  77. import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
  78. import com.sun.org.apache.xerces.internal.impl.xs.util.ShortListImpl;
  79. import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
  80. import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
  81. import com.sun.org.apache.xerces.internal.util.XMLChar;
  82. import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  83. /**
  84. * @author Sandy Gao, IBM
  85. * @author Neeraj Bajaj, Sun Microsystems, inc.
  86. *
  87. * @version $Id: XSSimpleTypeDecl.java,v 1.48 2004/01/19 15:30:20 sandygao Exp $
  88. */
  89. public class XSSimpleTypeDecl implements XSSimpleType {
  90. static final short DV_STRING = PRIMITIVE_STRING;
  91. static final short DV_BOOLEAN = PRIMITIVE_BOOLEAN;
  92. static final short DV_DECIMAL = PRIMITIVE_DECIMAL;
  93. static final short DV_FLOAT = PRIMITIVE_FLOAT;
  94. static final short DV_DOUBLE = PRIMITIVE_DOUBLE;
  95. static final short DV_DURATION = PRIMITIVE_DURATION;
  96. static final short DV_DATETIME = PRIMITIVE_DATETIME;
  97. static final short DV_TIME = PRIMITIVE_TIME;
  98. static final short DV_DATE = PRIMITIVE_DATE;
  99. static final short DV_GYEARMONTH = PRIMITIVE_GYEARMONTH;
  100. static final short DV_GYEAR = PRIMITIVE_GYEAR;
  101. static final short DV_GMONTHDAY = PRIMITIVE_GMONTHDAY;
  102. static final short DV_GDAY = PRIMITIVE_GDAY;
  103. static final short DV_GMONTH = PRIMITIVE_GMONTH;
  104. static final short DV_HEXBINARY = PRIMITIVE_HEXBINARY;
  105. static final short DV_BASE64BINARY = PRIMITIVE_BASE64BINARY;
  106. static final short DV_ANYURI = PRIMITIVE_ANYURI;
  107. static final short DV_QNAME = PRIMITIVE_QNAME;
  108. static final short DV_NOTATION = PRIMITIVE_NOTATION;
  109. static final short DV_ANYSIMPLETYPE = 0;
  110. static final short DV_ID = DV_NOTATION + 1;
  111. static final short DV_IDREF = DV_NOTATION + 2;
  112. static final short DV_ENTITY = DV_NOTATION + 3;
  113. static final short DV_INTEGER = DV_NOTATION + 4;
  114. static final short DV_LIST = DV_NOTATION + 5;
  115. static final short DV_UNION = DV_NOTATION + 6;
  116. static final TypeValidator[] fDVs = {
  117. new AnySimpleDV(),
  118. new StringDV(),
  119. new BooleanDV(),
  120. new DecimalDV(),
  121. new FloatDV(),
  122. new DoubleDV(),
  123. new DurationDV(),
  124. new DateTimeDV(),
  125. new TimeDV(),
  126. new DateDV(),
  127. new YearMonthDV(),
  128. new YearDV(),
  129. new MonthDayDV(),
  130. new DayDV(),
  131. new MonthDV(),
  132. new HexBinaryDV(),
  133. new Base64BinaryDV(),
  134. new AnyURIDV(),
  135. new QNameDV(),
  136. new QNameDV(), // notation use the same one as qname
  137. new IDDV(),
  138. new IDREFDV(),
  139. new EntityDV(),
  140. new IntegerDV(),
  141. new ListDV(),
  142. new UnionDV()
  143. };
  144. static final short NORMALIZE_NONE = 0;
  145. static final short NORMALIZE_TRIM = 1;
  146. static final short NORMALIZE_FULL = 2;
  147. static final short[] fDVNormalizeType = {
  148. NORMALIZE_NONE, //AnySimpleDV(),
  149. NORMALIZE_FULL, //StringDV(),
  150. NORMALIZE_TRIM, //BooleanDV(),
  151. NORMALIZE_TRIM, //DecimalDV(),
  152. NORMALIZE_TRIM, //FloatDV(),
  153. NORMALIZE_TRIM, //DoubleDV(),
  154. NORMALIZE_TRIM, //DurationDV(),
  155. NORMALIZE_TRIM, //DateTimeDV(),
  156. NORMALIZE_TRIM, //TimeDV(),
  157. NORMALIZE_TRIM, //DateDV(),
  158. NORMALIZE_TRIM, //YearMonthDV(),
  159. NORMALIZE_TRIM, //YearDV(),
  160. NORMALIZE_TRIM, //MonthDayDV(),
  161. NORMALIZE_TRIM, //DayDV(),
  162. NORMALIZE_TRIM, //MonthDV(),
  163. NORMALIZE_TRIM, //HexBinaryDV(),
  164. NORMALIZE_NONE, //Base64BinaryDV(), // Base64 know how to deal with spaces
  165. NORMALIZE_TRIM, //AnyURIDV(),
  166. NORMALIZE_TRIM, //QNameDV(),
  167. NORMALIZE_TRIM, //QNameDV(), // notation
  168. NORMALIZE_TRIM, //IDDV(),
  169. NORMALIZE_TRIM, //IDREFDV(),
  170. NORMALIZE_TRIM, //EntityDV(),
  171. NORMALIZE_TRIM, //IntegerDV(),
  172. NORMALIZE_FULL, //ListDV(),
  173. NORMALIZE_NONE, //UnionDV()
  174. };
  175. static final short SPECIAL_PATTERN_NONE = 0;
  176. static final short SPECIAL_PATTERN_NMTOKEN = 1;
  177. static final short SPECIAL_PATTERN_NAME = 2;
  178. static final short SPECIAL_PATTERN_NCNAME = 3;
  179. static final String[] SPECIAL_PATTERN_STRING = {
  180. "NONE", "NMTOKEN", "Name", "NCName"
  181. };
  182. static final String[] WS_FACET_STRING = {
  183. "preserve", "replace", "collapse"
  184. };
  185. static final String URI_SCHEMAFORSCHEMA = "http://www.w3.org/2001/XMLSchema";
  186. static final String ANY_TYPE = "anyType";
  187. static final ValidationContext fEmptyContext = new ValidationContext() {
  188. public boolean needFacetChecking() {
  189. return true;
  190. }
  191. public boolean needExtraChecking() {
  192. return false;
  193. }
  194. public boolean needToNormalize() {
  195. return true;
  196. }
  197. public boolean useNamespaces () {
  198. return true;
  199. }
  200. public boolean isEntityDeclared (String name) {
  201. return false;
  202. }
  203. public boolean isEntityUnparsed (String name) {
  204. return false;
  205. }
  206. public boolean isIdDeclared (String name) {
  207. return false;
  208. }
  209. public void addId(String name) {
  210. }
  211. public void addIdRef(String name) {
  212. }
  213. public String getSymbol (String symbol) {
  214. return null;
  215. }
  216. public String getURI(String prefix) {
  217. return null;
  218. }
  219. };
  220. // this will be true if this is a static XSSimpleTypeDecl
  221. // and hence must remain immutable (i.e., applyFacets
  222. // may not be permitted to have any effect).
  223. private boolean fIsImmutable = false;
  224. private XSSimpleTypeDecl fItemType;
  225. private XSSimpleTypeDecl[] fMemberTypes;
  226. // The most specific built-in type kind.
  227. private short fBuiltInKind;
  228. private String fTypeName;
  229. private String fTargetNamespace;
  230. private short fFinalSet = 0;
  231. private XSSimpleTypeDecl fBase;
  232. private short fVariety = -1;
  233. private short fValidationDV = -1;
  234. private short fFacetsDefined = 0;
  235. private short fFixedFacet = 0;
  236. //for constraining facets
  237. private short fWhiteSpace = 0;
  238. private int fLength = -1;
  239. private int fMinLength = -1;
  240. private int fMaxLength = -1;
  241. private int fTotalDigits = -1;
  242. private int fFractionDigits = -1;
  243. private Vector fPattern;
  244. private Vector fPatternStr;
  245. private Vector fEnumeration;
  246. private StringList fLexicalPattern;
  247. private StringList fLexicalEnumeration;
  248. private Object fMaxInclusive;
  249. private Object fMaxExclusive;
  250. private Object fMinExclusive;
  251. private Object fMinInclusive;
  252. // annotations for constraining facets
  253. public XSAnnotation lengthAnnotation;
  254. public XSAnnotation minLengthAnnotation;
  255. public XSAnnotation maxLengthAnnotation;
  256. public XSAnnotation whiteSpaceAnnotation;
  257. public XSAnnotation totalDigitsAnnotation;
  258. public XSAnnotation fractionDigitsAnnotation;
  259. public XSObjectListImpl patternAnnotations;
  260. public XSObjectList enumerationAnnotations;
  261. public XSAnnotation maxInclusiveAnnotation;
  262. public XSAnnotation maxExclusiveAnnotation;
  263. public XSAnnotation minInclusiveAnnotation;
  264. public XSAnnotation minExclusiveAnnotation;
  265. // facets as objects
  266. private XSObjectListImpl fFacets;
  267. // enumeration and pattern facets
  268. private XSObjectListImpl fMultiValueFacets;
  269. // simpleType annotations
  270. private XSObjectList fAnnotations = null;
  271. private short fPatternType = SPECIAL_PATTERN_NONE;
  272. // for fundamental facets
  273. private short fOrdered;
  274. private boolean fFinite;
  275. private boolean fBounded;
  276. private boolean fNumeric;
  277. /**
  278. * Unique ID for each simple type.
  279. * This information is used to assign unique names for simple types.
  280. */
  281. private final int fUniqueId = getNextId();
  282. private static int fIdCounter=0;
  283. private static synchronized int getNextId() { return fIdCounter++; }
  284. // default constructor
  285. public XSSimpleTypeDecl(){}
  286. //Create a new built-in primitive types (and id/idref/entity/integer)
  287. protected XSSimpleTypeDecl(XSSimpleTypeDecl base, String name, short validateDV,
  288. short ordered, boolean bounded, boolean finite,
  289. boolean numeric, boolean isImmutable, short builtInKind) {
  290. fIsImmutable = isImmutable;
  291. fBase = base;
  292. fTypeName = name;
  293. fTargetNamespace = URI_SCHEMAFORSCHEMA;
  294. // To simplify the code for anySimpleType, we treat it as an atomic type
  295. fVariety = VARIETY_ATOMIC;
  296. fValidationDV = validateDV;
  297. fFacetsDefined = FACET_WHITESPACE;
  298. if (validateDV == DV_STRING) {
  299. fWhiteSpace = WS_PRESERVE;
  300. } else {
  301. fWhiteSpace = WS_COLLAPSE;
  302. fFixedFacet = FACET_WHITESPACE;
  303. }
  304. this.fOrdered = ordered;
  305. this.fBounded = bounded;
  306. this.fFinite = finite;
  307. this.fNumeric = numeric;
  308. fAnnotations = null;
  309. // Specify the build in kind for this primitive type
  310. fBuiltInKind = builtInKind;
  311. }
  312. //Create a new simple type for restriction for built-in types
  313. protected XSSimpleTypeDecl(XSSimpleTypeDecl base, String name, String uri, short finalSet, boolean isImmutable,
  314. XSObjectList annotations, short builtInKind) {
  315. this(base, name, uri, finalSet, isImmutable, annotations);
  316. // Specify the build in kind for this built-in type
  317. fBuiltInKind = builtInKind;
  318. }
  319. //Create a new simple type for restriction.
  320. protected XSSimpleTypeDecl(XSSimpleTypeDecl base, String name, String uri, short finalSet, boolean isImmutable,
  321. XSObjectList annotations) {
  322. fBase = base;
  323. fTypeName = name;
  324. fTargetNamespace = uri;
  325. fFinalSet = finalSet;
  326. fAnnotations = annotations;
  327. fVariety = fBase.fVariety;
  328. fValidationDV = fBase.fValidationDV;
  329. switch (fVariety) {
  330. case VARIETY_ATOMIC:
  331. break;
  332. case VARIETY_LIST:
  333. fItemType = fBase.fItemType;
  334. break;
  335. case VARIETY_UNION:
  336. fMemberTypes = fBase.fMemberTypes;
  337. break;
  338. }
  339. // always inherit facets from the base.
  340. // in case a type is created, but applyFacets is not called
  341. fLength = fBase.fLength;
  342. fMinLength = fBase.fMinLength;
  343. fMaxLength = fBase.fMaxLength;
  344. fPattern = fBase.fPattern;
  345. fPatternStr = fBase.fPatternStr;
  346. fEnumeration = fBase.fEnumeration;
  347. fWhiteSpace = fBase.fWhiteSpace;
  348. fMaxExclusive = fBase.fMaxExclusive;
  349. fMaxInclusive = fBase.fMaxInclusive;
  350. fMinExclusive = fBase.fMinExclusive;
  351. fMinInclusive = fBase.fMinInclusive;
  352. fTotalDigits = fBase.fTotalDigits;
  353. fFractionDigits = fBase.fFractionDigits;
  354. fPatternType = fBase.fPatternType;
  355. fFixedFacet = fBase.fFixedFacet;
  356. fFacetsDefined = fBase.fFacetsDefined;
  357. //we also set fundamental facets information in case applyFacets is not called.
  358. caclFundamentalFacets();
  359. fIsImmutable = isImmutable;
  360. // Inherit from the base type
  361. fBuiltInKind = base.fBuiltInKind;
  362. }
  363. //Create a new simple type for list.
  364. protected XSSimpleTypeDecl(String name, String uri, short finalSet, XSSimpleTypeDecl itemType, boolean isImmutable,
  365. XSObjectList annotations) {
  366. fBase = fAnySimpleType;
  367. fTypeName = name;
  368. fTargetNamespace = uri;
  369. fFinalSet = finalSet;
  370. fAnnotations = annotations;
  371. fVariety = VARIETY_LIST;
  372. fItemType = (XSSimpleTypeDecl)itemType;
  373. fValidationDV = DV_LIST;
  374. fFacetsDefined = FACET_WHITESPACE;
  375. fFixedFacet = FACET_WHITESPACE;
  376. fWhiteSpace = WS_COLLAPSE;
  377. //setting fundamental facets
  378. caclFundamentalFacets();
  379. fIsImmutable = isImmutable;
  380. // Values of this type are lists
  381. fBuiltInKind = XSConstants.LIST_DT;
  382. }
  383. //Create a new simple type for union.
  384. protected XSSimpleTypeDecl(String name, String uri, short finalSet, XSSimpleTypeDecl[] memberTypes,
  385. XSObjectList annotations) {
  386. fBase = fAnySimpleType;
  387. fTypeName = name;
  388. fTargetNamespace = uri;
  389. fFinalSet = finalSet;
  390. fAnnotations = annotations;
  391. fVariety = VARIETY_UNION;
  392. fMemberTypes = memberTypes;
  393. fValidationDV = DV_UNION;
  394. // even for union, we set whitespace to something
  395. // this will never be used, but we can use fFacetsDefined to check
  396. // whether applyFacets() is allwwed: it's not allowed
  397. // if fFacetsDefined != 0
  398. fFacetsDefined = FACET_WHITESPACE;
  399. fWhiteSpace = WS_COLLAPSE;
  400. //setting fundamental facets
  401. caclFundamentalFacets();
  402. // none of the schema-defined types are unions, so just set
  403. // fIsImmutable to false.
  404. fIsImmutable = false;
  405. // No value can be of this type, so it's unavailable.
  406. fBuiltInKind = XSConstants.UNAVAILABLE_DT;
  407. }
  408. //set values for restriction.
  409. protected XSSimpleTypeDecl setRestrictionValues(XSSimpleTypeDecl base, String name, String uri, short finalSet,
  410. XSObjectList annotations) {
  411. //decline to do anything if the object is immutable.
  412. if(fIsImmutable) return null;
  413. fBase = base;
  414. fTypeName = name;
  415. fTargetNamespace = uri;
  416. fFinalSet = finalSet;
  417. fAnnotations = annotations;
  418. fVariety = fBase.fVariety;
  419. fValidationDV = fBase.fValidationDV;
  420. switch (fVariety) {
  421. case VARIETY_ATOMIC:
  422. break;
  423. case VARIETY_LIST:
  424. fItemType = fBase.fItemType;
  425. break;
  426. case VARIETY_UNION:
  427. fMemberTypes = fBase.fMemberTypes;
  428. break;
  429. }
  430. // always inherit facets from the base.
  431. // in case a type is created, but applyFacets is not called
  432. fLength = fBase.fLength;
  433. fMinLength = fBase.fMinLength;
  434. fMaxLength = fBase.fMaxLength;
  435. fPattern = fBase.fPattern;
  436. fPatternStr = fBase.fPatternStr;
  437. fEnumeration = fBase.fEnumeration;
  438. fWhiteSpace = fBase.fWhiteSpace;
  439. fMaxExclusive = fBase.fMaxExclusive;
  440. fMaxInclusive = fBase.fMaxInclusive;
  441. fMinExclusive = fBase.fMinExclusive;
  442. fMinInclusive = fBase.fMinInclusive;
  443. fTotalDigits = fBase.fTotalDigits;
  444. fFractionDigits = fBase.fFractionDigits;
  445. fPatternType = fBase.fPatternType;
  446. fFixedFacet = fBase.fFixedFacet;
  447. fFacetsDefined = fBase.fFacetsDefined;
  448. //we also set fundamental facets information in case applyFacets is not called.
  449. caclFundamentalFacets();
  450. return this;
  451. }
  452. //set values for list.
  453. protected XSSimpleTypeDecl setListValues(String name, String uri, short finalSet, XSSimpleTypeDecl itemType,
  454. XSObjectList annotations) {
  455. //decline to do anything if the object is immutable.
  456. if(fIsImmutable) return null;
  457. fBase = fAnySimpleType;
  458. fTypeName = name;
  459. fTargetNamespace = uri;
  460. fFinalSet = finalSet;
  461. fAnnotations = annotations;
  462. fVariety = VARIETY_LIST;
  463. fItemType = (XSSimpleTypeDecl)itemType;
  464. fValidationDV = DV_LIST;
  465. fFacetsDefined = FACET_WHITESPACE;
  466. fFixedFacet = FACET_WHITESPACE;
  467. fWhiteSpace = WS_COLLAPSE;
  468. //setting fundamental facets
  469. caclFundamentalFacets();
  470. return this;
  471. }
  472. //set values for union.
  473. protected XSSimpleTypeDecl setUnionValues(String name, String uri, short finalSet, XSSimpleTypeDecl[] memberTypes,
  474. XSObjectList annotations) {
  475. //decline to do anything if the object is immutable.
  476. if(fIsImmutable) return null;
  477. fBase = fAnySimpleType;
  478. fTypeName = name;
  479. fTargetNamespace = uri;
  480. fFinalSet = finalSet;
  481. fAnnotations = annotations;
  482. fVariety = VARIETY_UNION;
  483. fMemberTypes = memberTypes;
  484. fValidationDV = DV_UNION;
  485. // even for union, we set whitespace to something
  486. // this will never be used, but we can use fFacetsDefined to check
  487. // whether applyFacets() is allwwed: it's not allowed
  488. // if fFacetsDefined != 0
  489. fFacetsDefined = FACET_WHITESPACE;
  490. fWhiteSpace = WS_COLLAPSE;
  491. //setting fundamental facets
  492. caclFundamentalFacets();
  493. return this;
  494. }
  495. public short getType () {
  496. return XSConstants.TYPE_DEFINITION;
  497. }
  498. public short getTypeCategory () {
  499. return SIMPLE_TYPE;
  500. }
  501. public String getName() {
  502. return fTypeName;
  503. }
  504. public String getNamespace() {
  505. return fTargetNamespace;
  506. }
  507. public short getFinal(){
  508. return fFinalSet;
  509. }
  510. public boolean isFinal(short derivation) {
  511. return (fFinalSet & derivation) != 0;
  512. }
  513. public XSTypeDefinition getBaseType(){
  514. return fBase;
  515. }
  516. public boolean getAnonymous() {
  517. return fTypeName == null;
  518. }
  519. public short getVariety(){
  520. // for anySimpleType, return absent variaty
  521. return fValidationDV == DV_ANYSIMPLETYPE ? VARIETY_ABSENT : fVariety;
  522. }
  523. public boolean isIDType(){
  524. switch (fVariety) {
  525. case VARIETY_ATOMIC:
  526. return fValidationDV == DV_ID;
  527. case VARIETY_LIST:
  528. return fItemType.isIDType();
  529. case VARIETY_UNION:
  530. for (int i = 0; i < fMemberTypes.length; i++) {
  531. if (fMemberTypes[i].isIDType())
  532. return true;
  533. }
  534. }
  535. return false;
  536. }
  537. public short getWhitespace() throws DatatypeException{
  538. if (fVariety == VARIETY_UNION) {
  539. throw new DatatypeException("dt-whitespace", new Object[]{fTypeName});
  540. }
  541. return fWhiteSpace;
  542. }
  543. public short getPrimitiveKind() {
  544. if (fVariety == VARIETY_ATOMIC && fValidationDV != DV_ANYSIMPLETYPE) {
  545. if (fValidationDV == DV_ID || fValidationDV == DV_IDREF || fValidationDV == DV_ENTITY)
  546. return DV_STRING;
  547. else if (fValidationDV == DV_INTEGER)
  548. return DV_DECIMAL;
  549. else
  550. return fValidationDV;
  551. }
  552. else {
  553. // REVISIT: error situation. runtime exception?
  554. return (short)0;
  555. }
  556. }
  557. public short getBuiltInKind() {
  558. return this.fBuiltInKind;
  559. }
  560. public XSSimpleTypeDefinition getPrimitiveType() {
  561. if (fVariety == VARIETY_ATOMIC && fValidationDV != DV_ANYSIMPLETYPE) {
  562. XSSimpleTypeDecl pri = this;
  563. // recursively get base, until we reach anySimpleType
  564. while (pri.fBase != fAnySimpleType)
  565. pri = pri.fBase;
  566. return pri;
  567. }
  568. else {
  569. // REVISIT: error situation. runtime exception?
  570. return null;
  571. }
  572. }
  573. public XSSimpleTypeDefinition getItemType() {
  574. if (fVariety == VARIETY_LIST) {
  575. return fItemType;
  576. }
  577. else {
  578. // REVISIT: error situation. runtime exception?
  579. return null;
  580. }
  581. }
  582. public XSObjectList getMemberTypes() {
  583. if (fVariety == VARIETY_UNION) {
  584. return new XSObjectListImpl(fMemberTypes, fMemberTypes.length);
  585. }
  586. else {
  587. // REVISIT: error situation. runtime exception?
  588. return null;
  589. }
  590. }
  591. /**
  592. * If <restriction> is chosen
  593. */
  594. public void applyFacets(XSFacets facets, short presentFacet, short fixedFacet, ValidationContext context)
  595. throws InvalidDatatypeFacetException {
  596. applyFacets(facets, presentFacet, fixedFacet, SPECIAL_PATTERN_NONE, context);
  597. }
  598. /**
  599. * built-in derived types by restriction
  600. */
  601. void applyFacets1(XSFacets facets, short presentFacet, short fixedFacet) {
  602. try {
  603. applyFacets(facets, presentFacet, fixedFacet, SPECIAL_PATTERN_NONE, fDummyContext);
  604. } catch (InvalidDatatypeFacetException e) {
  605. // should never gets here, internel error
  606. throw new RuntimeException("internal error");
  607. }
  608. // we've now applied facets; so lock this object:
  609. fIsImmutable = true;
  610. }
  611. /**
  612. * built-in derived types by restriction
  613. */
  614. void applyFacets1(XSFacets facets, short presentFacet, short fixedFacet, short patternType) {
  615. try {
  616. applyFacets(facets, presentFacet, fixedFacet, patternType, fDummyContext);
  617. } catch (InvalidDatatypeFacetException e) {
  618. // should never gets here, internel error
  619. throw new RuntimeException("internal error");
  620. }
  621. // we've now applied facets; so lock this object:
  622. fIsImmutable = true;
  623. }
  624. /**
  625. * If <restriction> is chosen, or built-in derived types by restriction
  626. */
  627. void applyFacets(XSFacets facets, short presentFacet, short fixedFacet, short patternType, ValidationContext context)
  628. throws InvalidDatatypeFacetException {
  629. // if the object is immutable, should not apply facets...
  630. if(fIsImmutable) return;
  631. ValidatedInfo tempInfo = new ValidatedInfo();
  632. // clear facets. because we always inherit facets in the constructor
  633. // REVISIT: in fact, we don't need to clear them.
  634. // we can convert 5 string values (4 bounds + 1 enum) to actual values,
  635. // store them somewhere, then do facet checking at once, instead of
  636. // going through the following steps. (lots of checking are redundant:
  637. // for example, ((presentFacet & FACET_XXX) != 0))
  638. fFacetsDefined = 0;
  639. fFixedFacet = 0;
  640. int result = 0 ;
  641. // step 1: parse present facets
  642. short allowedFacet = fDVs[fValidationDV].getAllowedFacets();
  643. // length
  644. if ((presentFacet & FACET_LENGTH) != 0) {
  645. if ((allowedFacet & FACET_LENGTH) == 0) {
  646. reportError("cos-applicable-facets", new Object[]{"length", fTypeName});
  647. } else {
  648. fLength = facets.length;
  649. lengthAnnotation = facets.lengthAnnotation;
  650. fFacetsDefined |= FACET_LENGTH;
  651. if ((fixedFacet & FACET_LENGTH) != 0)
  652. fFixedFacet |= FACET_LENGTH;
  653. }
  654. }
  655. // minLength
  656. if ((presentFacet & FACET_MINLENGTH) != 0) {
  657. if ((allowedFacet & FACET_MINLENGTH) == 0) {
  658. reportError("cos-applicable-facets", new Object[]{"minLength", fTypeName});
  659. } else {
  660. fMinLength = facets.minLength;
  661. minLengthAnnotation = facets.minLengthAnnotation;
  662. fFacetsDefined |= FACET_MINLENGTH;
  663. if ((fixedFacet & FACET_MINLENGTH) != 0)
  664. fFixedFacet |= FACET_MINLENGTH;
  665. }
  666. }
  667. // maxLength
  668. if ((presentFacet & FACET_MAXLENGTH) != 0) {
  669. if ((allowedFacet & FACET_MAXLENGTH) == 0) {
  670. reportError("cos-applicable-facets", new Object[]{"maxLength", fTypeName});
  671. } else {
  672. fMaxLength = facets.maxLength;
  673. maxLengthAnnotation = facets.maxLengthAnnotation;
  674. fFacetsDefined |= FACET_MAXLENGTH;
  675. if ((fixedFacet & FACET_MAXLENGTH) != 0)
  676. fFixedFacet |= FACET_MAXLENGTH;
  677. }
  678. }
  679. // pattern
  680. if ((presentFacet & FACET_PATTERN) != 0) {
  681. if ((allowedFacet & FACET_PATTERN) == 0) {
  682. reportError("cos-applicable-facets", new Object[]{"pattern", fTypeName});
  683. } else {
  684. patternAnnotations = facets.patternAnnotations;
  685. RegularExpression regex = null;
  686. try {
  687. regex = new RegularExpression(facets.pattern, "X");
  688. } catch (Exception e) {
  689. reportError("InvalidRegex", new Object[]{facets.pattern, e.getLocalizedMessage()});
  690. }
  691. if (regex != null) {
  692. fPattern = new Vector();
  693. fPattern.addElement(regex);
  694. fPatternStr = new Vector();
  695. fPatternStr.addElement(facets.pattern);
  696. fFacetsDefined |= FACET_PATTERN;
  697. if ((fixedFacet & FACET_PATTERN) != 0)
  698. fFixedFacet |= FACET_PATTERN;
  699. }
  700. }
  701. }
  702. // enumeration
  703. if ((presentFacet & FACET_ENUMERATION) != 0) {
  704. if ((allowedFacet & FACET_ENUMERATION) == 0) {
  705. reportError("cos-applicable-facets", new Object[]{"enumeration", fTypeName});
  706. } else {
  707. fEnumeration = new Vector();
  708. Vector enumVals = facets.enumeration;
  709. Vector enumNSDecls = facets.enumNSDecls;
  710. ValidationContextImpl ctx = new ValidationContextImpl(context);
  711. enumerationAnnotations = facets.enumAnnotations;
  712. for (int i = 0; i < enumVals.size(); i++) {
  713. if (enumNSDecls != null)
  714. ctx.setNSContext((NamespaceContext)enumNSDecls.elementAt(i));
  715. try {
  716. // check 4.3.5.c0 must: enumeration values from the value space of base
  717. fEnumeration.addElement(this.fBase.validate((String)enumVals.elementAt(i), ctx, tempInfo));
  718. } catch (InvalidDatatypeValueException ide) {
  719. reportError("enumeration-valid-restriction", new Object[]{enumVals.elementAt(i), this.getBaseType().getName()});
  720. }
  721. }
  722. fFacetsDefined |= FACET_ENUMERATION;
  723. if ((fixedFacet & FACET_ENUMERATION) != 0)
  724. fFixedFacet |= FACET_ENUMERATION;
  725. }
  726. }
  727. // whiteSpace
  728. if ((presentFacet & FACET_WHITESPACE) != 0) {
  729. if ((allowedFacet & FACET_WHITESPACE) == 0) {
  730. reportError("cos-applicable-facets", new Object[]{"whiteSpace", fTypeName});
  731. } else {
  732. fWhiteSpace = facets.whiteSpace;
  733. whiteSpaceAnnotation = facets.whiteSpaceAnnotation;
  734. fFacetsDefined |= FACET_WHITESPACE;
  735. if ((fixedFacet & FACET_WHITESPACE) != 0)
  736. fFixedFacet |= FACET_WHITESPACE;
  737. }
  738. }
  739. boolean needCheckBase = true;
  740. // maxInclusive
  741. if ((presentFacet & FACET_MAXINCLUSIVE) != 0) {
  742. if ((allowedFacet & FACET_MAXINCLUSIVE) == 0) {
  743. reportError("cos-applicable-facets", new Object[]{"maxInclusive", fTypeName});
  744. } else {
  745. maxInclusiveAnnotation = facets.maxInclusiveAnnotation;
  746. try {
  747. fMaxInclusive = getActualValue(facets.maxInclusive, context, tempInfo, true);
  748. fFacetsDefined |= FACET_MAXINCLUSIVE;
  749. if ((fixedFacet & FACET_MAXINCLUSIVE) != 0)
  750. fFixedFacet |= FACET_MAXINCLUSIVE;
  751. } catch (InvalidDatatypeValueException ide) {
  752. reportError(ide.getKey(), ide.getArgs());
  753. reportError("FacetValueFromBase", new Object[]{fTypeName, facets.maxInclusive,
  754. "maxInclusive", fBase.getName()});
  755. }
  756. // maxInclusive from base
  757. if (((fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
  758. result = fDVs[fValidationDV].compare(fMaxInclusive, fBase.fMaxInclusive);
  759. if ((fBase.fFixedFacet & FACET_MAXINCLUSIVE) != 0 && result != 0) {
  760. reportError( "FixedFacetValue", new Object[]{"maxInclusive", fMaxInclusive, fBase.fMaxInclusive, fTypeName});
  761. }
  762. if (result == 0) {
  763. needCheckBase = false;
  764. }
  765. }
  766. if (needCheckBase) {
  767. try {
  768. fBase.validate(context, tempInfo);
  769. } catch (InvalidDatatypeValueException ide) {
  770. reportError(ide.getKey(), ide.getArgs());
  771. reportError("FacetValueFromBase", new Object[]{fTypeName, facets.maxInclusive,
  772. "maxInclusive", fBase.getName()});
  773. }
  774. }
  775. }
  776. }
  777. // maxExclusive
  778. needCheckBase = true;
  779. if ((presentFacet & FACET_MAXEXCLUSIVE) != 0) {
  780. if ((allowedFacet & FACET_MAXEXCLUSIVE) == 0) {
  781. reportError("cos-applicable-facets", new Object[]{"maxExclusive", fTypeName});
  782. } else {
  783. maxExclusiveAnnotation = facets.maxExclusiveAnnotation;
  784. try {
  785. fMaxExclusive = getActualValue(facets.maxExclusive, context, tempInfo, true);
  786. fFacetsDefined |= FACET_MAXEXCLUSIVE;
  787. if ((fixedFacet & FACET_MAXEXCLUSIVE) != 0)
  788. fFixedFacet |= FACET_MAXEXCLUSIVE;
  789. } catch (InvalidDatatypeValueException ide) {
  790. reportError(ide.getKey(), ide.getArgs());
  791. reportError("FacetValueFromBase", new Object[]{fTypeName, facets.maxExclusive,
  792. "maxExclusive", fBase.getName()});
  793. }
  794. // maxExclusive from base
  795. if (((fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0)) {
  796. result = fDVs[fValidationDV].compare(fMaxExclusive, fBase.fMaxExclusive);
  797. if ((fBase.fFixedFacet & FACET_MAXEXCLUSIVE) != 0 && result != 0) {
  798. reportError( "FixedFacetValue", new Object[]{"maxExclusive", facets.maxExclusive, fBase.fMaxExclusive, fTypeName});
  799. }
  800. if (result == 0) {
  801. needCheckBase = false;
  802. }
  803. }
  804. if (needCheckBase) {
  805. try {
  806. fBase.validate(context, tempInfo);
  807. } catch (InvalidDatatypeValueException ide) {
  808. reportError(ide.getKey(), ide.getArgs());
  809. reportError("FacetValueFromBase", new Object[]{fTypeName, facets.maxExclusive,
  810. "maxExclusive", fBase.getName()});
  811. }
  812. }
  813. }
  814. }
  815. // minExclusive
  816. needCheckBase = true;
  817. if ((presentFacet & FACET_MINEXCLUSIVE) != 0) {
  818. if ((allowedFacet & FACET_MINEXCLUSIVE) == 0) {
  819. reportError("cos-applicable-facets", new Object[]{"minExclusive", fTypeName});
  820. } else {
  821. minExclusiveAnnotation = facets.minExclusiveAnnotation;
  822. try {
  823. fMinExclusive = getActualValue(facets.minExclusive, context, tempInfo, true);
  824. fFacetsDefined |= FACET_MINEXCLUSIVE;
  825. if ((fixedFacet & FACET_MINEXCLUSIVE) != 0)
  826. fFixedFacet |= FACET_MINEXCLUSIVE;
  827. } catch (InvalidDatatypeValueException ide) {
  828. reportError(ide.getKey(), ide.getArgs());
  829. reportError("FacetValueFromBase", new Object[]{fTypeName, facets.minExclusive,
  830. "minExclusive", fBase.getName()});
  831. }
  832. // minExclusive from base
  833. if (((fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0)) {
  834. result = fDVs[fValidationDV].compare(fMinExclusive, fBase.fMinExclusive);
  835. if ((fBase.fFixedFacet & FACET_MINEXCLUSIVE) != 0 && result != 0) {
  836. reportError( "FixedFacetValue", new Object[]{"minExclusive", facets.minExclusive, fBase.fMinExclusive, fTypeName});
  837. }
  838. if (result == 0) {
  839. needCheckBase = false;
  840. }
  841. }
  842. if (needCheckBase) {
  843. try {
  844. fBase.validate(context, tempInfo);
  845. } catch (InvalidDatatypeValueException ide) {
  846. reportError(ide.getKey(), ide.getArgs());
  847. reportError("FacetValueFromBase", new Object[]{fTypeName, facets.minExclusive,
  848. "minExclusive", fBase.getName()});
  849. }
  850. }
  851. }
  852. }
  853. // minInclusive
  854. needCheckBase = true;
  855. if ((presentFacet & FACET_MININCLUSIVE) != 0) {
  856. if ((allowedFacet & FACET_MININCLUSIVE) == 0) {
  857. reportError("cos-applicable-facets", new Object[]{"minInclusive", fTypeName});
  858. } else {
  859. minInclusiveAnnotation = facets.minInclusiveAnnotation;
  860. try {
  861. fMinInclusive = getActualValue(facets.minInclusive, context, tempInfo, true);
  862. fFacetsDefined |= FACET_MININCLUSIVE;
  863. if ((fixedFacet & FACET_MININCLUSIVE) != 0)
  864. fFixedFacet |= FACET_MININCLUSIVE;
  865. } catch (InvalidDatatypeValueException ide) {
  866. reportError(ide.getKey(), ide.getArgs());
  867. reportError("FacetValueFromBase", new Object[]{fTypeName, facets.minInclusive,
  868. "minInclusive", fBase.getName()});
  869. }
  870. // minInclusive from base
  871. if (((fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
  872. result = fDVs[fValidationDV].compare(fMinInclusive, fBase.fMinInclusive);
  873. if ((fBase.fFixedFacet & FACET_MININCLUSIVE) != 0 && result != 0) {
  874. reportError( "FixedFacetValue", new Object[]{"minInclusive", facets.minInclusive, fBase.fMinInclusive, fTypeName});
  875. }
  876. if (result == 0) {
  877. needCheckBase = false;
  878. }
  879. }
  880. if (needCheckBase) {
  881. try {
  882. fBase.validate(context, tempInfo);
  883. } catch (InvalidDatatypeValueException ide) {
  884. reportError(ide.getKey(), ide.getArgs());
  885. reportError("FacetValueFromBase", new Object[]{fTypeName, facets.minInclusive,
  886. "minInclusive", fBase.getName()});
  887. }
  888. }
  889. }
  890. }
  891. // totalDigits
  892. if ((presentFacet & FACET_TOTALDIGITS) != 0) {
  893. if ((allowedFacet & FACET_TOTALDIGITS) == 0) {
  894. reportError("cos-applicable-facets", new Object[]{"totalDigits", fTypeName});
  895. } else {
  896. totalDigitsAnnotation = facets.totalDigitsAnnotation;
  897. fTotalDigits = facets.totalDigits;
  898. fFacetsDefined |= FACET_TOTALDIGITS;
  899. if ((fixedFacet & FACET_TOTALDIGITS) != 0)
  900. fFixedFacet |= FACET_TOTALDIGITS;
  901. }
  902. }
  903. // fractionDigits
  904. if ((presentFacet & FACET_FRACTIONDIGITS) != 0) {
  905. if ((allowedFacet & FACET_FRACTIONDIGITS) == 0) {
  906. reportError("cos-applicable-facets", new Object[]{"fractionDigits", fTypeName});
  907. } else {
  908. fFractionDigits = facets.fractionDigits;
  909. fractionDigitsAnnotation = facets.fractionDigitsAnnotation;
  910. fFacetsDefined |= FACET_FRACTIONDIGITS;
  911. if ((fixedFacet & FACET_FRACTIONDIGITS) != 0)
  912. fFixedFacet |= FACET_FRACTIONDIGITS;
  913. }
  914. }
  915. // token type: internal use, so do less checking
  916. if (patternType != SPECIAL_PATTERN_NONE) {
  917. fPatternType = patternType;
  918. }
  919. // step 2: check facets against each other: length, bounds
  920. if(fFacetsDefined != 0) {
  921. // check 4.3.1.c1 error: length & (maxLength | minLength)
  922. if((fFacetsDefined & FACET_LENGTH) != 0 ){
  923. if ((fFacetsDefined & FACET_MINLENGTH) != 0) {
  924. if ((fFacetsDefined & FACET_MAXLENGTH) != 0) {
  925. // length, minLength and maxLength defined
  926. reportError("length-minLength-maxLength.a", new Object[]{fTypeName, Integer.toString(fLength), Integer.toString(fMinLength), Integer.toString(fMaxLength)});
  927. }
  928. else {
  929. // length and minLength defined
  930. reportError("length-minLength-maxLength.b", new Object[]{fTypeName, Integer.toString(fLength), Integer.toString(fMinLength)});
  931. }
  932. }
  933. else if ((fFacetsDefined & FACET_MAXLENGTH) != 0) {
  934. // length and maxLength defined
  935. reportError("length-minLength-maxLength.c", new Object[]{fTypeName, Integer.toString(fLength), Integer.toString(fMaxLength)});
  936. }
  937. }
  938. // check 4.3.2.c1 must: minLength <= maxLength
  939. if(((fFacetsDefined & FACET_MINLENGTH ) != 0 ) && ((fFacetsDefined & FACET_MAXLENGTH) != 0))
  940. {
  941. if(fMinLength > fMaxLength)
  942. reportError("minLength-less-than-equal-to-maxLength", new Object[]{Integer.toString(fMinLength), Integer.toString(fMaxLength), fTypeName});
  943. }
  944. // check 4.3.8.c1 error: maxInclusive + maxExclusive
  945. if (((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) && ((fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
  946. reportError( "maxInclusive-maxExclusive", new Object[]{fMaxInclusive, fMaxExclusive, fTypeName});
  947. }
  948. // check 4.3.9.c1 error: minInclusive + minExclusive
  949. if (((fFacetsDefined & FACET_MINEXCLUSIVE) != 0) && ((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
  950. reportError("minInclusive-minExclusive", new Object[]{fMinInclusive, fMinExclusive, fTypeName});
  951. }
  952. // check 4.3.7.c1 must: minInclusive <= maxInclusive
  953. if (((fFacetsDefined & FACET_MAXINCLUSIVE) != 0) && ((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
  954. result = fDVs[fValidationDV].compare(fMinInclusive, fMaxInclusive);
  955. if (result != -1 && result != 0)
  956. reportError("minInclusive-less-than-equal-to-maxInclusive", new Object[]{fMinInclusive, fMaxInclusive, fTypeName});
  957. }
  958. // check 4.3.8.c2 must: minExclusive <= maxExclusive ??? minExclusive < maxExclusive
  959. if (((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) && ((fFacetsDefined & FACET_MINEXCLUSIVE) != 0)) {
  960. result = fDVs[fValidationDV].compare(fMinExclusive, fMaxExclusive);
  961. if (result != -1 && result != 0)
  962. reportError( "minExclusive-less-than-equal-to-maxExclusive", new Object[]{fMinExclusive, fMaxExclusive, fTypeName});
  963. }
  964. // check 4.3.9.c2 must: minExclusive < maxInclusive
  965. if (((fFacetsDefined & FACET_MAXINCLUSIVE) != 0) && ((fFacetsDefined & FACET_MINEXCLUSIVE) != 0)) {
  966. if (fDVs[fValidationDV].compare(fMinExclusive, fMaxInclusive) != -1)
  967. reportError( "minExclusive-less-than-maxInclusive", new Object[]{fMinExclusive, fMaxInclusive, fTypeName});
  968. }
  969. // check 4.3.10.c1 must: minInclusive < maxExclusive
  970. if (((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) && ((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
  971. if (fDVs[fValidationDV].compare(fMinInclusive, fMaxExclusive) != -1)
  972. reportError( "minInclusive-less-than-maxExclusive", new Object[]{fMinInclusive, fMaxExclusive, fTypeName});
  973. }
  974. // check 4.3.12.c1 must: fractionDigits <= totalDigits
  975. if (((fFacetsDefined & FACET_FRACTIONDIGITS) != 0) &&
  976. ((fFacetsDefined & FACET_TOTALDIGITS) != 0)) {
  977. if (fFractionDigits > fTotalDigits)
  978. reportError( "fractionDigits-totalDigits", new Object[]{Integer.toString(fFractionDigits), Integer.toString(fTotalDigits), fTypeName});
  979. }
  980. // step 3: check facets against base
  981. // check 4.3.1.c1 error: length & (fBase.maxLength | fBase.minLength)
  982. if((fFacetsDefined & FACET_LENGTH) != 0 ){
  983. if ((fBase.fFacetsDefined & FACET_MINLENGTH) != 0 &&
  984. fLength < fBase.fMinLength) {
  985. // length, fBase.minLength and fBase.maxLength defined
  986. reportError("length-minLength-maxLength.d", new Object[]{fTypeName, Integer.toString(fLength), Integer.toString(fBase.fMinLength)});
  987. }
  988. if ((fBase.fFacetsDefined & FACET_MAXLENGTH) != 0 &&
  989. fLength > fBase.fMaxLength) {
  990. // length and fBase.maxLength defined
  991. reportError("length-minLength-maxLength.e", new Object[]{fTypeName, Integer.toString(fLength), Integer.toString(fBase.fMaxLength)});
  992. }
  993. if ( (fBase.fFacetsDefined & FACET_LENGTH) != 0 ) {
  994. // check 4.3.1.c2 error: length != fBase.length
  995. if ( fLength != fBase.fLength )
  996. reportError( "length-valid-restriction", new Object[]{Integer.toString(fLength), Integer.toString(fBase.fLength), fTypeName});
  997. }
  998. }
  999. // check 4.3.1.c1 error: fBase.length & (maxLength | minLength)
  1000. else if((fBase.fFacetsDefined & FACET_LENGTH) != 0 ){
  1001. if ((fFacetsDefined & FACET_MINLENGTH) != 0 &&
  1002. fBase.fLength < fMinLength) {
  1003. // fBase.length, minLength and maxLength defined
  1004. reportError("length-minLength-maxLength.d", new Object[]{fTypeName, Integer.toString(fBase.fLength), Integer.toString(fMinLength)});
  1005. }
  1006. if ((fFacetsDefined & FACET_MAXLENGTH) != 0 &&
  1007. fBase.fLength > fMaxLength) {
  1008. // fBase.length and maxLength defined
  1009. reportError("length-minLength-maxLength.e", new Object[]{this, Integer.toString(fBase.fLength), Integer.toString(fMaxLength)});
  1010. }
  1011. }
  1012. // check 4.3.2.c1 must: minLength <= fBase.maxLength
  1013. if ( ((fFacetsDefined & FACET_MINLENGTH ) != 0 ) ) {
  1014. if ( (fBase.fFacetsDefined & FACET_MAXLENGTH ) != 0 ) {
  1015. if ( fMinLength > fBase.fMaxLength ) {
  1016. reportError("minLength-less-than-equal-to-maxLength", new Object[]{Integer.toString(fMinLength), Integer.toString(fBase.fMaxLength), fTypeName});
  1017. }
  1018. }
  1019. else if ( (fBase.fFacetsDefined & FACET_MINLENGTH) != 0 ) {
  1020. if ( (fBase.fFixedFacet & FACET_MINLENGTH) != 0 && fMinLength != fBase.fMinLength ) {
  1021. reportError( "FixedFacetValue", new Object[]{"minLength", Integer.toString(fMinLength), Integer.toString(fBase.fMinLength), fTypeName});
  1022. }
  1023. // check 4.3.2.c2 error: minLength < fBase.minLength
  1024. if ( fMinLength < fBase.fMinLength ) {
  1025. reportError( "minLength-valid-restriction", new Object[]{Integer.toString(fMinLength), Integer.toString(fBase.fMinLength), fTypeName});
  1026. }
  1027. }
  1028. }
  1029. // check 4.3.2.c1 must: maxLength < fBase.minLength
  1030. if ( ((fFacetsDefined & FACET_MAXLENGTH ) != 0 ) && ((fBase.fFacetsDefined & FACET_MINLENGTH ) != 0 )) {
  1031. if ( fMaxLength < fBase.fMinLength) {
  1032. reportError("minLength-less-than-equal-to-maxLength", new Object[]{Integer.toString(fBase.fMinLength), Integer.toString(fMaxLength)});
  1033. }
  1034. }
  1035. // check 4.3.3.c1 error: maxLength > fBase.maxLength
  1036. if ( (fFacetsDefined & FACET_MAXLENGTH) != 0 ) {
  1037. if ( (fBase.fFacetsDefined & FACET_MAXLENGTH) != 0 ){
  1038. if(( (fBase.fFixedFacet & FACET_MAXLENGTH) != 0 )&& fMaxLength != fBase.fMaxLength ) {
  1039. reportError( "FixedFacetValue", new Object[]{"maxLength", Integer.toString(fMaxLength), Integer.toString(fBase.fMaxLength), fTypeName});
  1040. }
  1041. if ( fMaxLength > fBase.fMaxLength ) {
  1042. reportError( "maxLength-valid-restriction", new Object[]{Integer.toString(fMaxLength), Integer.toString(fBase.fMaxLength), fTypeName});
  1043. }
  1044. }
  1045. }
  1046. /* // check 4.3.7.c2 error:
  1047. // maxInclusive > fBase.maxInclusive
  1048. // maxInclusive >= fBase.maxExclusive
  1049. // maxInclusive < fBase.minInclusive
  1050. // maxInclusive <= fBase.minExclusive
  1051. if (((fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
  1052. if (((fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
  1053. result = fDVs[fValidationDV].compare(fMaxInclusive, fBase.fMaxInclusive);
  1054. if ((fBase.fFixedFacet & FACET_MAXINCLUSIVE) != 0 && result != 0) {
  1055. reportError( "FixedFacetValue", new Object[]{"maxInclusive", fMaxInclusive, fBase.fMaxInclusive, fTypeName});
  1056. }
  1057. if (result != -1 && result != 0) {
  1058. reportError( "maxInclusive-valid-restriction.1", new Object[]{fMaxInclusive, fBase.fMaxInclusive, fTypeName});
  1059. }
  1060. }
  1061. if (((fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) &&
  1062. fDVs[fValidationDV].compare(fMaxInclusive, fBase.fMaxExclusive) != -1){
  1063. reportError( "maxInclusive-valid-restriction.1", new Object[]{fMaxInclusive, fBase.fMaxExclusive, fTypeName});
  1064. }
  1065. if ((( fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
  1066. result = fDVs[fValidationDV].compare(fMaxInclusive, fBase.fMinInclusive);
  1067. if (result != 1 && result != 0) {
  1068. reportError( "maxInclusive-valid-restriction.1", new Object[]{fMaxInclusive, fBase.fMinInclusive, fTypeName});
  1069. }
  1070. }
  1071. if ((( fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0) &&
  1072. fDVs[fValidationDV].compare(fMaxInclusive, fBase.fMinExclusive ) != 1)
  1073. reportError( "maxInclusive-valid-restriction.1", new Object[]{fMaxInclusive, fBase.fMinExclusive, fTypeName});
  1074. }
  1075. // check 4.3.8.c3 error:
  1076. // maxExclusive > fBase.maxExclusive
  1077. // maxExclusive > fBase.maxInclusive
  1078. // maxExclusive <= fBase.minInclusive
  1079. // maxExclusive <= fBase.minExclusive
  1080. if (((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0)) {
  1081. if ((( fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0)) {
  1082. result= fDVs[fValidationDV].compare(fMaxExclusive, fBase.fMaxExclusive);
  1083. if ((fBase.fFixedFacet & FACET_MAXEXCLUSIVE) != 0 && result != 0) {
  1084. reportError( "FixedFacetValue", new Object[]{"maxExclusive", fMaxExclusive, fBase.fMaxExclusive, fTypeName});
  1085. }
  1086. if (result != -1 && result != 0) {
  1087. reportError( "maxExclusive-valid-restriction.1", new Object[]{fMaxExclusive, fBase.fMaxExclusive, fTypeName});
  1088. }
  1089. }
  1090. if ((( fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
  1091. result= fDVs[fValidationDV].compare(fMaxExclusive, fBase.fMaxInclusive);
  1092. if (result != -1 && result != 0) {
  1093. reportError( "maxExclusive-valid-restriction.2", new Object[]{fMaxExclusive, fBase.fMaxInclusive, fTypeName});
  1094. }
  1095. }
  1096. if ((( fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0) &&
  1097. fDVs[fValidationDV].compare(fMaxExclusive, fBase.fMinExclusive ) != 1)
  1098. reportError( "maxExclusive-valid-restriction.3", new Object[]{fMaxExclusive, fBase.fMinExclusive, fTypeName});
  1099. if ((( fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0) &&
  1100. fDVs[fValidationDV].compare(fMaxExclusive, fBase.fMinInclusive) != 1)
  1101. reportError( "maxExclusive-valid-restriction.4", new Object[]{fMaxExclusive, fBase.fMinInclusive, fTypeName});
  1102. }
  1103. // check 4.3.9.c3 error:
  1104. // minExclusive < fBase.minExclusive
  1105. // minExclusive > fBase.maxInclusive
  1106. // minExclusive < fBase.minInclusive
  1107. // minExclusive >= fBase.maxExclusive
  1108. if (((fFacetsDefined & FACET_MINEXCLUSIVE) != 0)) {
  1109. if ((( fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0)) {
  1110. result= fDVs[fValidationDV].compare(fMinExclusive, fBase.fMinExclusive);
  1111. if ((fBase.fFixedFacet & FACET_MINEXCLUSIVE) != 0 && result != 0) {
  1112. reportError( "FixedFacetValue", new Object[]{"minExclusive", fMinExclusive, fBase.fMinExclusive, fTypeName});
  1113. }
  1114. if (result != 1 && result != 0) {
  1115. reportError( "minExclusive-valid-restriction.1", new Object[]{fMinExclusive, fBase.fMinExclusive, fTypeName});
  1116. }
  1117. }
  1118. if ((( fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
  1119. result=fDVs[fValidationDV].compare(fMinExclusive, fBase.fMaxInclusive);
  1120. if (result != -1 && result != 0) {
  1121. reportError( "minExclusive-valid-restriction.2", new Object[]{fMinExclusive, fBase.fMaxInclusive, fTypeName});
  1122. }
  1123. }
  1124. if ((( fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
  1125. result = fDVs[fValidationDV].compare(fMinExclusive, fBase.fMinInclusive);
  1126. if (result != 1 && result != 0) {
  1127. reportError( "minExclusive-valid-restriction.3", new Object[]{fMinExclusive, fBase.fMinInclusive, fTypeName});
  1128. }
  1129. }
  1130. if ((( fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) &&
  1131. fDVs[fValidationDV].compare(fMinExclusive, fBase.fMaxExclusive) != -1)
  1132. reportError( "minExclusive-valid-restriction.4", new Object[]{fMinExclusive, fBase.fMaxExclusive, fTypeName});
  1133. }
  1134. // check 4.3.10.c2 error:
  1135. // minInclusive < fBase.minInclusive
  1136. // minInclusive > fBase.maxInclusive
  1137. // minInclusive <= fBase.minExclusive
  1138. // minInclusive >= fBase.maxExclusive
  1139. if (((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
  1140. if (((fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
  1141. result = fDVs[fValidationDV].compare(fMinInclusive, fBase.fMinInclusive);
  1142. if ((fBase.fFixedFacet & FACET_MININCLUSIVE) != 0 && result != 0) {
  1143. reportError( "FixedFacetValue", new Object[]{"minInclusive", fMinInclusive, fBase.fMinInclusive, fTypeName});
  1144. }
  1145. if (result != 1 && result != 0) {
  1146. reportError( "minInclusive-valid-restriction.1", new Object[]{fMinInclusive, fBase.fMinInclusive, fTypeName});
  1147. }
  1148. }
  1149. if ((( fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
  1150. result=fDVs[fValidationDV].compare(fMinInclusive, fBase.fMaxInclusive);
  1151. if (result != -1 && result != 0) {
  1152. reportError( "minInclusive-valid-restriction.2", new Object[]{fMinInclusive, fBase.fMaxInclusive, fTypeName});
  1153. }
  1154. }
  1155. if ((( fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0) &&
  1156. fDVs[fValidationDV].compare(fMinInclusive, fBase.fMinExclusive ) != 1)
  1157. reportError( "minInclusive-valid-restriction.3", new Object[]{fMinInclusive, fBase.fMinExclusive, fTypeName});
  1158. if ((( fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) &&
  1159. fDVs[fValidationDV].compare(fMinInclusive, fBase.fMaxExclusive) != -1)
  1160. reportError( "minInclusive-valid-restriction.4", new Object[]{fMinInclusive, fBase.fMaxExclusive, fTypeName});
  1161. }
  1162. */
  1163. // check 4.3.11.c1 error: totalDigits > fBase.totalDigits
  1164. if (((fFacetsDefined & FACET_TOTALDIGITS) != 0)) {
  1165. if ((( fBase.fFacetsDefined & FACET_TOTALDIGITS) != 0)) {
  1166. if ((fBase.fFixedFacet & FACET_TOTALDIGITS) != 0 && fTotalDigits != fBase.fTotalDigits) {
  1167. reportError("FixedFacetValue", new Object[]{"totalDigits", Integer.toString(fTotalDigits), Integer.toString(fBase.fTotalDigits), fTypeName});
  1168. }
  1169. if (fTotalDigits > fBase.fTotalDigits) {
  1170. reportError( "totalDigits-valid-restriction", new Object[]{Integer.toString(fTotalDigits), Integer.toString(fBase.fTotalDigits), fTypeName});
  1171. }
  1172. }
  1173. }
  1174. // check 4.3.12.c1 must: fractionDigits <= base.totalDigits
  1175. if ((fFacetsDefined & FACET_FRACTIONDIGITS) != 0) {
  1176. if ((fBase.fFacetsDefined & FACET_TOTALDIGITS) != 0) {
  1177. if (fFractionDigits > fBase.fTotalDigits)
  1178. reportError( "fractionDigits-totalDigits", new Object[]{Integer.toString(fFractionDigits), Integer.toString(fTotalDigits), fTypeName});
  1179. }
  1180. }
  1181. // check 4.3.12.c2 error: fractionDigits > fBase.fractionDigits
  1182. // check fixed value for fractionDigits
  1183. if (((fFacetsDefined & FACET_FRACTIONDIGITS) != 0)) {
  1184. if ((( fBase.fFacetsDefined & FACET_FRACTIONDIGITS) != 0)) {
  1185. if ((fBase.fFixedFacet & FACET_FRACTIONDIGITS) != 0 && fFractionDigits != fBase.fFractionDigits) {
  1186. reportError("FixedFacetValue", new Object[]{"fractionDigits", Integer.toString(fFractionDigits), Integer.toString(fBase.fFractionDigits), fTypeName});
  1187. }
  1188. if (fFractionDigits > fBase.fFractionDigits) {
  1189. reportError( "fractionDigits-valid-restriction", new Object[]{Integer.toString(fFractionDigits), Integer.toString(fBase.fFractionDigits), fTypeName});
  1190. }
  1191. }
  1192. }
  1193. // check 4.3.6.c1 error:
  1194. // (whiteSpace = preserve || whiteSpace = replace) && fBase.whiteSpace = collapese or
  1195. // whiteSpace = preserve && fBase.whiteSpace = replace
  1196. if ( (fFacetsDefined & FACET_WHITESPACE) != 0 && (fBase.fFacetsDefined & FACET_WHITESPACE) != 0 ){
  1197. if ( (fBase.fFixedFacet & FACET_WHITESPACE) != 0 && fWhiteSpace != fBase.fWhiteSpace ) {
  1198. reportError( "FixedFacetValue", new Object[]{"whiteSpace", whiteSpaceValue(fWhiteSpace), whiteSpaceValue(fBase.fWhiteSpace), fTypeName});
  1199. }
  1200. if ( fWhiteSpace == WS_PRESERVE && fBase.fWhiteSpace == WS_COLLAPSE ){
  1201. reportError( "whiteSpace-valid-restriction.1", new Object[]{fTypeName, "preserve"});
  1202. }
  1203. if ( fWhiteSpace == WS_REPLACE && fBase.fWhiteSpace == WS_COLLAPSE ){
  1204. reportError( "whiteSpace-valid-restriction.1", new Object[]{fTypeName, "replace"});
  1205. }
  1206. if ( fWhiteSpace == WS_PRESERVE && fBase.fWhiteSpace == WS_REPLACE ){
  1207. reportError( "whiteSpace-valid-restriction.2", new Object[]{fTypeName});
  1208. }
  1209. }
  1210. }//fFacetsDefined != null
  1211. // step 4: inherit other facets from base (including fTokeyType)
  1212. // inherit length
  1213. if ( (fFacetsDefined & FACET_LENGTH) == 0 && (fBase.fFacetsDefined & FACET_LENGTH) != 0 ) {
  1214. fFacetsDefined |= FACET_LENGTH;
  1215. fLength = fBase.fLength;
  1216. lengthAnnotation = fBase.lengthAnnotation;
  1217. }
  1218. // inherit minLength
  1219. if ( (fFacetsDefined & FACET_MINLENGTH) == 0 && (fBase.fFacetsDefined & FACET_MINLENGTH) != 0 ) {
  1220. fFacetsDefined |= FACET_MINLENGTH;
  1221. fMinLength = fBase.fMinLength;
  1222. minLengthAnnotation = fBase.minLengthAnnotation;
  1223. }
  1224. // inherit maxLength
  1225. if ((fFacetsDefined & FACET_MAXLENGTH) == 0 && (fBase.fFacetsDefined & FACET_MAXLENGTH) != 0 ) {
  1226. fFacetsDefined |= FACET_MAXLENGTH;
  1227. fMaxLength = fBase.fMaxLength;
  1228. maxLengthAnnotation = fBase.maxLengthAnnotation;
  1229. }
  1230. // inherit pattern
  1231. if ( (fBase.fFacetsDefined & FACET_PATTERN) != 0 ) {
  1232. if ((fFacetsDefined & FACET_PATTERN) == 0) {
  1233. fPattern = fBase.fPattern;
  1234. fPatternStr = fBase.fPatternStr;
  1235. fFacetsDefined |= FACET_PATTERN;
  1236. }
  1237. else {
  1238. for (int i = fBase.fPattern.size()-1; i >= 0; i--) {
  1239. fPattern.addElement(fBase.fPattern.elementAt(i));
  1240. fPatternStr.addElement(fBase.fPatternStr.elementAt(i));
  1241. }
  1242. if (fBase.patternAnnotations != null){
  1243. for (int i = fBase.patternAnnotations.getLength()-1;i>=0;i--){
  1244. patternAnnotations.add(fBase.patternAnnotations.item(i));
  1245. }
  1246. }
  1247. }
  1248. }
  1249. // inherit whiteSpace
  1250. if ( (fFacetsDefined & FACET_WHITESPACE) == 0 && (fBase.fFacetsDefined & FACET_WHITESPACE) != 0 ) {
  1251. fFacetsDefined |= FACET_WHITESPACE;
  1252. fWhiteSpace = fBase.fWhiteSpace;
  1253. whiteSpaceAnnotation = fBase.whiteSpaceAnnotation;
  1254. }
  1255. // inherit enumeration
  1256. if ((fFacetsDefined & FACET_ENUMERATION) == 0 && (fBase.fFacetsDefined & FACET_ENUMERATION) != 0) {
  1257. fFacetsDefined |= FACET_ENUMERATION;
  1258. fEnumeration = fBase.fEnumeration;
  1259. enumerationAnnotations = fBase.enumerationAnnotations;
  1260. }
  1261. // inherit maxExclusive
  1262. if ((( fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) &&
  1263. !((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) && !((fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
  1264. fFacetsDefined |= FACET_MAXEXCLUSIVE;
  1265. fMaxExclusive = fBase.fMaxExclusive;
  1266. maxExclusiveAnnotation = fBase.maxExclusiveAnnotation;
  1267. }
  1268. // inherit maxInclusive
  1269. if ((( fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0) &&
  1270. !((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) && !((fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
  1271. fFacetsDefined |= FACET_MAXINCLUSIVE;
  1272. fMaxInclusive = fBase.fMaxInclusive;
  1273. maxInclusiveAnnotation = fBase.maxInclusiveAnnotation;
  1274. }
  1275. // inherit minExclusive
  1276. if ((( fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0) &&
  1277. !((fFacetsDefined & FACET_MINEXCLUSIVE) != 0) && !((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
  1278. fFacetsDefined |= FACET_MINEXCLUSIVE;
  1279. fMinExclusive = fBase.fMinExclusive;
  1280. minExclusiveAnnotation = fBase.minExclusiveAnnotation;
  1281. }
  1282. // inherit minExclusive
  1283. if ((( fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0) &&
  1284. !((fFacetsDefined & FACET_MINEXCLUSIVE) != 0) && !((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
  1285. fFacetsDefined |= FACET_MININCLUSIVE;
  1286. fMinInclusive = fBase.fMinInclusive;
  1287. minInclusiveAnnotation = fBase.minInclusiveAnnotation;
  1288. }
  1289. // inherit totalDigits
  1290. if ((( fBase.fFacetsDefined & FACET_TOTALDIGITS) != 0) &&
  1291. !((fFacetsDefined & FACET_TOTALDIGITS) != 0)) {
  1292. fFacetsDefined |= FACET_TOTALDIGITS;
  1293. fTotalDigits = fBase.fTotalDigits;
  1294. totalDigitsAnnotation = fBase.totalDigitsAnnotation;
  1295. }
  1296. // inherit fractionDigits
  1297. if ((( fBase.fFacetsDefined & FACET_FRACTIONDIGITS) != 0)
  1298. && !((fFacetsDefined & FACET_FRACTIONDIGITS) != 0)) {
  1299. fFacetsDefined |= FACET_FRACTIONDIGITS;
  1300. fFractionDigits = fBase.fFractionDigits;
  1301. fractionDigitsAnnotation = fBase.fractionDigitsAnnotation;
  1302. }
  1303. //inherit tokeytype
  1304. if ((fPatternType == SPECIAL_PATTERN_NONE ) && (fBase.fPatternType != SPECIAL_PATTERN_NONE)) {
  1305. fPatternType = fBase.fPatternType ;
  1306. }
  1307. // step 5: mark fixed values
  1308. fFixedFacet |= fBase.fFixedFacet;
  1309. //step 6: setting fundamental facets
  1310. caclFundamentalFacets();
  1311. } //applyFacets()
  1312. /**
  1313. * validate a value, and return the compiled form
  1314. */
  1315. public Object validate(String content, ValidationContext context, ValidatedInfo validatedInfo) throws InvalidDatatypeValueException {
  1316. if (context == null)
  1317. context = fEmptyContext;
  1318. if (validatedInfo == null)
  1319. validatedInfo = new ValidatedInfo();
  1320. else
  1321. validatedInfo.memberType = null;
  1322. // first normalize string value, and convert it to actual value
  1323. boolean needNormalize = context==null||context.needToNormalize();
  1324. Object ob = getActualValue(content, context, validatedInfo, needNormalize);
  1325. validate(context, validatedInfo);
  1326. return ob;
  1327. }
  1328. /**
  1329. * validate a value, and return the compiled form
  1330. */
  1331. public Object validate(Object content, ValidationContext context, ValidatedInfo validatedInfo) throws InvalidDatatypeValueException {
  1332. if (context == null)
  1333. context = fEmptyContext;
  1334. if (validatedInfo == null)
  1335. validatedInfo = new ValidatedInfo();
  1336. else
  1337. validatedInfo.memberType = null;
  1338. // first normalize string value, and convert it to actual value
  1339. boolean needNormalize = context==null||context.needToNormalize();
  1340. Object ob = getActualValue(content, context, validatedInfo, needNormalize);
  1341. validate(context, validatedInfo);
  1342. return ob;
  1343. }
  1344. /**
  1345. * validate an actual value against this DV
  1346. *
  1347. * @param value the actual value that needs to be validated
  1348. * @param context the validation context
  1349. * @param validatedInfo used to provide the actual value and member types
  1350. */
  1351. public void validate(ValidationContext context, ValidatedInfo validatedInfo)
  1352. throws InvalidDatatypeValueException {
  1353. if (context == null)
  1354. context = fEmptyContext;
  1355. // then validate the actual value against the facets
  1356. if (context.needFacetChecking() &&
  1357. (fFacetsDefined != 0 && fFacetsDefined != FACET_WHITESPACE)) {
  1358. checkFacets(validatedInfo);
  1359. }
  1360. // now check extra rules: for ID/IDREF/ENTITY
  1361. if (context.needExtraChecking()) {
  1362. checkExtraRules(context, validatedInfo);
  1363. }
  1364. }
  1365. private void checkFacets(ValidatedInfo validatedInfo) throws InvalidDatatypeValueException {
  1366. Object ob = validatedInfo.actualValue;
  1367. String content = validatedInfo.normalizedValue;
  1368. // For QName and NOTATION types, we don't check length facets
  1369. if (fValidationDV != DV_QNAME && fValidationDV != DV_NOTATION) {
  1370. int length = fDVs[fValidationDV].getDataLength(ob);
  1371. // maxLength
  1372. if ( (fFacetsDefined & FACET_MAXLENGTH) != 0 ) {
  1373. if ( length > fMaxLength ) {
  1374. throw new InvalidDatatypeValueException("cvc-maxLength-valid",
  1375. new Object[]{content, Integer.toString(length), Integer.toString(fMaxLength), fTypeName});
  1376. }
  1377. }
  1378. //minLength
  1379. if ( (fFacetsDefined & FACET_MINLENGTH) != 0 ) {
  1380. if ( length < fMinLength ) {
  1381. throw new InvalidDatatypeValueException("cvc-minLength-valid",
  1382. new Object[]{content, Integer.toString(length), Integer.toString(fMinLength), fTypeName});
  1383. }
  1384. }
  1385. //length
  1386. if ( (fFacetsDefined & FACET_LENGTH) != 0 ) {
  1387. if ( length != fLength ) {
  1388. throw new InvalidDatatypeValueException("cvc-length-valid",
  1389. new Object[]{content, Integer.toString(length), Integer.toString(fLength), fTypeName});
  1390. }
  1391. }
  1392. }
  1393. //enumeration
  1394. if ( ((fFacetsDefined & FACET_ENUMERATION) != 0 ) ) {
  1395. boolean present = false;
  1396. for (int i = 0; i < fEnumeration.size(); i++) {
  1397. if (fEnumeration.elementAt(i).equals(ob)) {
  1398. present = true;
  1399. break;
  1400. }
  1401. }
  1402. if(!present){
  1403. throw new InvalidDatatypeValueException("cvc-enumeration-valid",
  1404. new Object [] {content, fEnumeration.toString()});
  1405. }
  1406. }
  1407. //fractionDigits
  1408. if ((fFacetsDefined & FACET_FRACTIONDIGITS) != 0) {
  1409. int scale = fDVs[fValidationDV].getFractionDigits(ob);
  1410. if (scale > fFractionDigits) {
  1411. throw new InvalidDatatypeValueException("cvc-fractionDigits-valid",
  1412. new Object[] {content, Integer.toString(scale), Integer.toString(fFractionDigits)});
  1413. }
  1414. }
  1415. //totalDigits
  1416. if ((fFacetsDefined & FACET_TOTALDIGITS)!=0) {
  1417. int totalDigits = fDVs[fValidationDV].getTotalDigits(ob);
  1418. if (totalDigits > fTotalDigits) {
  1419. throw new InvalidDatatypeValueException("cvc-totalDigits-valid",
  1420. new Object[] {content, Integer.toString(totalDigits), Integer.toString(fTotalDigits)});
  1421. }
  1422. }
  1423. int compare;
  1424. //maxinclusive
  1425. if ( (fFacetsDefined & FACET_MAXINCLUSIVE) != 0 ) {
  1426. compare = fDVs[fValidationDV].compare(ob, fMaxInclusive);
  1427. if (compare != -1 && compare != 0) {
  1428. throw new InvalidDatatypeValueException("cvc-maxInclusive-valid",
  1429. new Object[] {content, fMaxInclusive, fTypeName});
  1430. }
  1431. }
  1432. //maxExclusive
  1433. if ( (fFacetsDefined & FACET_MAXEXCLUSIVE) != 0 ) {
  1434. compare = fDVs[fValidationDV].compare(ob, fMaxExclusive );
  1435. if (compare != -1) {
  1436. throw new InvalidDatatypeValueException("cvc-maxExclusive-valid",
  1437. new Object[] {content, fMaxExclusive, fTypeName});
  1438. }
  1439. }
  1440. //minInclusive
  1441. if ( (fFacetsDefined & FACET_MININCLUSIVE) != 0 ) {
  1442. compare = fDVs[fValidationDV].compare(ob, fMinInclusive);
  1443. if (compare != 1 && compare != 0) {
  1444. throw new InvalidDatatypeValueException("cvc-minInclusive-valid",
  1445. new Object[] {content, fMinInclusive, fTypeName});
  1446. }
  1447. }
  1448. //minExclusive
  1449. if ( (fFacetsDefined & FACET_MINEXCLUSIVE) != 0 ) {
  1450. compare = fDVs[fValidationDV].compare(ob, fMinExclusive);
  1451. if (compare != 1) {
  1452. throw new InvalidDatatypeValueException("cvc-minExclusive-valid",
  1453. new Object[] {content, fMinExclusive, fTypeName});
  1454. }
  1455. }
  1456. }
  1457. private void checkExtraRules(ValidationContext context, ValidatedInfo validatedInfo) throws InvalidDatatypeValueException {
  1458. Object ob = validatedInfo.actualValue;
  1459. if (fVariety == VARIETY_ATOMIC) {
  1460. fDVs[fValidationDV].checkExtraRules(ob, context);
  1461. } else if (fVariety == VARIETY_LIST) {
  1462. ListDV.ListData values = (ListDV.ListData)ob;
  1463. int len = values.length();
  1464. if (fItemType.fVariety == VARIETY_UNION) {
  1465. XSSimpleTypeDecl[] memberTypes = (XSSimpleTypeDecl[])validatedInfo.memberTypes;
  1466. XSSimpleType memberType = validatedInfo.memberType;
  1467. for (int i = len-1; i >= 0; i--) {
  1468. validatedInfo.actualValue = values.item(i);
  1469. validatedInfo.memberType = memberTypes[i];
  1470. fItemType.checkExtraRules(context, validatedInfo);
  1471. }
  1472. validatedInfo.memberType = memberType;
  1473. } else { // (fVariety == VARIETY_ATOMIC)
  1474. for (int i = len-1; i >= 0; i--) {
  1475. validatedInfo.actualValue = values.item(i);
  1476. fItemType.checkExtraRules(context, validatedInfo);
  1477. }
  1478. }
  1479. validatedInfo.actualValue = values;
  1480. } else { // (fVariety == VARIETY_UNION)
  1481. ((XSSimpleTypeDecl)validatedInfo.memberType).checkExtraRules(context, validatedInfo);
  1482. }
  1483. }// checkExtraRules()
  1484. //we can still return object for internal use.
  1485. private Object getActualValue(Object content, ValidationContext context,
  1486. ValidatedInfo validatedInfo, boolean needNormalize)
  1487. throws InvalidDatatypeValueException{
  1488. String nvalue;
  1489. if (needNormalize) {
  1490. nvalue = normalize(content, fWhiteSpace);
  1491. } else {
  1492. nvalue = content.toString();
  1493. }
  1494. if ( (fFacetsDefined & FACET_PATTERN ) != 0 ) {
  1495. RegularExpression regex;
  1496. for (int idx = fPattern.size()-1; idx >= 0; idx--) {
  1497. regex = (RegularExpression)fPattern.elementAt(idx);
  1498. if (!regex.matches(nvalue)){
  1499. throw new InvalidDatatypeValueException("cvc-pattern-valid",
  1500. new Object[]{content,
  1501. fPatternStr.elementAt(idx),
  1502. fTypeName});
  1503. }
  1504. }
  1505. }
  1506. if (fVariety == VARIETY_ATOMIC) {
  1507. // validate special kinds of token, in place of old pattern matching
  1508. if (fPatternType != SPECIAL_PATTERN_NONE) {
  1509. boolean seenErr = false;
  1510. if (fPatternType == SPECIAL_PATTERN_NMTOKEN) {
  1511. // PATTERN "\\c+"
  1512. seenErr = !XMLChar.isValidNmtoken(nvalue);
  1513. }
  1514. else if (fPatternType == SPECIAL_PATTERN_NAME) {
  1515. // PATTERN "\\i\\c*"
  1516. seenErr = !XMLChar.isValidName(nvalue);
  1517. }
  1518. else if (fPatternType == SPECIAL_PATTERN_NCNAME) {
  1519. // PATTERN "[\\i-[:]][\\c-[:]]*"
  1520. seenErr = !XMLChar.isValidNCName(nvalue);
  1521. }
  1522. if (seenErr) {
  1523. throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1",
  1524. new Object[]{nvalue, SPECIAL_PATTERN_STRING[fPatternType]});
  1525. }
  1526. }
  1527. validatedInfo.normalizedValue = nvalue;
  1528. Object avalue = fDVs[fValidationDV].getActualValue(nvalue, context);
  1529. validatedInfo.actualValue = avalue;
  1530. validatedInfo.actualValueType = fBuiltInKind;
  1531. return avalue;
  1532. } else if (fVariety == VARIETY_LIST) {
  1533. StringTokenizer parsedList = new StringTokenizer(nvalue, " ");
  1534. int countOfTokens = parsedList.countTokens() ;
  1535. Object[] avalue = new Object[countOfTokens];
  1536. boolean isUnion = fItemType.getVariety() == VARIETY_UNION;
  1537. short[] itemTypes = new short[isUnion ? countOfTokens : 1];
  1538. if (!isUnion)
  1539. itemTypes[0] = fItemType.fBuiltInKind;
  1540. XSSimpleTypeDecl[] memberTypes = new XSSimpleTypeDecl[countOfTokens];
  1541. for(int i = 0 ; i < countOfTokens ; i ++){
  1542. // we can't call fItemType.validate(), otherwise checkExtraRules()
  1543. // will be called twice: once in fItemType.validate, once in
  1544. // validate method of this type.
  1545. // so we take two steps to get the actual value:
  1546. // 1. fItemType.getActualValue()
  1547. // 2. fItemType.chekcFacets()
  1548. avalue[i] = fItemType.getActualValue(parsedList.nextToken(), context, validatedInfo, false);
  1549. if (context.needFacetChecking() &&
  1550. (fItemType.fFacetsDefined != 0 && fItemType.fFacetsDefined != FACET_WHITESPACE)) {
  1551. fItemType.checkFacets(validatedInfo);
  1552. }
  1553. memberTypes[i] = (XSSimpleTypeDecl)validatedInfo.memberType;
  1554. if (isUnion)
  1555. itemTypes[i] = memberTypes[i].fBuiltInKind;
  1556. }
  1557. ListDV.ListData v = new ListDV.ListData(avalue);
  1558. validatedInfo.actualValue = v;
  1559. validatedInfo.actualValueType = isUnion ? XSConstants.LISTOFUNION_DT : XSConstants.LIST_DT;
  1560. validatedInfo.memberType = null;
  1561. validatedInfo.memberTypes = memberTypes;
  1562. validatedInfo.itemValueTypes = new ShortListImpl(itemTypes, itemTypes.length);
  1563. validatedInfo.normalizedValue = nvalue;
  1564. return v;
  1565. } else { // (fVariety == VARIETY_UNION)
  1566. for(int i = 0 ; i < fMemberTypes.length; i++) {
  1567. try {
  1568. // we can't call fMemberType[i].validate(), otherwise checkExtraRules()
  1569. // will be called twice: once in fMemberType[i].validate, once in
  1570. // validate method of this type.
  1571. // so we take two steps to get the actual value:
  1572. // 1. fMemberType[i].getActualValue()
  1573. // 2. fMemberType[i].chekcFacets()
  1574. Object aValue = fMemberTypes[i].getActualValue(content, context, validatedInfo, true);
  1575. if (context.needFacetChecking() &&
  1576. (fMemberTypes[i].fFacetsDefined != 0 && fMemberTypes[i].fFacetsDefined != FACET_WHITESPACE)) {
  1577. fMemberTypes[i].checkFacets(validatedInfo);
  1578. }
  1579. validatedInfo.memberType = fMemberTypes[i];
  1580. return aValue;
  1581. } catch(InvalidDatatypeValueException invalidValue) {
  1582. }
  1583. }
  1584. throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.2",
  1585. new Object[]{content, fTypeName});
  1586. }
  1587. }//getActualValue()
  1588. public boolean isEqual(Object value1, Object value2) {
  1589. if (value1 == null)
  1590. return false;
  1591. return value1.equals(value2);
  1592. }//isEqual()
  1593. // normalize the string according to the whiteSpace facet
  1594. public static String normalize(String content, short ws) {
  1595. int len = content == null ? 0 : content.length();
  1596. if (len == 0 || ws == WS_PRESERVE)
  1597. return content;
  1598. StringBuffer sb = new StringBuffer();
  1599. if (ws == WS_REPLACE) {
  1600. char ch;
  1601. // when it's replace, just replace #x9, #xa, #xd by #x20
  1602. for (int i = 0; i < len; i++) {
  1603. ch = content.charAt(i);
  1604. if (ch != 0x9 && ch != 0xa && ch != 0xd)
  1605. sb.append(ch);
  1606. else
  1607. sb.append((char)0x20);
  1608. }
  1609. } else {
  1610. char ch;
  1611. int i;
  1612. boolean isLeading = true;
  1613. // when it's collapse
  1614. for (i = 0; i < len; i++) {
  1615. ch = content.charAt(i);
  1616. // append real characters, so we passed leading ws
  1617. if (ch != 0x9 && ch != 0xa && ch != 0xd && ch != 0x20) {
  1618. sb.append(ch);
  1619. isLeading = false;
  1620. }
  1621. else {
  1622. // for whitespaces, we skip all following ws
  1623. for (; i < len-1; i++) {
  1624. ch = content.charAt(i+1);
  1625. if (ch != 0x9 && ch != 0xa && ch != 0xd && ch != 0x20)
  1626. break;
  1627. }
  1628. // if it's not a leading or tailing ws, then append a space
  1629. if (i < len - 1 && !isLeading)
  1630. sb.append((char)0x20);
  1631. }
  1632. }
  1633. }
  1634. return sb.toString();
  1635. }
  1636. // normalize the string according to the whiteSpace facet
  1637. protected String normalize(Object content, short ws) {
  1638. if (content == null)
  1639. return null;
  1640. // If pattern is not defined, we can skip some of the normalization.
  1641. // Otherwise we have to normalize the data for correct result of
  1642. // pattern validation.
  1643. if ( (fFacetsDefined & FACET_PATTERN ) == 0 ) {
  1644. short norm_type = fDVNormalizeType[fValidationDV];
  1645. if (norm_type == NORMALIZE_NONE) {
  1646. return content.toString();
  1647. }
  1648. else if (norm_type == NORMALIZE_TRIM) {
  1649. return content.toString().trim();
  1650. }
  1651. }
  1652. if (!(content instanceof StringBuffer)) {
  1653. String strContent = content.toString();
  1654. return normalize(strContent, ws);
  1655. }
  1656. StringBuffer sb = (StringBuffer)content;
  1657. int len = sb.length();
  1658. if (len == 0)
  1659. return "";
  1660. if (ws == WS_PRESERVE)
  1661. return sb.toString();
  1662. if (ws == WS_REPLACE) {
  1663. char ch;
  1664. // when it's replace, just replace #x9, #xa, #xd by #x20
  1665. for (int i = 0; i < len; i++) {
  1666. ch = sb.charAt(i);
  1667. if (ch == 0x9 || ch == 0xa || ch == 0xd)
  1668. sb.setCharAt(i, (char)0x20);
  1669. }
  1670. } else {
  1671. char ch;
  1672. int i, j = 0;
  1673. boolean isLeading = true;
  1674. // when it's collapse
  1675. for (i = 0; i < len; i++) {
  1676. ch = sb.charAt(i);
  1677. // append real characters, so we passed leading ws
  1678. if (ch != 0x9 && ch != 0xa && ch != 0xd && ch != 0x20) {
  1679. sb.setCharAt(j++, ch);
  1680. isLeading = false;
  1681. }
  1682. else {
  1683. // for whitespaces, we skip all following ws
  1684. for (; i < len-1; i++) {
  1685. ch = sb.charAt(i+1);
  1686. if (ch != 0x9 && ch != 0xa && ch != 0xd && ch != 0x20)
  1687. break;
  1688. }
  1689. // if it's not a leading or tailing ws, then append a space
  1690. if (i < len - 1 && !isLeading)
  1691. sb.setCharAt(j++, (char)0x20);
  1692. }
  1693. }
  1694. sb.setLength(j);
  1695. }
  1696. return sb.toString();
  1697. }
  1698. void reportError(String key, Object[] args) throws InvalidDatatypeFacetException {
  1699. throw new InvalidDatatypeFacetException(key, args);
  1700. }
  1701. private String whiteSpaceValue(short ws){
  1702. return WS_FACET_STRING[ws];
  1703. }
  1704. public short getOrdered() {
  1705. return fOrdered;
  1706. }
  1707. public boolean getBounded(){
  1708. return fBounded;
  1709. }
  1710. public boolean getFinite(){
  1711. return fFinite;
  1712. }
  1713. public boolean getNumeric(){
  1714. return fNumeric;
  1715. }
  1716. public boolean isDefinedFacet(short facetName) {
  1717. if ((fFacetsDefined & facetName) != 0)
  1718. return true;
  1719. if (fPatternType != SPECIAL_PATTERN_NONE)
  1720. return facetName == FACET_PATTERN;
  1721. if (fValidationDV == DV_INTEGER)
  1722. return facetName == FACET_PATTERN || facetName == FACET_FRACTIONDIGITS;
  1723. return false;
  1724. }
  1725. public short getDefinedFacets() {
  1726. if (fPatternType != SPECIAL_PATTERN_NONE)
  1727. return (short)(fFacetsDefined | FACET_PATTERN);
  1728. if (fValidationDV == DV_INTEGER)
  1729. return (short)(fFacetsDefined | FACET_PATTERN | FACET_FRACTIONDIGITS);
  1730. return fFacetsDefined;
  1731. }
  1732. public boolean isFixedFacet(short facetName) {
  1733. if ((fFixedFacet & facetName) != 0)
  1734. return true;
  1735. if (fValidationDV == DV_INTEGER)
  1736. return facetName == FACET_FRACTIONDIGITS;
  1737. return false;
  1738. }
  1739. public short getFixedFacets() {
  1740. if (fValidationDV == DV_INTEGER)
  1741. return (short)(fFixedFacet | FACET_FRACTIONDIGITS);
  1742. return fFixedFacet;
  1743. }
  1744. public String getLexicalFacetValue(short facetName) {
  1745. switch (facetName) {
  1746. case FACET_LENGTH:
  1747. return (fLength == -1)?null:Integer.toString(fLength);
  1748. case FACET_MINLENGTH:
  1749. return (fMinLength == -1)?null:Integer.toString(fMinLength);
  1750. case FACET_MAXLENGTH:
  1751. return (fMaxLength == -1)?null:Integer.toString(fMaxLength);
  1752. case FACET_WHITESPACE:
  1753. return WS_FACET_STRING[fWhiteSpace];
  1754. case FACET_MAXINCLUSIVE:
  1755. return (fMaxInclusive == null)?null:fMaxInclusive.toString();
  1756. case FACET_MAXEXCLUSIVE:
  1757. return (fMaxExclusive == null)?null:fMaxExclusive.toString();
  1758. case FACET_MINEXCLUSIVE:
  1759. return (fMinExclusive == null)?null:fMinExclusive.toString();
  1760. case FACET_MININCLUSIVE:
  1761. return (fMinInclusive == null)?null:fMinInclusive.toString();
  1762. case FACET_TOTALDIGITS:
  1763. if (fValidationDV == DV_INTEGER)
  1764. return "0";
  1765. return (fTotalDigits == -1)?null:Integer.toString(fTotalDigits);
  1766. case FACET_FRACTIONDIGITS:
  1767. return (fFractionDigits == -1)?null:Integer.toString(fFractionDigits);
  1768. }
  1769. return null;
  1770. }
  1771. public StringList getLexicalEnumeration() {
  1772. if (fLexicalEnumeration == null){
  1773. if (fEnumeration == null)
  1774. return null;
  1775. int size = fEnumeration.size();
  1776. String[] strs = new String[size];
  1777. for (int i = 0; i < size; i++)
  1778. strs[i] = fEnumeration.elementAt(i).toString();
  1779. fLexicalEnumeration = new StringListImpl(strs, size);
  1780. }
  1781. return fLexicalEnumeration;
  1782. }
  1783. public StringList getLexicalPattern() {
  1784. if (fPatternType == SPECIAL_PATTERN_NONE && fValidationDV != DV_INTEGER && fPatternStr == null)
  1785. return null;
  1786. if (fLexicalPattern == null){
  1787. int size = fPatternStr == null ? 0 : fPatternStr.size();
  1788. String[] strs;
  1789. if (fPatternType == SPECIAL_PATTERN_NMTOKEN) {
  1790. strs = new String[size+1];
  1791. strs[size] = "\\c+";
  1792. }
  1793. else if (fPatternType == SPECIAL_PATTERN_NAME) {
  1794. strs = new String[size+1];
  1795. strs[size] = "\\i\\c*";
  1796. }
  1797. else if (fPatternType == SPECIAL_PATTERN_NCNAME) {
  1798. strs = new String[size+2];
  1799. strs[size] = "\\i\\c*";
  1800. strs[size+1] = "[\\i-[:]][\\c-[:]]*";
  1801. }
  1802. else if (fValidationDV == DV_INTEGER) {
  1803. strs = new String[size+1];
  1804. strs[size] = "[+\\-]?[0-9]+";
  1805. }
  1806. else {
  1807. strs = new String[size];
  1808. }
  1809. for (int i = 0; i < size; i++)
  1810. strs[i] = (String)fPatternStr.elementAt(i);
  1811. fLexicalPattern = new StringListImpl(strs, size);
  1812. }
  1813. return fLexicalPattern;
  1814. }
  1815. public XSObjectList getAnnotations() {
  1816. return fAnnotations;
  1817. }
  1818. private void caclFundamentalFacets() {
  1819. setOrdered();
  1820. setNumeric();
  1821. setBounded();
  1822. setCardinality();
  1823. }
  1824. private void setOrdered(){
  1825. // When {variety} is atomic, {value} is inherited from {value} of {base type definition}. For all "primitive" types {value} is as specified in the table in Fundamental Facets (C.1).
  1826. if(fVariety == VARIETY_ATOMIC){
  1827. this.fOrdered = fBase.fOrdered;
  1828. }
  1829. // When {variety} is list, {value} is false.
  1830. else if(fVariety == VARIETY_LIST){
  1831. this.fOrdered = ORDERED_FALSE;
  1832. }
  1833. // When {variety} is union, the {value} is partial unless one of the following:
  1834. // 1. If every member of {member type definitions} is derived from a common ancestor other than the simple ur-type, then {value} is the same as that ancestor's ordered facet.
  1835. // 2. If every member of {member type definitions} has a {value} of false for the ordered facet, then {value} is false.
  1836. else if(fVariety == VARIETY_UNION){
  1837. int length = fMemberTypes.length;
  1838. // REVISIT: is the length possible to be 0?
  1839. if (length == 0) {
  1840. this.fOrdered = ORDERED_PARTIAL;
  1841. return;
  1842. }
  1843. // we need to process the first member type before entering the loop
  1844. short ancestorId = getPrimitiveDV(fMemberTypes[0].fValidationDV);
  1845. boolean commonAnc = ancestorId != DV_ANYSIMPLETYPE;
  1846. boolean allFalse = fMemberTypes[0].fOrdered == ORDERED_FALSE;
  1847. // for the other member types, check whether the value is false
  1848. // and whether they have the same ancestor as the first one
  1849. for (int i = 1; i < fMemberTypes.length && (commonAnc || allFalse); i++) {
  1850. if (commonAnc)
  1851. commonAnc = ancestorId == getPrimitiveDV(fMemberTypes[i].fValidationDV);
  1852. if (allFalse)
  1853. allFalse = fMemberTypes[i].fOrdered == ORDERED_FALSE;
  1854. }
  1855. if (commonAnc) {
  1856. // REVISIT: all member types should have the same ordered value
  1857. // just use the first one. Can we assume this?
  1858. this.fOrdered = fMemberTypes[0].fOrdered;
  1859. } else if (allFalse) {
  1860. this.fOrdered = ORDERED_FALSE;
  1861. } else {
  1862. this.fOrdered = ORDERED_PARTIAL;
  1863. }
  1864. }
  1865. }//setOrdered
  1866. private void setNumeric(){
  1867. if(fVariety == VARIETY_ATOMIC){
  1868. this.fNumeric = fBase.fNumeric;
  1869. }
  1870. else if(fVariety == VARIETY_LIST){
  1871. this.fNumeric = false;
  1872. }
  1873. else if(fVariety == VARIETY_UNION){
  1874. XSSimpleType[] memberTypes = fMemberTypes;
  1875. for(int i = 0 ; i < memberTypes.length ; i++){
  1876. if(!memberTypes[i].getNumeric() ){
  1877. this.fNumeric = false;
  1878. return;
  1879. }
  1880. }
  1881. this.fNumeric = true;
  1882. }
  1883. }//setNumeric
  1884. private void setBounded(){
  1885. if(fVariety == VARIETY_ATOMIC){
  1886. if( (((this.fFacetsDefined & FACET_MININCLUSIVE) != 0) || ((this.fFacetsDefined & FACET_MINEXCLUSIVE) != 0))
  1887. && (((this.fFacetsDefined & FACET_MAXINCLUSIVE) != 0) || ((this.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0)) ){
  1888. this.fBounded = true;
  1889. }
  1890. else{
  1891. this.fBounded = false;
  1892. }
  1893. }
  1894. else if(fVariety == VARIETY_LIST){
  1895. if( ((this.fFacetsDefined & FACET_LENGTH) != 0 ) || ( ((this.fFacetsDefined & FACET_MINLENGTH) != 0 )
  1896. && ((this.fFacetsDefined & FACET_MAXLENGTH) != 0 )) ){
  1897. this.fBounded = true;
  1898. }
  1899. else{
  1900. this.fBounded = false;
  1901. }
  1902. }
  1903. else if(fVariety == VARIETY_UNION){
  1904. XSSimpleTypeDecl [] memberTypes = this.fMemberTypes;
  1905. short ancestorId = 0 ;
  1906. if(memberTypes.length > 0){
  1907. ancestorId = getPrimitiveDV(memberTypes[0].fValidationDV);
  1908. }
  1909. for(int i = 0 ; i < memberTypes.length ; i++){
  1910. if(!memberTypes[i].getBounded() || (ancestorId != getPrimitiveDV(memberTypes[i].fValidationDV)) ){
  1911. this.fBounded = false;
  1912. return;
  1913. }
  1914. }
  1915. this.fBounded = true;
  1916. }
  1917. }//setBounded
  1918. private boolean specialCardinalityCheck(){
  1919. if( (fBase.fValidationDV == XSSimpleTypeDecl.DV_DATE) || (fBase.fValidationDV == XSSimpleTypeDecl.DV_GYEARMONTH)
  1920. || (fBase.fValidationDV == XSSimpleTypeDecl.DV_GYEAR) || (fBase.fValidationDV == XSSimpleTypeDecl.DV_GMONTHDAY)
  1921. || (fBase.fValidationDV == XSSimpleTypeDecl.DV_GDAY) || (fBase.fValidationDV == XSSimpleTypeDecl.DV_GMONTH) ){
  1922. return true;
  1923. }
  1924. return false;
  1925. } //specialCardinalityCheck()
  1926. private void setCardinality(){
  1927. if(fVariety == VARIETY_ATOMIC){
  1928. if(fBase.fFinite){
  1929. this.fFinite = true;
  1930. }
  1931. else {// (!fBase.fFinite)
  1932. if ( ((this.fFacetsDefined & FACET_LENGTH) != 0 ) || ((this.fFacetsDefined & FACET_MAXLENGTH) != 0 )
  1933. || ((this.fFacetsDefined & FACET_TOTALDIGITS) != 0 ) ){
  1934. this.fFinite = true;
  1935. }
  1936. else if( (((this.fFacetsDefined & FACET_MININCLUSIVE) != 0 ) || ((this.fFacetsDefined & FACET_MINEXCLUSIVE) != 0 ))
  1937. && (((this.fFacetsDefined & FACET_MAXINCLUSIVE) != 0 ) || ((this.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0 )) ){
  1938. if( ((this.fFacetsDefined & FACET_FRACTIONDIGITS) != 0 ) || specialCardinalityCheck()){
  1939. this.fFinite = true;
  1940. }
  1941. else{
  1942. this.fFinite = false;
  1943. }
  1944. }
  1945. else{
  1946. this.fFinite = false;
  1947. }
  1948. }
  1949. }
  1950. else if(fVariety == VARIETY_LIST){
  1951. if( ((this.fFacetsDefined & FACET_LENGTH) != 0 ) || ( ((this.fFacetsDefined & FACET_MINLENGTH) != 0 )
  1952. && ((this.fFacetsDefined & FACET_MAXLENGTH) != 0 )) ){
  1953. this.fFinite = true;
  1954. }
  1955. else{
  1956. this.fFinite = false;
  1957. }
  1958. }
  1959. else if(fVariety == VARIETY_UNION){
  1960. XSSimpleType [] memberTypes = fMemberTypes;
  1961. for(int i = 0 ; i < memberTypes.length ; i++){
  1962. if(!(memberTypes[i].getFinite()) ){
  1963. this.fFinite = false;
  1964. return;
  1965. }
  1966. }
  1967. this.fFinite = true;
  1968. }
  1969. }//setCardinality
  1970. private short getPrimitiveDV(short validationDV){
  1971. if (validationDV == DV_ID || validationDV == DV_IDREF || validationDV == DV_ENTITY){
  1972. return DV_STRING;
  1973. }
  1974. else if (validationDV == DV_INTEGER) {
  1975. return DV_DECIMAL;
  1976. }
  1977. else {
  1978. return validationDV;
  1979. }
  1980. }//getPrimitiveDV()
  1981. public boolean derivedFromType(XSTypeDefinition ancestor, short derivation) {
  1982. // REVISIT: implement according to derivation
  1983. // ancestor is null, retur false
  1984. if (ancestor == null)
  1985. return false;
  1986. // ancestor is anyType, return true
  1987. // anyType is the only type whose base type is itself
  1988. if (ancestor.getBaseType() == ancestor)
  1989. return true;
  1990. // recursively get base, and compare it with ancestor
  1991. XSTypeDefinition type = this;
  1992. while (type != ancestor && // compare with ancestor
  1993. type != fAnySimpleType) { // reached anySimpleType
  1994. type = type.getBaseType();
  1995. }
  1996. return type == ancestor;
  1997. }
  1998. public boolean derivedFrom(String ancestorNS, String ancestorName, short derivation) {
  1999. // REVISIT: implement according to derivation
  2000. // ancestor is null, retur false
  2001. if (ancestorName == null)
  2002. return false;
  2003. // ancestor is anyType, return true
  2004. if (URI_SCHEMAFORSCHEMA.equals(ancestorNS) &&
  2005. ANY_TYPE.equals(ancestorName)) {
  2006. return true;
  2007. }
  2008. // recursively get base, and compare it with ancestor
  2009. XSTypeDefinition type = this;
  2010. while (!(ancestorName.equals(type.getName()) &&
  2011. ((ancestorNS == null && type.getNamespace() == null) ||
  2012. (ancestorNS != null && ancestorNS.equals(type.getNamespace())))) && // compare with ancestor
  2013. type != fAnySimpleType) { // reached anySimpleType
  2014. type = (XSTypeDefinition)type.getBaseType();
  2015. }
  2016. return type != fAnySimpleType;
  2017. }
  2018. static final XSSimpleTypeDecl fAnySimpleType = new XSSimpleTypeDecl(null, "anySimpleType", DV_ANYSIMPLETYPE, ORDERED_FALSE, false, true, false, true, XSConstants.ANYSIMPLETYPE_DT);
  2019. /**
  2020. * Validation context used to validate facet values.
  2021. */
  2022. static final ValidationContext fDummyContext = new ValidationContext() {
  2023. public boolean needFacetChecking() {
  2024. return true;
  2025. }
  2026. public boolean needExtraChecking() {
  2027. return false;
  2028. }
  2029. public boolean needToNormalize() {
  2030. return false;
  2031. }
  2032. public boolean useNamespaces() {
  2033. return true;
  2034. }
  2035. public boolean isEntityDeclared(String name) {
  2036. return false;
  2037. }
  2038. public boolean isEntityUnparsed(String name) {
  2039. return false;
  2040. }
  2041. public boolean isIdDeclared(String name) {
  2042. return false;
  2043. }
  2044. public void addId(String name) {
  2045. }
  2046. public void addIdRef(String name) {
  2047. }
  2048. public String getSymbol (String symbol) {
  2049. return null;
  2050. }
  2051. public String getURI(String prefix) {
  2052. return null;
  2053. }
  2054. };
  2055. /**
  2056. * A wrapper of ValidationContext, to provide a way of switching to a
  2057. * different Namespace declaration context.
  2058. */
  2059. class ValidationContextImpl implements ValidationContext {
  2060. ValidationContext fExternal;
  2061. ValidationContextImpl(ValidationContext external) {
  2062. fExternal = external;
  2063. }
  2064. NamespaceContext fNSContext;
  2065. void setNSContext(NamespaceContext nsContext) {
  2066. fNSContext = nsContext;
  2067. }
  2068. public boolean needFacetChecking() {
  2069. return fExternal.needFacetChecking();
  2070. }
  2071. public boolean needExtraChecking() {
  2072. return fExternal.needExtraChecking();
  2073. }
  2074. public boolean needToNormalize() {
  2075. return fExternal.needToNormalize();
  2076. }
  2077. // schema validation is predicated upon namespaces
  2078. public boolean useNamespaces() {
  2079. return true;
  2080. }
  2081. public boolean isEntityDeclared (String name) {
  2082. return fExternal.isEntityDeclared(name);
  2083. }
  2084. public boolean isEntityUnparsed (String name) {
  2085. return fExternal.isEntityUnparsed(name);
  2086. }
  2087. public boolean isIdDeclared (String name) {
  2088. return fExternal.isIdDeclared(name);
  2089. }
  2090. public void addId(String name) {
  2091. fExternal.addId(name);
  2092. }
  2093. public void addIdRef(String name) {
  2094. fExternal.addIdRef(name);
  2095. }
  2096. public String getSymbol (String symbol) {
  2097. return fExternal.getSymbol(symbol);
  2098. }
  2099. public String getURI(String prefix) {
  2100. if (fNSContext == null)
  2101. return fExternal.getURI(prefix);
  2102. else
  2103. return fNSContext.getURI(prefix);
  2104. }
  2105. }
  2106. public void reset(){
  2107. // if it's immutable, can't be reset:
  2108. if (fIsImmutable) return;
  2109. fItemType = null;
  2110. fMemberTypes = null;
  2111. fTypeName = null;
  2112. fTargetNamespace = null;
  2113. fFinalSet = 0;
  2114. fBase = null;
  2115. fVariety = -1;
  2116. fValidationDV = -1;
  2117. fFacetsDefined = 0;
  2118. fFixedFacet = 0;
  2119. //for constraining facets
  2120. fWhiteSpace = 0;
  2121. fLength = -1;
  2122. fMinLength = -1;
  2123. fMaxLength = -1;
  2124. fTotalDigits = -1;
  2125. fFractionDigits = -1;
  2126. fPattern = null;
  2127. fPatternStr = null;
  2128. fEnumeration = null;
  2129. fLexicalPattern = null;
  2130. fLexicalEnumeration = null;
  2131. fMaxInclusive = null;
  2132. fMaxExclusive = null;
  2133. fMinExclusive = null;
  2134. fMinInclusive = null;
  2135. lengthAnnotation = null;
  2136. minLengthAnnotation = null;
  2137. maxLengthAnnotation = null;
  2138. whiteSpaceAnnotation = null;
  2139. totalDigitsAnnotation = null;
  2140. fractionDigitsAnnotation = null;
  2141. patternAnnotations = null;
  2142. enumerationAnnotations = null;
  2143. maxInclusiveAnnotation = null;
  2144. maxExclusiveAnnotation = null;
  2145. minInclusiveAnnotation = null;
  2146. minExclusiveAnnotation = null;
  2147. fPatternType = SPECIAL_PATTERN_NONE;
  2148. fAnnotations = null;
  2149. fFacets = null;
  2150. // REVISIT: reset for fundamental facets
  2151. }
  2152. /**
  2153. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
  2154. */
  2155. public XSNamespaceItem getNamespaceItem() {
  2156. // REVISIT: implement
  2157. return null;
  2158. }
  2159. /**
  2160. * @see java.lang.Object#toString()
  2161. */
  2162. public String toString() {
  2163. return this.fTargetNamespace+"," +this.fTypeName;
  2164. }
  2165. /* (non-Javadoc)
  2166. * @see com.sun.org.apache.xerces.internal.xs.XSSimpleTypeDefinition#getFacets()
  2167. */
  2168. public XSObjectList getFacets() {
  2169. if (fFacets == null &&
  2170. (fFacetsDefined != 0 || fValidationDV == DV_INTEGER)) {
  2171. XSFacetImpl[] facets = new XSFacetImpl[10];
  2172. int count = 0;
  2173. if ((fFacetsDefined & FACET_WHITESPACE) != 0) {
  2174. facets[count] =
  2175. new XSFacetImpl(
  2176. FACET_WHITESPACE,
  2177. WS_FACET_STRING[fWhiteSpace],
  2178. (fFixedFacet & FACET_WHITESPACE) != 0,
  2179. whiteSpaceAnnotation);
  2180. count++;
  2181. }
  2182. if (fLength != -1) {
  2183. facets[count] =
  2184. new XSFacetImpl(
  2185. FACET_LENGTH,
  2186. Integer.toString(fLength),
  2187. (fFixedFacet & FACET_LENGTH) != 0,
  2188. lengthAnnotation);
  2189. count++;
  2190. }
  2191. if (fMinLength != -1) {
  2192. facets[count] =
  2193. new XSFacetImpl(
  2194. FACET_MINLENGTH,
  2195. Integer.toString(fMinLength),
  2196. (fFixedFacet & FACET_MINLENGTH) != 0,
  2197. minLengthAnnotation);
  2198. count++;
  2199. }
  2200. if (fMaxLength != -1) {
  2201. facets[count] =
  2202. new XSFacetImpl(
  2203. FACET_MAXLENGTH,
  2204. Integer.toString(fMaxLength),
  2205. (fFixedFacet & FACET_MAXLENGTH) != 0,
  2206. maxLengthAnnotation);
  2207. count++;
  2208. }
  2209. if (fTotalDigits != -1) {
  2210. facets[count] =
  2211. new XSFacetImpl(
  2212. FACET_TOTALDIGITS,
  2213. Integer.toString(fTotalDigits),
  2214. (fFixedFacet & FACET_TOTALDIGITS) != 0,
  2215. totalDigitsAnnotation);
  2216. count++;
  2217. }
  2218. if (fValidationDV == DV_INTEGER) {
  2219. facets[count] =
  2220. new XSFacetImpl(
  2221. FACET_FRACTIONDIGITS,
  2222. "0",
  2223. true,
  2224. null);
  2225. count++;
  2226. }
  2227. if (fFractionDigits != -1) {
  2228. facets[count] =
  2229. new XSFacetImpl(
  2230. FACET_FRACTIONDIGITS,
  2231. Integer.toString(fFractionDigits),
  2232. (fFixedFacet & FACET_FRACTIONDIGITS) != 0,
  2233. fractionDigitsAnnotation);
  2234. count++;
  2235. }
  2236. if (fMaxInclusive != null) {
  2237. facets[count] =
  2238. new XSFacetImpl(
  2239. FACET_MAXINCLUSIVE,
  2240. fMaxInclusive.toString(),
  2241. (fFixedFacet & FACET_MAXINCLUSIVE) != 0,
  2242. maxInclusiveAnnotation);
  2243. count++;
  2244. }
  2245. if (fMaxExclusive != null) {
  2246. facets[count] =
  2247. new XSFacetImpl(
  2248. FACET_MAXEXCLUSIVE,
  2249. fMaxExclusive.toString(),
  2250. (fFixedFacet & FACET_MAXEXCLUSIVE) != 0,
  2251. maxExclusiveAnnotation);
  2252. count++;
  2253. }
  2254. if (fMinExclusive != null) {
  2255. facets[count] =
  2256. new XSFacetImpl(
  2257. FACET_MINEXCLUSIVE,
  2258. fMinExclusive.toString(),
  2259. (fFixedFacet & FACET_MINEXCLUSIVE) != 0,
  2260. minExclusiveAnnotation);
  2261. count++;
  2262. }
  2263. if (fMinInclusive != null) {
  2264. facets[count] =
  2265. new XSFacetImpl(
  2266. FACET_MININCLUSIVE,
  2267. fMinInclusive.toString(),
  2268. (fFixedFacet & FACET_MININCLUSIVE) != 0,
  2269. minInclusiveAnnotation);
  2270. count++;
  2271. }
  2272. fFacets = new XSObjectListImpl(facets, count);
  2273. }
  2274. return fFacets;
  2275. }
  2276. public XSObjectList getMultiValueFacets(){
  2277. if (fMultiValueFacets == null &&
  2278. ((fFacetsDefined & FACET_ENUMERATION) != 0 ||
  2279. (fFacetsDefined & FACET_PATTERN) != 0 ||
  2280. fPatternType != SPECIAL_PATTERN_NONE ||
  2281. fValidationDV == DV_INTEGER)) {
  2282. XSMVFacetImpl[] facets = new XSMVFacetImpl[2];
  2283. int count = 0;
  2284. if ((fFacetsDefined & FACET_PATTERN) != 0 ||
  2285. fPatternType != SPECIAL_PATTERN_NONE ||
  2286. fValidationDV == DV_INTEGER) {
  2287. facets[count] =
  2288. new XSMVFacetImpl(
  2289. FACET_PATTERN,
  2290. this.getLexicalPattern(),
  2291. patternAnnotations);
  2292. count++;
  2293. }
  2294. if (fEnumeration != null) {
  2295. facets[count] =
  2296. new XSMVFacetImpl(
  2297. FACET_ENUMERATION,
  2298. this.getLexicalEnumeration(),
  2299. enumerationAnnotations);
  2300. count++;
  2301. }
  2302. fMultiValueFacets = new XSObjectListImpl(facets, count);
  2303. }
  2304. return fMultiValueFacets;
  2305. }
  2306. private static final class XSFacetImpl implements XSFacet {
  2307. final short kind;
  2308. final String value;
  2309. final boolean fixed;
  2310. final XSAnnotation annotation;
  2311. public XSFacetImpl(short kind, String value, boolean fixed, XSAnnotation annotation) {
  2312. this.kind = kind;
  2313. this.value = value;
  2314. this.fixed = fixed;
  2315. this.annotation = annotation;
  2316. }
  2317. /* (non-Javadoc)
  2318. * @see com.sun.org.apache.xerces.internal.xs.XSFacet#getAnnotation()
  2319. */
  2320. public XSAnnotation getAnnotation() {
  2321. return annotation;
  2322. }
  2323. /* (non-Javadoc)
  2324. * @see com.sun.org.apache.xerces.internal.xs.XSFacet#getFacetKind()
  2325. */
  2326. public short getFacetKind() {
  2327. return kind;
  2328. }
  2329. /* (non-Javadoc)
  2330. * @see com.sun.org.apache.xerces.internal.xs.XSFacet#getLexicalFacetValue()
  2331. */
  2332. public String getLexicalFacetValue() {
  2333. return value;
  2334. }
  2335. /* (non-Javadoc)
  2336. * @see com.sun.org.apache.xerces.internal.xs.XSFacet#isFixed()
  2337. */
  2338. public boolean getFixed() {
  2339. return fixed;
  2340. }
  2341. /* (non-Javadoc)
  2342. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getName()
  2343. */
  2344. public String getName() {
  2345. return null;
  2346. }
  2347. /* (non-Javadoc)
  2348. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespace()
  2349. */
  2350. public String getNamespace() {
  2351. return null;
  2352. }
  2353. /* (non-Javadoc)
  2354. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
  2355. */
  2356. public XSNamespaceItem getNamespaceItem() {
  2357. // REVISIT: implement
  2358. return null;
  2359. }
  2360. /* (non-Javadoc)
  2361. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getType()
  2362. */
  2363. public short getType() {
  2364. return XSConstants.FACET;
  2365. }
  2366. }
  2367. private static final class XSMVFacetImpl implements XSMultiValueFacet {
  2368. final short kind;
  2369. XSObjectList annotations;
  2370. StringList values;
  2371. public XSMVFacetImpl(short kind, StringList values, XSObjectList annotations) {
  2372. this.kind = kind;
  2373. this.values = values;
  2374. this.annotations = annotations;
  2375. }
  2376. /* (non-Javadoc)
  2377. * @see com.sun.org.apache.xerces.internal.xs.XSFacet#getFacetKind()
  2378. */
  2379. public short getFacetKind() {
  2380. return kind;
  2381. }
  2382. /* (non-Javadoc)
  2383. * @see com.sun.org.apache.xerces.internal.xs.XSMultiValueFacet#getAnnotations()
  2384. */
  2385. public XSObjectList getAnnotations() {
  2386. return annotations;
  2387. }
  2388. /* (non-Javadoc)
  2389. * @see com.sun.org.apache.xerces.internal.xs.XSMultiValueFacet#getLexicalFacetValues()
  2390. */
  2391. public StringList getLexicalFacetValues() {
  2392. return values;
  2393. }
  2394. /* (non-Javadoc)
  2395. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getName()
  2396. */
  2397. public String getName() {
  2398. return null;
  2399. }
  2400. /* (non-Javadoc)
  2401. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespace()
  2402. */
  2403. public String getNamespace() {
  2404. return null;
  2405. }
  2406. /* (non-Javadoc)
  2407. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
  2408. */
  2409. public XSNamespaceItem getNamespaceItem() {
  2410. // REVISIT: implement
  2411. return null;
  2412. }
  2413. /* (non-Javadoc)
  2414. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getType()
  2415. */
  2416. public short getType() {
  2417. return XSConstants.MULTIVALUE_FACET;
  2418. }
  2419. }
  2420. /**
  2421. * @see org.w3c.dom.TypeInfo#getTypeName()
  2422. */
  2423. public String getTypeName() {
  2424. if(getAnonymous())
  2425. return "anonymousSimpleType"+fUniqueId;
  2426. else
  2427. return getName();
  2428. }
  2429. /**
  2430. * @see org.w3c.dom.TypeInfo#getTypeNamespace()
  2431. */
  2432. public String getTypeNamespace() {
  2433. return getNamespace();
  2434. }
  2435. public boolean isDerivedFrom(String typeNamespaceArg, String typeNameArg, int derivationMethod) {
  2436. return derivedFrom(typeNamespaceArg,typeNameArg,(short)derivationMethod);
  2437. }
  2438. } // class XSSimpleTypeDecl