1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 /* 19 * $Id: AttList.java 468655 2006-10-28 07:12:06Z minchau $ 20 */ 21 package org.apache.xml.utils; 22 23 import org.w3c.dom.Attr; 24 import org.w3c.dom.NamedNodeMap; 25 import org.w3c.dom.Node; 26 27 import org.xml.sax.Attributes; 28 29 /** 30 * Wraps a DOM attribute list in a SAX Attributes. 31 * @xsl.usage internal 32 */ 33 public class AttList implements Attributes 34 { 35 36 /** List of attribute nodes */ 37 NamedNodeMap m_attrs; 38 39 /** Index of last attribute node */ 40 int m_lastIndex; 41 42 // ARGHH!! JAXP Uses Xerces without setting the namespace processing to ON! 43 // DOM2Helper m_dh = new DOM2Helper(); 44 45 /** Local reference to DOMHelper */ 46 DOMHelper m_dh; 47 48 // /** 49 // * Constructor AttList 50 // * 51 // * 52 // * @param attrs List of attributes this will contain 53 // */ 54 // public AttList(NamedNodeMap attrs) 55 // { 56 // 57 // m_attrs = attrs; 58 // m_lastIndex = m_attrs.getLength() - 1; 59 // m_dh = new DOM2Helper(); 60 // } 61 62 /** 63 * Constructor AttList 64 * 65 * 66 * @param attrs List of attributes this will contain 67 * @param dh DOMHelper 68 */ 69 public AttList(NamedNodeMap attrs, DOMHelper dh) 70 { 71 72 m_attrs = attrs; 73 m_lastIndex = m_attrs.getLength() - 1; 74 m_dh = dh; 75 } 76 77 /** 78 * Get the number of attribute nodes in the list 79 * 80 * 81 * @return number of attribute nodes 82 */ 83 public int getLength() 84 { 85 return m_attrs.getLength(); 86 } 87 88 /** 89 * Look up an attribute's Namespace URI by index. 90 * 91 * @param index The attribute index (zero-based). 92 * @return The Namespace URI, or the empty string if none 93 * is available, or null if the index is out of 94 * range. 95 */ 96 public String getURI(int index) 97 { 98 String ns = m_dh.getNamespaceOfNode(((Attr) m_attrs.item(index))); 99 if(null == ns) 100 ns = ""; 101 return ns; 102 } 103 104 /** 105 * Look up an attribute's local name by index. 106 * 107 * @param index The attribute index (zero-based). 108 * @return The local name, or the empty string if Namespace 109 * processing is not being performed, or null 110 * if the index is out of range. 111 */ 112 public String getLocalName(int index) 113 { 114 return m_dh.getLocalNameOfNode(((Attr) m_attrs.item(index))); 115 } 116 117 /** 118 * Look up an attribute's qualified name by index. 119 * 120 * 121 * @param i The attribute index (zero-based). 122 * 123 * @return The attribute's qualified name 124 */ 125 public String getQName(int i) 126 { 127 return ((Attr) m_attrs.item(i)).getName(); 128 } 129 130 /** 131 * Get the attribute's node type by index 132 * 133 * 134 * @param i The attribute index (zero-based) 135 * 136 * @return the attribute's node type 137 */ 138 public String getType(int i) 139 { 140 return "CDATA"; // for the moment 141 } 142 143 /** 144 * Get the attribute's node value by index 145 * 146 * 147 * @param i The attribute index (zero-based) 148 * 149 * @return the attribute's node value 150 */ 151 public String getValue(int i) 152 { 153 return ((Attr) m_attrs.item(i)).getValue(); 154 } 155 156 /** 157 * Get the attribute's node type by name 158 * 159 * 160 * @param name Attribute name 161 * 162 * @return the attribute's node type 163 */ 164 public String getType(String name) 165 { 166 return "CDATA"; // for the moment 167 } 168 169 /** 170 * Look up an attribute's type by Namespace name. 171 * 172 * @param uri The Namespace URI, or the empty String if the 173 * name has no Namespace URI. 174 * @param localName The local name of the attribute. 175 * @return The attribute type as a string, or null if the 176 * attribute is not in the list or if Namespace 177 * processing is not being performed. 178 */ 179 public String getType(String uri, String localName) 180 { 181 return "CDATA"; // for the moment 182 } 183 184 /** 185 * Look up an attribute's value by name. 186 * 187 * 188 * @param name The attribute node's name 189 * 190 * @return The attribute node's value 191 */ 192 public String getValue(String name) 193 { 194 Attr attr = ((Attr) m_attrs.getNamedItem(name)); 195 return (null != attr) 196 ? attr.getValue() : null; 197 } 198 199 /** 200 * Look up an attribute's value by Namespace name. 201 * 202 * @param uri The Namespace URI, or the empty String if the 203 * name has no Namespace URI. 204 * @param localName The local name of the attribute. 205 * @return The attribute value as a string, or null if the 206 * attribute is not in the list. 207 */ 208 public String getValue(String uri, String localName) 209 { 210 Node a=m_attrs.getNamedItemNS(uri,localName); 211 return (a==null) ? null : a.getNodeValue(); 212 } 213 214 /** 215 * Look up the index of an attribute by Namespace name. 216 * 217 * @param uri The Namespace URI, or the empty string if 218 * the name has no Namespace URI. 219 * @param localPart The attribute's local name. 220 * @return The index of the attribute, or -1 if it does not 221 * appear in the list. 222 */ 223 public int getIndex(String uri, String localPart) 224 { 225 for(int i=m_attrs.getLength()-1;i>=0;--i) 226 { 227 Node a=m_attrs.item(i); 228 String u=a.getNamespaceURI(); 229 if( (u==null ? uri==null : u.equals(uri)) 230 && 231 a.getLocalName().equals(localPart) ) 232 return i; 233 } 234 return -1; 235 } 236 237 /** 238 * Look up the index of an attribute by raw XML 1.0 name. 239 * 240 * @param qName The qualified (prefixed) name. 241 * @return The index of the attribute, or -1 if it does not 242 * appear in the list. 243 */ 244 public int getIndex(String qName) 245 { 246 for(int i=m_attrs.getLength()-1;i>=0;--i) 247 { 248 Node a=m_attrs.item(i); 249 if(a.getNodeName().equals(qName) ) 250 return i; 251 } 252 return -1; 253 } 254 } 255 256