View Javadoc

1   /*
2    * Copyright (c) 2010 Commsen International. All rights reserved.
3    * 
4    * This file is part of JWebThumb library.
5    *	
6    * JWebThumb library is free software: you can redistribute it and/or modify 
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 2 of the License, or
9    * (at your option) any later version.
10   * 
11   * JWebThumb library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebThumb library.  If not, see <http://www.gnu.org/licenses/lgpl.html>.
18   */
19  
20  package com.commsen.jwebthumb.simplexml;
21  
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.io.StringWriter;
25  import java.util.logging.Level;
26  import java.util.logging.Logger;
27  
28  import org.simpleframework.xml.Serializer;
29  import org.simpleframework.xml.core.Persister;
30  
31  /***
32   * Utility class responsible for converting objects to XML and XMLs to Objects. The class relies on
33   * <a href="http://simple.sourceforge.net/">Simple XML serialization project</a>
34   * 
35   * 
36   * @author <a href="mailto:MilenDyankov@gmail.com">Milen Dyankov</a>
37   * @since 0.3
38   */
39  public class SimpleXmlSerializer {
40  
41  	private static final Logger LOGGER = Logger.getLogger(SimpleXmlSerializer.class.getName());
42  
43  
44  	/***
45  	 * Converts property annotated object to XML
46  	 * 
47  	 * 
48  	 * @param object property annotated object
49  	 * @return XML representation of the object
50  	 */
51  	public static String generateRequest(Object object) {
52  		Serializer serializer = new Persister();
53  		StringWriter writer = new StringWriter();
54  		try {
55  			serializer.write(object, writer);
56  		} catch (Exception e) {
57  			LOGGER.log(Level.SEVERE, "Failed to generate XML request!", e);
58  			return null;
59  		}
60  		return writer.toString();
61  	}
62  
63  
64  	/***
65  	 * Converts property annotated object to XML and writes it to given stream
66  	 * 
67  	 * @param object property annotated object
68  	 * @param outputStream the stream to write the XML to
69  	 */
70  	public static void generateRequest(Object object, OutputStream outputStream) {
71  		Serializer serializer = new Persister();
72  		try {
73  			serializer.write(object, outputStream);
74  		} catch (Exception e) {
75  			LOGGER.log(Level.SEVERE, "Failed to generate XML request!", e);
76  		}
77  	}
78  
79  
80  	/***
81  	 * Converts XML to appropriate object based on objects' annotations
82  	 * 
83  	 * @param <T>
84  	 * @param xml string containing the XML to be converted to object
85  	 * @param clazz the type of the return object
86  	 * @return
87  	 */
88  	public static <T> T parseResponse(String xml, Class<T> clazz) {
89  		Serializer serializer = new Persister(new JWebThumbMatcher());
90  		try {
91  			return serializer.read(clazz, xml);
92  		} catch (Exception e) {
93  			LOGGER.log(Level.SEVERE, "Failed to parse XML response!", e);
94  			return null;
95  		}
96  	}
97  
98  
99  	/***
100 	 * Converts XML stream to appropriate object based on objects' annotations
101 	 * 
102 	 * @param <T>
103 	 * @param inputStream the stream to read the XML from
104 	 * @param clazz the type of the return object
105 	 * @return
106 	 */
107 	public static <T> T parseResponse(InputStream inputStream, Class<T> clazz) {
108 		Serializer serializer = new Persister(new JWebThumbMatcher());
109 		try {
110 			return serializer.read(clazz, inputStream);
111 		} catch (Exception e) {
112 			LOGGER.log(Level.SEVERE, "Failed to parse XML response!", e);
113 			return null;
114 		}
115 	}
116 
117 }