Debian Codegraph @

mod/convert-to-dtd/src/main/com/thaiopensource/relaxng/output/dtd/NamespaceManager.java

raw
1 package com.thaiopensource.relaxng.output.dtd;
2
3 import com.thaiopensource.relaxng.edit.NameClass;
4 import com.thaiopensource.relaxng.edit.NameNameClass;
5 import com.thaiopensource.xml.util.WellKnownNamespaces;
6
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.Map;
10 import java.util.Set;
11
12 class NamespaceManager {
13 // map namespace URIs to non-empty prefix
14 private final Map<String, String> namespaceUriMap = new HashMap<String, String>();
15 private String defaultNamespaceUri = null;
16 private final Set<String> usedPrefixes = new HashSet<String>();
17 private final Set<String> unassignedNamespaceUris = new HashSet<String>();
18
19 NamespaceManager() {
20 usedPrefixes.add("xml");
21 namespaceUriMap.put(WellKnownNamespaces.XML, "xml");
22 }
23
24 String getPrefixForNamespaceUri(String ns) {
25 return namespaceUriMap.get(ns);
26 }
27
28 String getDefaultNamespaceUri() {
29 return defaultNamespaceUri;
30 }
31
32 void assignPrefixes() {
33 if (defaultNamespaceUri == null)
34 defaultNamespaceUri = "";
35 int n = 0;
36 for (String ns : unassignedNamespaceUris) {
37 for (; ;) {
38 ++n;
39 String prefix = "ns" + Integer.toString(n);
40 if (!usedPrefixes.contains(prefix)) {
41 namespaceUriMap.put(ns, prefix);
42 break;
43 }
44 }
45 }
46 }
47
48 void noteName(NameNameClass nc, boolean defaultable) {
49 String ns = nc.getNamespaceUri();
50 if (ns.equals("") || ns == NameClass.INHERIT_NS) {
51 if (defaultable)
52 defaultNamespaceUri = "";
53 return;
54 }
55 String assignedPrefix = namespaceUriMap.get(ns);
56 if (assignedPrefix != null)
57 return;
58 String prefix = nc.getPrefix();
59 if (prefix == null) {
60 if (defaultNamespaceUri == null && defaultable)
61 defaultNamespaceUri = ns;
62 unassignedNamespaceUris.add(ns);
63 }
64 else {
65 if (usedPrefixes.contains(prefix))
66 unassignedNamespaceUris.add(ns);
67 else {
68 usedPrefixes.add(prefix);
69 namespaceUriMap.put(ns, prefix);
70 unassignedNamespaceUris.remove(ns);
71 }
72 }
73 }
74 }