KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > common > toolspec > ToolSpec


1 package org.objectweb.celtix.tools.common.toolspec;
2
3 import java.io.InputStream JavaDoc;
4 import java.io.OutputStream JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.logging.Level JavaDoc;
9 import java.util.logging.Logger JavaDoc;
10
11 import javax.xml.transform.Transformer JavaDoc;
12 import javax.xml.transform.TransformerException JavaDoc;
13 import javax.xml.transform.TransformerFactory JavaDoc;
14 import javax.xml.transform.dom.DOMSource JavaDoc;
15 import javax.xml.transform.stream.StreamResult JavaDoc;
16 import javax.xml.transform.stream.StreamSource JavaDoc;
17
18 import org.w3c.dom.Document JavaDoc;
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.Node JavaDoc;
21 import org.w3c.dom.NodeList JavaDoc;
22
23 import org.objectweb.celtix.common.i18n.Message;
24 import org.objectweb.celtix.common.logging.LogUtils;
25 import org.objectweb.celtix.tools.common.ToolException;
26 import org.objectweb.celtix.tools.common.dom.ExtendedDocumentBuilder;
27
28 public class ToolSpec {
29
30     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(ToolSpec.class);
31
32     private final ExtendedDocumentBuilder builder = new ExtendedDocumentBuilder();
33     private Document JavaDoc doc;
34     private Tool handler;
35
36     public ToolSpec() {
37     }
38
39     public ToolSpec(InputStream JavaDoc in) throws ToolException {
40         this(in, true);
41     }
42
43     public ToolSpec(InputStream JavaDoc in, boolean validate) throws ToolException {
44         if (in == null) {
45             throw new NullPointerException JavaDoc("Cannot create a ToolSpec object from a null stream");
46         }
47         try {
48             builder.setValidating(validate);
49             this.doc = builder.parse(in);
50         } catch (Exception JavaDoc ex) {
51             Message message = new Message("FAIL_TO_PARSING_TOOLSPCE_STREAM", LOG);
52             throw new ToolException(message, ex);
53         }
54     }
55
56     public ToolSpec(Document JavaDoc d) {
57         if (d == null) {
58             throw new NullPointerException JavaDoc("Cannot create a ToolSpec object from "
59                                            + "a null org.w3c.dom.Document");
60         }
61         this.doc = d;
62     }
63
64     public ExtendedDocumentBuilder getDocumentBuilder() {
65         return builder;
66     }
67
68     public boolean isValidInputStream(String JavaDoc id) {
69         Element JavaDoc streams = getStreams();
70
71         if (streams == null) {
72             return false;
73         }
74         NodeList JavaDoc nl = streams.getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "instream");
75
76         for (int i = 0; i < nl.getLength(); i++) {
77             if (((Element JavaDoc)nl.item(i)).getAttribute("id").equals(id)) {
78                 return true;
79             }
80         }
81         return false;
82     }
83
84     public Element JavaDoc getElementById(String JavaDoc id) {
85         return doc.getElementById(id);
86     }
87
88     public boolean hasHandler() {
89         return doc.getDocumentElement().hasAttribute("handler");
90     }
91
92     public Tool getHandler() throws ToolException {
93         if (!hasHandler()) {
94             return null;
95         }
96
97         if (handler == null) {
98             String JavaDoc handlerClz = doc.getDocumentElement().getAttribute("handler");
99
100             try {
101                 handler = (Tool)Class.forName(handlerClz).newInstance();
102             } catch (Exception JavaDoc ex) {
103                 Message message = new Message("FAIL_TO_INSTANTIATE_HANDLER", LOG, handlerClz);
104                 throw new ToolException(message, ex);
105             }
106         }
107         return handler;
108     }
109
110     public Tool getHandler(ClassLoader JavaDoc loader) throws ToolException {
111         if (!hasHandler()) {
112             return null;
113         }
114
115         if (handler == null) {
116             String JavaDoc handlerClz = doc.getDocumentElement().getAttribute("handler");
117
118             try {
119                 handler = (Tool)Class.forName(handlerClz, true, loader).newInstance();
120             } catch (Exception JavaDoc ex) {
121                 Message message = new Message("FAIL_TO_INSTANTIATE_HANDLER", LOG, handlerClz);
122                 throw new ToolException(message, ex);
123             }
124         }
125         return handler;
126     }
127
128     public Element JavaDoc getStreams() {
129         NodeList JavaDoc nl = doc.getDocumentElement().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "streams");
130
131         if (nl.getLength() > 0) {
132             return (Element JavaDoc)nl.item(0);
133         } else {
134             return null;
135         }
136     }
137
138     public List JavaDoc getInstreamIds() {
139         List JavaDoc<Object JavaDoc> res = new ArrayList JavaDoc<Object JavaDoc>();
140         Element JavaDoc streams = getStreams();
141
142         if (streams != null) {
143             NodeList JavaDoc nl = streams.getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "instream");
144
145             for (int i = 0; i < nl.getLength(); i++) {
146                 res.add(((Element JavaDoc)nl.item(i)).getAttribute("id"));
147             }
148         }
149         return Collections.unmodifiableList(res);
150     }
151
152     public List JavaDoc getOutstreamIds() {
153         List JavaDoc<Object JavaDoc> res = new ArrayList JavaDoc<Object JavaDoc>();
154         Element JavaDoc streams = getStreams();
155
156         if (streams != null) {
157             NodeList JavaDoc nl = streams.getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "outstream");
158
159             for (int i = 0; i < nl.getLength(); i++) {
160                 res.add(((Element JavaDoc)nl.item(i)).getAttribute("id"));
161             }
162         }
163         return Collections.unmodifiableList(res);
164     }
165
166     public Element JavaDoc getUsage() {
167         return (Element JavaDoc)doc.getDocumentElement().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "usage")
168             .item(0);
169     }
170
171     public void transform(InputStream JavaDoc stylesheet, OutputStream JavaDoc out) throws TransformerException JavaDoc {
172         Transformer JavaDoc trans = TransformerFactory.newInstance().newTransformer(new StreamSource JavaDoc(stylesheet));
173         trans.transform(new DOMSource JavaDoc(doc), new StreamResult JavaDoc(out));
174     }
175
176     public Element JavaDoc getPipeline() {
177         NodeList JavaDoc nl = doc.getDocumentElement().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "pipeline");
178
179         if (nl.getLength() > 0) {
180             return (Element JavaDoc)nl.item(0);
181         } else {
182             return null;
183         }
184     }
185
186     public NodeList JavaDoc getUsageForms() {
187         return getUsage().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "form");
188     }
189
190     /**
191      * Arguments can have streamref attributes which associate them with a
192      * stream. Tools usually request streams and rely on them being ready. If an
193      * argument is given a streamref, then the container constructs a stream
194      * from the argument value. This would usually be a simple FileInputStream
195      * or FileOutputStream. The mechanics of this are left for the container to
196      * sort out, but that is the reason why this getter method exists.
197      */

198     public String JavaDoc getStreamRefName(String JavaDoc streamId) {
199         if (getUsage() != null) {
200             NodeList JavaDoc nl = getUsage().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "associatedArgument");
201
202             for (int i = 0; i < nl.getLength(); i++) {
203                 if (((Element JavaDoc)nl.item(i)).getAttribute("streamref").equals(streamId)) {
204                     return ((Element JavaDoc)nl.item(i).getParentNode()).getAttribute("id");
205                 }
206             }
207             nl = getUsage().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "argument");
208             for (int i = 0; i < nl.getLength(); i++) {
209                 if (((Element JavaDoc)nl.item(i)).getAttribute("streamref").equals(streamId)) {
210                     return ((Element JavaDoc)nl.item(i)).getAttribute("id");
211                 }
212             }
213         }
214         return null;
215     }
216
217     public String JavaDoc getParameterDefault(String JavaDoc name) {
218         Element JavaDoc el = getElementById(name);
219         if (LOG.isLoggable(Level.FINE)) {
220             LOG.fine("Element with id " + name + " is " + el);
221         }
222         if (el != null) {
223             if (LOG.isLoggable(Level.FINE)) {
224                 LOG.fine("local name is " + el.getLocalName());
225             }
226             if ("argument".equals(el.getLocalName())) {
227                 if (el.hasAttribute("default")) {
228                     return el.getAttribute("default");
229                 }
230             } else if ("option".equals(el.getLocalName())) {
231                 NodeList JavaDoc assArgs = el.getElementsByTagNameNS("http://www.xsume.com/Xpipe/ToolSpecification",
232                                                              "associatedArgument");
233
234                 if (assArgs.getLength() > 0) {
235                     Element JavaDoc assArg = (Element JavaDoc)assArgs.item(0);
236
237                     if (assArg.hasAttribute("default")) {
238                         return assArg.getAttribute("default");
239                     }
240                 }
241             }
242         }
243         return null;
244     }
245
246     public String JavaDoc getAnnotation() {
247         String JavaDoc result = null;
248         Element JavaDoc element = doc.getDocumentElement();
249         NodeList JavaDoc list = element.getChildNodes();
250
251         for (int i = 0; i < list.getLength(); i++) {
252             if ((list.item(i).getNodeType() == Node.ELEMENT_NODE)
253                 && ("annotation".equals(list.item(i).getNodeName()))) {
254                 result = list.item(i).getFirstChild().getNodeValue();
255                 break;
256             }
257         }
258         return result;
259     }
260
261 }
262
Popular Tags