source: DCWoRMS/trunk/src/simulator/utils/GSSimXML.java @ 477

Revision 477, 3.8 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.utils;
2
3import java.io.IOException;
4import java.io.Reader;
5import java.io.StringReader;
6import java.io.StringWriter;
7import java.io.Writer;
8
9import org.castor.mapping.BindingType;
10import org.exolab.castor.xml.ClassDescriptorResolverFactory;
11import org.exolab.castor.xml.Introspector;
12import org.exolab.castor.xml.MarshalException;
13import org.exolab.castor.xml.Marshaller;
14import org.exolab.castor.xml.Unmarshaller;
15import org.exolab.castor.xml.ValidationException;
16import org.exolab.castor.xml.XMLClassDescriptorResolver;
17
18/**
19 * This class provides static methods for marshalling and unmarshalling xml documents.
20 * It uses static cache objects, making the marshalling and unmarshalling process far quicker.
21 *
22 * @author Stanislaw Szczepanowski
23 * @see <a href="http://www.castor.org/xml-best-practice.html#Performance-Considerations">Castor - Performance Considerations</a>
24 */
25public class GSSimXML {
26
27        /**
28         * Hidden constructor
29         */
30        protected GSSimXML() {
31        }
32       
33        /** The class descriptor resolver */
34        protected static XMLClassDescriptorResolver mcdr = (XMLClassDescriptorResolver) ClassDescriptorResolverFactory.createClassDescriptorResolver(BindingType.XML);
35       
36        /** The unmarshaller object */
37        protected static Unmarshaller unmarshaller = new Unmarshaller();
38       
39        /** The marshaller object */
40        protected static Marshaller marshaller = new Marshaller();
41       
42        //static initialization
43        static {
44                mcdr.setIntrospector(new Introspector());
45                unmarshaller.setResolver(mcdr);
46                marshaller.setResolver(mcdr);
47        }
48       
49        /**
50         * A generic method for marshalling.
51         * @param <T> the type of the object that is to be marshalled
52         * @param object the object that is to be marshalled
53         * @param writer the writer, where the result is to be written to
54         * @return true, if the operation succeeded; false otherwise
55         */
56        public static synchronized <T> boolean marshal(T object, Writer writer) {
57                try {
58                        marshaller.setWriter(writer);
59                        marshaller.marshal(object);
60                } catch (IOException e) {
61                        e.printStackTrace();
62                        return false;
63                } catch (MarshalException e) {
64                        e.printStackTrace();
65                        return false;
66                } catch (ValidationException e) {
67                        e.printStackTrace();
68                        return false;
69                }
70                return true;
71        }
72       
73        /**
74         * A generic method for marshalling.
75         * Performs a marshalling and return the result in form of a {@link String} object.
76         * @param <T> the type of the object that is to be marshalled
77         * @param object the object that is to be marshalled
78         * @return a string containing the marshalled xml or <code>null</code> if any error occurred
79         */
80        public static synchronized <T> String marshal(T object) {
81                StringWriter sw = new StringWriter();
82                if (marshal(object, sw))
83                        return sw.toString();
84                else
85                        return null;
86        }
87
88        /**
89         * A generic method for unmarshalling.
90         * @param <T> the type of the object that is to be unmarshalled
91         * @param clazz the class of the object that is to be marshalled
92         * @param reader the reader, from which the result is to be read
93         * @return the unmarshalled object; <code>null</code> if error occurs
94         */
95        public static synchronized <T> T unmarshal(Class<T> clazz, Reader reader) {
96                unmarshaller.setClass(clazz);
97                T result = null;
98                try {
99                        result = (T) unmarshaller.unmarshal(reader);
100                } catch (MarshalException e) {
101                        e.printStackTrace();
102                        return null;
103                } catch (ValidationException e) {
104                        e.printStackTrace();
105                        return null;
106                }
107                return result;
108        }
109       
110        /**
111         * A generic method for unmarshalling.
112         * @param <T> the type of the object that is to be unmarshalled
113         * @param clazz the class of the object that is to be marshalled
114         * @param xml the string, containing an xml document, from which the result is to be read
115         * @return the unmarshalled object; <code>null</code> if error occurs
116         */
117        public static synchronized <T> T unmarshal(Class<T> clazz, String xml) {
118                return unmarshal(clazz, new StringReader(xml));
119        }
120}
Note: See TracBrowser for help on using the repository browser.