1 package com.thaiopensource.validate.mns; 2 3 import org.xml.sax.Attributes ; 4 5 class NamespaceFilteredAttributes implements Attributes { 6 private final String ns; 7 private final boolean keepLocal; 8 private final Attributes attributes; 9 private final int[] indexMap; 10 private final int[] reverseIndexMap; 11 12 public NamespaceFilteredAttributes(String ns, boolean keepLocal, Attributes attributes) { 13 this.ns = ns; 14 this.keepLocal = keepLocal; 15 this.attributes = attributes; 16 int n = 0; 17 for (int i = 0, len = attributes.getLength(); i < len; i++) 18 if (keepAttribute(attributes.getURI(i))) 19 n++; 20 indexMap = new int[n]; 21 reverseIndexMap = new int[attributes.getLength()]; 22 n = 0; 23 for (int i = 0, len = attributes.getLength(); i < len; i++) { 24 if (keepAttribute(attributes.getURI(i))) { 25 reverseIndexMap[i] = n; 26 indexMap[n++] = i; 27 } 28 else 29 reverseIndexMap[i] = -1; 30 } 31 } 32 33 private boolean keepAttribute(String uri) { 34 return uri.equals(ns) || (keepLocal && uri.equals("")); 35 } 36 37 public int getLength() { 38 return indexMap.length; 39 } 40 41 public String getURI(int index) { 42 if (index < 0 || index >= indexMap.length) 43 return null; 44 return attributes.getURI(indexMap[index]); 45 } 46 47 public String getLocalName(int index) { 48 if (index < 0 || index >= indexMap.length) 49 return null; 50 return attributes.getLocalName(indexMap[index]); 51 } 52 53 public String getQName(int index) { 54 if (index < 0 || index >= indexMap.length) 55 return null; 56 return attributes.getQName(indexMap[index]); 57 } 58 59 public String getType(int index) { 60 if (index < 0 || index >= indexMap.length) 61 return null; 62 return attributes.getType(indexMap[index]); 63 } 64 65 public String getValue(int index) { 66 if (index < 0 || index >= indexMap.length) 67 return null; 68 return attributes.getValue(indexMap[index]); 69 } 70 71 public int getIndex(String uri, String localName) { 72 int n = attributes.getIndex(uri, localName); 73 if (n < 0) 74 return n; 75 return reverseIndexMap[n]; 76 } 77 78 public int getIndex(String qName) { 79 int n = attributes.getIndex(qName); 80 if (n < 0) 81 return n; 82 return reverseIndexMap[n]; 83 } 84 85 public String getType(String uri, String localName) { 86 if (keepAttribute(uri)) 87 return attributes.getType(uri, localName); 88 return null; 89 } 90 91 public String getValue(String uri, String localName) { 92 if (keepAttribute(uri)) 93 return attributes.getValue(uri, localName); 94 return null; 95 } 96 97 public String getType(String qName) { 98 int i = getIndex(qName); 99 if (i < 0) 100 return null; 101 return getType(i); 102 } 103 104 public String getValue(String qName) { 105 int i = getIndex(qName); 106 if (i < 0) 107 return null; 108 return getValue(i); 109 } 110 } 111 | Popular Tags |