KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > relaxng > impl > OverlapDetector


1 package com.thaiopensource.relaxng.impl;
2
3 import com.thaiopensource.xml.util.Name;
4
5 class OverlapDetector implements NameClassVisitor {
6   private final NameClass nc1;
7   private final NameClass nc2;
8   private boolean overlaps = false;
9
10   private static final String JavaDoc 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(Name 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   }
28
29   public void visitNsName(String JavaDoc ns) {
30     probe(new Name(ns, IMPOSSIBLE));
31   }
32
33   public void visitNsNameExcept(String JavaDoc ns, NameClass ex) {
34     probe(new Name(ns, IMPOSSIBLE));
35     ex.accept(this);
36   }
37
38   public void visitAnyName() {
39     probe(new Name(IMPOSSIBLE, IMPOSSIBLE));
40   }
41
42   public void visitAnyNameExcept(NameClass ex) {
43     probe(new Name(IMPOSSIBLE, IMPOSSIBLE));
44     ex.accept(this);
45   }
46
47   public void visitName(Name name) {
48     probe(name);
49   }
50
51   public void visitNull() {
52   }
53
54   public void visitError() {
55   }
56
57   static boolean overlap(NameClass nc1, NameClass nc2) {
58     if (nc2 instanceof SimpleNameClass) {
59       SimpleNameClass snc = (SimpleNameClass)nc2;
60       return nc1.contains(snc.getName());
61     }
62     if (nc1 instanceof SimpleNameClass) {
63       SimpleNameClass snc = (SimpleNameClass)nc1;
64       return nc2.contains(snc.getName());
65     }
66     return new OverlapDetector(nc1, nc2).overlaps;
67   }
68 }
69
Popular Tags