1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   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 java.util.AbstractList;
  25 
  26 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  27 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  28 import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
  29 
  30 /**
  31  * Represent the schema list types
  32  *
  33  * @xerces.internal
  34  *
  35  * @author Neeraj Bajaj, Sun Microsystems, inc.
  36  * @author Sandy Gao, IBM
  37  *
  38  * @version $Id: ListDV.java,v 1.7 2010-11-01 04:39:47 joehw Exp $
  39  */
  40 public class ListDV extends TypeValidator{
  41 
  42     public short getAllowedFacets(){
  43           return (XSSimpleTypeDecl.FACET_LENGTH | XSSimpleTypeDecl.FACET_MINLENGTH | XSSimpleTypeDecl.FACET_MAXLENGTH | XSSimpleTypeDecl.FACET_PATTERN | 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) throws InvalidDatatypeValueException{
  49         return content;
  50     }
  51 
  52     // length of a list type is the number of items in the list
  53     public int getDataLength(Object value) {
  54         return ((ListData)value).getLength();
  55     }
  56 
  57     final static class ListData extends AbstractList implements ObjectList {
  58         final Object[] data;
  59         private String canonical;
  60         public ListData(Object[] data) {
  61             this.data = data;
  62         }
  63         public synchronized String toString() {
  64             if (canonical == null) {
  65                 int len = data.length;
  66                 StringBuffer buf = new StringBuffer();
  67                 if (len > 0) {
  68                     buf.append(data[0].toString());
  69                 }
  70                 for (int i = 1; i < len; i++) {
  71                     buf.append(' ');
  72                     buf.append(data[i].toString());
  73                 }
  74                 canonical = buf.toString();
  75             }
  76             return canonical;
  77         }
  78         public int getLength() {
  79             return data.length;
  80         }
  81         public boolean equals(Object obj) {
  82             if (!(obj instanceof ListData))
  83                 return false;
  84             Object[] odata = ((ListData)obj).data;
  85 
  86             int count = data.length;
  87             if (count != odata.length)
  88                 return false;
  89 
  90             for (int i = 0 ; i < count ; i++) {
  91                 if (!data[i].equals(odata[i]))
  92                     return false;
  93             }//end of loop
  94 
  95             //everything went fine.
  96             return true;
  97         }
  98 
  99         public int hashCode() {
 100             int hash = 0;
 101             for (int i = 0; i < data.length; ++i) {
 102                 hash ^= data[i].hashCode();
 103             }
 104             return hash;
 105         }
 106 
 107         public boolean contains(Object item) {
 108             for (int i = 0;i < data.length; i++) {
 109                 if (item == data[i]) {
 110                     return true;
 111                 }
 112             }
 113             return false;
 114         }
 115 
 116         public Object item(int index) {
 117             if (index < 0 || index >= data.length) {
 118                 return null;
 119             }
 120             return data[index];
 121         }
 122 
 123         /*
 124          * List methods
 125          */
 126 
 127         public Object get(int index) {
 128             if (index >= 0 && index < data.length) {
 129                 return data[index];
 130             }
 131             throw new IndexOutOfBoundsException("Index: " + index);
 132         }
 133 
 134         public int size() {
 135             return getLength();
 136         }
 137     }
 138 } // class ListDV