1 package org.objectweb.celtix.tools.common.toolspec; 2 3 import java.io.InputStream ; 4 import java.io.OutputStream ; 5 import java.util.ArrayList ; 6 import java.util.Collections ; 7 import java.util.List ; 8 import java.util.logging.Level ; 9 import java.util.logging.Logger ; 10 11 import javax.xml.transform.Transformer ; 12 import javax.xml.transform.TransformerException ; 13 import javax.xml.transform.TransformerFactory ; 14 import javax.xml.transform.dom.DOMSource ; 15 import javax.xml.transform.stream.StreamResult ; 16 import javax.xml.transform.stream.StreamSource ; 17 18 import org.w3c.dom.Document ; 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.NodeList ; 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 LOG = LogUtils.getL7dLogger(ToolSpec.class); 31 32 private final ExtendedDocumentBuilder builder = new ExtendedDocumentBuilder(); 33 private Document doc; 34 private Tool handler; 35 36 public ToolSpec() { 37 } 38 39 public ToolSpec(InputStream in) throws ToolException { 40 this(in, true); 41 } 42 43 public ToolSpec(InputStream in, boolean validate) throws ToolException { 44 if (in == null) { 45 throw new NullPointerException ("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 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 d) { 57 if (d == null) { 58 throw new NullPointerException ("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 id) { 69 Element streams = getStreams(); 70 71 if (streams == null) { 72 return false; 73 } 74 NodeList nl = streams.getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "instream"); 75 76 for (int i = 0; i < nl.getLength(); i++) { 77 if (((Element )nl.item(i)).getAttribute("id").equals(id)) { 78 return true; 79 } 80 } 81 return false; 82 } 83 84 public Element getElementById(String 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 handlerClz = doc.getDocumentElement().getAttribute("handler"); 99 100 try { 101 handler = (Tool)Class.forName(handlerClz).newInstance(); 102 } catch (Exception 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 loader) throws ToolException { 111 if (!hasHandler()) { 112 return null; 113 } 114 115 if (handler == null) { 116 String handlerClz = doc.getDocumentElement().getAttribute("handler"); 117 118 try { 119 handler = (Tool)Class.forName(handlerClz, true, loader).newInstance(); 120 } catch (Exception 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 getStreams() { 129 NodeList nl = doc.getDocumentElement().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "streams"); 130 131 if (nl.getLength() > 0) { 132 return (Element )nl.item(0); 133 } else { 134 return null; 135 } 136 } 137 138 public List getInstreamIds() { 139 List <Object > res = new ArrayList <Object >(); 140 Element streams = getStreams(); 141 142 if (streams != null) { 143 NodeList nl = streams.getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "instream"); 144 145 for (int i = 0; i < nl.getLength(); i++) { 146 res.add(((Element )nl.item(i)).getAttribute("id")); 147 } 148 } 149 return Collections.unmodifiableList(res); 150 } 151 152 public List getOutstreamIds() { 153 List <Object > res = new ArrayList <Object >(); 154 Element streams = getStreams(); 155 156 if (streams != null) { 157 NodeList nl = streams.getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "outstream"); 158 159 for (int i = 0; i < nl.getLength(); i++) { 160 res.add(((Element )nl.item(i)).getAttribute("id")); 161 } 162 } 163 return Collections.unmodifiableList(res); 164 } 165 166 public Element getUsage() { 167 return (Element )doc.getDocumentElement().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "usage") 168 .item(0); 169 } 170 171 public void transform(InputStream stylesheet, OutputStream out) throws TransformerException { 172 Transformer trans = TransformerFactory.newInstance().newTransformer(new StreamSource (stylesheet)); 173 trans.transform(new DOMSource (doc), new StreamResult (out)); 174 } 175 176 public Element getPipeline() { 177 NodeList nl = doc.getDocumentElement().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "pipeline"); 178 179 if (nl.getLength() > 0) { 180 return (Element )nl.item(0); 181 } else { 182 return null; 183 } 184 } 185 186 public NodeList getUsageForms() { 187 return getUsage().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "form"); 188 } 189 190 198 public String getStreamRefName(String streamId) { 199 if (getUsage() != null) { 200 NodeList nl = getUsage().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "associatedArgument"); 201 202 for (int i = 0; i < nl.getLength(); i++) { 203 if (((Element )nl.item(i)).getAttribute("streamref").equals(streamId)) { 204 return ((Element )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 )nl.item(i)).getAttribute("streamref").equals(streamId)) { 210 return ((Element )nl.item(i)).getAttribute("id"); 211 } 212 } 213 } 214 return null; 215 } 216 217 public String getParameterDefault(String name) { 218 Element 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 assArgs = el.getElementsByTagNameNS("http://www.xsume.com/Xpipe/ToolSpecification", 232 "associatedArgument"); 233 234 if (assArgs.getLength() > 0) { 235 Element assArg = (Element )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 getAnnotation() { 247 String result = null; 248 Element element = doc.getDocumentElement(); 249 NodeList 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 |