workaroud for snor issue
[vfc/nfvo/wfengine.git] / wso2 / wso2bpel-ext / wso2bpel-core / BPEL4RESTLight / src / main / java / de / unistuttgart / iaas / bpel / util / BPELVariableInjectionUtil.java
1 /**
2  * Copyright 2011
3  * 
4  * @author Uwe Breitenbuecher
5  * 
6  *         This class provides some methods for BPEL-Variable-Injection
7  */
8  package de.unistuttgart.iaas.bpel.util;
9
10 import java.io.IOException;
11 import java.io.StringReader;
12 import java.io.StringWriter;
13
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;
22
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;
30
31
32 public class BPELVariableInjectionUtil {
33         
34         /**
35          * This method serializes a Node into a String
36          * 
37          * @param node
38          * @return String representation of the node
39          */
40         public static String nodeToString(Node node) {
41                 try {
42                         
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();
48                         }
49                         
50                         // Create transformer
51                         TransformerFactory transformerFactory = TransformerFactory.newInstance();
52                         Transformer transformer = transformerFactory.newTransformer();
53                         
54                         // Transform Node into a String representation by regarding some
55                         // formatting rules
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));
61                         
62                         // Return build string
63                         return stringWriter.toString();
64                 } catch (Exception e) {
65                         e.printStackTrace();
66                 }
67                 
68                 // If any error occurs, return empty string
69                 return "";
70         }
71         
72         /**
73          * This method executes the BPEL-Variable-Injection. It replaces referenced
74          * BPEL-Variables with corresponding content
75          * 
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
80          */
81         public static Element replaceExtensionVariables(ExtensionContext context, Element element) {
82                 
83                 try {
84                         String BPELCodeAsString;
85                         
86                         // Transform BPEL-Code (DOM-Representation) into a String
87                         BPELCodeAsString = nodeToString(element);
88                         
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) != ']') {
95                                                 endIndex++;
96                                         }
97                                         
98                                         // Extract name of referenced variable
99                                         String variableName = BPELCodeAsString.substring(startIndex + 9, endIndex);
100                                         
101                                         // Extract content of referenced variable
102                                         Node variableContent = context.readVariable(variableName);
103                                         
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");
108                                         
109                                         // Replace variable-reference with corresponding content
110                                         BPELCodeAsString = BPELCodeAsString.replace("$bpelvar[" + variableName + "]", nodeToString(variableContent));
111                                         
112                                         System.out.println("The full bpel script as string: \n" + BPELCodeAsString + "\n");
113                                         startIndex = BPELCodeAsString.indexOf("$bpelvar[");
114                                 }
115                                 
116                                 // Transform modified code (String) into DOM-Representation
117                                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
118                                 factory.setNamespaceAware(true);
119                                 DocumentBuilder builder = factory.newDocumentBuilder();
120                                 
121                                 InputSource inputSource = new InputSource();
122                                 inputSource.setCharacterStream(new StringReader(BPELCodeAsString));
123                                 Document newDocument = builder.parse(inputSource);
124                                 
125                                 // Return first child (because Document root is not needed)
126                                 return (Element) newDocument.getFirstChild();
127                                 
128                         } else {
129                                 
130                                 // If no referenced variables are found, return original code
131                                 return element;
132                         }
133                 } catch (ParserConfigurationException e) {
134                         e.printStackTrace();
135                 } catch (SAXException e) {
136                         // TODO Auto-generated catch block
137                         e.printStackTrace();
138                 } catch (IOException e) {
139                         // TODO Auto-generated catch block
140                         e.printStackTrace();
141                 } catch (FaultException e) {
142                         // TODO Auto-generated catch block
143                         e.printStackTrace();
144                 }
145                 
146                 return null;
147         }
148         
149 }