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