/*********************************************************************************************************************** | |
* Copyright (c) 2010 Attensity Europe GmbH. | |
* All rights reserved. | |
* This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 | |
* which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html | |
**********************************************************************************************************************/ | |
package org.eclipse.smila.processing.designer.model.processor.util; | |
import java.util.Map; | |
import javax.wsdl.extensions.ExtensionRegistry; | |
import javax.xml.namespace.QName; | |
import org.eclipse.bpel.model.Activity; | |
import org.eclipse.bpel.model.Process; | |
import org.eclipse.bpel.model.extensions.BPELActivityDeserializer; | |
import org.eclipse.bpel.model.resource.BPELReader; | |
import org.eclipse.emf.common.util.URI; | |
import org.eclipse.smila.processing.designer.model.processor.*; | |
import org.eclipse.smila.processing.designer.model.record.*; | |
import org.eclipse.smila.processing.designer.model.record.util.RecordConstants; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.Node; | |
/** | |
* Reads the {@link InvokeService}s from the XML file. | |
* | |
* Copyright (c) 2010 Attensity Europe GmbH | |
* | |
* @author Lars Kunze | |
*/ | |
public class InvokeServiceDeserializer implements BPELActivityDeserializer | |
{ | |
@Override | |
public Activity unmarshall(QName elementType, Node node, Activity activity, Process process, | |
@SuppressWarnings("rawtypes") Map nsMap, ExtensionRegistry extReg, URI uri, BPELReader bpelReader) | |
{ | |
if (ProcessorConstants.NODE_INVOKE_SERVICE.equals(elementType.getLocalPart())) | |
{ | |
Element invokeServiceElement = (Element) node; | |
InvokeService invokeService; | |
if (activity instanceof InvokeService) | |
{ | |
// Use the existing model object | |
invokeService = (InvokeService) activity; | |
} | |
else | |
{ | |
// Create a new InvokeService model object | |
invokeService = ProcessorFactory.eINSTANCE.createInvokeService(); | |
// Attach the DOM node to our new InvokeService activity | |
invokeService.setElement((Element) node); | |
} | |
// Handle the child nodes | |
for (Node child = invokeServiceElement.getFirstChild(); child != null; child = child.getNextSibling()) | |
{ | |
if (child instanceof Element) | |
{ | |
Element element = (Element) child; | |
if (child.getNodeType() == Node.ELEMENT_NODE) | |
{ | |
if (ProcessorConstants.NODE_SERVICE.equals(element.getLocalName())) | |
{ | |
// (a) Handle node 'service' | |
invokeService.getService().setName(element.getAttribute(ProcessorConstants.ATT_NAME)); | |
invokeService.getService().setElement(element); | |
} | |
else if (ProcessorConstants.NODE_VARIABLES.equals(element.getLocalName())) | |
{ | |
// (b) Handle node 'variables' | |
invokeService.getVariables().setInput(element.getAttribute(ProcessorConstants.ATT_INPUT)); | |
invokeService.getVariables().setInput(element.getAttribute(ProcessorConstants.ATT_OUTPUT)); | |
invokeService.getVariables().setElement(element); | |
} | |
else if (ProcessorConstants.NODE_SET_ANNOTATIONS.equals(element.getLocalName())) | |
{ | |
// (c) Handle node 'setAnnotations' | |
SetAnnotations setAnnotations = invokeService.getSetAnnotations(); | |
// Disconnect from old element | |
setAnnotations.setElement(null); | |
// Read the new properties | |
setAnnotations.getAnnotations().clear(); | |
readAnnotations(setAnnotations, null, element); | |
// And connect to new element | |
setAnnotations.setElement(element); | |
} | |
} | |
} | |
} | |
return invokeService; | |
} | |
System.out.println("Cannot handle this kind of element"); | |
return null; | |
} | |
/** | |
* Recursive method to read the 'An' and 'V' nodes. | |
* | |
* @param setAnnotations The 'setAnnotations' object to which the read 'An' and 'V' nodes have to be added | |
* @param anParent The parent 'an' node | |
* @param nodeList The list of nodes to read | |
*/ | |
private void readAnnotations(SetAnnotations setAnnotations, Annotation annotationParent, Element elementSetAnnotation) | |
{ | |
for (Node child = elementSetAnnotation.getFirstChild(); child != null; child = child.getNextSibling()) | |
{ | |
if (child instanceof Element) | |
{ | |
Element element = (Element) child; | |
if (RecordConstants.NODE_AN.equals(element.getLocalName())) | |
{ | |
// Handle node 'An' | |
String attributeN = element.getAttribute(RecordConstants.ATT_N); | |
Annotation annotation = RecordFactory.eINSTANCE.createAnnotation(); | |
annotation.setElement(element); | |
annotation.setName(attributeN); | |
if (annotationParent == null) | |
{ | |
annotationParent = annotation; | |
setAnnotations.getAnnotations().add(annotation); | |
} | |
else | |
{ | |
annotationParent.getAnnotations().add(annotation); | |
} | |
if (element.hasChildNodes()) | |
{ | |
readAnnotations(setAnnotations, annotation, element); | |
} | |
} | |
else if (element.getLocalName().equals(RecordConstants.NODE_V)) | |
{ | |
// Handle node 'V' | |
String attributeN = element.getAttribute(RecordConstants.ATT_N); | |
AnnotationValue annotationValue = RecordFactory.eINSTANCE.createAnnotationValue(); | |
annotationValue.setElement(element); | |
annotationValue.setName(attributeN); | |
annotationValue.setValue(DOMUtils.readTextContent(element)); | |
annotationParent.getAnnotationValues().add(annotationValue); | |
} | |
} | |
} | |
} | |
} |