| 1 |
package com.thaiopensource.relaxng.output.xsd; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.output.xsd.basic.Attribute; |
| 4 |
import com.thaiopensource.relaxng.output.xsd.basic.Definition; |
| 5 |
import com.thaiopensource.relaxng.output.xsd.basic.Element; |
| 6 |
import com.thaiopensource.relaxng.output.xsd.basic.GroupDefinition; |
| 7 |
import com.thaiopensource.relaxng.output.xsd.basic.GroupRef; |
| 8 |
import com.thaiopensource.relaxng.output.xsd.basic.Include; |
| 9 |
import com.thaiopensource.relaxng.output.xsd.basic.Particle; |
| 10 |
import com.thaiopensource.relaxng.output.xsd.basic.ParticleChoice; |
| 11 |
import com.thaiopensource.relaxng.output.xsd.basic.Schema; |
| 12 |
import com.thaiopensource.relaxng.output.xsd.basic.SchemaWalker; |
| 13 |
import com.thaiopensource.relaxng.output.xsd.basic.Structure; |
| 14 |
import com.thaiopensource.relaxng.output.xsd.basic.Wildcard; |
| 15 |
import com.thaiopensource.relaxng.output.xsd.basic.WildcardAttribute; |
| 16 |
import com.thaiopensource.relaxng.output.xsd.basic.WildcardElement; |
| 17 |
import com.thaiopensource.util.VoidValue; |
| 18 |
import com.thaiopensource.xml.util.Name; |
| 19 |
|
| 20 |
import java.util.HashMap; |
| 21 |
import java.util.HashSet; |
| 22 |
import java.util.Iterator; |
| 23 |
import java.util.List; |
| 24 |
import java.util.Map; |
| 25 |
import java.util.Set; |
| 26 |
import java.util.Vector; |
| 27 |
|
| 28 |
public class NamespaceManager { |
| 29 |
private final Schema schema; |
| 30 |
private final Map<Name, NameInfo> elementNameMap = new HashMap<Name, NameInfo>(); |
| 31 |
private final Map<Name, NameInfo> attributeNameMap = new HashMap<Name, NameInfo>(); |
| 32 |
private final Map<Object, Name> substitutionGroupMap = new HashMap<Object, Name>(); |
| 33 |
private final Map<String, Name> groupDefinitionAbstractElementMap = new HashMap<String, Name>(); |
| 34 |
private final Map<Name, List<Name>> abstractElementSubstitutionGroupMemberMap = new HashMap<Name, List<Name>>(); |
| 35 |
|
| 36 |
static class SourceUri { |
| 37 |
String targetNamespace; |
| 38 |
// list of strings giving included URIs |
| 39 |
final List<String> includes = new Vector<String>(); |
| 40 |
} |
| 41 |
|
| 42 |
static class TargetNamespace { |
| 43 |
String rootSchema; |
| 44 |
final List<Structure> movedStructures = new Vector<Structure>(); |
| 45 |
final Set<Structure> movedStructureSet = new HashSet<Structure>(); |
| 46 |
final Map<Structure, String> movedStructureNameMap = new HashMap<Structure, String>(); |
| 47 |
final Set<String> movedElementNameSet = new HashSet<String>(); |
| 48 |
final Set<String> movedAttributeNameSet = new HashSet<String>(); |
| 49 |
boolean movedOtherElement = false; |
| 50 |
boolean movedOtherAttribute = false; |
| 51 |
String otherElementName; |
| 52 |
String otherAttributeName; |
| 53 |
} |
| 54 |
|
| 55 |
static class NameInfo { |
| 56 |
static final int OCCUR_NONE = 0; |
| 57 |
static final int OCCUR_NESTED = 1; |
| 58 |
static final int OCCUR_TOP = 2; |
| 59 |
static final int OCCUR_MOVE = 3; |
| 60 |
static final int OCCUR_ROOT = 4; |
| 61 |
int occur = OCCUR_NONE; |
| 62 |
Structure globalType = null; |
| 63 |
} |
| 64 |
|
| 65 |
// Maps sourceUri to SourceUri |
| 66 |
private final Map<String, SourceUri> sourceUriMap = new HashMap<String, SourceUri>(); |
| 67 |
// Maps targetNamespace to TargetNamespace |
| 68 |
private final Map<String, TargetNamespace> targetNamespaceMap = new HashMap<String,TargetNamespace>(); |
| 69 |
|
| 70 |
class IncludeFinder extends SchemaWalker { |
| 71 |
private final SourceUri source; |
| 72 |
IncludeFinder(Schema schema) { |
| 73 |
source = lookupSourceUri(schema.getUri()); |
| 74 |
schema.accept(this); |
| 75 |
} |
| 76 |
|
| 77 |
public void visitInclude(Include include) { |
| 78 |
Schema included = include.getIncludedSchema(); |
| 79 |
source.includes.add(included.getUri()); |
| 80 |
new IncludeFinder(included); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
class RootMarker extends SchemaWalker { |
| 85 |
public void visitGroup(GroupDefinition def) { |
| 86 |
} |
| 87 |
|
| 88 |
public VoidValue visitElement(Element p) { |
| 89 |
NameInfo info = lookupElementName(p.getName()); |
| 90 |
info.globalType = p; |
| 91 |
info.occur = NameInfo.OCCUR_ROOT; |
| 92 |
lookupTargetNamespace(p.getName().getNamespaceUri()); |
| 93 |
return VoidValue.VOID; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
static class NamespaceUsage { |
| 98 |
int elementCount; |
| 99 |
int attributeCount; |
| 100 |
static boolean isBetter(NamespaceUsage n1, NamespaceUsage n2) { |
| 101 |
return (n1.elementCount > n2.elementCount |
| 102 |
|| (n1.elementCount == n2.elementCount |
| 103 |
&& n1.attributeCount > n2.attributeCount)); |
| 104 |
} |
| 105 |
public boolean equals(Object obj) { |
| 106 |
if (!(obj instanceof NamespaceUsage)) |
| 107 |
return false; |
| 108 |
NamespaceUsage other = (NamespaceUsage)obj; |
| 109 |
return (elementCount == other.elementCount |
| 110 |
&& attributeCount == other.attributeCount); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
class TargetNamespaceSelector extends SchemaWalker { |
| 115 |
private boolean nested; |
| 116 |
private final Map<String, NamespaceUsage> namespaceUsageMap = new HashMap<String, NamespaceUsage>(); |
| 117 |
|
| 118 |
TargetNamespaceSelector(Schema schema) { |
| 119 |
schema.accept(this); |
| 120 |
lookupSourceUri(schema.getUri()).targetNamespace = selectTargetNamespace(); |
| 121 |
} |
| 122 |
|
| 123 |
public VoidValue visitElement(Element element) { |
| 124 |
NamespaceUsage usage = getUsage(element.getName().getNamespaceUri()); |
| 125 |
if (!nested) |
| 126 |
usage.elementCount++; |
| 127 |
boolean saveNested = nested; |
| 128 |
nested = true; |
| 129 |
element.getComplexType().accept(this); |
| 130 |
nested = saveNested; |
| 131 |
return VoidValue.VOID; |
| 132 |
} |
| 133 |
|
| 134 |
public VoidValue visitAttribute(Attribute a) { |
| 135 |
NamespaceUsage usage = getUsage(a.getName().getNamespaceUri()); |
| 136 |
if (!nested) |
| 137 |
usage.attributeCount++; |
| 138 |
return VoidValue.VOID; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
public VoidValue visitWildcardElement(WildcardElement p) { |
| 143 |
return visitWildcard(p.getWildcard()); |
| 144 |
} |
| 145 |
|
| 146 |
public VoidValue visitWildcardAttribute(WildcardAttribute a) { |
| 147 |
return visitWildcard(a.getWildcard()); |
| 148 |
} |
| 149 |
|
| 150 |
private VoidValue visitWildcard(Wildcard wc) { |
| 151 |
String ns = otherNamespace(wc); |
| 152 |
if (ns != null) { |
| 153 |
lookupTargetNamespace(ns); |
| 154 |
if (!nested) |
| 155 |
getUsage(ns).attributeCount++; |
| 156 |
} |
| 157 |
return VoidValue.VOID; |
| 158 |
} |
| 159 |
|
| 160 |
private NamespaceUsage getUsage(String ns) { |
| 161 |
NamespaceUsage usage = namespaceUsageMap.get(ns); |
| 162 |
if (usage == null) { |
| 163 |
usage = new NamespaceUsage(); |
| 164 |
namespaceUsageMap.put(ns, usage); |
| 165 |
if (!ns.equals("")) |
| 166 |
lookupTargetNamespace(ns); |
| 167 |
} |
| 168 |
return usage; |
| 169 |
} |
| 170 |
|
| 171 |
public void visitInclude(Include include) { |
| 172 |
new TargetNamespaceSelector(include.getIncludedSchema()); |
| 173 |
} |
| 174 |
|
| 175 |
String selectTargetNamespace() { |
| 176 |
Map.Entry<String,NamespaceUsage> best = null; |
| 177 |
for (Map.Entry<String, NamespaceUsage> tem : namespaceUsageMap.entrySet()) { |
| 178 |
if (best == null |
| 179 |
|| NamespaceUsage.isBetter(tem.getValue(), |
| 180 |
best.getValue()) |
| 181 |
// avoid output depending on order of hash table iteration |
| 182 |
|| (tem.getValue().equals(best.getValue()) |
| 183 |
&& (tem.getKey()).compareTo(best.getKey()) < 0)) |
| 184 |
best = tem; |
| 185 |
} |
| 186 |
namespaceUsageMap.clear(); |
| 187 |
if (best == null) |
| 188 |
return null; |
| 189 |
String targetNamespace = best.getKey(); |
| 190 |
// for "" case |
| 191 |
lookupTargetNamespace(targetNamespace); |
| 192 |
return targetNamespace; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
class GlobalElementSelector extends SchemaWalker { |
| 197 |
private final boolean absentTargetNamespace; |
| 198 |
private boolean nested = false; |
| 199 |
|
| 200 |
GlobalElementSelector(Schema schema) { |
| 201 |
absentTargetNamespace = getTargetNamespace(schema.getUri()).equals(""); |
| 202 |
schema.accept(this); |
| 203 |
} |
| 204 |
|
| 205 |
public VoidValue visitElement(Element element) { |
| 206 |
Name name = element.getName(); |
| 207 |
if (!name.getNamespaceUri().equals("") || absentTargetNamespace) { |
| 208 |
NameInfo info = lookupElementName(name); |
| 209 |
int occur = nested ? NameInfo.OCCUR_NESTED : NameInfo.OCCUR_TOP; |
| 210 |
if (occur > info.occur) { |
| 211 |
info.occur = occur; |
| 212 |
info.globalType = element; |
| 213 |
} |
| 214 |
else if (occur == info.occur && !element.equals(info.globalType)) |
| 215 |
info.globalType = null; |
| 216 |
} |
| 217 |
boolean saveNested = nested; |
| 218 |
nested = true; |
| 219 |
element.getComplexType().accept(this); |
| 220 |
nested = saveNested; |
| 221 |
return VoidValue.VOID; |
| 222 |
} |
| 223 |
|
| 224 |
public void visitInclude(Include include) { |
| 225 |
new GlobalElementSelector(include.getIncludedSchema()); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
class StructureMover extends SchemaWalker { |
| 230 |
private final String currentNamespace; |
| 231 |
|
| 232 |
StructureMover(String currentNamespace) { |
| 233 |
this.currentNamespace = currentNamespace; |
| 234 |
} |
| 235 |
|
| 236 |
public VoidValue visitElement(Element p) { |
| 237 |
NameInfo info = lookupElementName(p.getName()); |
| 238 |
String ns = p.getName().getNamespaceUri(); |
| 239 |
if (ns.equals(currentNamespace) || (ns.equals("") && !p.equals(info.globalType))) |
| 240 |
p.getComplexType().accept(this); |
| 241 |
else { |
| 242 |
noteMoved(info, p); |
| 243 |
moveStructure(p); |
| 244 |
p.getComplexType().accept(new StructureMover(ns)); |
| 245 |
} |
| 246 |
return VoidValue.VOID; |
| 247 |
} |
| 248 |
|
| 249 |
public VoidValue visitAttribute(Attribute a) { |
| 250 |
String ns = a.getName().getNamespaceUri(); |
| 251 |
if (!ns.equals("") && !ns.equals(currentNamespace)) { |
| 252 |
noteMoved(lookupAttributeName(a.getName()), a); |
| 253 |
moveStructure(a); |
| 254 |
} |
| 255 |
return VoidValue.VOID; |
| 256 |
} |
| 257 |
|
| 258 |
private void noteMoved(NameInfo info, Structure s) { |
| 259 |
if (info.occur < NameInfo.OCCUR_MOVE) { |
| 260 |
info.occur = NameInfo.OCCUR_MOVE; |
| 261 |
info.globalType = s; |
| 262 |
} |
| 263 |
else if (info.occur == NameInfo.OCCUR_MOVE && !s.equals(info.globalType)) |
| 264 |
info.globalType = null; |
| 265 |
} |
| 266 |
|
| 267 |
private void moveStructure(Structure p) { |
| 268 |
TargetNamespace tn = lookupTargetNamespace(p.getName().getNamespaceUri()); |
| 269 |
if (!tn.movedStructureSet.contains(p)) { |
| 270 |
tn.movedStructureSet.add(p); |
| 271 |
tn.movedStructures.add(p); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
public void visitInclude(Include include) { |
| 276 |
Schema included = include.getIncludedSchema(); |
| 277 |
included.accept(new StructureMover(getTargetNamespace(included.getUri()))); |
| 278 |
} |
| 279 |
|
| 280 |
public VoidValue visitWildcardElement(WildcardElement p) { |
| 281 |
return visitWildcard(p.getWildcard(), true); |
| 282 |
} |
| 283 |
|
| 284 |
public VoidValue visitWildcardAttribute(WildcardAttribute a) { |
| 285 |
return visitWildcard(a.getWildcard(), false); |
| 286 |
} |
| 287 |
|
| 288 |
private VoidValue visitWildcard(Wildcard wc, boolean isElement) { |
| 289 |
String ns = otherNamespace(wc); |
| 290 |
if (ns != null && !ns.equals(currentNamespace)) { |
| 291 |
TargetNamespace tn = lookupTargetNamespace(ns); |
| 292 |
if (isElement) |
| 293 |
tn.movedOtherElement = true; |
| 294 |
else |
| 295 |
tn.movedOtherAttribute = true; |
| 296 |
} |
| 297 |
return VoidValue.VOID; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
NamespaceManager(Schema schema, Guide guide, SourceUriGenerator sug) { |
| 302 |
this.schema = schema; |
| 303 |
new IncludeFinder(schema); |
| 304 |
schema.accept(new RootMarker()); |
| 305 |
assignTargetNamespaces(); |
| 306 |
new GlobalElementSelector(schema); |
| 307 |
findSubstitutionGroups(guide); |
| 308 |
chooseRootSchemas(sug); |
| 309 |
schema.accept(new StructureMover(getTargetNamespace(schema.getUri()))); |
| 310 |
} |
| 311 |
|
| 312 |
private void assignTargetNamespaces() { |
| 313 |
new TargetNamespaceSelector(schema); |
| 314 |
// TODO maybe use info from <start> to select which targetNamespace of included schemas to use |
| 315 |
String ns = filterUpTargetNamespace(schema.getUri()); |
| 316 |
if (ns == null) { |
| 317 |
lookupTargetNamespace(""); |
| 318 |
lookupSourceUri(schema.getUri()).targetNamespace = ""; |
| 319 |
ns = ""; |
| 320 |
} |
| 321 |
inheritDownTargetNamespace(schema.getUri(), ns); |
| 322 |
} |
| 323 |
|
| 324 |
private String filterUpTargetNamespace(String sourceUri) { |
| 325 |
String ns = getTargetNamespace(sourceUri); |
| 326 |
if (ns != null) |
| 327 |
return ns; |
| 328 |
List<String> includes = lookupSourceUri(sourceUri).includes; |
| 329 |
if (includes.size() == 0) |
| 330 |
return null; |
| 331 |
Map<String, Integer> occurMap = new HashMap<String, Integer>(); |
| 332 |
for (String include : includes) { |
| 333 |
String tem = filterUpTargetNamespace(include); |
| 334 |
if (tem != null) { |
| 335 |
Integer count = occurMap.get(tem); |
| 336 |
occurMap.put(tem, count == null ? 1 : count + 1); |
| 337 |
} |
| 338 |
} |
| 339 |
Map.Entry<String,Integer> best = null; |
| 340 |
boolean bestAmbig = false; |
| 341 |
for (Map.Entry<String, Integer> tem : occurMap.entrySet()) { |
| 342 |
if (best == null || tem.getValue() > best.getValue()) { |
| 343 |
best = tem; |
| 344 |
bestAmbig = false; |
| 345 |
} |
| 346 |
else if ((tem.getValue()).intValue() == (best.getValue()).intValue()) |
| 347 |
bestAmbig = true; |
| 348 |
} |
| 349 |
if (best == null || bestAmbig) |
| 350 |
return null; |
| 351 |
ns = best.getKey(); |
| 352 |
lookupSourceUri(sourceUri).targetNamespace = ns; |
| 353 |
return ns; |
| 354 |
} |
| 355 |
|
| 356 |
private void inheritDownTargetNamespace(String sourceUri, String targetNamespace) { |
| 357 |
for (String uri : lookupSourceUri(sourceUri).includes) { |
| 358 |
String ns = lookupSourceUri(uri).targetNamespace; |
| 359 |
if (ns == null) { |
| 360 |
ns = targetNamespace; |
| 361 |
lookupSourceUri(uri).targetNamespace = ns; |
| 362 |
} |
| 363 |
inheritDownTargetNamespace(uri, ns); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
private void chooseRootSchemas(SourceUriGenerator sug) { |
| 368 |
for (Map.Entry<String, TargetNamespace> entry : targetNamespaceMap.entrySet()) { |
| 369 |
String ns = entry.getKey(); |
| 370 |
List<String> list = new Vector<String>(); |
| 371 |
findRootSchemas(schema.getUri(), ns, list); |
| 372 |
if (list.size() == 1) |
| 373 |
(entry.getValue()).rootSchema = list.get(0); |
| 374 |
else { |
| 375 |
String sourceUri = sug.generateSourceUri(ns); |
| 376 |
lookupSourceUri(sourceUri).includes.addAll(list); |
| 377 |
lookupSourceUri(sourceUri).targetNamespace = ns; |
| 378 |
(entry.getValue()).rootSchema = sourceUri; |
| 379 |
schema.addInclude(sourceUri, schema.getEncoding(), null, null); |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
boolean isGlobal(Element element) { |
| 385 |
return element.equals(lookupElementName(element.getName()).globalType); |
| 386 |
} |
| 387 |
|
| 388 |
Element getGlobalElement(Name name) { |
| 389 |
NameInfo info = elementNameMap.get(name); |
| 390 |
if (info == null) |
| 391 |
return null; |
| 392 |
return (Element)info.globalType; |
| 393 |
} |
| 394 |
|
| 395 |
boolean isGlobal(Attribute attribute) { |
| 396 |
return attribute.equals(lookupAttributeName(attribute.getName()).globalType); |
| 397 |
} |
| 398 |
|
| 399 |
String getProxyName(Structure struct) { |
| 400 |
String ns = struct.getName().getNamespaceUri(); |
| 401 |
TargetNamespace tn = lookupTargetNamespace(ns); |
| 402 |
String name = tn.movedStructureNameMap.get(struct); |
| 403 |
if (name == null) { |
| 404 |
name = generateName(ns, tn, struct.getName().getLocalName(), struct instanceof Element); |
| 405 |
tn.movedStructureNameMap.put(struct, name); |
| 406 |
} |
| 407 |
return name; |
| 408 |
} |
| 409 |
|
| 410 |
String getOtherElementName(String ns) { |
| 411 |
TargetNamespace tn = lookupTargetNamespace(ns); |
| 412 |
if (!tn.movedOtherElement) |
| 413 |
return null; |
| 414 |
if (tn.otherElementName == null) |
| 415 |
tn.otherElementName = generateName(ns, tn, "local", true); |
| 416 |
return tn.otherElementName; |
| 417 |
} |
| 418 |
|
| 419 |
String getOtherAttributeName(String ns) { |
| 420 |
TargetNamespace tn = lookupTargetNamespace(ns); |
| 421 |
if (!tn.movedOtherAttribute) |
| 422 |
return null; |
| 423 |
if (tn.otherAttributeName == null) |
| 424 |
tn.otherAttributeName = generateName(ns, tn, "local", false); |
| 425 |
return tn.otherAttributeName; |
| 426 |
} |
| 427 |
|
| 428 |
private String generateName(String ns, TargetNamespace tn, String base, boolean isElement) { |
| 429 |
Set<String> movedStructureNameSet = isElement ? tn.movedElementNameSet : tn.movedAttributeNameSet; |
| 430 |
String name = base; |
| 431 |
for (int n = 1;; n++) { |
| 432 |
if (!movedStructureNameSet.contains(name)) { |
| 433 |
Definition def; |
| 434 |
if (isElement) |
| 435 |
def = schema.getGroup(name); |
| 436 |
else |
| 437 |
def = schema.getAttributeGroup(name); |
| 438 |
if (def == null |
| 439 |
|| !getTargetNamespace(def.getParentSchema().getUri()).equals(ns) |
| 440 |
|| (def instanceof GroupDefinition |
| 441 |
&& getElementNameForGroupRef((GroupDefinition)def) != null)) |
| 442 |
break; |
| 443 |
} |
| 444 |
name = base + Integer.toString(n); |
| 445 |
} |
| 446 |
movedStructureNameSet.add(name); |
| 447 |
return name; |
| 448 |
} |
| 449 |
|
| 450 |
static class GroupDefinitionFinder extends SchemaWalker { |
| 451 |
final List<GroupDefinition> list = new Vector<GroupDefinition>(); |
| 452 |
|
| 453 |
public void visitGroup(GroupDefinition def) { |
| 454 |
list.add(def); |
| 455 |
} |
| 456 |
|
| 457 |
static List<GroupDefinition> findGroupDefinitions(Schema schema) { |
| 458 |
GroupDefinitionFinder gdf = new GroupDefinitionFinder(); |
| 459 |
schema.accept(gdf); |
| 460 |
return gdf.list; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
private void findSubstitutionGroups(Guide guide) { |
| 465 |
List<GroupDefinition> groups = GroupDefinitionFinder.findGroupDefinitions(schema); |
| 466 |
Map<Name, String> elementNameToGroupName = new HashMap<Name, String>(); |
| 467 |
while (addAbstractElements(guide, groups, elementNameToGroupName)) |
| 468 |
; |
| 469 |
cleanSubstitutionGroupMap(elementNameToGroupName); |
| 470 |
cleanAbstractElementSubstitutionGroupMemberMap(elementNameToGroupName); |
| 471 |
} |
| 472 |
|
| 473 |
private boolean addAbstractElements(Guide guide, List<GroupDefinition> groups, Map<Name, String> elementNameToGroupName) { |
| 474 |
Set<Name> newAbstractElements = new HashSet<Name>(); |
| 475 |
for (GroupDefinition def : groups) { |
| 476 |
if (guide.getGroupEnableAbstractElement(def.getName()) |
| 477 |
&& getGroupDefinitionAbstractElementName(def) == null) { |
| 478 |
Name elementName = abstractElementName(def); |
| 479 |
if (elementName != null) { |
| 480 |
List<Name> members = substitutionGroupMembers(def); |
| 481 |
if (members != null) { |
| 482 |
elementNameToGroupName.put(elementName, def.getName()); |
| 483 |
addSubstitutionGroup(elementName, members, newAbstractElements); |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
if (newAbstractElements.size() == 0) |
| 489 |
return false; |
| 490 |
for (Name name : newAbstractElements) { |
| 491 |
groupDefinitionAbstractElementMap.put(elementNameToGroupName.get(name), name); |
| 492 |
} |
| 493 |
return true; |
| 494 |
} |
| 495 |
|
| 496 |
private void addSubstitutionGroup(Name elementName, List<Name> members, Set<Name> newAbstractElements) { |
| 497 |
for (Name member : members) { |
| 498 |
Name old = getSubstitutionGroup(member); |
| 499 |
if (old != null && !old.equals(elementName)) { |
| 500 |
newAbstractElements.remove(old); |
| 501 |
return; |
| 502 |
} |
| 503 |
substitutionGroupMap.put(member, elementName); |
| 504 |
} |
| 505 |
newAbstractElements.add(elementName); |
| 506 |
abstractElementSubstitutionGroupMemberMap.put(elementName, members); |
| 507 |
} |
| 508 |
|
| 509 |
private void cleanSubstitutionGroupMap(Map<Name, String> elementNameToGroupName) { |
| 510 |
for (Iterator<Map.Entry<Object,Name>> iter = substitutionGroupMap.entrySet().iterator(); iter.hasNext();) { |
| 511 |
Map.Entry<Object,Name> entry = iter.next(); |
| 512 |
Name head = entry.getValue(); |
| 513 |
if (groupDefinitionAbstractElementMap.get(elementNameToGroupName.get(head)) == null) |
| 514 |
iter.remove(); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
private void cleanAbstractElementSubstitutionGroupMemberMap(Map<Name, String> elementNameToGroupName) { |
| 519 |
for (Iterator<Name> iter = abstractElementSubstitutionGroupMemberMap.keySet().iterator(); iter.hasNext();) { |
| 520 |
if (groupDefinitionAbstractElementMap.get(elementNameToGroupName.get(iter.next())) == null) |
| 521 |
iter.remove(); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
private Name abstractElementName(GroupDefinition def) { |
| 526 |
Name name = new Name(getTargetNamespace(def.getParentSchema().getUri()), def.getName()); |
| 527 |
if (lookupElementName(name).globalType != null) |
| 528 |
return null; |
| 529 |
return name; |
| 530 |
} |
| 531 |
|
| 532 |
private List<Name> substitutionGroupMembers(GroupDefinition def) { |
| 533 |
if (def.getParticle() instanceof Element) |
| 534 |
return null; |
| 535 |
List<Name> members = new Vector<Name>(); |
| 536 |
if (!particleMembers(def.getParticle(), members)) |
| 537 |
return null; |
| 538 |
return members; |
| 539 |
} |
| 540 |
|
| 541 |
private boolean particleMembers(Particle child, List<Name> members) { |
| 542 |
if (child instanceof Element) { |
| 543 |
Element e = (Element)child; |
| 544 |
if (!isGlobal(e)) |
| 545 |
return false; |
| 546 |
members.add(e.getName()); |
| 547 |
} |
| 548 |
else if (child instanceof GroupRef) { |
| 549 |
Name name = getElementNameForGroupRef(schema.getGroup(((GroupRef)child).getName())); |
| 550 |
if (name == null) |
| 551 |
return false; |
| 552 |
members.add(name); |
| 553 |
} |
| 554 |
else if (child instanceof ParticleChoice) { |
| 555 |
for (Particle particle : ((ParticleChoice)child).getChildren()) { |
| 556 |
if (!particleMembers(particle, members)) |
| 557 |
return false; |
| 558 |
} |
| 559 |
} |
| 560 |
else |
| 561 |
return false; |
| 562 |
return true; |
| 563 |
} |
| 564 |
|
| 565 |
Name getElementNameForGroupRef(GroupDefinition def) { |
| 566 |
Name abstractElementName = getGroupDefinitionAbstractElementName(def); |
| 567 |
if (abstractElementName != null) |
| 568 |
return abstractElementName; |
| 569 |
return getGroupDefinitionSingleElementName(def); |
| 570 |
} |
| 571 |
|
| 572 |
boolean isGroupDefinitionOmitted(GroupDefinition def) { |
| 573 |
return getGroupDefinitionSingleElementName(def) != null; |
| 574 |
} |
| 575 |
|
| 576 |
Name getGroupDefinitionAbstractElementName(GroupDefinition def) { |
| 577 |
return groupDefinitionAbstractElementMap.get(def.getName()); |
| 578 |
} |
| 579 |
|
| 580 |
List<Name> getAbstractElementSubstitutionGroupMembers(Name name) { |
| 581 |
return abstractElementSubstitutionGroupMemberMap.get(name); |
| 582 |
} |
| 583 |
|
| 584 |
private Name getGroupDefinitionSingleElementName(GroupDefinition def) { |
| 585 |
Particle particle = def.getParticle(); |
| 586 |
if (!(particle instanceof Element) || !isGlobal((Element)particle)) |
| 587 |
return null; |
| 588 |
return ((Element)particle).getName(); |
| 589 |
} |
| 590 |
|
| 591 |
Name getSubstitutionGroup(Name name) { |
| 592 |
return substitutionGroupMap.get(name); |
| 593 |
} |
| 594 |
|
| 595 |
String getTargetNamespace(String schemaUri) { |
| 596 |
return lookupSourceUri(schemaUri).targetNamespace; |
| 597 |
} |
| 598 |
|
| 599 |
boolean isTargetNamespace(String ns) { |
| 600 |
return targetNamespaceMap.get(ns) != null; |
| 601 |
} |
| 602 |
|
| 603 |
Set<String> getTargetNamespaces() { |
| 604 |
return targetNamespaceMap.keySet(); |
| 605 |
} |
| 606 |
|
| 607 |
String getRootSchema(String targetNamespace) { |
| 608 |
return lookupTargetNamespace(targetNamespace).rootSchema; |
| 609 |
} |
| 610 |
|
| 611 |
List<Structure> getMovedStructures(String namespace) { |
| 612 |
return lookupTargetNamespace(namespace).movedStructures; |
| 613 |
} |
| 614 |
|
| 615 |
List<String> effectiveIncludes(String sourceUri) { |
| 616 |
String ns = getTargetNamespace(sourceUri); |
| 617 |
List<String> list = new Vector<String>(); |
| 618 |
for (String uri : lookupSourceUri(sourceUri).includes) |
| 619 |
findRootSchemas(uri, ns, list); |
| 620 |
return list; |
| 621 |
} |
| 622 |
|
| 623 |
private void findRootSchemas(String sourceUri, String ns, List<String> list) { |
| 624 |
if (getTargetNamespace(sourceUri).equals(ns)) |
| 625 |
list.add(sourceUri); |
| 626 |
else { |
| 627 |
for (String uri : lookupSourceUri(sourceUri).includes) |
| 628 |
findRootSchemas(uri, ns, list); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
private SourceUri lookupSourceUri(String uri) { |
| 633 |
SourceUri s = sourceUriMap.get(uri); |
| 634 |
if (s == null) { |
| 635 |
s = new SourceUri(); |
| 636 |
sourceUriMap.put(uri, s); |
| 637 |
} |
| 638 |
return s; |
| 639 |
} |
| 640 |
|
| 641 |
private TargetNamespace lookupTargetNamespace(String ns) { |
| 642 |
TargetNamespace t = targetNamespaceMap.get(ns); |
| 643 |
if (t == null) { |
| 644 |
t = new TargetNamespace(); |
| 645 |
targetNamespaceMap.put(ns, t); |
| 646 |
} |
| 647 |
return t; |
| 648 |
} |
| 649 |
|
| 650 |
private NameInfo lookupElementName(Name name) { |
| 651 |
NameInfo info = elementNameMap.get(name); |
| 652 |
if (info == null) { |
| 653 |
info = new NameInfo(); |
| 654 |
elementNameMap.put(name, info); |
| 655 |
} |
| 656 |
return info; |
| 657 |
} |
| 658 |
|
| 659 |
private NameInfo lookupAttributeName(Name name) { |
| 660 |
NameInfo info = attributeNameMap.get(name); |
| 661 |
if (info == null) { |
| 662 |
info = new NameInfo(); |
| 663 |
attributeNameMap.put(name, info); |
| 664 |
} |
| 665 |
return info; |
| 666 |
} |
| 667 |
|
| 668 |
static String otherNamespace(Wildcard wc) { |
| 669 |
if (wc.isPositive()) |
| 670 |
return null; |
| 671 |
Set<String> namespaces = wc.getNamespaces(); |
| 672 |
switch (namespaces.size()) { |
| 673 |
case 2: |
| 674 |
if (!namespaces.contains("")) |
| 675 |
return null; |
| 676 |
Iterator<String> iter = namespaces.iterator(); |
| 677 |
String ns = iter.next(); |
| 678 |
if (!ns.equals("")) |
| 679 |
return ns; |
| 680 |
return iter.next(); |
| 681 |
case 1: |
| 682 |
if (namespaces.contains("")) |
| 683 |
return ""; |
| 684 |
} |
| 685 |
return null; |
| 686 |
} |
| 687 |
|
| 688 |
} |