01: package org.kohsuke.rngom.nc;
02:
03: import org.kohsuke.rngom.ast.om.ParsedNameClass;
04:
05: import javax.xml.namespace.QName;
06: import java.io.Serializable;
07: import java.util.HashSet;
08: import java.util.Set;
09:
10: /**
11: * Name class is a set of {@link QName}s.
12: */
13: public abstract class NameClass implements ParsedNameClass,
14: Serializable {
15: static final int SPECIFICITY_NONE = -1;
16: static final int SPECIFICITY_ANY_NAME = 0;
17: static final int SPECIFICITY_NS_NAME = 1;
18: static final int SPECIFICITY_NAME = 2;
19:
20: /**
21: * Returns true if the given {@link QName} is a valid name
22: * for this QName.
23: */
24: public abstract boolean contains(QName name);
25:
26: public abstract int containsSpecificity(QName name);
27:
28: /**
29: * Visitor pattern support.
30: */
31: public abstract <V> V accept(NameClassVisitor<V> visitor);
32:
33: /**
34: * Returns true if the name class accepts infinite number of
35: * {@link QName}s.
36: *
37: * <p>
38: * Intuitively, this method returns true if the name class is
39: * some sort of wildcard.
40: */
41: public abstract boolean isOpen();
42:
43: /**
44: * If the name class is closed (IOW !{@link #isOpen()}),
45: * return the set of names in this name class. Otherwise the behavior
46: * is undefined.
47: */
48: public Set<QName> listNames() {
49: final Set<QName> names = new HashSet<QName>();
50: accept(new NameClassWalker() {
51: public Void visitName(QName name) {
52: names.add(name);
53: return null;
54: }
55: });
56: return names;
57: }
58:
59: /**
60: * Returns true if the intersection between this name class
61: * and the specified name class is non-empty.
62: */
63: public final boolean hasOverlapWith(NameClass nc2) {
64: return OverlapDetector.overlap(this , nc2);
65: }
66:
67: /** Sigleton instance that represents "anyName". */
68: public static final NameClass ANY = new AnyNameClass();
69:
70: /**
71: * Sigleton instance that accepts no name.
72: *
73: * <p>
74: * This instance is useful when doing boolean arithmetic over
75: * name classes (such as computing an inverse of a given name class, etc),
76: * even though it can never appear in a RELAX NG surface syntax.
77: *
78: * <p>
79: * Internally, this instance is also used for:
80: * <ol>
81: * <li>Used to recover from errors during parsing.
82: * <li>Mark element patterns with <notAllowed/> content model.
83: * </ol>
84: */
85: public static final NameClass NULL = new NullNameClass();
86: }
|