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