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