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.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import org.w3c.dom.Element;
027: import org.w3c.dom.NodeList;
028:
029: import org.apache.cxf.common.logging.LogUtils;
030: import org.apache.cxf.tools.common.toolspec.Tool;
031:
032: public class OptionGroup implements TokenConsumer {
033:
034: private static final Logger LOG = LogUtils
035: .getL7dLogger(OptionGroup.class);
036: private final Element element;
037:
038: private final List<Object> options = new ArrayList<Object>();
039:
040: public OptionGroup(Element el) {
041: this .element = el;
042: NodeList optionEls = element.getElementsByTagNameNS(
043: Tool.TOOL_SPEC_PUBLIC_ID, "option");
044:
045: for (int i = 0; i < optionEls.getLength(); i++) {
046: options.add(new Option((Element) optionEls.item(i)));
047: }
048: }
049:
050: public boolean accept(TokenInputStream args, Element result,
051: ErrorVisitor errors) {
052: if (LOG.isLoggable(Level.FINE)) {
053: LOG.fine("Accepting token stream for optionGroup: " + this
054: + ", tokens are now " + args + ", running through "
055: + options.size() + " options");
056: }
057: // Give all the options the chance to exclusively consume the given
058: // string:
059: boolean accepted = false;
060:
061: for (Iterator it = options.iterator(); it.hasNext();) {
062: Option option = (Option) it.next();
063:
064: if (option.accept(args, result, errors)) {
065: if (LOG.isLoggable(Level.FINE)) {
066: LOG
067: .fine("Option " + option
068: + " accepted the token");
069: }
070: accepted = true;
071: break;
072: }
073: }
074: if (!accepted) {
075: if (LOG.isLoggable(Level.FINE)) {
076: LOG.fine("No option accepted the token, returning");
077: }
078: return false;
079: }
080:
081: return true;
082: }
083:
084: public boolean isSatisfied(ErrorVisitor errors) {
085: // Return conjunction of all isSatisfied results from every option
086: for (Iterator it = options.iterator(); it.hasNext();) {
087: if (!((Option) it.next()).isSatisfied(errors)) {
088: return false;
089: }
090: }
091: return true;
092: }
093:
094: public String getId() {
095: return element.getAttribute("id");
096: }
097:
098: public String toString() {
099: if (element.hasAttribute("id")) {
100: return getId();
101: } else {
102: return super.toString();
103: }
104: }
105: }
|