Coverage Report - org.apache.shindig.social.core.util.BeanXmlConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanXmlConverter
42%
17/40
N/A
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 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  
     // Print no line endings
 55  3
     writer.setEndOfLine("");
 56  3
     writer.setWriteEmptyElements(false);
 57  
 
 58  
     // Still left to do:
 59  
     //
 60  
     // Fix map output with custom outputter:
 61  
     // for a map with {key : value, key2 : value2} we need:
 62  
     // <key>value</key> <key2>value2</key2>
 63  
 
 64  
     // Supress empty lists
 65  
 
 66  
     // Within a list the items need to be renamed - this probably means with need a .betwixt file
 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  
 }