ByteArraySerializer.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 javax.xml.XMLConstants;
24 import javax.xml.namespace.QName;
25 
26 import org.apache.muse.core.serializer.Serializer;
27 import org.apache.muse.util.xml.XmlUtils;
28 import org.apache.muse.ws.addressing.soap.SoapFault;
29 import org.apache.xerces.impl.dv.util.Base64;
30 import org.w3c.dom.Element;
31 
32 /**
33  * Implementation of Muse Serializer for byte array type.
34  *  
35  @author Andrea Gazzarini
36  */
37 public class ByteArraySerializer implements Serializer {
38   
39   /**
40    * Return a byte array  representation of the given xml element.
41    
42    @param xml the element to unmarshal.
43    @throws SoapFault when the unmarshalling fails.
44    */
45   public Object fromXML(Element xmlthrows SoapFault 
46   {
47     try 
48     {
49       return Base64.decode(xml.getTextContent());
50     catch (Exception exception
51     {
52       throw new SoapFault(exception);
53     }
54   }
55 
56   /**
57    * Returns the java type associated to this class.
58    
59    @return the java type associated to this class.
60    */
61   public Class<?> getSerializableType() 
62   {
63     return byte[].class;
64   }
65 
66   /**
67    * Return an xml representation of the given byte array with the given name.
68    
69    @param object the byte array to marshal.
70    @param qname the qualified (xml) name of the object to use in xml representation.
71    @return the xml representation of the byte array.
72    @throws SoapFault when the marshalling fails.
73    */
74   public Element toXML(Object object, QName qnamethrows SoapFault 
75   {
76     Element element = XmlUtils.createElement(
77         qname, 
78         Base64.encode((byte[]) object));
79     element.setAttribute("xmlns:xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
80     element.setAttribute("xsi:type","xsd:base64Binary");
81     return element;
82   }
83 }