001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.ukit.jaxp;
028:
029: import org.xml.sax.Attributes;
030:
031: /**
032: * SAX Attributes interface implementation.
033: */
034:
035: /* package */class Attrs implements org.xml.sax.Attributes {
036: /**
037: * Attributes string array. Each individual attribute is reprecented by
038: * four strings: namespace URL(+0), qname(+1), local name(+2), value(+3),
039: * type(+4).
040: * In order to find attribute by the attrubute index, the attribute
041: * index MUST be multiplied by 8. The result will point to the attribute
042: * namespace URL.
043: */
044: /* package */String[] mItems;
045:
046: /**
047: * Number of attributes in the attributes string array.
048: */
049: private char mLength;
050:
051: /**
052: * Constructor.
053: */
054: /* package */Attrs() {
055: // The default number of attributies capacity is 8.
056: mItems = new String[(8 << 3)];
057: }
058:
059: /**
060: * Sets up the number of attributes and ensure the capacity of
061: * the attribute string array.
062: *
063: * @param length The number of attributes in the object.
064: */
065: /* package */void setLength(char length) {
066: if (length > ((char) (mItems.length >> 3))) {
067: mItems = new String[length << 3];
068: }
069: mLength = length;
070: }
071:
072: /**
073: * Return the number of attributes in the list.
074: *
075: * <p>Once you know the number of attributes, you can iterate
076: * through the list.</p>
077: *
078: * @return The number of attributes in the list.
079: * @see #getURI(int)
080: * @see #getLocalName(int)
081: * @see #getQName(int)
082: * @see #getType(int)
083: * @see #getValue(int)
084: */
085: public int getLength() {
086: return mLength;
087: }
088:
089: /**
090: * Look up an attribute's Namespace URI by index.
091: *
092: * @param index The attribute index (zero-based).
093: * @return The Namespace URI, or the empty string if none
094: * is available, or null if the index is out of
095: * range.
096: * @see #getLength
097: */
098: public String getURI(int index) {
099: return ((index >= 0) && (index < mLength)) ? (mItems[index << 3])
100: : null;
101: }
102:
103: /**
104: * Look up an attribute's local name by index.
105: *
106: * @param index The attribute index (zero-based).
107: * @return The local name, or the empty string if Namespace
108: * processing is not being performed, or null
109: * if the index is out of range.
110: * @see #getLength
111: */
112: public String getLocalName(int index) {
113: return ((index >= 0) && (index < mLength)) ? (mItems[(index << 3) + 2])
114: : null;
115: }
116:
117: /**
118: * Look up an attribute's XML 1.0 qualified name by index.
119: *
120: * @param index The attribute index (zero-based).
121: * @return The XML 1.0 qualified name, or the empty string
122: * if none is available, or null if the index
123: * is out of range.
124: * @see #getLength
125: */
126: public String getQName(int index) {
127: if ((index < 0) || (index >= mLength))
128: return null;
129: return mItems[(index << 3) + 1];
130: }
131:
132: /**
133: * Look up an attribute's type by index.
134: *
135: * <p>The attribute type is one of the strings "CDATA", "ID",
136: * "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES",
137: * or "NOTATION" (always in upper case).</p>
138: *
139: * <p>If the parser has not read a declaration for the attribute,
140: * or if the parser does not report attribute types, then it must
141: * return the value "CDATA" as stated in the XML 1.0 Recommentation
142: * (clause 3.3.3, "Attribute-Value Normalization").</p>
143: *
144: * <p>For an enumerated attribute that is not a notation, the
145: * parser will report the type as "NMTOKEN".</p>
146: *
147: * @param index The attribute index (zero-based).
148: * @return The attribute's type as a string, or null if the
149: * index is out of range.
150: * @see #getLength
151: */
152: public String getType(int index) {
153: return ((index >= 0) && (index < (mItems.length >> 3))) ? (mItems[(index << 3) + 4])
154: : null;
155: }
156:
157: /**
158: * Look up an attribute's value by index.
159: *
160: * <p>If the attribute value is a list of tokens (IDREFS,
161: * ENTITIES, or NMTOKENS), the tokens will be concatenated
162: * into a single string with each token separated by a
163: * single space.</p>
164: *
165: * @param index The attribute index (zero-based).
166: * @return The attribute's value as a string, or null if the
167: * index is out of range.
168: * @see #getLength
169: */
170: public String getValue(int index) {
171: return ((index >= 0) && (index < mLength)) ? (mItems[(index << 3) + 3])
172: : null;
173: }
174:
175: /**
176: * Look up the index of an attribute by Namespace name.
177: *
178: * @param uri The Namespace URI, or the empty string if
179: * the name has no Namespace URI.
180: * @param localName The attribute's local name.
181: * @return The index of the attribute, or -1 if it does not
182: * appear in the list.
183: */
184: public int getIndex(String uri, String localName) {
185: char len = mLength;
186: char idx = 0;
187: while (idx < len) {
188: if ((mItems[idx << 3]).equals(uri)
189: && mItems[(idx << 3) + 2].equals(localName))
190: return idx;
191: idx++;
192: }
193: return -1;
194: }
195:
196: /**
197: * Look up the index of an attribute by XML 1.0 qualified name.
198: *
199: * @param qName The qualified (prefixed) name.
200: * @return The index of the attribute, or -1 if it does not
201: * appear in the list.
202: */
203: public int getIndex(String qName) {
204: char len = mLength;
205: char idx = 0;
206: while (idx < len) {
207: if (getQName(idx).equals(qName))
208: return idx;
209: idx++;
210: }
211: return -1;
212: }
213:
214: /**
215: * Look up an attribute's type by Namespace name.
216: *
217: * <p>See {@link #getType(int) getType(int)} for a description
218: * of the possible types.</p>
219: *
220: * @param uri The Namespace URI, or the empty String if the
221: * name has no Namespace URI.
222: * @param localName The local name of the attribute.
223: * @return The attribute type as a string, or null if the
224: * attribute is not in the list or if Namespace
225: * processing is not being performed.
226: */
227: public String getType(String uri, String localName) {
228: int idx = getIndex(uri, localName);
229: return (idx >= 0) ? (mItems[(idx << 3) + 4]) : null;
230: }
231:
232: /**
233: * Look up an attribute's type by XML 1.0 qualified name.
234: *
235: * <p>See {@link #getType(int) getType(int)} for a description
236: * of the possible types.</p>
237: *
238: * @param qName The XML 1.0 qualified name.
239: * @return The attribute type as a string, or null if the
240: * attribute is not in the list or if qualified names
241: * are not available.
242: */
243: public String getType(String qName) {
244: int idx = getIndex(qName);
245: return (idx >= 0) ? (mItems[(idx << 3) + 4]) : null;
246: }
247:
248: /**
249: * Look up an attribute's value by Namespace name.
250: *
251: * <p>See {@link #getValue(int) getValue(int)} for a description
252: * of the possible values.</p>
253: *
254: * @param uri The Namespace URI, or the empty String if the
255: * name has no Namespace URI.
256: * @param localName The local name of the attribute.
257: * @return The attribute value as a string, or null if the
258: * attribute is not in the list.
259: */
260: public String getValue(String uri, String localName) {
261: int idx = getIndex(uri, localName);
262: return (idx >= 0) ? (mItems[(idx << 3) + 3]) : null;
263: }
264:
265: /**
266: * Look up an attribute's value by XML 1.0 qualified name.
267: *
268: * <p>See {@link #getValue(int) getValue(int)} for a description
269: * of the possible values.</p>
270: *
271: * @param qName The XML 1.0 qualified name.
272: * @return The attribute value as a string, or null if the
273: * attribute is not in the list or if qualified names
274: * are not available.
275: */
276: public String getValue(String qName) {
277: int idx = getIndex(qName);
278: return (idx >= 0) ? (mItems[(idx << 3) + 3]) : null;
279: }
280: }
|