1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 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.util;
  22 
  23 import com.sun.org.apache.xerces.internal.impl.Constants;
  24 import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
  25 import org.xml.sax.AttributeList;
  26 import org.xml.sax.ext.Attributes2;
  27 
  28 /**
  29  * Wraps {@link XMLAttributes} and makes it look like
  30  * {@link AttributeList} and {@link Attributes}.
  31  *
  32  * @author Arnaud Le Hors, IBM
  33  * @author Andy Clark, IBM
  34  *
  35  */
  36 public final class AttributesProxy
  37     implements AttributeList, Attributes2 {
  38 
  39     //
  40     // Data
  41     //
  42 
  43     /** XML attributes. */
  44     private XMLAttributes fAttributes;
  45 
  46     //
  47     // Constructors
  48     //
  49 
  50     public AttributesProxy(XMLAttributes attributes) {
  51         fAttributes = attributes;
  52     }
  53 
  54     //
  55     // Public methods
  56     //
  57 
  58     /** Sets the XML attributes to be wrapped. */
  59     public void setAttributes(XMLAttributes attributes) {
  60         fAttributes = attributes;
  61     } // setAttributes(XMLAttributes)
  62 
  63     public XMLAttributes getAttributes() {
  64         return fAttributes;
  65     }
  66 
  67     /*
  68      * Attributes methods
  69      */
  70 
  71     public int getLength() {
  72         return fAttributes.getLength();
  73     }
  74 
  75     public String getQName(int index) {
  76         return fAttributes.getQName(index);
  77     }
  78 
  79     public String getURI(int index) {
  80         // This hides the fact that internally we use null instead of empty string
  81         // SAX requires the URI to be a string or an empty string
  82         String uri = fAttributes.getURI(index);
  83         return uri != null ? uri : XMLSymbols.EMPTY_STRING;
  84     }
  85 
  86     public String getLocalName(int index) {
  87         return fAttributes.getLocalName(index);
  88     }
  89 
  90     public String getType(int i) {
  91         return fAttributes.getType(i);
  92     }
  93 
  94     public String getType(String name) {
  95         return fAttributes.getType(name);
  96     }
  97 
  98     public String getType(String uri, String localName) {
  99         return uri.equals(XMLSymbols.EMPTY_STRING) ?
 100                 fAttributes.getType(null, localName) :
 101                     fAttributes.getType(uri, localName);
 102     }
 103 
 104     public String getValue(int i) {
 105         return fAttributes.getValue(i);
 106     }
 107 
 108     public String getValue(String name) {
 109         return fAttributes.getValue(name);
 110     }
 111 
 112     public String getValue(String uri, String localName) {
 113         return uri.equals(XMLSymbols.EMPTY_STRING) ?
 114                 fAttributes.getValue(null, localName) :
 115                     fAttributes.getValue(uri, localName);
 116     }
 117 
 118     public int getIndex(String qName) {
 119         return fAttributes.getIndex(qName);
 120     }
 121 
 122     public int getIndex(String uri, String localPart) {
 123         return uri.equals(XMLSymbols.EMPTY_STRING) ?
 124                 fAttributes.getIndex(null, localPart) :
 125                     fAttributes.getIndex(uri, localPart);
 126     }
 127 
 128     /*
 129      * Attributes2 methods
 130      */
 131 
 132     public boolean isDeclared(int index) {
 133         if (index < 0 || index >= fAttributes.getLength()) {
 134             throw new ArrayIndexOutOfBoundsException(index);
 135         }
 136         return Boolean.TRUE.equals(
 137             fAttributes.getAugmentations(index).getItem(
 138             Constants.ATTRIBUTE_DECLARED));
 139     }
 140 
 141     public boolean isDeclared(String qName) {
 142         int index = getIndex(qName);
 143         if (index == -1) {
 144             throw new IllegalArgumentException(qName);
 145         }
 146         return Boolean.TRUE.equals(
 147             fAttributes.getAugmentations(index).getItem(
 148             Constants.ATTRIBUTE_DECLARED));
 149     }
 150 
 151     public boolean isDeclared(String uri, String localName) {
 152         int index = getIndex(uri, localName);
 153         if (index == -1) {
 154             throw new IllegalArgumentException(localName);
 155         }
 156         return Boolean.TRUE.equals(
 157             fAttributes.getAugmentations(index).getItem(
 158             Constants.ATTRIBUTE_DECLARED));
 159     }
 160 
 161     public boolean isSpecified(int index) {
 162         if (index < 0 || index >= fAttributes.getLength()) {
 163             throw new ArrayIndexOutOfBoundsException(index);
 164         }
 165         return fAttributes.isSpecified(index);
 166     }
 167 
 168     public boolean isSpecified(String qName) {
 169         int index = getIndex(qName);
 170         if (index == -1) {
 171             throw new IllegalArgumentException(qName);
 172         }
 173         return fAttributes.isSpecified(index);
 174     }
 175 
 176     public boolean isSpecified(String uri, String localName) {
 177         int index = getIndex(uri, localName);
 178         if (index == -1) {
 179             throw new IllegalArgumentException(localName);
 180         }
 181         return fAttributes.isSpecified(index);
 182     }
 183 
 184     /*
 185      * AttributeList methods
 186      */
 187 
 188     public String getName(int i) {
 189         return fAttributes.getQName(i);
 190     }
 191 
 192 }