001: package net.sf.saxon.tinytree;
002:
003: import net.sf.saxon.om.AttributeCollection;
004: import net.sf.saxon.om.NamePool;
005: import net.sf.saxon.event.LocationProvider;
006: import net.sf.saxon.style.StandardNames;
007:
008: /**
009: * An implementation of the AttributeCollection interface based directly on the
010: * TinyTree data structure.
011: */
012:
013: public class TinyAttributeCollection implements AttributeCollection {
014:
015: int element;
016: TinyTree tree;
017: int firstAttribute;
018:
019: public TinyAttributeCollection(TinyTree tree, int element) {
020: this .tree = tree;
021: this .element = element;
022: this .firstAttribute = tree.alpha[element];
023: }
024:
025: /**
026: * Set the location provider. This must be set if the methods getSystemId() and getLineNumber()
027: * are to be used to get location information for an attribute.
028: */
029:
030: public void setLocationProvider(LocationProvider provider) {
031: //
032: }
033:
034: /**
035: * Return the number of attributes in the list.
036: *
037: * @return The number of attributes in the list.
038: */
039:
040: public int getLength() {
041: int i = firstAttribute;
042: while (i < tree.numberOfAttributes
043: && tree.attParent[i] == element) {
044: i++;
045: }
046: return i - firstAttribute;
047: }
048:
049: /**
050: * Get the namecode of an attribute (by position).
051: *
052: * @param index The position of the attribute in the list.
053: * @return The display name of the attribute as a string, or null if there
054: * is no attribute at that position.
055: */
056:
057: public int getNameCode(int index) {
058: return tree.attCode[firstAttribute + index];
059: }
060:
061: /**
062: * Get the type annotation of an attribute (by position).
063: *
064: * @param index The position of the attribute in the list.
065: * @return The type annotation of the attribute as the fingerprint of the type name.
066: * The bit {@link net.sf.saxon.om.NodeInfo.IS_DTD_TYPE} represents a DTD-derived type.
067: */
068:
069: public int getTypeAnnotation(int index) {
070: if (tree.attTypeCode == null) {
071: return StandardNames.XDT_UNTYPED_ATOMIC;
072: }
073: ;
074: return tree.attTypeCode[firstAttribute + index];
075: }
076:
077: /**
078: * Get the locationID of an attribute (by position)
079: *
080: * @param index The position of the attribute in the list.
081: * @return The location identifier of the attribute. This can be supplied
082: * to a {@link net.sf.saxon.event.LocationProvider} in order to obtain the
083: * actual system identifier and line number of the relevant location
084: */
085:
086: public int getLocationId(int index) {
087: return 0;
088: }
089:
090: /**
091: * Get the systemId part of the location of an attribute, at a given index.
092: * <p/>
093: * <p>Attribute location information is not available from a SAX parser, so this method
094: * is not useful for getting the location of an attribute in a source document. However,
095: * in a Saxon result document, the location information represents the location in the
096: * stylesheet of the instruction used to generate this attribute, which is useful for
097: * debugging.</p>
098: *
099: * @param index the required attribute
100: * @return the systemId of the location of the attribute
101: */
102:
103: public String getSystemId(int index) {
104: return tree.getSystemId(element);
105: }
106:
107: /**
108: * Get the line number part of the location of an attribute, at a given index.
109: * <p/>
110: * <p>Attribute location information is not available from a SAX parser, so this method
111: * is not useful for getting the location of an attribute in a source document. However,
112: * in a Saxon result document, the location information represents the location in the
113: * stylesheet of the instruction used to generate this attribute, which is useful for
114: * debugging.</p>
115: *
116: * @param index the required attribute
117: * @return the line number of the location of the attribute
118: */
119:
120: public int getLineNumber(int index) {
121: return -1;
122: }
123:
124: /**
125: * Get the properties of an attribute (by position)
126: *
127: * @param index The position of the attribute in the list.
128: * @return The properties of the attribute. This is a set
129: * of bit-settings defined in class {@link net.sf.saxon.event.ReceiverOptions}. The
130: * most interesting of these is {{@link net.sf.saxon.event.ReceiverOptions#DEFAULTED_ATTRIBUTE},
131: * which indicates an attribute that was added to an element as a result of schema validation.
132: */
133:
134: public int getProperties(int index) {
135: return 0;
136: }
137:
138: /**
139: * Get the prefix of the name of an attribute (by position).
140: *
141: * @param index The position of the attribute in the list.
142: * @return The prefix of the attribute name as a string, or null if there
143: * is no attribute at that position. Returns "" for an attribute that
144: * has no prefix.
145: */
146:
147: public String getPrefix(int index) {
148: return tree.getNamePool().getPrefix(getNameCode(index));
149: }
150:
151: /**
152: * Get the lexical QName of an attribute (by position).
153: *
154: * @param index The position of the attribute in the list.
155: * @return The lexical QName of the attribute as a string, or null if there
156: * is no attribute at that position.
157: */
158:
159: public String getQName(int index) {
160: return tree.getNamePool().getDisplayName(getNameCode(index));
161: }
162:
163: /**
164: * Get the local name of an attribute (by position).
165: *
166: * @param index The position of the attribute in the list.
167: * @return The local name of the attribute as a string, or null if there
168: * is no attribute at that position.
169: */
170:
171: public String getLocalName(int index) {
172: return tree.getNamePool().getLocalName(getNameCode(index));
173: }
174:
175: /**
176: * Get the namespace URI of an attribute (by position).
177: *
178: * @param index The position of the attribute in the list.
179: * @return The local name of the attribute as a string, or null if there
180: * is no attribute at that position.
181: */
182:
183: public String getURI(int index) {
184: return tree.getNamePool().getURI(getNameCode(index));
185: }
186:
187: /**
188: * Get the index of an attribute (by name).
189: *
190: * @param uri The namespace uri of the attribute.
191: * @param localname The local name of the attribute.
192: * @return The index position of the attribute
193: */
194:
195: public int getIndex(String uri, String localname) {
196: int fingerprint = tree.getNamePool().getFingerprint(uri,
197: localname);
198: return getIndexByFingerprint(fingerprint);
199: }
200:
201: /**
202: * Get the index, given the fingerprint
203: */
204:
205: public int getIndexByFingerprint(int fingerprint) {
206: int i = firstAttribute;
207: while (tree.attParent[i] == element) {
208: if ((tree.attCode[i] & NamePool.FP_MASK) == fingerprint) {
209: return i - firstAttribute;
210: }
211: i++;
212: }
213: return -1;
214: }
215:
216: /**
217: * Get the attribute value using its fingerprint
218: */
219:
220: public String getValueByFingerprint(int fingerprint) {
221: return getValue(getIndexByFingerprint(fingerprint));
222: }
223:
224: /**
225: * Get the value of an attribute (by name).
226: *
227: * @param uri The namespace uri of the attribute.
228: * @param localname The local name of the attribute.
229: * @return The index position of the attribute
230: */
231:
232: public String getValue(String uri, String localname) {
233: return getValue(getIndex(uri, localname));
234: }
235:
236: /**
237: * Get the value of an attribute (by position).
238: *
239: * @param index The position of the attribute in the list.
240: * @return The attribute value as a string, or null if
241: * there is no attribute at that position.
242: */
243:
244: public String getValue(int index) {
245: return tree.attValue[firstAttribute + index].toString();
246: }
247:
248: /**
249: * Determine whether a given attribute has the is-ID property set
250: */
251:
252: public boolean isId(int index) {
253: return ((getTypeAnnotation(index) & NamePool.FP_MASK) == StandardNames.XS_ID)
254: || ((getNameCode(index) & NamePool.FP_MASK) == StandardNames.XML_ID);
255: }
256: }
|