01: package org.kohsuke.rngom.binary.visitor;
02:
03: import org.kohsuke.rngom.binary.Pattern;
04: import org.kohsuke.rngom.nc.NameClass;
05:
06: import java.util.HashSet;
07: import java.util.Set;
08:
09: /**
10: * Visits a pattern and creates a list of possible child elements.
11: *
12: * <p>
13: * One can use a similar technique to introspect a pattern.
14: *
15: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
16: */
17: public class ChildElementFinder extends PatternWalker {
18:
19: private final Set children = new HashSet();
20:
21: /**
22: * Represents a child element.
23: */
24: public static class Element {
25: public final NameClass nc;
26: public final Pattern content;
27:
28: public Element(NameClass nc, Pattern content) {
29: this .nc = nc;
30: this .content = content;
31: }
32:
33: public boolean equals(Object o) {
34: if (this == o)
35: return true;
36: if (!(o instanceof Element))
37: return false;
38:
39: final Element element = (Element) o;
40:
41: if (content != null ? !content.equals(element.content)
42: : element.content != null)
43: return false;
44: if (nc != null ? !nc.equals(element.nc)
45: : element.nc != null)
46: return false;
47:
48: return true;
49: }
50:
51: public int hashCode() {
52: int result;
53: result = (nc != null ? nc.hashCode() : 0);
54: result = 29 * result
55: + (content != null ? content.hashCode() : 0);
56: return result;
57: }
58: }
59:
60: /**
61: * Returns a set of {@link Element}.
62: */
63: public Set getChildren() {
64: return children;
65: }
66:
67: public void visitElement(NameClass nc, Pattern content) {
68: children.add(new Element(nc, content));
69: }
70:
71: public void visitAttribute(NameClass ns, Pattern value) {
72: // there will be no element inside attribute,
73: // so don't go in there.
74: }
75:
76: public void visitList(Pattern p) {
77: // there will be no element inside a list,
78: // so don't go in there.
79: }
80: }
|