View Javadoc

1   package fr.in2p3.jsaga.helpers.cloner;
2   
3   import org.exolab.castor.xml.Marshaller;
4   import org.exolab.castor.xml.Unmarshaller;
5   import org.w3c.dom.Document;
6   
7   import javax.xml.parsers.DocumentBuilderFactory;
8   import java.io.Serializable;
9   
10  /* ***************************************************
11  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
12  * ***             http://cc.in2p3.fr/             ***
13  * ***************************************************
14  * File:   BeanCloner
15  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
16  * Date:   24 oct. 2008
17  * ***************************************************
18  * Description:                                      */
19  /**
20   *
21   */
22  public class BeanCloner {
23      /**
24       * deeply copy a castor bean
25       * @param source the source castor bean
26       * @return the target castor bean
27       */
28      public Serializable cloneBean(Serializable source) throws CloneNotSupportedException {
29          try {
30              Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
31              Marshaller.marshal(source, doc);
32              return (Serializable) Unmarshaller.unmarshal(source.getClass(), doc);
33          } catch (Exception e) {
34              throw new CloneNotSupportedException(e.getMessage());
35          }
36      }
37  }