1 22 23 package org.xquark.util; 24 25 import java.util.Iterator ; 26 import java.util.NoSuchElementException ; 27 28 import org.w3c.dom.Attr ; 29 import org.w3c.dom.NamedNodeMap ; 30 import org.xml.sax.Attributes ; 31 32 35 public class DOM2SAXAttrs implements Attributes 36 { 37 private static final String RCSRevision = "$Revision: 1.1 $"; 38 private static final String RCSName = "$Name: $"; 39 40 private NamedNodeMap atts; 41 private int length = 0; 42 private boolean hasXMLNSAttrs = false; private boolean namespacePrefixes = false; 46 47 public DOM2SAXAttrs() 48 { 49 } 50 public DOM2SAXAttrs(NamedNodeMap atts) 51 { 52 setDOMAttributes(atts); 53 } 54 55 public DOM2SAXAttrs(NamedNodeMap atts, boolean namespacePrefixes) 56 { 57 setNamespacePrefixes(namespacePrefixes); setDOMAttributes(atts); 59 } 60 61 public DOM2SAXAttrs(boolean namespacePrefixes) 62 { 63 setNamespacePrefixes(namespacePrefixes); 64 } 65 66 public void setDOMAttributes(NamedNodeMap atts) 67 { 68 this.atts = atts; 69 if (namespacePrefixes) length = atts.getLength(); 71 else { 73 length = 0; 74 for (int i = 0; i < atts.getLength(); i++) 75 { 76 Attr a = (Attr )atts.item(i); 77 if (isPrefixDef(a)) 78 hasXMLNSAttrs = true; 79 else 80 length++; 81 } 82 } 83 } 84 85 public void setNamespacePrefixes(boolean namespacePrefixes) 86 { 87 this.namespacePrefixes = namespacePrefixes; 88 } 89 90 95 96 public Iterator getPrefixIterator() 97 { 98 if (!hasXMLNSAttrs || namespacePrefixes) return null; 100 else 101 return new Iterator () { 102 int i = 0; int remainingPrefixes = atts.getLength() - length; public boolean hasNext() 105 { 106 return (remainingPrefixes > 0); 107 } 108 public Object next() 109 { 110 if (remainingPrefixes == 0) 111 throw new NoSuchElementException ("No more xmlns attributes"); 112 Attr a; 113 do 114 { 115 a = (Attr )atts.item(i); 116 i++; 117 } 118 while (!isPrefixDef(a)); 119 120 remainingPrefixes--; 121 return a; 122 } 123 public void remove() 124 { 125 throw new UnsupportedOperationException (); 126 } 127 }; 128 } 129 130 private Attr getAttr(int index) 134 { 135 if (namespacePrefixes || !hasXMLNSAttrs) 136 return (Attr )atts.item(index); 137 else 138 { 139 for (int i = 0; i < atts.getLength(); i++) 140 { 141 Attr a = (Attr )atts.item(i); 142 if (!isPrefixDef(a)) 143 { 144 if (index == 0) 145 return a; 146 index--; 147 } 148 } 149 } 150 return null; 151 } 152 153 private int getIndex(Attr attr) 154 { 155 if (attr != null && !isPrefixDef(attr)) 156 { 157 int result = 0; 158 for (int i = 0; i < atts.getLength(); i++) 159 { 160 Attr a = (Attr )atts.item(i); 161 if (!hasXMLNSAttrs || !isPrefixDef(a)) 162 { 163 if (attr == atts.item(i)) 164 return result; 165 result++; 166 } 167 } 168 } 169 return -1; 170 } 171 172 private String getLocalName(Attr attr) 173 { 174 if (attr != null && !isPrefixDef(attr)) 175 return attr.getLocalName() == null ? "" : attr.getLocalName(); 176 else 177 return null; 178 } 179 180 private String getQName(Attr attr) 181 { 182 if (attr != null && !isPrefixDef(attr)) 183 return attr.getName() == null ? "" : attr.getName(); 184 else 185 return null; 186 } 187 188 private String getURI(Attr attr) 189 { 190 if (attr != null && !isPrefixDef(attr)) 191 return attr.getNamespaceURI() == null ? "" : attr.getNamespaceURI(); 192 else 193 return null; 194 } 195 196 private String getValue(Attr attr) 197 { 198 if (attr != null && !isPrefixDef(attr)) 199 return attr.getValue(); 200 else 201 return null; 202 } 203 204 private boolean isPrefixDef(Attr attr) 205 { 206 return (attr.getNamespaceURI() != null) 207 && (attr.getNamespaceURI().equals(SAXConstants.XMLNS_URI)); 208 } 209 210 public int getLength() 214 { 215 return length; 216 } 217 218 public String getLocalName(int index) 219 { 220 return getLocalName(getAttr(index)); 221 } 222 223 public String getQName(int index) 224 { 225 return getQName(getAttr(index)); 226 } 227 228 public String getType(int index) 229 { 230 return "CDATA"; 231 } 232 233 public String getURI(int index) 234 { 235 return getURI(getAttr(index)); 236 } 237 238 public String getValue(int index) { 239 return getValue(getAttr(index)); 240 } 241 242 public int getIndex(String uri, String localPart) 243 { 244 if ((uri == null) || (uri.length() == 0)) 245 return getIndex((Attr )atts.getNamedItem(localPart)); 246 else 247 return getIndex((Attr )atts.getNamedItemNS(uri, localPart)); 248 } 249 250 public String getType(String uri, String localPart) 251 { 252 return "CDATA"; 253 } 254 255 public String getValue(String uri, String localPart) 256 { 257 if ((uri == null) || (uri.length() == 0)) 258 return getValue((Attr )atts.getNamedItem(localPart)); 259 else 260 return getValue((Attr )atts.getNamedItemNS(uri, localPart)); 261 } 262 263 public int getIndex(String qName) 264 { 265 return getIndex((Attr )atts.getNamedItem(qName)); 266 } 267 268 public String getType(String qName) 269 { 270 return "CDATA"; 271 } 272 273 public String getValue(String qName) 274 { 275 return getValue((Attr )atts.getNamedItem(qName)); 276 } 277 278 } 279 280 | Popular Tags |