InvocationResultSerializer.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.management.wsdm.muse.serializer;
022 
023 import java.util.Map;
024 
025 import javax.xml.namespace.QName;
026 
027 import org.apache.muse.core.serializer.Serializer;
028 import org.apache.muse.core.serializer.SerializerRegistry;
029 import org.apache.muse.util.xml.XmlUtils;
030 import org.apache.muse.ws.addressing.soap.SoapFault;
031 import org.apache.qpid.management.wsdm.capabilities.Result;
032 import org.w3c.dom.Element;
033 
034 /**
035  * Implementation of Muse Serializer for Result type.
036  *  
037  * @author Andrea Gazzarini
038  */
039 public class InvocationResultSerializer implements Serializer 
040 {  
041   private Serializer _longSerializer = SerializerRegistry.getInstance().getSerializer(long.class);
042   private Serializer _stringSerializer = SerializerRegistry.getInstance().getSerializer(String.class);
043   private Serializer _mapSerializer = SerializerRegistry.getInstance().getSerializer(Map.class);
044   
045   /**
046    * Return a UUID representation of the given xml element.
047    * 
048    * @param xml the element to unmarshal.
049    * @throws SoapFault when the unmarshalling fails.
050    */  
051   @SuppressWarnings("unchecked")
052   public Object fromXML(Element elementData) throws SoapFault 
053   {
054     long statusCode = 0;
055     String statusText = null;
056     Map<String, Object> outputSection = null;
057     
058     Element[] elements = XmlUtils.getAllElements(elementData);
059     for (Element element : elements)
060     {
061       if ("statusCode".equals(element.getNodeName()))
062       {
063         statusCode = (Long) _longSerializer.fromXML(element);
064       } else if ("statusText".equals(element.getNodeName()))
065       {
066         statusText = (String) _stringSerializer.fromXML(element);
067       } else if ("outputParameters".equals(element.getNodeName()))
068       {
069         outputSection = (Map<String, Object>) _mapSerializer.fromXML(element);
070       } 
071     }
072     
073     return new Result(statusCode,statusText,outputSection);
074   }
075 
076   /**
077    * Returns the java type associated to this class.
078    * 
079    * @return the java type associated to this class.
080    */
081   public Class<?> getSerializableType() 
082   {
083     return Result.class;
084   }
085 
086   /**
087    * Return an xml representation of the given UUID with the given name.
088    * 
089    * @param object the UUID to marshal.
090    * @param qname the qualified (xml) name of the object to use in xml representation.
091    * @return the xml representation of the UUID.
092    * @throws SoapFault when the marshalling fails.
093    */
094   public Element toXML(Object obj, QName qname) throws SoapFault 
095   {
096     Result result = (Result) obj;
097     Element root = XmlUtils.createElement(qname);
098     Element statusCode = SerializerRegistry.getInstance().getSerializer(long.class).toXML(result.getStatusCode(), new QName("statusCode"));
099     Element statusText = SerializerRegistry.getInstance().getSerializer(String.class).toXML(result.getStatusText(), new QName("statusText"));    
100     
101     root.appendChild(statusCode);
102     root.appendChild(statusText);
103     if (result.getOutputParameters() != null)
104     {
105       Element outputSection = SerializerRegistry.getInstance().getSerializer(Map.class).toXML(result.getOutputParameters(), new QName("outputParameters"));
106       root.appendChild(outputSection);
107     }
108     return root;
109     
110   }
111 }