ValidatedInfo.java revision 635:65e6291d9ba9
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.sun.org.apache.xerces.internal.impl.dv;
19
20import com.sun.org.apache.xerces.internal.xs.ShortList;
21import com.sun.org.apache.xerces.internal.xs.XSConstants;
22
23/**
24 * Class to get the information back after content is validated. This info
25 * would be filled by validate().
26 *
27 * @xerces.internal
28 *
29 * @author Neeraj Bajaj, Sun Microsystems, inc.
30 *
31 */
32public class ValidatedInfo {
33
34    /**
35     * The normalized value of a string value
36     */
37    public String normalizedValue;
38
39    /**
40     * The actual value from a string value (QName, Boolean, etc.)
41     * An array of Objects if the type is a list.
42     */
43    public Object actualValue;
44
45    /**
46     * The type of the actual value. It's one of the _DT constants
47     * defined in XSConstants.java. The value is used to indicate
48     * the most specific built-in type.
49     * (i.e. short instead of decimal or integer).
50     */
51    public short actualValueType;
52
53    /**
54     * If the type is a union type, then the member type which
55     * actually validated the string value.
56     */
57    public XSSimpleType memberType;
58
59    /**
60     * If
61     * 1. the type is a union type where one of the member types is a list, or
62     *    if the type is a list; and
63     * 2. the item type of the list is a union type
64     * then an array of member types used to validate the values.
65     */
66    public XSSimpleType[] memberTypes;
67
68    /**
69     * In the case the value is a list or a list of unions, this value
70     * indicates the type(s) of the items in the list.
71     * For a normal list, the length of the array is 1; for list of unions,
72     * the length of the array is the same as the length of the list.
73     */
74    public ShortList itemValueTypes;
75
76    /**
77     * reset the state of this object
78     */
79    public void reset() {
80        this.normalizedValue = null;
81        this.actualValue = null;
82        this.memberType = null;
83        this.memberTypes = null;
84    }
85
86    /**
87     * Return a string representation of the value. If there is an actual
88     * value, use toString; otherwise, use the normalized value.
89     */
90    public String stringValue() {
91        if (actualValue == null)
92            return normalizedValue;
93        else
94            return actualValue.toString();
95    }
96
97    /**
98     * Returns true if the two ValidatedInfo objects can be compared in the same
99     * value space.
100     */
101    public static boolean isComparable(ValidatedInfo info1, ValidatedInfo info2) {
102        final short primitiveType1 = convertToPrimitiveKind(info1.actualValueType);
103        final short primitiveType2 = convertToPrimitiveKind(info2.actualValueType);
104        if (primitiveType1 != primitiveType2) {
105            return (primitiveType1 == XSConstants.ANYSIMPLETYPE_DT && primitiveType2 == XSConstants.STRING_DT ||
106                    primitiveType1 == XSConstants.STRING_DT && primitiveType2 == XSConstants.ANYSIMPLETYPE_DT);
107        }
108        else if (primitiveType1 == XSConstants.LIST_DT || primitiveType1 == XSConstants.LISTOFUNION_DT) {
109            final ShortList typeList1 = info1.itemValueTypes;
110            final ShortList typeList2 = info2.itemValueTypes;
111            final int typeList1Length = typeList1 != null ? typeList1.getLength() : 0;
112            final int typeList2Length = typeList2 != null ? typeList2.getLength() : 0;
113            if (typeList1Length != typeList2Length) {
114                return false;
115            }
116            for (int i = 0; i < typeList1Length; ++i) {
117                final short primitiveItem1 = convertToPrimitiveKind(typeList1.item(i));
118                final short primitiveItem2 = convertToPrimitiveKind(typeList2.item(i));
119                if (primitiveItem1 != primitiveItem2) {
120                    if (primitiveItem1 == XSConstants.ANYSIMPLETYPE_DT && primitiveItem2 == XSConstants.STRING_DT ||
121                        primitiveItem1 == XSConstants.STRING_DT && primitiveItem2 == XSConstants.ANYSIMPLETYPE_DT) {
122                        continue;
123                    }
124                    return false;
125                }
126            }
127        }
128        return true;
129    }
130
131    /**
132     * Returns the primitive type of the given type.
133     * @param valueType A value type as defined in XSConstants.
134     * @return The primitive type from which valueType was derived.
135     */
136    private static short convertToPrimitiveKind(short valueType) {
137        /** Primitive datatypes. */
138        if (valueType <= XSConstants.NOTATION_DT) {
139            return valueType;
140        }
141        /** Types derived from string. */
142        if (valueType <= XSConstants.ENTITY_DT) {
143            return XSConstants.STRING_DT;
144        }
145        /** Types derived from decimal. */
146        if (valueType <= XSConstants.POSITIVEINTEGER_DT) {
147            return XSConstants.DECIMAL_DT;
148        }
149        /** Other types. */
150        return valueType;
151    }
152}
153