KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > helpers > NSStack


1 package org.objectweb.celtix.helpers;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.ListIterator JavaDoc;
6
7 public class NSStack {
8
9     private static final String JavaDoc NS_PREFIX_PREFIX = "ns";
10
11     private final List JavaDoc<List JavaDoc<NSDecl>> stack = new ArrayList JavaDoc<List JavaDoc<NSDecl>>();
12     private List JavaDoc<NSDecl> top;
13     private int size;
14     private int nsPrefixCount = 1;
15
16     public synchronized void push() {
17         top = new ArrayList JavaDoc<NSDecl>();
18         stack.add(top);
19         size++;
20     }
21
22     /**
23      * Leave a scope: this removes any NS declarations that were added
24      * in the last scope. Note that I don't bother to validate that you
25      * don't call popScope too many times; that's your problem.
26      */

27     public synchronized void pop() {
28         stack.remove(--size);
29         top = null;
30         if (size != 0) {
31             top = stack.get(size - 1);
32         }
33     }
34
35     /**
36      * Add a new declaration to the current scope. This is visible within
37      * the current scope as well as from any nested scopes.
38      *
39      * @param prefix the prefix to be used for this namespace
40      * @param URI the namespace name of this namespace.
41      */

42     public synchronized void add(String JavaDoc prefix, String JavaDoc uri) {
43         top.add(new NSDecl(prefix, uri));
44     }
45
46     /**
47      * Add a new declaration to the current scope using a unique prefix
48      * and return the prefix. This is useful when one just wants to add a
49      * decl and doesn't want to have to deal with creating unique prefixes.
50      * If the namespace name is already declared and in scope, then the
51      * previously declared prefix is returned.
52      *
53      * @param URI the namespace name of this namespace
54      * @return the unique prefix created or previously declared
55      * for this namespace
56      */

57     public synchronized String JavaDoc add(String JavaDoc uri) {
58         String JavaDoc uniquePrefix = getPrefix(uri);
59
60         if (uniquePrefix == null) {
61             do {
62                 uniquePrefix = NS_PREFIX_PREFIX + nsPrefixCount++;
63             } while (getURI(uniquePrefix) != null);
64             add(uniquePrefix, uri);
65         }
66         return uniquePrefix;
67     }
68
69     /**
70      * Return the prefix associated with the given namespace name by
71      * looking thru all the namespace declarations that are in scope.
72      *
73      * @param URI the namespace name for whom a declared prefix is desired
74      * @return the prefix or null if namespace name not found
75      */

76     public String JavaDoc getPrefix(String JavaDoc uri) {
77         for (int i = size - 1; i >= 0; i--) {
78             List JavaDoc<NSDecl> scope = stack.get(i);
79             ListIterator JavaDoc<NSDecl> lsIterator = scope.listIterator();
80
81             while (lsIterator.hasNext()) {
82                 NSDecl nsd = lsIterator.next();
83
84                 if (nsd.getUri().equals(uri)) {
85                     return nsd.getPrefix();
86                 }
87             }
88         }
89         return null;
90     }
91    
92     /**
93      * Return the namespace name associated with the given prefix by
94      * looking thru all the namespace declarations that are in scope.
95      *
96      * @param prefix the prefix for whom a declared namespace name is desired
97      * @return the namespace name or null if prefix not found
98      */

99     public String JavaDoc getURI(String JavaDoc prefix) {
100         for (int i = size - 1; i >= 0; i--) {
101             List JavaDoc<NSDecl> scope = stack.get(i);
102             ListIterator JavaDoc<NSDecl> lsIterator = scope.listIterator();
103
104             while (lsIterator.hasNext()) {
105                 NSDecl nsd = lsIterator.next();
106
107                 if (nsd.getPrefix().equals(prefix)) {
108                     return nsd.getUri();
109                 }
110             }
111         }
112         return null;
113     }
114
115 }
116
117
Popular Tags