Coverage Report - org.apache.shindig.social.core.util.BeanAtomConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanAtomConverter
0%
0/42
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  
 // TODO: This does not produce valid atom sytnax yet
 35  0
 public class BeanAtomConverter implements BeanConverter {
 36  0
   private static Logger logger =
 37  
       Logger.getLogger(BeanAtomConverter.class.getName());
 38  
 
 39  
 
 40  
   public String getContentType() {
 41  0
     return "application/atom+xml";
 42  
   }
 43  
 
 44  
   public String convertToString(Object pojo) {
 45  0
     return convertToXml(pojo);
 46  
   }
 47  
 
 48  
   public String convertToXml(Object obj) {
 49  0
     String xmlHead="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
 50  0
     StringWriter outputWriter = new StringWriter();
 51  0
     BeanWriter writer = new BeanWriter(outputWriter);
 52  0
     IntrospectionConfiguration configuration = writer.getXMLIntrospector().getConfiguration();
 53  0
     configuration.setAttributesForPrimitives(false);
 54  0
     configuration.setWrapCollectionsInElement(true);
 55  
 
 56  0
     writer.getBindingConfiguration().setMapIDs(false);
 57  
     // Print no line endings
 58  0
     writer.setEndOfLine("");
 59  0
     writer.setWriteEmptyElements(false);
 60  
 
 61  
     // Still left to do:
 62  
     //
 63  
     // Fix map output with custom outputter:
 64  
     // for a map with {key : value, key2 : value2} we need:
 65  
     // <key>value</key> <key2>value2</key2>
 66  
 
 67  
     // Supress empty lists
 68  
 
 69  
     // Within a list the items need to be renamed - this probably means with need a .betwixt file
 70  
 
 71  0
     String toReturn = xmlHead;
 72  
     try {
 73  0
       writer.write("response", obj);
 74  0
       toReturn =toReturn+ outputWriter.toString();
 75  0
       logger.finest("XML is: " + toReturn + "\n **** \n\n");
 76  
 
 77  0
     } catch (SAXException e) {
 78  0
       logger.log(Level.SEVERE, e.getMessage(), e);
 79  0
     } catch (IOException e) {
 80  0
       logger.log(Level.SEVERE, e.getMessage(), e);
 81  0
     } catch (IntrospectionException e) {
 82  0
       logger.log(Level.SEVERE, e.getMessage(), e);
 83  
     } finally {
 84  0
       try {
 85  0
         writer.close();
 86  0
       } catch (IOException e) {
 87  0
         logger.log(Level.FINEST, e.getMessage(), e);
 88  0
       }
 89  0
     }
 90  
 
 91  0
     return toReturn;
 92  
   }
 93  
 
 94  
   public <T> T convertToObject(String xml, Class<T> className) {
 95  0
     String errorMessage = "Could not convert " + xml + " to " + className;
 96  0
     xml=xml.substring(xml.indexOf("?>") + 2);
 97  0
     BeanReader reader = new BeanReader();
 98  
     try {
 99  0
       reader.registerBeanClass("activity", className);
 100  0
       StringReader rd = new StringReader(xml);
 101  0
       return (T) reader.parse(rd);
 102  0
     } catch (IntrospectionException e) {
 103  0
       throw new RuntimeException(errorMessage, e);
 104  0
     } catch (IOException e) {
 105  0
       throw new RuntimeException(errorMessage, e);
 106  0
     } catch (SAXException e) {
 107  0
       throw new RuntimeException(errorMessage, e);
 108  
     }
 109  
   }
 110  
 }