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.*;
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023:
024: import org.w3c.dom.*;
025:
026: import org.apache.cxf.common.logging.LogUtils;
027: import org.apache.cxf.tools.common.toolspec.ToolSpec;
028:
029: public class CommandDocument {
030: private static final Logger LOG = LogUtils
031: .getL7dLogger(CommandDocument.class);
032:
033: private final Document doc;
034: private final ToolSpec toolspec;
035: private final List<Object> values;
036:
037: CommandDocument(ToolSpec ts, Document d) {
038:
039: if (ts == null) {
040: throw new NullPointerException(
041: "CommandDocument cannot be created with a null toolspec");
042: }
043: this .toolspec = ts;
044:
045: if (d == null) {
046: throw new NullPointerException(
047: "CommandDocument cannot be created with a null document");
048: }
049: values = new ArrayList<Object>();
050: this .doc = d;
051: NodeList nl = doc.getDocumentElement().getElementsByTagName(
052: "option");
053:
054: for (int i = 0; i < nl.getLength(); i++) {
055: values.add(nl.item(i));
056: }
057: nl = doc.getDocumentElement().getElementsByTagName("argument");
058: for (int i = 0; i < nl.getLength(); i++) {
059: values.add(nl.item(i));
060: }
061: }
062:
063: public Document getDocument() {
064: return doc;
065: }
066:
067: public boolean hasParameter(String name) {
068: return getParameters(name).length > 0;
069: }
070:
071: public String getParameter(String name) {
072: if (LOG.isLoggable(Level.FINE)) {
073: LOG.fine("Getting parameter " + name);
074: }
075: String[] res = getParameters(name);
076:
077: if (res.length == 0) {
078: return null;
079: }
080: return res[0];
081: }
082:
083: public String[] getParameters(String name) {
084: if (LOG.isLoggable(Level.FINE)) {
085: LOG.fine("Getting parameters for " + name);
086: }
087: List<Object> result = new ArrayList<Object>();
088:
089: if (values != null) {
090: for (Iterator it = values.iterator(); it.hasNext();) {
091: Element el = (Element) it.next();
092:
093: if (el.getAttribute("name").equals(name)) {
094: if (el.hasChildNodes()) {
095: result.add(el.getFirstChild().getNodeValue());
096: } else {
097: result.add("true");
098: }
099: }
100: }
101: }
102: if (result.isEmpty()) {
103: String def = toolspec.getParameterDefault(name);
104:
105: if (def != null) {
106: result.add(def);
107: }
108: }
109: return result.toArray(new String[result.size()]);
110: }
111:
112: public String[] getParameterNames() {
113: List<Object> result = new ArrayList<Object>();
114:
115: if (values != null) {
116: for (Iterator it = values.iterator(); it.hasNext();) {
117: Element el = (Element) it.next();
118:
119: result.add(el.getAttribute("name"));
120: }
121: }
122: return result.toArray(new String[result.size()]);
123: }
124:
125: }
|