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 468654 2006-10-28 07:09:23Z minchau $ 20 */ 21 package org.apache.xml.serializer.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 * 32 * This class is a copy of the one in org.apache.xml.utils. 33 * It exists to cut the serializers dependancy on that package. 34 * A minor changes from that package are: 35 * DOMHelper reference changed to DOM2Helper, class is not "public" 36 * 37 * This class is not a public API, it is only public because it is 38 * used in org.apache.xml.serializer. 39 * 40 * @xsl.usage internal 41 */ 42 public final class AttList implements Attributes 43 { 44 45 /** List of attribute nodes */ 46 NamedNodeMap m_attrs; 47 48 /** Index of last attribute node */ 49 int m_lastIndex; 50 51 // ARGHH!! JAXP Uses Xerces without setting the namespace processing to ON! 52 // DOM2Helper m_dh = new DOM2Helper(); 53 54 /** Local reference to DOMHelper */ 55 DOM2Helper m_dh; 56 57 // /** 58 // * Constructor AttList 59 // * 60 // * 61 // * @param attrs List of attributes this will contain 62 // */ 63 // public AttList(NamedNodeMap attrs) 64 // { 65 // 66 // m_attrs = attrs; 67 // m_lastIndex = m_attrs.getLength() - 1; 68 // m_dh = new DOM2Helper(); 69 // } 70 71 /** 72 * Constructor AttList 73 * 74 * 75 * @param attrs List of attributes this will contain 76 * @param dh DOMHelper 77 */ 78 public AttList(NamedNodeMap attrs, DOM2Helper dh) 79 { 80 81 m_attrs = attrs; 82 m_lastIndex = m_attrs.getLength() - 1; 83 m_dh = dh; 84 } 85 86 /** 87 * Get the number of attribute nodes in the list 88 * 89 * 90 * @return number of attribute nodes 91 */ 92 public int getLength() 93 { 94 return m_attrs.getLength(); 95 } 96 97 /** 98 * Look up an attribute's Namespace URI by index. 99 * 100 * @param index The attribute index (zero-based). 101 * @return The Namespace URI, or the empty string if none 102 * is available, or null if the index is out of 103 * range. 104 */ 105 public String getURI(int index) 106 { 107 String ns = m_dh.getNamespaceOfNode(((Attr) m_attrs.item(index))); 108 if(null == ns) 109 ns = ""; 110 return ns; 111 } 112 113 /** 114 * Look up an attribute's local name by index. 115 * 116 * @param index The attribute index (zero-based). 117 * @return The local name, or the empty string if Namespace 118 * processing is not being performed, or null 119 * if the index is out of range. 120 */ 121 public String getLocalName(int index) 122 { 123 return m_dh.getLocalNameOfNode(((Attr) m_attrs.item(index))); 124 } 125 126 /** 127 * Look up an attribute's qualified name by index. 128 * 129 * 130 * @param i The attribute index (zero-based). 131 * 132 * @return The attribute's qualified name 133 */ 134 public String getQName(int i) 135 { 136 return ((Attr) m_attrs.item(i)).getName(); 137 } 138 139 /** 140 * Get the attribute's node type by index 141 * 142 * 143 * @param i The attribute index (zero-based) 144 * 145 * @return the attribute's node type 146 */ 147 public String getType(int i) 148 { 149 return "CDATA"; // for the moment 150 } 151 152 /** 153 * Get the attribute's node value by index 154 * 155 * 156 * @param i The attribute index (zero-based) 157 * 158 * @return the attribute's node value 159 */ 160 public String getValue(int i) 161 { 162 return ((Attr) m_attrs.item(i)).getValue(); 163 } 164 165 /** 166 * Get the attribute's node type by name 167 * 168 * 169 * @param name Attribute name 170 * 171 * @return the attribute's node type 172 */ 173 public String getType(String name) 174 { 175 return "CDATA"; // for the moment 176 } 177 178 /** 179 * Look up an attribute's type by Namespace name. 180 * 181 * @param uri The Namespace URI, or the empty String if the 182 * name has no Namespace URI. 183 * @param localName The local name of the attribute. 184 * @return The attribute type as a string, or null if the 185 * attribute is not in the list or if Namespace 186 * processing is not being performed. 187 */ 188 public String getType(String uri, String localName) 189 { 190 return "CDATA"; // for the moment 191 } 192 193 /** 194 * Look up an attribute's value by name. 195 * 196 * 197 * @param name The attribute node's name 198 * 199 * @return The attribute node's value 200 */ 201 public String getValue(String name) 202 { 203 Attr attr = ((Attr) m_attrs.getNamedItem(name)); 204 return (null != attr) 205 ? attr.getValue() : null; 206 } 207 208 /** 209 * Look up an attribute's value by Namespace name. 210 * 211 * @param uri The Namespace URI, or the empty String if the 212 * name has no Namespace URI. 213 * @param localName The local name of the attribute. 214 * @return The attribute value as a string, or null if the 215 * attribute is not in the list. 216 */ 217 public String getValue(String uri, String localName) 218 { 219 Node a=m_attrs.getNamedItemNS(uri,localName); 220 return (a==null) ? null : a.getNodeValue(); 221 } 222 223 /** 224 * Look up the index of an attribute by Namespace name. 225 * 226 * @param uri The Namespace URI, or the empty string if 227 * the name has no Namespace URI. 228 * @param localPart The attribute's local name. 229 * @return The index of the attribute, or -1 if it does not 230 * appear in the list. 231 */ 232 public int getIndex(String uri, String localPart) 233 { 234 for(int i=m_attrs.getLength()-1;i>=0;--i) 235 { 236 Node a=m_attrs.item(i); 237 String u=a.getNamespaceURI(); 238 if( (u==null ? uri==null : u.equals(uri)) 239 && 240 a.getLocalName().equals(localPart) ) 241 return i; 242 } 243 return -1; 244 } 245 246 /** 247 * Look up the index of an attribute by raw XML 1.0 name. 248 * 249 * @param qName The qualified (prefixed) name. 250 * @return The index of the attribute, or -1 if it does not 251 * appear in the list. 252 */ 253 public int getIndex(String qName) 254 { 255 for(int i=m_attrs.getLength()-1;i>=0;--i) 256 { 257 Node a=m_attrs.item(i); 258 if(a.getNodeName().equals(qName) ) 259 return i; 260 } 261 return -1; 262 } 263 } 264 265