KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.celtix.tools.common.toolspec.parser;
2
3 import java.util.*;
4 import java.util.logging.Level JavaDoc;
5 import java.util.logging.Logger JavaDoc;
6
7 import org.w3c.dom.*;
8
9 import org.objectweb.celtix.common.logging.LogUtils;
10 import org.objectweb.celtix.tools.common.toolspec.ToolSpec;
11
12 public class CommandDocument {
13     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(CommandDocument.class);
14
15     private final Document doc;
16     private final ToolSpec toolspec;
17     private final List<Object JavaDoc> values;
18
19     CommandDocument(ToolSpec ts, Document d) {
20
21         if (ts == null) {
22             throw new NullPointerException JavaDoc("CommandDocument cannot be created with a null toolspec");
23         }
24         this.toolspec = ts;
25
26         if (d == null) {
27             throw new NullPointerException JavaDoc("CommandDocument cannot be created with a null document");
28         }
29         values = new ArrayList<Object JavaDoc>();
30         this.doc = d;
31         NodeList nl = doc.getDocumentElement().getElementsByTagName("option");
32
33         for (int i = 0; i < nl.getLength(); i++) {
34             values.add(nl.item(i));
35         }
36         nl = doc.getDocumentElement().getElementsByTagName("argument");
37         for (int i = 0; i < nl.getLength(); i++) {
38             values.add(nl.item(i));
39         }
40     }
41
42     public Document getDocument() {
43         return doc;
44     }
45
46     public boolean hasParameter(String JavaDoc name) {
47         return getParameters(name).length > 0;
48     }
49
50     public String JavaDoc getParameter(String JavaDoc name) {
51         if (LOG.isLoggable(Level.FINE)) {
52             LOG.fine("Getting parameter " + name);
53         }
54         String JavaDoc[] res = getParameters(name);
55
56         if (res.length == 0) {
57             return null;
58         }
59         return res[0];
60     }
61
62     public String JavaDoc[] getParameters(String JavaDoc name) {
63         if (LOG.isLoggable(Level.FINE)) {
64             LOG.fine("Getting parameters for " + name);
65         }
66         List<Object JavaDoc> result = new ArrayList<Object JavaDoc>();
67
68         if (values != null) {
69             for (Iterator it = values.iterator(); it.hasNext();) {
70                 Element el = (Element)it.next();
71
72                 if (el.getAttribute("name").equals(name)) {
73                     if (el.hasChildNodes()) {
74                         result.add(el.getFirstChild().getNodeValue());
75                     } else {
76                         result.add("true");
77                     }
78                 }
79             }
80         }
81         if (result.isEmpty()) {
82             String JavaDoc def = toolspec.getParameterDefault(name);
83
84             if (def != null) {
85                 result.add(def);
86             }
87         }
88         return result.toArray(new String JavaDoc[result.size()]);
89     }
90
91     public String JavaDoc[] getParameterNames() {
92         List<Object JavaDoc> result = new ArrayList<Object JavaDoc>();
93
94         if (values != null) {
95             for (Iterator it = values.iterator(); it.hasNext();) {
96                 Element el = (Element)it.next();
97
98                 result.add(el.getAttribute("name"));
99             }
100         }
101         return result.toArray(new String JavaDoc[result.size()]);
102     }
103
104 }
105
Popular Tags