| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.shindig.social.core.util; |
| 19 | |
|
| 20 | |
import org.apache.shindig.social.opensocial.service.BeanConverter; |
| 21 | |
|
| 22 | |
import org.apache.commons.betwixt.IntrospectionConfiguration; |
| 23 | |
import org.apache.commons.betwixt.io.BeanReader; |
| 24 | |
import org.apache.commons.betwixt.io.BeanWriter; |
| 25 | |
import org.xml.sax.SAXException; |
| 26 | |
|
| 27 | |
import java.beans.IntrospectionException; |
| 28 | |
import java.io.IOException; |
| 29 | |
import java.io.StringReader; |
| 30 | |
import java.io.StringWriter; |
| 31 | |
import java.util.logging.Level; |
| 32 | |
import java.util.logging.Logger; |
| 33 | |
|
| 34 | 3 | public class BeanXmlConverter implements BeanConverter { |
| 35 | 1 | private static Logger logger = |
| 36 | |
Logger.getLogger(BeanXmlConverter.class.getName()); |
| 37 | |
|
| 38 | |
public String getContentType() { |
| 39 | 0 | return "application/xml"; |
| 40 | |
} |
| 41 | |
|
| 42 | |
public String convertToString(Object pojo) { |
| 43 | 0 | return convertToXml(pojo); |
| 44 | |
} |
| 45 | |
|
| 46 | |
public String convertToXml(Object obj) { |
| 47 | 3 | StringWriter outputWriter = new StringWriter(); |
| 48 | 3 | BeanWriter writer = new BeanWriter(outputWriter); |
| 49 | 3 | IntrospectionConfiguration configuration = writer.getXMLIntrospector().getConfiguration(); |
| 50 | 3 | configuration.setAttributesForPrimitives(false); |
| 51 | 3 | configuration.setWrapCollectionsInElement(true); |
| 52 | |
|
| 53 | 3 | writer.getBindingConfiguration().setMapIDs(false); |
| 54 | |
|
| 55 | 3 | writer.setEndOfLine(""); |
| 56 | 3 | writer.setWriteEmptyElements(false); |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | 3 | String toReturn = null; |
| 69 | |
try { |
| 70 | 3 | writer.write("response", obj); |
| 71 | 3 | toReturn = outputWriter.toString(); |
| 72 | 3 | logger.finest("XML is: " + toReturn + "\n **** \n\n"); |
| 73 | |
|
| 74 | 0 | } catch (SAXException e) { |
| 75 | 0 | logger.log(Level.SEVERE, e.getMessage(), e); |
| 76 | 0 | } catch (IOException e) { |
| 77 | 0 | logger.log(Level.SEVERE, e.getMessage(), e); |
| 78 | 0 | } catch (IntrospectionException e) { |
| 79 | 0 | logger.log(Level.SEVERE, e.getMessage(), e); |
| 80 | |
} finally { |
| 81 | 0 | try { |
| 82 | 3 | writer.close(); |
| 83 | 0 | } catch (IOException e) { |
| 84 | 0 | logger.log(Level.FINEST, e.getMessage(), e); |
| 85 | 3 | } |
| 86 | 0 | } |
| 87 | |
|
| 88 | 3 | return toReturn; |
| 89 | |
} |
| 90 | |
|
| 91 | |
public <T> T convertToObject(String xml, Class<T> className) { |
| 92 | 0 | String errorMessage = "Could not convert " + xml + " to " + className; |
| 93 | |
|
| 94 | 0 | BeanReader reader = new BeanReader(); |
| 95 | |
try { |
| 96 | 0 | reader.registerBeanClass("activity", className); |
| 97 | 0 | StringReader rd = new StringReader(xml); |
| 98 | 0 | return (T) reader.parse(rd); |
| 99 | 0 | } catch (IntrospectionException e) { |
| 100 | 0 | throw new RuntimeException(errorMessage, e); |
| 101 | 0 | } catch (IOException e) { |
| 102 | 0 | throw new RuntimeException(errorMessage, e); |
| 103 | 0 | } catch (SAXException e) { |
| 104 | 0 | throw new RuntimeException(errorMessage, e); |
| 105 | |
} |
| 106 | |
} |
| 107 | |
} |