KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > validate > mns > NamespaceFilteredAttributes


1 package com.thaiopensource.validate.mns;
2
3 import org.xml.sax.Attributes JavaDoc;
4
5 class NamespaceFilteredAttributes implements Attributes JavaDoc {
6   private final String JavaDoc ns;
7   private final boolean keepLocal;
8   private final Attributes JavaDoc attributes;
9   private final int[] indexMap;
10   private final int[] reverseIndexMap;
11
12   public NamespaceFilteredAttributes(String JavaDoc ns, boolean keepLocal, Attributes JavaDoc 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 JavaDoc uri) {
34     return uri.equals(ns) || (keepLocal && uri.equals(""));
35   }
36
37   public int getLength() {
38     return indexMap.length;
39   }
40
41   public String JavaDoc getURI(int index) {
42     if (index < 0 || index >= indexMap.length)
43       return null;
44     return attributes.getURI(indexMap[index]);
45   }
46
47   public String JavaDoc getLocalName(int index) {
48     if (index < 0 || index >= indexMap.length)
49       return null;
50     return attributes.getLocalName(indexMap[index]);
51   }
52
53   public String JavaDoc getQName(int index) {
54     if (index < 0 || index >= indexMap.length)
55       return null;
56     return attributes.getQName(indexMap[index]);
57   }
58
59   public String JavaDoc getType(int index) {
60     if (index < 0 || index >= indexMap.length)
61       return null;
62     return attributes.getType(indexMap[index]);
63   }
64
65   public String JavaDoc 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 JavaDoc uri, String JavaDoc 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 JavaDoc qName) {
79     int n = attributes.getIndex(qName);
80     if (n < 0)
81       return n;
82     return reverseIndexMap[n];
83   }
84
85   public String JavaDoc getType(String JavaDoc uri, String JavaDoc localName) {
86     if (keepAttribute(uri))
87       return attributes.getType(uri, localName);
88     return null;
89   }
90
91   public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localName) {
92     if (keepAttribute(uri))
93       return attributes.getValue(uri, localName);
94     return null;
95   }
96
97   public String JavaDoc getType(String JavaDoc qName) {
98     int i = getIndex(qName);
99     if (i < 0)
100       return null;
101     return getType(i);
102   }
103
104   public String JavaDoc getValue(String JavaDoc qName) {
105     int i = getIndex(qName);
106     if (i < 0)
107       return null;
108     return getValue(i);
109   }
110 }
111
Popular Tags