01: package org.objectweb.celtix.tools.common.toolspec.parser;
02:
03: import java.util.ArrayList;
04: import java.util.Iterator;
05: import java.util.List;
06: import java.util.logging.Level;
07: import java.util.logging.Logger;
08:
09: import org.w3c.dom.Element;
10: import org.w3c.dom.NodeList;
11:
12: import org.objectweb.celtix.common.logging.LogUtils;
13: import org.objectweb.celtix.tools.common.toolspec.Tool;
14:
15: public class OptionGroup implements TokenConsumer {
16:
17: private static final Logger LOG = LogUtils
18: .getL7dLogger(OptionGroup.class);
19: private final Element element;
20:
21: private final List<Object> options = new ArrayList<Object>();
22:
23: public OptionGroup(Element el) {
24: this .element = el;
25: NodeList optionEls = element.getElementsByTagNameNS(
26: Tool.TOOL_SPEC_PUBLIC_ID, "option");
27:
28: for (int i = 0; i < optionEls.getLength(); i++) {
29: options.add(new Option((Element) optionEls.item(i)));
30: }
31: }
32:
33: public boolean accept(TokenInputStream args, Element result,
34: ErrorVisitor errors) {
35: if (LOG.isLoggable(Level.INFO)) {
36: LOG.info("Accepting token stream for optionGroup: " + this
37: + ", tokens are now " + args + ", running through "
38: + options.size() + " options");
39: }
40: // Give all the options the chance to exclusively consume the given
41: // string:
42: boolean accepted = false;
43:
44: for (Iterator it = options.iterator(); it.hasNext();) {
45: Option option = (Option) it.next();
46:
47: if (option.accept(args, result, errors)) {
48: if (LOG.isLoggable(Level.INFO)) {
49: LOG
50: .info("Option " + option
51: + " accepted the token");
52: }
53: accepted = true;
54: break;
55: }
56: }
57: if (!accepted) {
58: if (LOG.isLoggable(Level.INFO)) {
59: LOG.info("No option accepted the token, returning");
60: }
61: return false;
62: }
63:
64: return true;
65: }
66:
67: public boolean isSatisfied(ErrorVisitor errors) {
68: // Return conjunction of all isSatisfied results from every option
69: for (Iterator it = options.iterator(); it.hasNext();) {
70: if (!((Option) it.next()).isSatisfied(errors)) {
71: return false;
72: }
73: }
74: return true;
75: }
76:
77: public String getId() {
78: return element.getAttribute("id");
79: }
80:
81: public String toString() {
82: if (element.hasAttribute("id")) {
83: return getId();
84: } else {
85: return super.toString();
86: }
87: }
88: }
|