1 package org.objectweb.celtix.tools.common.toolspec.parser; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 import java.util.logging.Level ; 6 import java.util.logging.Logger ; 7 8 import org.w3c.dom.Element ; 9 import org.w3c.dom.NodeList ; 10 11 import org.objectweb.celtix.common.logging.LogUtils; 12 import org.objectweb.celtix.tools.common.toolspec.Tool; 13 14 15 public class Form implements TokenConsumer { 16 17 private static final Logger LOG = LogUtils.getL7dLogger(Form.class); 18 private final Element element; 19 20 private final List <Object > arguments = new ArrayList <Object >(); 21 private final List <Object > optionGroups = new ArrayList <Object >(); 22 private final List <Object > options = new ArrayList <Object >(); 23 24 public Form(Element el) { 25 this.element = el; 26 NodeList list = element.getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "optionGroup"); 27 28 for (int i = 0; i < list.getLength(); i++) { 29 optionGroups.add(new OptionGroup((Element )list.item(i))); 30 } 31 list = element.getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "argument"); 32 for (int i = 0; i < list.getLength(); i++) { 33 arguments.add(new Argument((Element )list.item(i))); 34 } 35 getOptions(element); 36 } 37 38 private void getOptions(Element el) { 39 NodeList children = el.getChildNodes(); 40 41 for (int i = 0; i < children.getLength(); i++) { 42 if ("option".equals(children.item(i).getNodeName())) { 43 options.add(new Option((Element )children.item(i))); 44 } 45 } 46 } 47 48 52 public boolean accept(TokenInputStream args, Element result, ErrorVisitor errors) { 53 if (LOG.isLoggable(Level.INFO)) { 54 LOG.info("Accepting token stream for form of usage: " + this + ", tokens are " + args); 55 } 56 int oldpos = args.getPosition(); 57 if (LOG.isLoggable(Level.INFO)) { 58 LOG.info("Position is: " + oldpos); 59 } 60 boolean hasInfo = hasInfoOption(args); 61 args.setPosition(oldpos); 62 while (args.available() > 0) { 63 if (LOG.isLoggable(Level.INFO)) { 64 LOG.info("Args is available"); 65 } 66 boolean accepted = false; 67 for (int i = 0; i < optionGroups.size(); i++) { 68 OptionGroup optionGroup = (OptionGroup)optionGroups.get(i); 69 if (optionGroup.accept(args, result, errors)) { 70 accepted = true; 71 break; 72 } 73 } 74 75 if (!accepted) { 76 for (int i = 0; i < options.size(); i++) { 77 Option option = (Option)options.get(i); 78 if (option.accept(args, result, errors)) { 79 accepted = true; 80 break; 81 } 82 } 83 } 84 85 if (!accepted) { 86 break; 87 } 88 } 89 90 for (int i = 0; i < optionGroups.size(); i++) { 91 OptionGroup optionGroup = (OptionGroup)optionGroups.get(i); 92 93 if (!optionGroup.isSatisfied(errors) && !hasInfo) { 94 return false; 95 } 96 } 97 98 for (int i = 0; i < options.size(); i++) { 99 Option option = (Option)options.get(i); 100 101 if (!option.isSatisfied(errors) && !hasInfo) { 102 return false; 103 } 104 } 105 106 if (arguments != null) { 107 for (int i = 0; i < arguments.size(); i++) { 108 Argument argument = (Argument)arguments.get(i); 109 110 argument.accept(args, result, errors); 111 if (!argument.isSatisfied(errors) && !hasInfo) { 112 return false; 113 } 114 } 115 } 116 117 if (args.available() > 0) { 118 String next = args.peek(); 119 120 if (next.startsWith("-")) { 121 errors.add(new ErrorVisitor.UnexpectedOption(next)); 122 } else { 123 errors.add(new ErrorVisitor.UnexpectedArgument(next)); 124 } 125 if (LOG.isLoggable(Level.INFO)) { 126 LOG.info(this + " form is returning false as there are more args available" 127 + " that haven't been consumed"); 128 } 129 args.setPosition(oldpos); 130 return false; 131 } 132 if (LOG.isLoggable(Level.INFO)) { 134 LOG.info("Form " + this + " is returning true"); 135 } 136 return true; 137 } 138 139 public boolean hasInfoOption(TokenInputStream args) { 140 int pos = args.getPosition(); 141 args.setPosition(0); 142 String optionValue; 143 while (args.available() > 0) { 144 optionValue = args.read(); 145 if ("-?".equals(optionValue) || "-help".equals(optionValue) || "-h".equals(optionValue) 146 || "-v".equals(optionValue)) { 147 return true; 148 } 149 } 150 args.setPosition(pos); 151 return false; 152 } 153 154 public boolean isSatisfied(ErrorVisitor errors) { 155 return true; 156 } 157 158 public String getName() { 159 if (element.hasAttribute("value")) { 160 return element.getAttribute("value"); 161 } else { 162 return "default"; 163 } 164 165 } 166 167 public String toString() { 168 return getName(); 169 } 170 171 } 172 | Popular Tags |