View Javadoc

1   package org.srinivas.siteworks.dbrestserver.utilities;
2   
3   import java.io.StringWriter;
4   import javax.xml.bind.JAXBContext;
5   import javax.xml.bind.JAXBException;
6   import javax.xml.bind.Marshaller;
7   import javax.xml.bind.PropertyException;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
11  
12  /**
13   * The Class DbRestServerUtility.
14   */
15  public class DbRestServerUtility {
16  
17  	private static final Logger log = LoggerFactory.getLogger(DbRestServerUtility.class);
18  
19  	/**
20  	 * Fireworks collection to xml.
21  	 *
22  	 * @param fireworks the fws
23  	 * @param stringWriter the string writer
24  	 */
25  	public static void fireWorksCollectionToXML(FireWorks fireworks, StringWriter stringWriter) {
26  		try {
27  			JAXBContext jaxbContext = JAXBContext.newInstance(FireWorks.class);
28  			Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
29  			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
30  			jaxbMarshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
31  					new NamespacePrefixMapper() {
32  						@Override
33  						public String getPreferredPrefix(String namespaceuri, String suggestion,
34  								boolean requireprefix) {
35  							String prefix = "";
36  							if ("urn:fireworks".equalsIgnoreCase(namespaceuri)) {
37  								prefix = "fws";
38  							}
39  
40  							if ("urn:firework".equalsIgnoreCase(namespaceuri)) {
41  								prefix = "fw";
42  							}
43  							return prefix;
44  						}
45  					});
46  
47  			jaxbMarshaller.marshal(fireworks, stringWriter);
48  		} catch (PropertyException e) {
49  			log.info("Error", e);
50  		} catch (JAXBException e) {
51  			log.info("Error", e);
52  		}
53  	}
54  
55  	/**
56  	 * Fireworks to xml.
57  	 *
58  	 * @param fireworks the fireworks
59  	 * @param stringWriter the string writer
60  	 */
61  	public static void fireWorksToXML(FireWork fireworks, StringWriter stringWriter) {
62  		try {
63  			JAXBContext jaxbContext = JAXBContext.newInstance(FireWork.class);
64  			Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
65  			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
66  			jaxbMarshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
67  					new NamespacePrefixMapper() {
68  						@Override
69  						public String getPreferredPrefix(String namespaceuri, String suggestion,
70  								boolean requireprefix) {
71  							return "fw";
72  						}
73  					});
74  
75  			jaxbMarshaller.marshal(fireworks, stringWriter);
76  		} catch (PropertyException e) {
77  			log.info("Error", e);
78  		} catch (JAXBException e) {
79  			log.info("Error", e);
80  		}
81  	}
82  }