Ignore:
Timestamp:
03/20/15 16:00:10 (9 years ago)
Author:
sherbold
Message:
  • refactored and commented UMLUtils
  • created UTPUtils
  • moved DOM related methods from UMLUtils to SOAPUtils
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-plugin-http/src/main/java/de/ugoe/cs/autoquest/plugin/http/SOAPUtils.java

    r1928 r1929  
    1616 
    1717import java.io.StringWriter; 
     18import java.util.ArrayList; 
    1819import java.util.Iterator; 
    1920import java.util.LinkedList; 
     
    2728import javax.xml.transform.stream.StreamResult; 
    2829 
     30import org.w3c.dom.Attr; 
    2931import org.w3c.dom.Element; 
    3032import org.w3c.dom.Node; 
     33import org.w3c.dom.NodeList; 
    3134 
    3235import de.ugoe.cs.autoquest.eventcore.Event; 
     
    4346 */ 
    4447public class SOAPUtils { 
    45      
     48 
    4649    /** 
    4750     * <p> 
     
    6972                                                                      eventType.getServiceName(), 
    7073                                                                      eventType.getClientName(), 
    71                                                                       eventType.getSoapRequestBody()))); 
     74                                                                      eventType 
     75                                                                          .getSoapRequestBody()))); 
    7276                } 
    7377                else { 
     
    8084        return newSequence; 
    8185    } 
    82      
     86 
    8387    /** 
    8488     * <p> 
     
    8993     *            sequence where the events are replaced 
    9094     */ 
    91     public static void removeNonSOAPEvents(List<Event> sequence) 
    92     { 
     95    public static void removeNonSOAPEvents(List<Event> sequence) { 
    9396        for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { 
    9497            Event event = eventIter.next(); 
     
    188191        return requestBody; 
    189192    } 
    190      
    191     /** 
    192      * <p> 
    193      * returns the XML serialization of a DOM node; located here because it is used for SOAP request bodies 
     193 
     194    /** 
     195     * <p> 
     196     * returns the XML serialization of a DOM node; located here because it is used for SOAP request 
     197     * bodies 
    194198     * </p> 
    195199     *  
     
    199203     */ 
    200204    public static String getSerialization(Node node) { 
    201         if( node==null ) { 
     205        if (node == null) { 
    202206            return null; 
    203207        } 
     
    214218        } 
    215219    } 
    216      
     220 
     221    /** 
     222     * <p> 
     223     * Fetches all {@link Element}s that are direct children of the parent node, whose name matches 
     224     * the given name. 
     225     * </p> 
     226     *  
     227     * @param typeNameRaw 
     228     *            name of elements that are looked for 
     229     * @param parentNode 
     230     *            DOM node in which the elements are searched for 
     231     * @return found {@link Element}s 
     232     */ 
     233    public static List<Element> getMatchingChildNode(String typeNameRaw, Element parentNode) { 
     234        List<Element> matchingNodes = new ArrayList<>(); 
     235        Node parameterNode = null; 
     236        if (parentNode != null) { 
     237            NodeList parameterNodes = parentNode.getChildNodes(); 
     238            String[] typeNameSplit = typeNameRaw.split(":"); 
     239            String typeName = typeNameSplit[typeNameSplit.length - 1]; 
     240            for (int i = 0; i < parameterNodes.getLength(); i++) { 
     241                parameterNode = parameterNodes.item(i); 
     242                if (parameterNode.getNodeType() == Node.ELEMENT_NODE) { 
     243                    String[] parameterNodeSplit = parameterNode.getNodeName().split(":"); 
     244                    String parameterNodeName = parameterNodeSplit[parameterNodeSplit.length - 1]; 
     245                    if (typeName.equals(parameterNodeName)) { 
     246                        matchingNodes.add((Element) parameterNode); 
     247                    } 
     248                } 
     249            } 
     250        } 
     251        return matchingNodes; 
     252    } 
     253 
     254    /** 
     255     * <p> 
     256     * Returns the values found in a currentNode for a defined valueName. To this aim, this methods 
     257     * first determines if there is an attribute with the name "valueName". If this is the case, the 
     258     * value of this attribute is returned. Otherwise, the methods looks for {@link Element}s that 
     259     * are direct children of the provided DOM node with the given name and returns the text content 
     260     * of those nodes. 
     261     * </p> 
     262     * <p> 
     263     * In case no values can be found, an empty list is returned 
     264     *  
     265     * @param valueName 
     266     *            name of the value that is retrieved 
     267     * @param node 
     268     *            node for which the value is retrieved 
     269     * @return list of the found values. 
     270     */ 
     271    public static List<String> getValuesFromElement(String valueName, Element node) { 
     272        List<String> attributeValues = new LinkedList<>(); 
     273 
     274        if (node != null) { 
     275            // first check attributes of the node 
     276            Attr attribute = node.getAttributeNode(valueName); 
     277            if (attribute != null) { 
     278                attributeValues.add(attribute.getValue()); 
     279            } 
     280            else { 
     281                // now check elements 
     282                List<Element> elements = getMatchingChildNode(valueName, node); 
     283                for (Element element : elements) { 
     284                    attributeValues.add(element.getTextContent()); 
     285                } 
     286            } 
     287        } 
     288 
     289        return attributeValues; 
     290    } 
     291 
    217292    /** 
    218293     * <p> 
Note: See TracChangeset for help on using the changeset viewer.