org.objectweb.celtix.tools.common.toolspec.ToolSpec Maven / Gradle / Ivy
The newest version!
package org.objectweb.celtix.tools.common.toolspec;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.objectweb.celtix.common.i18n.Message;
import org.objectweb.celtix.common.logging.LogUtils;
import org.objectweb.celtix.tools.common.ToolException;
import org.objectweb.celtix.tools.common.dom.ExtendedDocumentBuilder;
public class ToolSpec {
private static final Logger LOG = LogUtils.getL7dLogger(ToolSpec.class);
private final ExtendedDocumentBuilder builder = new ExtendedDocumentBuilder();
private Document doc;
private Tool handler;
public ToolSpec() {
}
public ToolSpec(InputStream in) throws ToolException {
this(in, true);
}
public ToolSpec(InputStream in, boolean validate) throws ToolException {
if (in == null) {
throw new NullPointerException("Cannot create a ToolSpec object from a null stream");
}
try {
builder.setValidating(validate);
this.doc = builder.parse(in);
} catch (Exception ex) {
Message message = new Message("FAIL_TO_PARSING_TOOLSPCE_STREAM", LOG);
throw new ToolException(message, ex);
}
}
public ToolSpec(Document d) {
if (d == null) {
throw new NullPointerException("Cannot create a ToolSpec object from "
+ "a null org.w3c.dom.Document");
}
this.doc = d;
}
public ExtendedDocumentBuilder getDocumentBuilder() {
return builder;
}
public boolean isValidInputStream(String id) {
Element streams = getStreams();
if (streams == null) {
return false;
}
NodeList nl = streams.getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "instream");
for (int i = 0; i < nl.getLength(); i++) {
if (((Element)nl.item(i)).getAttribute("id").equals(id)) {
return true;
}
}
return false;
}
public Element getElementById(String id) {
return doc.getElementById(id);
}
public boolean hasHandler() {
return doc.getDocumentElement().hasAttribute("handler");
}
public Tool getHandler() throws ToolException {
if (!hasHandler()) {
return null;
}
if (handler == null) {
String handlerClz = doc.getDocumentElement().getAttribute("handler");
try {
handler = (Tool)Class.forName(handlerClz).newInstance();
} catch (Exception ex) {
Message message = new Message("FAIL_TO_INSTANTIATE_HANDLER", LOG, handlerClz);
throw new ToolException(message, ex);
}
}
return handler;
}
public Tool getHandler(ClassLoader loader) throws ToolException {
if (!hasHandler()) {
return null;
}
if (handler == null) {
String handlerClz = doc.getDocumentElement().getAttribute("handler");
try {
handler = (Tool)Class.forName(handlerClz, true, loader).newInstance();
} catch (Exception ex) {
Message message = new Message("FAIL_TO_INSTANTIATE_HANDLER", LOG, handlerClz);
throw new ToolException(message, ex);
}
}
return handler;
}
public Element getStreams() {
NodeList nl = doc.getDocumentElement().getElementsByTagNameNS(Tool.TOOL_SPEC_PUBLIC_ID, "streams");
if (nl.getLength() > 0) {
return (Element)nl.item(0);
} else {
return null;
}
}
public List getInstreamIds() {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy