KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > util > AttributesProxy


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.util;
18
19 import org.apache.xerces.impl.Constants;
20 import org.apache.xerces.xni.XMLAttributes;
21 import org.xml.sax.AttributeList JavaDoc;
22 import org.xml.sax.ext.Attributes2 JavaDoc;
23
24 /**
25  * Wraps {@link XMLAttributes} and makes it look like
26  * {@link AttributeList} and {@link Attributes}.
27  *
28  * @author Arnaud Le Hors, IBM
29  * @author Andy Clark, IBM
30  *
31  * @version $Id: AttributesProxy.java,v 1.2 2005/06/13 18:19:45 mrglavas Exp $
32  */

33 public final class AttributesProxy
34     implements AttributeList JavaDoc, Attributes2 JavaDoc {
35     
36     //
37
// Data
38
//
39

40     /** XML attributes. */
41     private XMLAttributes fAttributes;
42     
43     //
44
// Constructors
45
//
46

47     public AttributesProxy(XMLAttributes attributes) {
48         fAttributes = attributes;
49     }
50
51     //
52
// Public methods
53
//
54

55     /** Sets the XML attributes to be wrapped. */
56     public void setAttributes(XMLAttributes attributes) {
57         fAttributes = attributes;
58     } // setAttributes(XMLAttributes)
59

60     public XMLAttributes getAttributes() {
61         return fAttributes;
62     }
63
64     /*
65      * Attributes methods
66      */

67
68     public int getLength() {
69         return fAttributes.getLength();
70     }
71
72     public String JavaDoc getQName(int index) {
73         return fAttributes.getQName(index);
74     }
75
76     public String JavaDoc getURI(int index) {
77         // This hides the fact that internally we use null instead of empty string
78
// SAX requires the URI to be a string or an empty string
79
String JavaDoc uri = fAttributes.getURI(index);
80         return uri != null ? uri : XMLSymbols.EMPTY_STRING;
81     }
82
83     public String JavaDoc getLocalName(int index) {
84         return fAttributes.getLocalName(index);
85     }
86
87     public String JavaDoc getType(int i) {
88         return fAttributes.getType(i);
89     }
90
91     public String JavaDoc getType(String JavaDoc name) {
92         return fAttributes.getType(name);
93     }
94
95     public String JavaDoc getType(String JavaDoc uri, String JavaDoc localName) {
96         return uri.equals(XMLSymbols.EMPTY_STRING) ?
97                 fAttributes.getType(null, localName) :
98                     fAttributes.getType(uri, localName);
99     }
100
101     public String JavaDoc getValue(int i) {
102         return fAttributes.getValue(i);
103     }
104
105     public String JavaDoc getValue(String JavaDoc name) {
106         return fAttributes.getValue(name);
107     }
108
109     public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localName) {
110         return uri.equals(XMLSymbols.EMPTY_STRING) ?
111                 fAttributes.getValue(null, localName) :
112                     fAttributes.getValue(uri, localName);
113     }
114
115     public int getIndex(String JavaDoc qName) {
116         return fAttributes.getIndex(qName);
117     }
118
119     public int getIndex(String JavaDoc uri, String JavaDoc localPart) {
120         return uri.equals(XMLSymbols.EMPTY_STRING) ?
121                 fAttributes.getIndex(null, localPart) :
122                     fAttributes.getIndex(uri, localPart);
123     }
124     
125     /*
126      * Attributes2 methods
127      */

128     
129     public boolean isDeclared(int index) {
130         if (index < 0 || index >= fAttributes.getLength()) {
131             throw new ArrayIndexOutOfBoundsException JavaDoc(index);
132         }
133         return Boolean.TRUE.equals(
134             fAttributes.getAugmentations(index).getItem(
135             Constants.ATTRIBUTE_DECLARED));
136     }
137     
138     public boolean isDeclared(String JavaDoc qName) {
139         int index = getIndex(qName);
140         if (index == -1) {
141             throw new IllegalArgumentException JavaDoc(qName);
142         }
143         return Boolean.TRUE.equals(
144             fAttributes.getAugmentations(index).getItem(
145             Constants.ATTRIBUTE_DECLARED));
146     }
147     
148     public boolean isDeclared(String JavaDoc uri, String JavaDoc localName) {
149         int index = getIndex(uri, localName);
150         if (index == -1) {
151             throw new IllegalArgumentException JavaDoc(localName);
152         }
153         return Boolean.TRUE.equals(
154             fAttributes.getAugmentations(index).getItem(
155             Constants.ATTRIBUTE_DECLARED));
156     }
157             
158     public boolean isSpecified(int index) {
159         if (index < 0 || index >= fAttributes.getLength()) {
160             throw new ArrayIndexOutOfBoundsException JavaDoc(index);
161         }
162         return fAttributes.isSpecified(index);
163     }
164     
165     public boolean isSpecified(String JavaDoc qName) {
166         int index = getIndex(qName);
167         if (index == -1) {
168             throw new IllegalArgumentException JavaDoc(qName);
169         }
170         return fAttributes.isSpecified(index);
171     }
172     
173     public boolean isSpecified(String JavaDoc uri, String JavaDoc localName) {
174         int index = getIndex(uri, localName);
175         if (index == -1) {
176             throw new IllegalArgumentException JavaDoc(localName);
177         }
178         return fAttributes.isSpecified(index);
179     }
180     
181     /*
182      * AttributeList methods
183      */

184
185     public String JavaDoc getName(int i) {
186         return fAttributes.getQName(i);
187     }
188
189 }
Popular Tags