KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > common > toolspec > parser > Argument


1 package org.objectweb.celtix.tools.common.toolspec.parser;
2
3 import java.util.logging.Level JavaDoc;
4 import java.util.logging.Logger JavaDoc;
5
6 import org.w3c.dom.Element JavaDoc;
7
8 import org.objectweb.celtix.common.logging.LogUtils;
9 import org.objectweb.celtix.tools.common.toolspec.ToolSpec;
10
11 public class Argument implements TokenConsumer {
12
13     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(Argument.class);
14
15     protected ToolSpec toolspec;
16
17     private final Element JavaDoc element;
18     private int numMatches;
19
20     public Argument(Element JavaDoc el) {
21         this.element = el;
22     }
23
24     public boolean accept(TokenInputStream args, Element JavaDoc result, ErrorVisitor errors) {
25         if (LOG.isLoggable(Level.INFO)) {
26             LOG.info("Accepting token stream for argument: " + this);
27         }
28         int minOccurs;
29         if ("unbounded".equals(element.getAttribute("minOccurs"))) {
30             minOccurs = 0;
31         } else {
32             minOccurs = Integer.parseInt(element.getAttribute("minOccurs"));
33         }
34         if (minOccurs == 0) {
35             addElement(args, result);
36             return true;
37         }
38         if (minOccurs > args.available()) {
39             return false;
40         }
41         if (args.peekPre().endsWith(",") && args.peekPre().startsWith("-")) {
42             if (args.hasNext()) {
43                 args.readNext();
44             } else {
45                 return false;
46             }
47         }
48         for (int i = 0; i < minOccurs; i++) {
49             if (args.peek().startsWith("-")) {
50                 errors.add(new ErrorVisitor.UnexpectedOption(args.peek()));
51                 return false;
52             }
53             addElement(args, result);
54         }
55         return true;
56     }
57
58     private void addElement(TokenInputStream args, Element JavaDoc result) {
59         Element JavaDoc argEl = result.getOwnerDocument().createElementNS("http://www.xsume.com/Xutil/Command",
60                                                                   "argument");
61         argEl.setAttribute("name", getName());
62         if (!args.isOutOfBound()) {
63             argEl.appendChild(result.getOwnerDocument().createTextNode(args.read()));
64         }
65         result.appendChild(argEl);
66         numMatches++;
67     }
68
69     private boolean isAtleastMinimum() {
70         boolean result = true;
71         int minOccurs = 0;
72
73         if (!"".equals(element.getAttribute("minOccurs"))) {
74             result = numMatches >= Integer.parseInt(element.getAttribute("minOccurs"));
75         } else {
76             result = numMatches >= minOccurs;
77         }
78         return result;
79     }
80
81     private boolean isNoGreaterThanMaximum() {
82         boolean result = true;
83         // int maxOccurs = 1;
84
if ("unbounded".equals(element.getAttribute("maxOccurs"))
85             || "".equals(element.getAttribute("maxOccurs"))) {
86             return true;
87         }
88         if (!"".equals(element.getAttribute("maxOccurs"))) {
89             result = numMatches <= Integer.parseInt(element.getAttribute("maxOccurs"));
90         }
91         return result;
92     }
93
94     public boolean isSatisfied(ErrorVisitor errors) {
95         boolean result = true;
96
97         if (errors.getErrors().size() > 0) {
98             result = false;
99         }
100         if (result && !isAtleastMinimum()) {
101             errors.add(new ErrorVisitor.MissingArgument(getName()));
102             result = false;
103         }
104         if (result && !isNoGreaterThanMaximum()) {
105             errors.add(new ErrorVisitor.DuplicateArgument(getName()));
106             result = false;
107         }
108
109         return result;
110     }
111
112     public void setToolSpec(ToolSpec toolSpec) {
113         this.toolspec = toolSpec;
114     }
115
116     public String JavaDoc getName() {
117         return element.getAttribute("id");
118     }
119
120     public String JavaDoc toString() {
121         return getName();
122     }
123 }
124
Popular Tags