001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.util;
019:
020: import org.apache.xerces.impl.Constants;
021: import org.apache.xerces.xni.XMLAttributes;
022: import org.xml.sax.AttributeList;
023: import org.xml.sax.ext.Attributes2;
024:
025: /**
026: * Wraps {@link XMLAttributes} and makes it look like
027: * {@link AttributeList} and {@link Attributes2}.
028: *
029: * @author Arnaud Le Hors, IBM
030: * @author Andy Clark, IBM
031: *
032: * @version $Id: AttributesProxy.java 449487 2006-09-24 21:11:28Z mrglavas $
033: */
034: public final class AttributesProxy implements AttributeList,
035: Attributes2 {
036:
037: //
038: // Data
039: //
040:
041: /** XML attributes. */
042: private XMLAttributes fAttributes;
043:
044: //
045: // Constructors
046: //
047:
048: public AttributesProxy(XMLAttributes attributes) {
049: fAttributes = attributes;
050: }
051:
052: //
053: // Public methods
054: //
055:
056: /** Sets the XML attributes to be wrapped. */
057: public void setAttributes(XMLAttributes attributes) {
058: fAttributes = attributes;
059: } // setAttributes(XMLAttributes)
060:
061: public XMLAttributes getAttributes() {
062: return fAttributes;
063: }
064:
065: /*
066: * Attributes methods
067: */
068:
069: public int getLength() {
070: return fAttributes.getLength();
071: }
072:
073: public String getQName(int index) {
074: return fAttributes.getQName(index);
075: }
076:
077: public String getURI(int index) {
078: // This hides the fact that internally we use null instead of empty string
079: // SAX requires the URI to be a string or an empty string
080: String uri = fAttributes.getURI(index);
081: return uri != null ? uri : XMLSymbols.EMPTY_STRING;
082: }
083:
084: public String getLocalName(int index) {
085: return fAttributes.getLocalName(index);
086: }
087:
088: public String getType(int i) {
089: return fAttributes.getType(i);
090: }
091:
092: public String getType(String name) {
093: return fAttributes.getType(name);
094: }
095:
096: public String getType(String uri, String localName) {
097: return uri.equals(XMLSymbols.EMPTY_STRING) ? fAttributes
098: .getType(null, localName) : fAttributes.getType(uri,
099: localName);
100: }
101:
102: public String getValue(int i) {
103: return fAttributes.getValue(i);
104: }
105:
106: public String getValue(String name) {
107: return fAttributes.getValue(name);
108: }
109:
110: public String getValue(String uri, String localName) {
111: return uri.equals(XMLSymbols.EMPTY_STRING) ? fAttributes
112: .getValue(null, localName) : fAttributes.getValue(uri,
113: localName);
114: }
115:
116: public int getIndex(String qName) {
117: return fAttributes.getIndex(qName);
118: }
119:
120: public int getIndex(String uri, String localPart) {
121: return uri.equals(XMLSymbols.EMPTY_STRING) ? fAttributes
122: .getIndex(null, localPart) : fAttributes.getIndex(uri,
123: localPart);
124: }
125:
126: /*
127: * Attributes2 methods
128: */
129:
130: public boolean isDeclared(int index) {
131: if (index < 0 || index >= fAttributes.getLength()) {
132: throw new ArrayIndexOutOfBoundsException(index);
133: }
134: return Boolean.TRUE.equals(fAttributes.getAugmentations(index)
135: .getItem(Constants.ATTRIBUTE_DECLARED));
136: }
137:
138: public boolean isDeclared(String qName) {
139: int index = getIndex(qName);
140: if (index == -1) {
141: throw new IllegalArgumentException(qName);
142: }
143: return Boolean.TRUE.equals(fAttributes.getAugmentations(index)
144: .getItem(Constants.ATTRIBUTE_DECLARED));
145: }
146:
147: public boolean isDeclared(String uri, String localName) {
148: int index = getIndex(uri, localName);
149: if (index == -1) {
150: throw new IllegalArgumentException(localName);
151: }
152: return Boolean.TRUE.equals(fAttributes.getAugmentations(index)
153: .getItem(Constants.ATTRIBUTE_DECLARED));
154: }
155:
156: public boolean isSpecified(int index) {
157: if (index < 0 || index >= fAttributes.getLength()) {
158: throw new ArrayIndexOutOfBoundsException(index);
159: }
160: return fAttributes.isSpecified(index);
161: }
162:
163: public boolean isSpecified(String qName) {
164: int index = getIndex(qName);
165: if (index == -1) {
166: throw new IllegalArgumentException(qName);
167: }
168: return fAttributes.isSpecified(index);
169: }
170:
171: public boolean isSpecified(String uri, String localName) {
172: int index = getIndex(uri, localName);
173: if (index == -1) {
174: throw new IllegalArgumentException(localName);
175: }
176: return fAttributes.isSpecified(index);
177: }
178:
179: /*
180: * AttributeList methods
181: */
182:
183: public String getName(int i) {
184: return fAttributes.getQName(i);
185: }
186:
187: }
|