1
2
3
4
5
6
7
8 package net.kwfgrid.gworkflowdl.structure;
9
10 import org.apache.log4j.Logger;
11 import org.jdom.Document;
12 import org.jdom.Element;
13 import org.jdom.JDOMException;
14 import org.jdom.Namespace;
15 import org.jdom.input.SAXBuilder;
16 import org.jdom.output.Format;
17 import org.jdom.output.XMLOutputter;
18
19 import java.io.*;
20
21
22 /***
23 * Input and output of XML strings (and files)
24 * and transformation to and from JDom Elements.
25 */
26 public final class JdomString {
27
28 public static final String SCHEMA_LOCATION_PATH = System.getProperty("gworkflowdl.xsd.path", "http://www.gridworkflow.org/kwfgrid/src/xsd") + "/";
29
30 public static final String SCHEMA_LOCATION =
31 "http://www.gridworkflow.org/gworkflowdl "+ SCHEMA_LOCATION_PATH +"gworkflowdl_2_1.xsd "+
32 "http://www.gridworkflow.org/gworkflowdl/operationclass "+ SCHEMA_LOCATION_PATH + "gworkflowdl_operationclass_2_0.xsd";
33 public static final Namespace NAMESPACE_XSI = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
34 public static final Namespace NAMESPACE_XSD = Namespace.getNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
35 public static final Namespace NAMESPACE_SOAPENC = Namespace.getNamespace("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
36
37 private static final boolean ENABLE_VALIDATION = Boolean.valueOf(System.getProperty("gworkflowdl.xml.validation", "false"));
38
39 private static final String FEATURE_VALIDATION = "http://xml.org/sax/features/validation";
40 private static final String FEATURE_XSD = "http://apache.org/xml/features/validation/schema";
41 private static final String PROPERTY_XSD = "http://apache.org/xml/properties/schema/external-schemaLocation";
42
43 public static final Namespace wfSpace = Namespace.getNamespace("http://www.gridworkflow.org/gworkflowdl");
44 public static final Namespace ocSpace = Namespace.getNamespace("oc", "http://www.gridworkflow.org/gworkflowdl/operationclass");
45
46 public static final String targetNamespace = "http://www.gridworkflow.org/gworkflowdl";
47
48
49 /***
50 * forbidds JdomString object construction.
51 */
52 private JdomString() {
53 }
54
55 /***
56 * Jdom element to XML file.
57 *
58 * @param element JDom Element
59 * @param file XML output file
60 */
61 public static void element2xmlFile(final Element element, final String file) throws IOException {
62 Element elem = (Element) element.clone();
63 elem.detach();
64
65 final Document doc = new Document(elem);
66
67 final XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat());
68 outp.output(doc, new FileOutputStream(file));
69 }
70
71 /***
72 * Jdom Element to XML String.
73 *
74 * @param element JDom Element
75 * @return XML String
76 */
77 public static String element2string(final Element element) throws IOException {
78 Element elem = (Element) element.clone();
79 elem.detach();
80 final Document doc = new Document(elem);
81 return document2string(doc);
82 }
83
84 /***
85 * Jdom Document to XML String.
86 */
87 public static String document2string(final Document doc) throws IOException {
88 final Writer writer = new StringWriter();
89 final XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat());
90 outp.output(doc, writer);
91 return writer.toString();
92 }
93
94 /***
95 * Jdom Element to XML String. Strip the document header.
96 *
97 * @param element JDom Element
98 * @return XML String
99 */
100 public static String element2stringNoHeader(final Element element)
101 throws IOException {
102
103 Element elem = (Element) element.clone();
104 elem.detach();
105
106 final Writer writer = new StringWriter();
107 final XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat());
108 outp.output(elem, writer);
109
110 return writer.toString();
111
112 }
113
114 /***
115 * XML file to Jdom element.
116 *
117 * @param file XML file
118 * @return element JDom Element
119 * @throws JDOMException
120 * @throws IOException
121 */
122 public static Element workflowFile2element(final String file)
123 throws JDOMException, IOException {
124 Document doc;
125
126 final SAXBuilder builder = createSAXBuilderGWDL(true,true);
127 doc = builder.build(new File(file));
128 return doc.getRootElement();
129 }
130
131 /***
132 * XML String to Jdom document.
133 *
134 * @param string XML file
135 * @return JDom Element
136 * @throws WorkflowFormatException
137 */
138 public static Document string2document(final String string)
139 throws WorkflowFormatException {
140 Document doc;
141 try {
142 final StringReader reader = new StringReader(string);
143 final SAXBuilder builder = new SAXBuilder(false);
144
145
146
147
148
149 doc = builder.build(reader);
150 } catch (IOException ioe) {
151 throw new WorkflowFormatException(ioe.toString(), ioe);
152 } catch (JDOMException jde) {
153 throw new WorkflowFormatException(jde.toString(), jde);
154 }
155
156 return doc;
157 }
158
159 /***
160 * XML String to Jdom element.
161 *
162 * @param string XML file
163 * @return JDom Element
164 * @throws WorkflowFormatException
165 */
166 public static Element string2element(final String string)
167 throws WorkflowFormatException {
168 Document doc;
169 try {
170 final StringReader reader = new StringReader(string);
171
172 final SAXBuilder builder = new SAXBuilder(false);
173
174
175
176
177
178 doc = builder.build(reader);
179 } catch (IOException ioe) {
180 throw new WorkflowFormatException(ioe.toString(), ioe);
181 } catch (JDOMException jde) {
182 throw new WorkflowFormatException(jde.toString(), jde);
183 }
184
185 return doc.getRootElement();
186 }
187
188
189 /***
190 * Workflow to XML String.
191 *
192 * @param wf Workflow
193 * @return XML Workflow description
194 * @throws IOException
195 */
196 public static String workflow2string(final Workflow wf)
197 throws IOException {
198 return element2string(WorkflowJdom.java2element(wf));
199 }
200
201
202 /***
203 * XML Workflow description string to Workflow.
204 *
205 * @param string XML Workflow description
206 * @return new Workflow
207 * @throws WorkflowFormatException
208 *
209 */
210 public static Workflow string2workflow(final String string) throws WorkflowFormatException, CapacityException {
211 Element wfe = string2verifyWorkflowElement(string);
212 return WorkflowJdom.element2java(wfe);
213 }
214
215 /***
216 Copy the workflow representation in the specified GWorkflowDL String into the
217 specified Workflow instance. All previous contents of <code>target</code> will be overwritten.
218 @param gworkflowdl The XML represenation of the workflow in GWorkflowDL syntax.
219 @param target The workflow.
220 */
221 public static void string2workflow(String gworkflowdl, Workflow target) throws WorkflowFormatException, CapacityException {
222 Element wfe = string2verifyWorkflowElement(gworkflowdl);
223 WorkflowJdom.element2java(wfe, target);
224 }
225
226 /***
227 * verifies String wfxml by xsd and returns corresponding Document
228 *
229 * @param wfxml XML description of workflow
230 * @return JDOM Document
231 * @throws WorkflowFormatException
232 *
233 */
234 public static Element string2verifyWorkflowElement(final String wfxml)
235 throws WorkflowFormatException {
236
237 final SAXBuilder builder = createSAXBuilderGWDL(true,true);
238 Document doc;
239
240 try {
241 doc = builder.build(new StringReader(wfxml));
242 } catch (IOException ioe) {
243 throw new WorkflowFormatException(ioe.toString(), ioe);
244 } catch (JDOMException jde) {
245 throw new WorkflowFormatException(jde.toString(), jde);
246 }
247
248 return doc.getRootElement();
249 }
250
251 public static Document string2workflowDocument(final String xml,boolean useXsd, boolean useDefaultXsdLocation) throws IOException, JDOMException {
252 final SAXBuilder builder = createSAXBuilderGWDL(useXsd, useDefaultXsdLocation);
253 return builder.build(new StringReader(xml));
254 }
255
256 private static SAXBuilder createSAXBuilderGWDL(boolean useXsd, boolean useDefaultXsdLocation) {
257 final SAXBuilder builder = new SAXBuilder(true);
258 builder.setFeature(FEATURE_VALIDATION, ENABLE_VALIDATION);
259 Logger.getLogger(JdomString.class).info("XML Validation is "+ (ENABLE_VALIDATION ? "enabled." : "disabled."));
260 if (useXsd) {
261 builder.setFeature(FEATURE_XSD, true);
262 if (useDefaultXsdLocation) builder.setProperty(PROPERTY_XSD, SCHEMA_LOCATION);
263 }
264 return builder;
265 }
266
267 }
268
269