1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.betwixt.strategy;
20
21 import java.util.HashMap;
22
23 /***
24 * <p>Maps namespace <code>URI</code>'s to prefixes.
25 * </p><p>
26 * When validating xml documents including namespaces,
27 * the issue of prefixes (the short expression before the colon in a universal name)
28 * becomes important.
29 * DTDs are not namespace aware and so a fixed prefixed must be chosen
30 * and used consistently.
31 * This class is used to supply consistent, user specified prefixes.
32 * </p>
33 * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
34 * @version $Revision: 438373 $
35 */
36 public class NamespacePrefixMapper {
37
38 private int count = 0;
39 private HashMap prefixesByUri = new HashMap();
40
41 /***
42 * Gets the prefix to be used with the given namespace URI
43 * @param namespaceUri
44 * @return prefix, not null
45 */
46 public String getPrefix(String namespaceUri) {
47 String prefix = (String) prefixesByUri.get(namespaceUri);
48 if (prefix == null) {
49 prefix = generatePrefix(namespaceUri);
50 setPrefix(namespaceUri, prefix);
51 }
52 return prefix;
53 }
54
55 /***
56 * Sets the prefix to be used for the given namespace URI.
57 * This method does not check for clashes amongst the namespaces.
58 * Possibly it should.
59 * @param namespaceUri
60 * @param prefix
61 */
62 public void setPrefix(String namespaceUri, String prefix) {
63 prefixesByUri.put(namespaceUri, prefix);
64 }
65
66 /***
67 * Generates a prefix for the given namespace Uri.
68 * Used to assign prefixes for unassigned namespaces.
69 * Subclass may wish to override this method to provide more
70 * sophisticated implementations.
71 * @param namespaceUri URI, not null
72 * @return prefix, not null
73 */
74 protected String generatePrefix(String namespaceUri) {
75 String prefix = "bt" + ++count;
76 if (prefixesByUri.values().contains(prefix)) {
77 prefix = generatePrefix(namespaceUri);
78 }
79 return prefix;
80 }
81
82 }