001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.dv;
019:
020: import org.apache.xerces.xs.ShortList;
021: import org.apache.xerces.xs.XSConstants;
022:
023: /**
024: * Class to get the information back after content is validated. This info
025: * would be filled by validate().
026: *
027: * @xerces.internal
028: *
029: * @author Neeraj Bajaj, Sun Microsystems, inc.
030: *
031: * @version $Id: ValidatedInfo.java 446751 2006-09-15 21:54:06Z mrglavas $
032: */
033: public class ValidatedInfo {
034:
035: /**
036: * The normalized value of a string value
037: */
038: public String normalizedValue;
039:
040: /**
041: * The actual value from a string value (QName, Boolean, etc.)
042: * An array of Objects if the type is a list.
043: */
044: public Object actualValue;
045:
046: /**
047: * The type of the actual value. It's one of the _DT constants
048: * defined in XSConstants.java. The value is used to indicate
049: * the most specific built-in type.
050: * (i.e. short instead of decimal or integer).
051: */
052: public short actualValueType;
053:
054: /**
055: * If the type is a union type, then the member type which
056: * actually validated the string value.
057: */
058: public XSSimpleType memberType;
059:
060: /**
061: * If
062: * 1. the type is a union type where one of the member types is a list, or
063: * if the type is a list; and
064: * 2. the item type of the list is a union type
065: * then an array of member types used to validate the values.
066: */
067: public XSSimpleType[] memberTypes;
068:
069: /**
070: * In the case the value is a list or a list of unions, this value
071: * indicates the type(s) of the items in the list.
072: * For a normal list, the length of the array is 1; for list of unions,
073: * the length of the array is the same as the length of the list.
074: */
075: public ShortList itemValueTypes;
076:
077: /**
078: * reset the state of this object
079: */
080: public void reset() {
081: this .normalizedValue = null;
082: this .actualValue = null;
083: this .memberType = null;
084: this .memberTypes = null;
085: }
086:
087: /**
088: * Return a string representation of the value. If there is an actual
089: * value, use toString; otherwise, use the normalized value.
090: */
091: public String stringValue() {
092: if (actualValue == null)
093: return normalizedValue;
094: else
095: return actualValue.toString();
096: }
097:
098: /**
099: * Returns true if the two ValidatedInfo objects can be compared in the same
100: * value space.
101: */
102: public static boolean isComparable(ValidatedInfo info1,
103: ValidatedInfo info2) {
104: final short primitiveType1 = convertToPrimitiveKind(info1.actualValueType);
105: final short primitiveType2 = convertToPrimitiveKind(info2.actualValueType);
106: if (primitiveType1 != primitiveType2) {
107: return (primitiveType1 == XSConstants.ANYSIMPLETYPE_DT
108: && primitiveType2 == XSConstants.STRING_DT || primitiveType1 == XSConstants.STRING_DT
109: && primitiveType2 == XSConstants.ANYSIMPLETYPE_DT);
110: } else if (primitiveType1 == XSConstants.LIST_DT
111: || primitiveType1 == XSConstants.LISTOFUNION_DT) {
112: final ShortList typeList1 = info1.itemValueTypes;
113: final ShortList typeList2 = info2.itemValueTypes;
114: final int typeList1Length = typeList1 != null ? typeList1
115: .getLength() : 0;
116: final int typeList2Length = typeList2 != null ? typeList2
117: .getLength() : 0;
118: if (typeList1Length != typeList2Length) {
119: return false;
120: }
121: for (int i = 0; i < typeList1Length; ++i) {
122: final short primitiveItem1 = convertToPrimitiveKind(typeList1
123: .item(i));
124: final short primitiveItem2 = convertToPrimitiveKind(typeList2
125: .item(i));
126: if (primitiveItem1 != primitiveItem2) {
127: if (primitiveItem1 == XSConstants.ANYSIMPLETYPE_DT
128: && primitiveItem2 == XSConstants.STRING_DT
129: || primitiveItem1 == XSConstants.STRING_DT
130: && primitiveItem2 == XSConstants.ANYSIMPLETYPE_DT) {
131: continue;
132: }
133: return false;
134: }
135: }
136: }
137: return true;
138: }
139:
140: /**
141: * Returns the primitive type of the given type.
142: * @param valueType A value type as defined in XSConstants.
143: * @return The primitive type from which valueType was derived.
144: */
145: private static short convertToPrimitiveKind(short valueType) {
146: /** Primitive datatypes. */
147: if (valueType <= XSConstants.NOTATION_DT) {
148: return valueType;
149: }
150: /** Types derived from string. */
151: if (valueType <= XSConstants.ENTITY_DT) {
152: return XSConstants.STRING_DT;
153: }
154: /** Types derived from decimal. */
155: if (valueType <= XSConstants.POSITIVEINTEGER_DT) {
156: return XSConstants.DECIMAL_DT;
157: }
158: /** Other types. */
159: return valueType;
160: }
161: }
|