4 * @author Uwe Breitenbuecher
6 * This class provides some methods for BPEL-Variable-Injection
8 package de.unistuttgart.iaas.bpel.util;
10 import java.io.IOException;
11 import java.io.StringReader;
12 import java.io.StringWriter;
14 import javax.xml.parsers.DocumentBuilder;
15 import javax.xml.parsers.DocumentBuilderFactory;
16 import javax.xml.parsers.ParserConfigurationException;
17 import javax.xml.transform.OutputKeys;
18 import javax.xml.transform.Transformer;
19 import javax.xml.transform.TransformerFactory;
20 import javax.xml.transform.dom.DOMSource;
21 import javax.xml.transform.stream.StreamResult;
23 import org.apache.ode.bpel.common.FaultException;
24 import org.apache.ode.bpel.runtime.extension.ExtensionContext;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.w3c.dom.Node;
28 import org.xml.sax.InputSource;
29 import org.xml.sax.SAXException;
32 public class BPELVariableInjectionUtil {
35 * This method serializes a Node into a String
38 * @return String representation of the node
40 public static String nodeToString(Node node) {
43 if (node != null && node.getLocalName().equals("temporary-simple-type-wrapper")) {
44 // this is a temporary hack for string variables and the likes,
45 // as you may see ODE wrappes simpletypes in wrapper-elements,
46 // but this isn't great here
47 return node.getTextContent();
51 TransformerFactory transformerFactory = TransformerFactory.newInstance();
52 Transformer transformer = transformerFactory.newTransformer();
54 // Transform Node into a String representation by regarding some
56 StringWriter stringWriter = new StringWriter();
57 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
58 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
59 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
60 transformer.transform(new DOMSource(node), new StreamResult(stringWriter));
62 // Return build string
63 return stringWriter.toString();
64 } catch (Exception e) {
68 // If any error occurs, return empty string
73 * This method executes the BPEL-Variable-Injection. It replaces referenced
74 * BPEL-Variables with corresponding content
76 * @param context ExtensionContext of process
77 * @param element DOM-Representation of the BPEL-Code in which the
78 * Variable-Injection has to be done
79 * @return modified BPEL-Code as DOM-Representation
81 public static Element replaceExtensionVariables(ExtensionContext context, Element element) {
84 String BPELCodeAsString;
86 // Transform BPEL-Code (DOM-Representation) into a String
87 BPELCodeAsString = nodeToString(element);
89 // Find and replace referenced BPEL-Variables
90 int startIndex = BPELCodeAsString.indexOf("$bpelvar[");
91 if (startIndex != -1) {
92 while (startIndex != -1) {
93 int endIndex = startIndex;
94 while (BPELCodeAsString.charAt(endIndex) != ']') {
98 // Extract name of referenced variable
99 String variableName = BPELCodeAsString.substring(startIndex + 9, endIndex);
101 // Extract content of referenced variable
102 Node variableContent = context.readVariable(variableName);
104 System.out.println("Replacing variable " + variableName + "(" + variableContent.getNamespaceURI() + " " + variableContent.getLocalName() + ") with content: \n");
105 System.out.println("NodeValue(): " + variableContent.getNodeValue() + "\n");
106 System.out.println("TextContent(): " + variableContent.getTextContent());
107 System.out.println("The full bpel script (before change) as string: \n" + BPELCodeAsString + "\n");
109 // Replace variable-reference with corresponding content
110 BPELCodeAsString = BPELCodeAsString.replace("$bpelvar[" + variableName + "]", nodeToString(variableContent));
112 System.out.println("The full bpel script as string: \n" + BPELCodeAsString + "\n");
113 startIndex = BPELCodeAsString.indexOf("$bpelvar[");
116 // Transform modified code (String) into DOM-Representation
117 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
118 factory.setNamespaceAware(true);
119 DocumentBuilder builder = factory.newDocumentBuilder();
121 InputSource inputSource = new InputSource();
122 inputSource.setCharacterStream(new StringReader(BPELCodeAsString));
123 Document newDocument = builder.parse(inputSource);
125 // Return first child (because Document root is not needed)
126 return (Element) newDocument.getFirstChild();
130 // If no referenced variables are found, return original code
133 } catch (ParserConfigurationException e) {
135 } catch (SAXException e) {
136 // TODO Auto-generated catch block
138 } catch (IOException e) {
139 // TODO Auto-generated catch block
141 } catch (FaultException e) {
142 // TODO Auto-generated catch block