DateSerializer.java
01 /*
02  *
03  * Licensed to the Apache Software Foundation (ASF) under one
04  * or more contributor license agreements.  See the NOTICE file
05  * distributed with this work for additional information
06  * regarding copyright ownership.  The ASF licenses this file
07  * to you under the Apache License, Version 2.0 (the
08  * "License"); you may not use this file except in compliance
09  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  */
21 package org.apache.qpid.management.wsdm.muse.serializer;
22 
23 import java.util.Date;
24 
25 import javax.xml.namespace.QName;
26 
27 import org.apache.muse.core.serializer.Serializer;
28 import org.apache.muse.util.xml.XmlUtils;
29 import org.apache.muse.ws.addressing.soap.SoapFault;
30 import org.w3c.dom.Element;
31 
32 /**
33  * Implementation of Muse Serializer for Date type.
34  * Note that Muse already ships a serializer but the formatter used on that class 
35  * is losing precision betweem marshal / unmarshal operations.
36  *  
37  @author Andrea Gazzarini
38  */
39 public class DateSerializer implements Serializer 
40 {  
41   /**
42    * Return a Date representation of the given xml element.
43    
44    @param xml the element to unmarshal.
45    @throws SoapFault when the unmarshalling fails.
46    */  
47   public Object fromXML(Element elementDatathrows SoapFault 
48   {
49     return new Date(Long.parseLong(elementData.getTextContent()));
50   }
51 
52   /**
53    * Returns the java type associated to this class.
54    
55    @return the java type associated to this class.
56    */
57   public Class<?> getSerializableType() 
58   {
59     return Date.class;
60   }
61 
62   /**
63    * Return an xml representation of the given UUID with the given name.
64    
65    @param object the UUID to marshal.
66    @param qname the qualified (xml) name of the object to use in xml representation.
67    @return the xml representation of the UUID.
68    @throws SoapFault when the marshalling fails.
69    */
70   public Element toXML(Object obj, QName qnamethrows SoapFault 
71   {
72     Date date = (Dateobj;
73     return XmlUtils.createElement(qname, String.valueOf(date.getTime()));
74   }
75 }