01: package org.kohsuke.rngom.nc;
02:
03: import javax.xml.namespace.QName;
04:
05: class OverlapDetector implements NameClassVisitor<Void> {
06: private NameClass nc1;
07: private NameClass nc2;
08: private boolean overlaps = false;
09:
10: static final String IMPOSSIBLE = "\u0000";
11:
12: private OverlapDetector(NameClass nc1, NameClass nc2) {
13: this .nc1 = nc1;
14: this .nc2 = nc2;
15: nc1.accept(this );
16: nc2.accept(this );
17: }
18:
19: private void probe(QName name) {
20: if (nc1.contains(name) && nc2.contains(name))
21: overlaps = true;
22: }
23:
24: public Void visitChoice(NameClass nc1, NameClass nc2) {
25: nc1.accept(this );
26: nc2.accept(this );
27: return null;
28: }
29:
30: public Void visitNsName(String ns) {
31: probe(new QName(ns, IMPOSSIBLE));
32: return null;
33: }
34:
35: public Void visitNsNameExcept(String ns, NameClass ex) {
36: probe(new QName(ns, IMPOSSIBLE));
37: ex.accept(this );
38: return null;
39: }
40:
41: public Void visitAnyName() {
42: probe(new QName(IMPOSSIBLE, IMPOSSIBLE));
43: return null;
44: }
45:
46: public Void visitAnyNameExcept(NameClass ex) {
47: probe(new QName(IMPOSSIBLE, IMPOSSIBLE));
48: ex.accept(this );
49: return null;
50: }
51:
52: public Void visitName(QName name) {
53: probe(name);
54: return null;
55: }
56:
57: public Void visitNull() {
58: return null;
59: }
60:
61: static boolean overlap(NameClass nc1, NameClass nc2) {
62: if (nc2 instanceof SimpleNameClass) {
63: SimpleNameClass snc = (SimpleNameClass) nc2;
64: return nc1.contains(snc.name);
65: }
66: if (nc1 instanceof SimpleNameClass) {
67: SimpleNameClass snc = (SimpleNameClass) nc1;
68: return nc2.contains(snc.name);
69: }
70: return new OverlapDetector(nc1, nc2).overlaps;
71: }
72: }
|