summaryrefslogtreecommitdiffstats
path: root/wso2/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/util/BPELVariableInjectionUtil.java
blob: 2298bf183629705f84ec090adf017b3f15c75abb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
 * Copyright 2011
 * 
 * @author Uwe Breitenbuecher
 * 
 *         This class provides some methods for BPEL-Variable-Injection
 */
 package de.unistuttgart.iaas.bpel.util;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.ode.bpel.common.FaultException;
import org.apache.ode.bpel.runtime.extension.ExtensionContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class BPELVariableInjectionUtil {
	
	/**
	 * This method serializes a Node into a String
	 * 
	 * @param node
	 * @return String representation of the node
	 */
	public static String nodeToString(Node node) {
		try {
			
			if (node != null && node.getLocalName().equals("temporary-simple-type-wrapper")) {
				// this is a temporary hack for string variables and the likes,
				// as you may see ODE wrappes simpletypes in wrapper-elements,
				// but this isn't great here
				return node.getTextContent();
			}
			
			// Create transformer
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			
			// Transform Node into a String representation by regarding some
			// formatting rules
			StringWriter stringWriter = new StringWriter();
			transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
			transformer.transform(new DOMSource(node), new StreamResult(stringWriter));
			
			// Return build string
			return stringWriter.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// If any error occurs, return empty string
		return "";
	}
	
	/**
	 * This method executes the BPEL-Variable-Injection. It replaces referenced
	 * BPEL-Variables with corresponding content
	 * 
	 * @param context ExtensionContext of process
	 * @param element DOM-Representation of the BPEL-Code in which the
	 *            Variable-Injection has to be done
	 * @return modified BPEL-Code as DOM-Representation
	 */
	public static Element replaceExtensionVariables(ExtensionContext context, Element element) {
		
		try {
			String BPELCodeAsString;
			
			// Transform BPEL-Code (DOM-Representation) into a String
			BPELCodeAsString = nodeToString(element);
			
			// Find and replace referenced BPEL-Variables
			int startIndex = BPELCodeAsString.indexOf("$bpelvar[");
			if (startIndex != -1) {
				while (startIndex != -1) {
					int endIndex = startIndex;
					while (BPELCodeAsString.charAt(endIndex) != ']') {
						endIndex++;
					}
					
					// Extract name of referenced variable
					String variableName = BPELCodeAsString.substring(startIndex + 9, endIndex);
					
					// Extract content of referenced variable
					Node variableContent = context.readVariable(variableName);
					
					System.out.println("Replacing variable " + variableName + "(" + variableContent.getNamespaceURI() + " " + variableContent.getLocalName() + ") with content: \n");
					System.out.println("NodeValue(): " + variableContent.getNodeValue() + "\n");
					System.out.println("TextContent(): " + variableContent.getTextContent());
					System.out.println("The full bpel script (before change) as string: \n" + BPELCodeAsString + "\n");
					
					// Replace variable-reference with corresponding content
					BPELCodeAsString = BPELCodeAsString.replace("$bpelvar[" + variableName + "]", nodeToString(variableContent));
					
					System.out.println("The full bpel script as string: \n" + BPELCodeAsString + "\n");
					startIndex = BPELCodeAsString.indexOf("$bpelvar[");
				}
				
				// Transform modified code (String) into DOM-Representation
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				factory.setNamespaceAware(true);
				DocumentBuilder builder = factory.newDocumentBuilder();
				
				InputSource inputSource = new InputSource();
				inputSource.setCharacterStream(new StringReader(BPELCodeAsString));
				Document newDocument = builder.parse(inputSource);
				
				// Return first child (because Document root is not needed)
				return (Element) newDocument.getFirstChild();
				
			} else {
				
				// If no referenced variables are found, return original code
				return element;
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FaultException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
}