1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2001,2002,2004,2005 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * 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 java.util.AbstractList;
  24 
  25 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  26 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  27 import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
  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 | XSSimpleTypeDecl.FACET_MAXLENGTH | XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_ENUMERATION | XSSimpleTypeDecl.FACET_WHITESPACE );
  42     }
  43 
  44     // this method should never be called: XSSimpleTypeDecl is responsible for
  45     // calling the item type for the convertion
  46     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException{
  47         return content;
  48     }
  49 
  50     // length of a list type is the number of items in the list
  51     public int getDataLength(Object value) {
  52         return ((ListData)value).getLength();
  53     }
  54 
  55     final static class ListData extends AbstractList implements ObjectList {
  56         final Object[] data;
  57         private String canonical;
  58         public ListData(Object[] data) {
  59             this.data = data;
  60         }
  61         public synchronized String toString() {
  62             if (canonical == null) {
  63                 int len = data.length;
  64                 StringBuffer buf = new StringBuffer();
  65                 if (len > 0) {
  66                     buf.append(data[0].toString());
  67                 }
  68                 for (int i = 1; i < len; i++) {
  69                     buf.append(' ');
  70                     buf.append(data[i].toString());
  71                 }
  72                 canonical = buf.toString();
  73             }
  74             return canonical;
  75         }
  76         public int getLength() {
  77             return data.length;
  78         }
  79         public boolean equals(Object obj) {
  80             if (!(obj instanceof ListData))
  81                 return false;
  82             Object[] odata = ((ListData)obj).data;
  83 
  84             int count = data.length;
  85             if (count != odata.length)
  86                 return false;
  87 
  88             for (int i = 0 ; i < count ; i++) {
  89                 if (!data[i].equals(odata[i]))
  90                     return false;
  91             }//end of loop
  92 
  93             //everything went fine.
  94             return true;
  95         }
  96 
  97         public int hashCode() {
  98             int hash = 0;
  99             for (int i = 0; i < data.length; ++i) {
 100                 hash ^= data[i].hashCode();
 101             }
 102             return hash;
 103         }
 104 
 105         public boolean contains(Object item) {
 106             for (int i = 0;i < data.length; i++) {
 107                 if (item == data[i]) {
 108                     return true;
 109                 }
 110             }
 111             return false;
 112         }
 113 
 114         public Object item(int index) {
 115             if (index < 0 || index >= data.length) {
 116                 return null;
 117             }
 118             return data[index];
 119         }
 120 
 121         /*
 122          * List methods
 123          */
 124 
 125         public Object get(int index) {
 126             if (index >= 0 && index < data.length) {
 127                 return data[index];
 128             }
 129             throw new IndexOutOfBoundsException("Index: " + index);
 130         }
 131 
 132         public int size() {
 133             return getLength();
 134         }
 135     }
 136 } // class ListDV