1 /*
2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3  */
4 /*
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements.  See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License.  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 package com.sun.org.apache.xerces.internal.impl.dv.xs;
22 
23 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
24 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
25 import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
26 import java.util.AbstractList;
27 
28 /**
29  * Represent the schema list types
30  *
31  * @xerces.internal
32  *
33  * @author Neeraj Bajaj, Sun Microsystems, inc.
34  * @author Sandy Gao, IBM
35  *
36  * @LastModified: Oct 2017
37  */
38 public class ListDV extends TypeValidator{
39 
getAllowedFacets()40     public short getAllowedFacets(){
41           return (XSSimpleTypeDecl.FACET_LENGTH | XSSimpleTypeDecl.FACET_MINLENGTH |
42                   XSSimpleTypeDecl.FACET_MAXLENGTH | XSSimpleTypeDecl.FACET_PATTERN |
43                   XSSimpleTypeDecl.FACET_ENUMERATION | XSSimpleTypeDecl.FACET_WHITESPACE );
44     }
45 
46     // this method should never be called: XSSimpleTypeDecl is responsible for
47     // calling the item type for the convertion
getActualValue(String content, ValidationContext context)48     public Object getActualValue(String content, ValidationContext context)
49             throws InvalidDatatypeValueException{
50         return content;
51     }
52 
53     // length of a list type is the number of items in the list
getDataLength(Object value)54     public int getDataLength(Object value) {
55         return ((ListData)value).getLength();
56     }
57 
58     final static class ListData extends AbstractList<Object> implements ObjectList {
59         final Object[] data;
60         private String canonical;
ListData(Object[] data)61         public ListData(Object[] data) {
62             this.data = data;
63         }
toString()64         public synchronized String toString() {
65             if (canonical == null) {
66                 int len = data.length;
67                 StringBuffer buf = new StringBuffer();
68                 if (len > 0) {
69                     buf.append(data[0].toString());
70                 }
71                 for (int i = 1; i < len; i++) {
72                     buf.append(' ');
73                     buf.append(data[i].toString());
74                 }
75                 canonical = buf.toString();
76             }
77             return canonical;
78         }
getLength()79         public int getLength() {
80             return data.length;
81         }
equals(Object obj)82         public boolean equals(Object obj) {
83             if (!(obj instanceof ListData))
84                 return false;
85             Object[] odata = ((ListData)obj).data;
86 
87             int count = data.length;
88             if (count != odata.length)
89                 return false;
90 
91             for (int i = 0 ; i < count ; i++) {
92                 if (!data[i].equals(odata[i]))
93                     return false;
94             }//end of loop
95 
96             //everything went fine.
97             return true;
98         }
99 
hashCode()100         public int hashCode() {
101             int hash = 0;
102             for (int i = 0; i < data.length; ++i) {
103                 hash ^= data[i].hashCode();
104             }
105             return hash;
106         }
107 
contains(Object item)108         public boolean contains(Object item) {
109             for (int i = 0;i < data.length; i++) {
110                 if (item == data[i]) {
111                     return true;
112                 }
113             }
114             return false;
115         }
116 
item(int index)117         public Object item(int index) {
118             if (index < 0 || index >= data.length) {
119                 return null;
120             }
121             return data[index];
122         }
123 
124         /*
125          * List methods
126          */
127 
get(int index)128         public Object get(int index) {
129             if (index >= 0 && index < data.length) {
130                 return data[index];
131             }
132             throw new IndexOutOfBoundsException("Index: " + index);
133         }
134 
size()135         public int size() {
136             return getLength();
137         }
138     }
139 } // class ListDV
140