Skip to content
Snippets Groups Projects
Commit b50b8bfd authored by ribeaudc's avatar ribeaudc
Browse files

add:

- XML serialization for Java Beans.

SVN: 598
parent 3b02552e
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.utilities;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* Some utilities around <i>Java Bean</i>s.
*
* @author Christian Ribeaud
*/
public final class BeanUtils
{
private BeanUtils()
{
// Can not be instantiated.
}
/**
* Encodes given object into a XML string.
* <p>
* To decode the returned XML string, you should use {@link #xmlDecode(String)}.
* </p>
*/
public final static String xmlEncode(Object object)
{
assert object != null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(out));
encoder.writeObject(object);
encoder.close();
return out.toString();
}
/**
* This method decodes given XML string into an <code>Object</code>.
* <p>
* By using this method, we assume that the returned <code>Object</code> has been encoded using
* {@link #xmlEncode(Object)}.
* </p>
*/
public final static Object xmlDecode(String xmlString)
{
assert xmlString != null;
ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes());
XMLDecoder encoder = new XMLDecoder(new BufferedInputStream(in));
Object result = encoder.readObject();
encoder.close();
return result;
}
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.utilities;
import java.util.ArrayList;
import java.util.List;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.Test;
/**
* Test cases for the {@link BeanUtils} class.
*
* @author Christian Ribeaud
*/
public final class BeanUtilsTest
{
/**
* Test method for {@link ch.systemsx.cisd.common.utilities.BeanUtils#xmlEncode(java.lang.Object)}.
*/
@Test
public final void testXmlEnDecode()
{
List<SimpleBean> list = new ArrayList<SimpleBean>();
SimpleBean bean = new SimpleBean();
bean.setFirstName("Tanja");
bean.setLastName("Berg");
list.add(bean);
bean = new SimpleBean();
bean.setFirstName("Christian");
bean.setLastName("Ribeaud");
list.add(bean);
String xml = BeanUtils.xmlEncode(list);
List newList = (List) BeanUtils.xmlDecode(xml);
assert newList.size() == 2;
assertEquals(((SimpleBean)newList.get(0)).getFirstName(), "Tanja");
assertEquals(((SimpleBean)newList.get(1)).getLastName(), "Ribeaud");
}
// This MUST be public.
public final static class SimpleBean
{
private String firstName;
private String lastName;
/** Returns <code>firstName</code>. */
public final String getFirstName()
{
return firstName;
}
/** Returns <code>lastName</code>. */
public final String getLastName()
{
return lastName;
}
/** Sets <code>firstName</code>. */
public final void setFirstName(String firstName)
{
this.firstName = firstName;
}
/** Sets <code>lastName</code>. */
public final void setLastName(String lastName)
{
this.lastName = lastName;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment