001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.common.toolspec.parser;
019:
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022:
023: import org.w3c.dom.Element;
024:
025: import org.apache.cxf.common.logging.LogUtils;
026: import org.apache.cxf.tools.common.toolspec.ToolSpec;
027:
028: public class Argument implements TokenConsumer {
029:
030: private static final Logger LOG = LogUtils
031: .getL7dLogger(Argument.class);
032:
033: protected ToolSpec toolspec;
034:
035: private final Element element;
036: private int numMatches;
037:
038: public Argument(Element el) {
039: this .element = el;
040: }
041:
042: public boolean accept(TokenInputStream args, Element result,
043: ErrorVisitor errors) {
044: if (LOG.isLoggable(Level.FINE)) {
045: LOG.fine("Accepting token stream for argument: " + this );
046: }
047: int minOccurs;
048: if ("unbounded".equals(element.getAttribute("minOccurs"))) {
049: minOccurs = 0;
050: } else {
051: minOccurs = Integer.parseInt(element
052: .getAttribute("minOccurs"));
053: }
054: if (minOccurs == 0) {
055: addElement(args, result);
056: return true;
057: }
058: if (minOccurs > args.available()) {
059: return false;
060: }
061: if (args.peekPre().endsWith(",")
062: && args.peekPre().startsWith("-")) {
063: if (args.hasNext()) {
064: args.readNext();
065: } else {
066: return false;
067: }
068: }
069: for (int i = 0; i < minOccurs; i++) {
070: if (args.peek().startsWith("-")) {
071: errors.add(new ErrorVisitor.UnexpectedOption(args
072: .peek()));
073: return false;
074: }
075: addElement(args, result);
076: }
077: return true;
078: }
079:
080: private void addElement(TokenInputStream args, Element result) {
081: Element argEl = result.getOwnerDocument().createElementNS(
082: "http://cxf.apache.org/Xutil/Command", "argument");
083: argEl.setAttribute("name", getName());
084: if (!args.isOutOfBound()) {
085: argEl.appendChild(result.getOwnerDocument().createTextNode(
086: args.read()));
087: }
088: result.appendChild(argEl);
089: numMatches++;
090: }
091:
092: private boolean isAtleastMinimum() {
093: boolean result = true;
094: int minOccurs = 0;
095:
096: if (!"".equals(element.getAttribute("minOccurs"))) {
097: result = numMatches >= Integer.parseInt(element
098: .getAttribute("minOccurs"));
099: } else {
100: result = numMatches >= minOccurs;
101: }
102: return result;
103: }
104:
105: private boolean isNoGreaterThanMaximum() {
106: boolean result = true;
107: // int maxOccurs = 1;
108: if ("unbounded".equals(element.getAttribute("maxOccurs"))
109: || "".equals(element.getAttribute("maxOccurs"))) {
110: return true;
111: }
112: if (!"".equals(element.getAttribute("maxOccurs"))) {
113: result = numMatches <= Integer.parseInt(element
114: .getAttribute("maxOccurs"));
115: }
116: return result;
117: }
118:
119: public boolean isSatisfied(ErrorVisitor errors) {
120: if (errors.getErrors().size() > 0) {
121: return false;
122: }
123: if (!isAtleastMinimum()) {
124: errors.add(new ErrorVisitor.MissingArgument(getName()));
125: return false;
126: }
127: if (!isNoGreaterThanMaximum()) {
128: errors.add(new ErrorVisitor.DuplicateArgument(getName()));
129: return false;
130: }
131:
132: return true;
133: }
134:
135: public void setToolSpec(ToolSpec toolSpec) {
136: this .toolspec = toolSpec;
137: }
138:
139: public String getName() {
140: return element.getAttribute("id");
141: }
142:
143: public String toString() {
144: return getName();
145: }
146: }
|