| 1 |
package com.thaiopensource.relaxng.output.xsd; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.edit.AnyNameNameClass; |
| 4 |
import com.thaiopensource.relaxng.edit.ChoiceNameClass; |
| 5 |
import com.thaiopensource.relaxng.edit.NameClass; |
| 6 |
import com.thaiopensource.relaxng.edit.NameClassVisitor; |
| 7 |
import com.thaiopensource.relaxng.edit.NameNameClass; |
| 8 |
import com.thaiopensource.relaxng.edit.NsNameNameClass; |
| 9 |
import com.thaiopensource.util.VoidValue; |
| 10 |
import com.thaiopensource.relaxng.output.xsd.basic.Wildcard; |
| 11 |
import com.thaiopensource.xml.util.Name; |
| 12 |
|
| 13 |
import java.util.Collections; |
| 14 |
import java.util.HashSet; |
| 15 |
import java.util.List; |
| 16 |
import java.util.Set; |
| 17 |
|
| 18 |
class WildcardBuilder implements NameClassVisitor<VoidValue> { |
| 19 |
private boolean inExcept = false; |
| 20 |
private final String inheritedNamespace; |
| 21 |
private Wildcard wildcard = null; |
| 22 |
private Set<Name> excludedNames; |
| 23 |
private Set<String> namespaces; |
| 24 |
private String inNs = null; |
| 25 |
|
| 26 |
static Wildcard createWildcard(NameClass nc, String inheritedNamespace) { |
| 27 |
WildcardBuilder builder = new WildcardBuilder(inheritedNamespace); |
| 28 |
nc.accept(builder); |
| 29 |
return builder.wildcard; |
| 30 |
} |
| 31 |
|
| 32 |
private void combineWildcard(Wildcard wc) { |
| 33 |
if (wildcard == null) |
| 34 |
wildcard = wc; |
| 35 |
else |
| 36 |
wildcard = Wildcard.union(wildcard, wc); |
| 37 |
} |
| 38 |
|
| 39 |
private WildcardBuilder(String inheritedNamespace) { |
| 40 |
this.inheritedNamespace = inheritedNamespace; |
| 41 |
} |
| 42 |
|
| 43 |
public VoidValue visitChoice(ChoiceNameClass nc) { |
| 44 |
List<NameClass> list = nc.getChildren(); |
| 45 |
for (int i = 0, len = list.size(); i < len; i++) |
| 46 |
(list.get(i)).accept(this); |
| 47 |
return VoidValue.VOID; |
| 48 |
} |
| 49 |
|
| 50 |
public VoidValue visitAnyName(AnyNameNameClass nc) { |
| 51 |
if (!inExcept) { |
| 52 |
if (nc.getExcept() != null) { |
| 53 |
namespaces = new HashSet<String>(); |
| 54 |
excludedNames = new HashSet<Name>(); |
| 55 |
inExcept = true; |
| 56 |
nc.getExcept().accept(this); |
| 57 |
inExcept = false; |
| 58 |
} |
| 59 |
else { |
| 60 |
namespaces = Collections.emptySet(); |
| 61 |
excludedNames = Collections.emptySet(); |
| 62 |
} |
| 63 |
combineWildcard(new Wildcard(false, namespaces, excludedNames)); |
| 64 |
} |
| 65 |
return VoidValue.VOID; |
| 66 |
} |
| 67 |
|
| 68 |
public VoidValue visitNsName(NsNameNameClass nc) { |
| 69 |
String ns = resolve(nc.getNs()); |
| 70 |
if (!inExcept) { |
| 71 |
if (nc.getExcept() != null) { |
| 72 |
namespaces = null; |
| 73 |
excludedNames = new HashSet<Name>(); |
| 74 |
inNs = ns; |
| 75 |
inExcept = true; |
| 76 |
nc.getExcept().accept(this); |
| 77 |
inExcept = false; |
| 78 |
inNs = null; |
| 79 |
} |
| 80 |
else |
| 81 |
excludedNames = Collections.emptySet(); |
| 82 |
namespaces = new HashSet<String>(); |
| 83 |
namespaces.add(ns); |
| 84 |
combineWildcard(new Wildcard(true, namespaces, excludedNames)); |
| 85 |
} |
| 86 |
else if (inNs == null) |
| 87 |
namespaces.add(ns); |
| 88 |
return VoidValue.VOID; |
| 89 |
} |
| 90 |
|
| 91 |
public VoidValue visitName(NameNameClass nc) { |
| 92 |
if (inExcept) { |
| 93 |
String ns = resolve(nc.getNamespaceUri()); |
| 94 |
if (inNs == null || inNs.equals(ns)) |
| 95 |
excludedNames.add(new Name(ns, nc.getLocalName())); |
| 96 |
} |
| 97 |
return VoidValue.VOID; |
| 98 |
} |
| 99 |
|
| 100 |
private String resolve(String ns) { |
| 101 |
if (ns == NameNameClass.INHERIT_NS) |
| 102 |
return inheritedNamespace; |
| 103 |
return ns; |
| 104 |
} |
| 105 |
} |