KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > validate > nrl > FilteredAttributes


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