| 1 |
package com.thaiopensource.relaxng.output.xsd; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.edit.AbstractVisitor; |
| 4 |
import com.thaiopensource.relaxng.edit.AttributePattern; |
| 5 |
import com.thaiopensource.relaxng.edit.ChoiceNameClass; |
| 6 |
import com.thaiopensource.relaxng.edit.CompositePattern; |
| 7 |
import com.thaiopensource.relaxng.edit.DefineComponent; |
| 8 |
import com.thaiopensource.relaxng.edit.DivComponent; |
| 9 |
import com.thaiopensource.relaxng.edit.ElementPattern; |
| 10 |
import com.thaiopensource.relaxng.edit.IncludeComponent; |
| 11 |
import com.thaiopensource.relaxng.edit.NameNameClass; |
| 12 |
import com.thaiopensource.relaxng.edit.UnaryPattern; |
| 13 |
import com.thaiopensource.relaxng.edit.ValuePattern; |
| 14 |
import com.thaiopensource.relaxng.edit.NamespaceContext; |
| 15 |
import com.thaiopensource.util.VoidValue; |
| 16 |
import com.thaiopensource.xml.util.Naming; |
| 17 |
import com.thaiopensource.xml.util.WellKnownNamespaces; |
| 18 |
|
| 19 |
import java.util.HashMap; |
| 20 |
import java.util.HashSet; |
| 21 |
import java.util.Map; |
| 22 |
import java.util.Set; |
| 23 |
|
| 24 |
public class PrefixManager implements SourceUriGenerator { |
| 25 |
|
| 26 |
private final Map<String, String> prefixMap = new HashMap<String, String>(); |
| 27 |
private final Set<String> usedPrefixes = new HashSet<String>(); |
| 28 |
/** |
| 29 |
* Set of prefixes that cannot be used for schema namespace. |
| 30 |
*/ |
| 31 |
private final Set<String> reservedPrefixes = new HashSet<String>(); |
| 32 |
private int nextGenIndex = 1; |
| 33 |
static private final String[] xsdPrefixes = { "xs", "xsd" }; |
| 34 |
static private final int MAX_PREFIX_LENGTH = 10; |
| 35 |
|
| 36 |
static class PrefixUsage { |
| 37 |
int count; |
| 38 |
} |
| 39 |
|
| 40 |
class PrefixSelector extends AbstractVisitor { |
| 41 |
private final SchemaInfo si; |
| 42 |
private String inheritedNamespace; |
| 43 |
private final Map<String, Map<String, PrefixUsage>> namespacePrefixUsageMap = new HashMap<String, Map<String, PrefixUsage>>(); |
| 44 |
|
| 45 |
PrefixSelector(SchemaInfo si) { |
| 46 |
this.si = si; |
| 47 |
this.inheritedNamespace = ""; |
| 48 |
si.getGrammar().componentsAccept(this); |
| 49 |
NamespaceContext context = si.getGrammar().getContext(); |
| 50 |
if (context != null) { |
| 51 |
for (String prefix : context.getPrefixes()) { |
| 52 |
if (!prefix.equals("")) |
| 53 |
notePrefix(prefix, resolveNamespace(context.getNamespace(prefix))); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public VoidValue visitElement(ElementPattern p) { |
| 59 |
p.getNameClass().accept(this); |
| 60 |
p.getChild().accept(this); |
| 61 |
return VoidValue.VOID; |
| 62 |
} |
| 63 |
|
| 64 |
public VoidValue visitAttribute(AttributePattern p) { |
| 65 |
return p.getNameClass().accept(this); |
| 66 |
} |
| 67 |
|
| 68 |
public VoidValue visitChoice(ChoiceNameClass nc) { |
| 69 |
nc.childrenAccept(this); |
| 70 |
return VoidValue.VOID; |
| 71 |
} |
| 72 |
|
| 73 |
public VoidValue visitName(NameNameClass nc) { |
| 74 |
notePrefix(nc.getPrefix(), resolveNamespace(nc.getNamespaceUri())); |
| 75 |
return VoidValue.VOID; |
| 76 |
} |
| 77 |
|
| 78 |
public VoidValue visitValue(ValuePattern p) { |
| 79 |
for (Map.Entry<String, String> entry : p.getPrefixMap().entrySet()) { |
| 80 |
String prefix = entry.getKey(); |
| 81 |
if (prefix != null && !prefix.equals("")) { |
| 82 |
String ns = resolveNamespace(entry.getValue()); |
| 83 |
notePrefix(prefix, ns); |
| 84 |
if (!ns.equals(WellKnownNamespaces.XML_SCHEMA)) |
| 85 |
reservedPrefixes.add(prefix); |
| 86 |
} |
| 87 |
} |
| 88 |
return VoidValue.VOID; |
| 89 |
} |
| 90 |
|
| 91 |
private String resolveNamespace(String ns) { |
| 92 |
return ns == NameNameClass.INHERIT_NS ? inheritedNamespace : ns; |
| 93 |
} |
| 94 |
|
| 95 |
private void notePrefix(String prefix, String ns) { |
| 96 |
if (prefix == null || ns == null || ns.equals("")) |
| 97 |
return; |
| 98 |
Map<String, PrefixUsage> prefixUsageMap = namespacePrefixUsageMap.get(ns); |
| 99 |
if (prefixUsageMap == null) { |
| 100 |
prefixUsageMap = new HashMap<String, PrefixUsage>(); |
| 101 |
namespacePrefixUsageMap.put(ns, prefixUsageMap); |
| 102 |
} |
| 103 |
PrefixUsage prefixUsage = prefixUsageMap.get(prefix); |
| 104 |
if (prefixUsage == null) { |
| 105 |
prefixUsage = new PrefixUsage(); |
| 106 |
prefixUsageMap.put(prefix, prefixUsage); |
| 107 |
} |
| 108 |
prefixUsage.count++; |
| 109 |
} |
| 110 |
|
| 111 |
public VoidValue visitComposite(CompositePattern p) { |
| 112 |
p.childrenAccept(this); |
| 113 |
return VoidValue.VOID; |
| 114 |
} |
| 115 |
|
| 116 |
public VoidValue visitUnary(UnaryPattern p) { |
| 117 |
return p.getChild().accept(this); |
| 118 |
} |
| 119 |
|
| 120 |
public VoidValue visitDefine(DefineComponent c) { |
| 121 |
c.getBody().accept(this); |
| 122 |
return VoidValue.VOID; |
| 123 |
} |
| 124 |
|
| 125 |
public VoidValue visitDiv(DivComponent c) { |
| 126 |
c.componentsAccept(this); |
| 127 |
return VoidValue.VOID; |
| 128 |
} |
| 129 |
|
| 130 |
public VoidValue visitInclude(IncludeComponent c) { |
| 131 |
String saveInheritedNamespace = inheritedNamespace; |
| 132 |
inheritedNamespace = c.getNs(); |
| 133 |
si.getSchema(c.getUri()).componentsAccept(this); |
| 134 |
inheritedNamespace = saveInheritedNamespace; |
| 135 |
return VoidValue.VOID; |
| 136 |
} |
| 137 |
|
| 138 |
void assignPrefixes() { |
| 139 |
for (Map.Entry<String, Map<String, PrefixUsage>> entry : namespacePrefixUsageMap.entrySet()) { |
| 140 |
String ns = entry.getKey(); |
| 141 |
if (!ns.equals("") && !ns.equals(WellKnownNamespaces.XML)) { |
| 142 |
Map<String, PrefixUsage> prefixUsageMap = entry.getValue(); |
| 143 |
if (prefixUsageMap != null) { |
| 144 |
Map.Entry<String, PrefixUsage> best = null; |
| 145 |
for (Map.Entry<String, PrefixUsage> tem : prefixUsageMap.entrySet()) { |
| 146 |
if ((best == null |
| 147 |
|| (tem.getValue()).count > (best.getValue()).count) |
| 148 |
&& prefixOk(tem.getKey(), ns)) |
| 149 |
best = tem; |
| 150 |
} |
| 151 |
if (best != null) |
| 152 |
usePrefix(best.getKey(), ns); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
PrefixManager(SchemaInfo si) { |
| 160 |
usePrefix("xml", WellKnownNamespaces.XML); |
| 161 |
new PrefixSelector(si).assignPrefixes(); |
| 162 |
} |
| 163 |
|
| 164 |
String getPrefix(String namespace) { |
| 165 |
String prefix = prefixMap.get(namespace); |
| 166 |
if (prefix == null && namespace.equals(WellKnownNamespaces.XML_SCHEMA)) { |
| 167 |
for (int i = 0; i < xsdPrefixes.length; i++) |
| 168 |
if (tryUsePrefix(xsdPrefixes[i], namespace)) |
| 169 |
return xsdPrefixes[i]; |
| 170 |
} |
| 171 |
if (prefix == null) |
| 172 |
prefix = tryUseUri(namespace); |
| 173 |
if (prefix == null) { |
| 174 |
do { |
| 175 |
prefix = "ns" + Integer.toString(nextGenIndex++); |
| 176 |
} while (!tryUsePrefix(prefix, namespace)); |
| 177 |
} |
| 178 |
return prefix; |
| 179 |
} |
| 180 |
|
| 181 |
private String tryUseUri(String namespace) { |
| 182 |
String segment = chooseSegment(namespace); |
| 183 |
if (segment == null) |
| 184 |
return null; |
| 185 |
if (segment.length() <= MAX_PREFIX_LENGTH && tryUsePrefix(segment, namespace)) |
| 186 |
return segment; |
| 187 |
for (int i = 1; i <= segment.length(); i++) { |
| 188 |
String prefix = segment.substring(0, i); |
| 189 |
if (tryUsePrefix(prefix, namespace)) |
| 190 |
return prefix; |
| 191 |
} |
| 192 |
return null; |
| 193 |
} |
| 194 |
|
| 195 |
private boolean tryUsePrefix(String prefix, String namespace) { |
| 196 |
if (!prefixOk(prefix, namespace)) |
| 197 |
return false; |
| 198 |
usePrefix(prefix, namespace); |
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
private boolean prefixOk(String prefix, String namespace) { |
| 203 |
return (!usedPrefixes.contains(prefix) |
| 204 |
&& !(reservedPrefixes.contains(prefix) && namespace.equals(WellKnownNamespaces.XML_SCHEMA))); |
| 205 |
} |
| 206 |
|
| 207 |
private void usePrefix(String prefix, String namespace) { |
| 208 |
usedPrefixes.add(prefix); |
| 209 |
prefixMap.put(namespace, prefix); |
| 210 |
} |
| 211 |
|
| 212 |
static private String chooseSegment(String ns) { |
| 213 |
int off = ns.indexOf('#'); |
| 214 |
if (off >= 0) { |
| 215 |
String segment = ns.substring(off + 1).toLowerCase(); |
| 216 |
if (Naming.isNcname(segment)) |
| 217 |
return segment; |
| 218 |
} |
| 219 |
else |
| 220 |
off = ns.length(); |
| 221 |
for (;;) { |
| 222 |
int i = ns.lastIndexOf('/', off - 1); |
| 223 |
if (i < 0 || (i > 0 && ns.charAt(i - 1) == '/')) |
| 224 |
break; |
| 225 |
String segment = ns.substring(i + 1, off).toLowerCase(); |
| 226 |
if (segmentOk(segment)) |
| 227 |
return segment; |
| 228 |
off = i; |
| 229 |
} |
| 230 |
off = ns.indexOf(':'); |
| 231 |
if (off >= 0) { |
| 232 |
String segment = ns.substring(off + 1).toLowerCase(); |
| 233 |
if (segmentOk(segment)) |
| 234 |
return segment; |
| 235 |
} |
| 236 |
return null; |
| 237 |
} |
| 238 |
|
| 239 |
private static boolean segmentOk(String segment) { |
| 240 |
return Naming.isNcname(segment) && !segment.equals("ns") && !segment.equals("namespace"); |
| 241 |
} |
| 242 |
|
| 243 |
public String generateSourceUri(String ns) { |
| 244 |
// TODO add method to OutputDirectory to do this properly |
| 245 |
if (ns.equals("")) |
| 246 |
return "local"; |
| 247 |
else |
| 248 |
return "/" + getPrefix(ns); |
| 249 |
} |
| 250 |
} |