1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * @LastModified: Oct 2017
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xerces.internal.impl.dv.xs;
  23 
  24 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  25 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  26 import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
  27 import java.util.AbstractList;
  28 
  29 /**
  30  * Represent the schema list types
  31  *
  32  * @xerces.internal
  33  *
  34  * @author Neeraj Bajaj, Sun Microsystems, inc.
  35  * @author Sandy Gao, IBM
  36  *
  37  */
  38 public class ListDV extends TypeValidator{
  39 
  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
  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
  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;
  61         public ListData(Object[] data) {
  62             this.data = data;
  63         }
  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         }
  79         public int getLength() {
  80             return data.length;
  81         }
  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 
 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 
 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 
 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 
 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 
 135         public int size() {
 136             return getLength();
 137         }
 138     }
 139 } // class ListDV