1 package fr.in2p3.jsaga.impl.job.description;
2
3 import fr.in2p3.jsaga.adaptor.language.SAGALanguageAdaptor;
4 import fr.in2p3.jsaga.helpers.xslt.XSLTransformer;
5 import fr.in2p3.jsaga.helpers.xslt.XSLTransformerFactory;
6 import org.ogf.saga.error.NoSuccessException;
7 import org.ogf.saga.job.JobDescription;
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10
11 import javax.xml.parsers.DocumentBuilderFactory;
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class SAGAJobDescriptionImpl extends AbstractJobDescriptionImpl implements JobDescription {
26 private static SAGALanguageAdaptor s_adaptor = new SAGALanguageAdaptor();
27 static {
28 s_adaptor.initParser();
29 }
30
31 public Document getAsDocument() throws NoSuccessException {
32
33 Document document;
34 try {
35 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
36 Element root = document.createElement("attributes");
37 document.appendChild(root);
38 String[] attributeNames = super.listAttributes();
39 for (int i=0; i<attributeNames.length; i++) {
40 String key = attributeNames[i];
41 Element elem = document.createElement(key);
42 if (super.isVectorAttribute(key)) {
43 String[] values = super.getVectorAttribute(key);
44 for (int v=0; v<values.length; v++) {
45 Element item = document.createElement("value");
46 item.appendChild(document.createTextNode(values[v]));
47 elem.appendChild(item);
48 }
49 } else {
50 String value = super.getAttribute(key);
51 elem.setAttribute("value", value);
52 }
53 root.appendChild(elem);
54 }
55 } catch (NoSuccessException e) {
56 throw e;
57 } catch (Exception e) {
58 throw new NoSuccessException(e);
59 }
60
61
62 String stylesheet = s_adaptor.getTranslator();
63 if (stylesheet == null) {
64 throw new NoSuccessException("[INTERNAL ERROR] Stylesheet is null");
65 }
66 try {
67 XSLTransformer t = XSLTransformerFactory.getInstance().getCached(stylesheet);
68 return t.transformToDOM(document);
69 } catch (Exception e) {
70 throw new NoSuccessException(e);
71 }
72 }
73 }