01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.xml.internal.rngom.nc;
27:
28: import javax.xml.namespace.QName;
29:
30: class OverlapDetector implements NameClassVisitor<Void> {
31: private NameClass nc1;
32: private NameClass nc2;
33: private boolean overlaps = false;
34:
35: static final String IMPOSSIBLE = "\u0000";
36:
37: private OverlapDetector(NameClass nc1, NameClass nc2) {
38: this .nc1 = nc1;
39: this .nc2 = nc2;
40: nc1.accept(this );
41: nc2.accept(this );
42: }
43:
44: private void probe(QName name) {
45: if (nc1.contains(name) && nc2.contains(name))
46: overlaps = true;
47: }
48:
49: public Void visitChoice(NameClass nc1, NameClass nc2) {
50: nc1.accept(this );
51: nc2.accept(this );
52: return null;
53: }
54:
55: public Void visitNsName(String ns) {
56: probe(new QName(ns, IMPOSSIBLE));
57: return null;
58: }
59:
60: public Void visitNsNameExcept(String ns, NameClass ex) {
61: probe(new QName(ns, IMPOSSIBLE));
62: ex.accept(this );
63: return null;
64: }
65:
66: public Void visitAnyName() {
67: probe(new QName(IMPOSSIBLE, IMPOSSIBLE));
68: return null;
69: }
70:
71: public Void visitAnyNameExcept(NameClass ex) {
72: probe(new QName(IMPOSSIBLE, IMPOSSIBLE));
73: ex.accept(this );
74: return null;
75: }
76:
77: public Void visitName(QName name) {
78: probe(name);
79: return null;
80: }
81:
82: public Void visitNull() {
83: return null;
84: }
85:
86: static boolean overlap(NameClass nc1, NameClass nc2) {
87: if (nc2 instanceof SimpleNameClass) {
88: SimpleNameClass snc = (SimpleNameClass) nc2;
89: return nc1.contains(snc.name);
90: }
91: if (nc1 instanceof SimpleNameClass) {
92: SimpleNameClass snc = (SimpleNameClass) nc1;
93: return nc2.contains(snc.name);
94: }
95: return new OverlapDetector(nc1, nc2).overlaps;
96: }
97: }
|