1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xchain.framework.sax;
17
18 import java.util.Map;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Set;
22 import java.util.LinkedList;
23
24
25
26
27
28
29
30
31
32
33 public class ReversePrefixMappingContext
34 extends PrefixMappingContext
35 {
36
37 protected Map<String, LinkedList<String>> namespaceMap = new HashMap<String, LinkedList<String>>();
38
39 public ReversePrefixMappingContext()
40 {
41 }
42
43 private void removeMapping( String prefix, String namespaceUri )
44 {
45 LinkedList<String> prefixList = namespaceMap.get(namespaceUri);
46 if( prefixList != null ) {
47 int firstIndex = prefixList.indexOf(prefix);
48 if( firstIndex >= 0 ) {
49 prefixList.remove(firstIndex);
50 if( prefixList.isEmpty() ) {
51 namespaceMap.remove(namespaceUri);
52 }
53 }
54 }
55 }
56
57 private void addMapping( String prefix, String namespaceUri )
58 {
59 LinkedList<String> prefixList = namespaceMap.get(namespaceUri);
60 if( prefixList == null ) {
61 namespaceMap.put(namespaceUri, prefixList = new LinkedList<String>());
62 }
63 prefixList.addFirst(prefix);
64 }
65
66 public void startDocument()
67 {
68 super.startDocument();
69 }
70
71 public void endDocument()
72 {
73 super.endDocument();
74 }
75
76 public void startPrefixMapping( String prefix, String namespaceUri )
77 {
78 super.startPrefixMapping( prefix, namespaceUri );
79 addMapping(prefix, namespaceUri);
80 }
81
82
83
84
85 public void endPrefixMapping( String prefix )
86 {
87
88 String currentNamespaceUri = prefixMap.get(prefix).getFirst();
89
90
91 super.endPrefixMapping( prefix );
92
93
94 removeMapping( prefix, currentNamespaceUri );
95 }
96
97
98
99
100
101
102
103
104 public String lookUpPrefix( String namespaceUri )
105 {
106 LinkedList<String> prefixList = namespaceMap.get(namespaceUri);
107
108 if( prefixList == null ) {
109 return null;
110 }
111 else {
112 return prefixList.getFirst();
113 }
114 }
115 }