001: package org.enhydra.shark.toolagent;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.InputStream;
006: import java.io.StringReader;
007: import java.io.StringWriter;
008: import java.net.URL;
009: import java.net.URLConnection;
010:
011: import javax.xml.transform.Result;
012: import javax.xml.transform.Source;
013: import javax.xml.transform.Transformer;
014: import javax.xml.transform.TransformerFactory;
015: import javax.xml.transform.dom.DOMResult;
016: import javax.xml.transform.dom.DOMSource;
017: import javax.xml.transform.stream.StreamResult;
018: import javax.xml.transform.stream.StreamSource;
019:
020: import org.enhydra.shark.Shark;
021: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
022: import org.enhydra.shark.api.client.wfservice.SharkConnection;
023: import org.enhydra.shark.api.client.wfservice.WMEntity;
024: import org.enhydra.shark.api.internal.toolagent.AppParameter;
025: import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
026: import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
027: import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
028: import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
029: import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
030: import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
031: import org.w3c.dom.Node;
032:
033: /**
034: * XSLTToolAgent.
035: *
036: * @author Sasa Bojanic
037: * @author Svjetlana Milidrag
038: */
039: public class XSLTToolAgent extends AbstractToolAgent {
040:
041: public static final String SCRIPT_EXT_ATTR_NAME = "Script";
042:
043: public static final String TRANSFORMER_NAME = "transformer_name"; // xsl name
044:
045: public static final String TRANSFORMER_PATH = "transformer_path"; // xsl path
046:
047: public static final String TRANSFORMER_NODE = "transformer_node"; // xsl node
048:
049: public static final String TRANSFORMER_SCRIPT = "transformer_script"; // xsl node
050:
051: public static final String SOURCE = "source";
052:
053: public static final String RESULT = "result";
054:
055: public static final String RESULT_TYPE = "result_type";
056:
057: protected ByteArrayOutputStream bout = null;
058:
059: protected StringWriter sw = null;
060:
061: protected String outParamType = null;
062:
063: public void invokeApplication(WMSessionHandle _shandle,
064: long _handle, WMEntity _appInfo, WMEntity _toolInfo,
065: String _applicationName, String _procInstId, String _assId,
066: AppParameter[] _parameters, Integer _appMode)
067: throws ApplicationNotStarted, ApplicationNotDefined,
068: ApplicationBusy, ToolAgentGeneralException {
069: super .invokeApplication(_shandle, _handle, _appInfo, _toolInfo,
070: _applicationName, _procInstId, _assId, _parameters,
071: _appMode);
072: status = APP_STATUS_RUNNING;
073: try {
074:
075: Transformer xsltTrans = null;
076:
077: SharkConnection sc = Shark.getInstance()
078: .getSharkConnection();
079:
080: sc.attachToHandle(_shandle);
081:
082: xsltTrans = initTransformer();
083:
084: Source src = initSource();
085:
086: AppParameter outParam = getParameterByName(this .parameters,
087: RESULT);
088:
089: Result res = initResult(outParam);
090:
091: initParameters(xsltTrans);
092:
093: xsltTrans.transform(src, res);
094:
095: if (outParamType.equalsIgnoreCase("byte[]")) {
096: outParam.the_value = bout.toByteArray();
097: bout.close();
098: } else if (outParamType.equalsIgnoreCase("String")) {
099: outParam.the_value = sw.toString();
100: sw.close();
101: } else if (outParamType.equalsIgnoreCase("Node")) {
102: outParam.the_value = ((DOMResult) res).getNode();
103:
104: }
105: status = APP_STATUS_FINISHED;
106: } catch (Throwable e) {
107: status = APP_STATUS_TERMINATED;
108: if (e instanceof ApplicationNotStarted) {
109: throw (ApplicationNotStarted) e;
110: } else if (e instanceof ApplicationNotDefined) {
111: throw (ApplicationNotDefined) e;
112: } else if (e instanceof ApplicationBusy) {
113: throw (ApplicationBusy) e;
114: } else if (e instanceof ToolAgentGeneralException) {
115: throw (ToolAgentGeneralException) e;
116: } else {
117: throw new ToolAgentGeneralException(e);
118: }
119: }
120: }
121:
122: protected void initParameters(Transformer xsltTrans)
123: throws Exception {
124:
125: if (this .parameters != null) {
126: for (int i = 1; i < this .parameters.length; i++) {
127: String name = this .parameters[i].the_formal_name;
128: if (!name.equals(TRANSFORMER_NAME)
129: && !name.equals(TRANSFORMER_PATH)
130: && !name.equals(TRANSFORMER_NODE)
131: && !name.equals(TRANSFORMER_SCRIPT)
132: && !name.equals(SOURCE) && !name.equals(RESULT)
133: && !name.equals(RESULT_TYPE)) {
134: xsltTrans.setParameter(name,
135: this .parameters[i].the_value);
136: }
137: }
138: }
139:
140: }
141:
142: protected Result initResult(AppParameter param) throws Exception {
143: Result res = null;
144:
145: if (param != null) {
146: Class resultClass = param.the_class;
147: if (resultClass.equals(String.class)) {
148: outParamType = "String";
149: sw = new StringWriter();
150: res = new StreamResult(sw);
151: } else if (resultClass.equals(Node.class)) {
152: outParamType = "Node";
153: res = new DOMResult();
154: } else if (resultClass.equals(byte[].class)) {
155: outParamType = "byte[]";
156: bout = new ByteArrayOutputStream();
157: res = new StreamResult(bout);
158: } else if (resultClass.equals(Object.class)) {
159: AppParameter param1 = getParameterByName(
160: this .parameters, RESULT_TYPE);
161: if (param1 != null) {
162: String type = param1.the_value.toString();
163: outParamType = type;
164: if (type.equalsIgnoreCase("String")) {
165: sw = new StringWriter();
166: res = new StreamResult(sw);
167: } else if (type.equalsIgnoreCase("byte[]")) {
168: bout = new ByteArrayOutputStream();
169: res = new StreamResult(bout);
170: } else if (type.equalsIgnoreCase("Node")) {
171: res = new DOMResult();
172: }
173: } else {
174: throw new Exception(
175: "No type definition for Object!");
176: }
177:
178: }
179:
180: } else {
181: throw new Exception("There is no output parameter!");
182: }
183: return res;
184: }
185:
186: protected Source initSource() throws Exception {
187:
188: AppParameter param = getParameterByName(this .parameters, SOURCE);
189: Object in = null;
190:
191: if (param != null) {
192: in = param.the_value;
193:
194: } else {
195: in = Shark.getInstance().getXPILHandler()
196: .getProcessVariables(this .shandle, this .procInstId,
197: null);
198:
199: }
200: Source src = null;
201: if (in instanceof byte[]) {
202: src = new StreamSource(
203: new ByteArrayInputStream((byte[]) in));
204: } else if (in instanceof String) {
205: src = new StreamSource(new StringReader((String) in));
206: } else if (in instanceof Node) {
207: src = new DOMSource((Node) in);
208: }
209: return src;
210:
211: }
212:
213: protected Transformer initTransformer() throws Exception {
214: Transformer ret = null;
215:
216: AppParameter param = getParameterByName(this .parameters,
217: TRANSFORMER_NAME);
218:
219: if (param != null) {
220: ret = getTransformerByName((String) param.the_value);
221: }
222: if (ret == null) {
223: param = getParameterByName(this .parameters,
224: TRANSFORMER_PATH);
225: if (param != null) {
226: ret = getTransformerByPath((String) param.the_value);
227: }
228: }
229: if (ret == null) {
230: param = getParameterByName(this .parameters,
231: TRANSFORMER_NODE);
232: if (param != null) {
233: ret = getTransformerByNode((Node) param.the_value);
234: }
235: }
236: if (ret == null) {
237: param = getParameterByName(this .parameters,
238: TRANSFORMER_SCRIPT);
239: if (param != null) {
240: ret = getTransformerByScript((String) param.the_value);
241: }
242: }
243:
244: if (ret == null) {
245: ret = getTransformerByExtendedAttribute();
246: }
247:
248: return ret;
249: }
250:
251: protected Transformer getTransformerByName(String name)
252: throws Exception {
253: Transformer ret = null;
254: try {
255: URL u = XSLTToolAgent.class.getClassLoader().getResource(
256: name);
257: URLConnection urlConnection = u.openConnection();
258: InputStream is = urlConnection.getInputStream();
259:
260: ret = TransformerFactory.newInstance().newTransformer(
261: new StreamSource(is));
262: } catch (Exception ex) {
263: throw new Exception(
264: "There is no specified resource on classpath!");
265: }
266:
267: return ret;
268:
269: }
270:
271: protected Transformer getTransformerByPath(String path)
272: throws Exception {
273: Transformer ret = null;
274:
275: ret = TransformerFactory.newInstance().newTransformer(
276: new StreamSource(path));
277:
278: return ret;
279: }
280:
281: protected Transformer getTransformerByNode(Node node)
282: throws Exception {
283: Transformer ret = null;
284:
285: ret = TransformerFactory.newInstance().newTransformer(
286: new DOMSource(node));
287:
288: return ret;
289: }
290:
291: protected Transformer getTransformerByScript(String script)
292: throws Exception {
293: Transformer ret = null;
294: if (script != null) {
295: byte[] bytes = script.getBytes("UTF8");
296: InputStream is = new ByteArrayInputStream(bytes);
297:
298: ret = TransformerFactory.newInstance().newTransformer(
299: new StreamSource(is));
300: }
301: return ret;
302: }
303:
304: protected Transformer getTransformerByExtendedAttribute()
305: throws Exception {
306: Transformer ret = null;
307:
308: ExtendedAttributes eas = super
309: .readParamsFromExtAttributes((String) parameters[0].the_value);
310: ExtendedAttribute ea = eas
311: .getFirstExtendedAttributeForName(XSLTToolAgent.SCRIPT_EXT_ATTR_NAME);
312: if (ea != null) {
313: String script = ea.getVValue();
314: byte[] bytes = script.getBytes("UTF8");
315: InputStream is = new ByteArrayInputStream(bytes);
316:
317: ret = TransformerFactory.newInstance().newTransformer(
318: new StreamSource(is));
319: }
320:
321: return ret;
322: }
323:
324: }
|