View Javadoc

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  public class BeanAtomConverter implements BeanConverter {
36    private static Logger logger =
37        Logger.getLogger(BeanAtomConverter.class.getName());
38  
39  
40    public String getContentType() {
41      return "application/atom+xml";
42    }
43  
44    public String convertToString(Object pojo) {
45      return convertToXml(pojo);
46    }
47  
48    public String convertToXml(Object obj) {
49      String xmlHead="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
50      StringWriter outputWriter = new StringWriter();
51      BeanWriter writer = new BeanWriter(outputWriter);
52      IntrospectionConfiguration configuration = writer.getXMLIntrospector().getConfiguration();
53      configuration.setAttributesForPrimitives(false);
54      configuration.setWrapCollectionsInElement(true);
55  
56      writer.getBindingConfiguration().setMapIDs(false);
57      // Print no line endings
58      writer.setEndOfLine("");
59      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      String toReturn = xmlHead;
72      try {
73        writer.write("response", obj);
74        toReturn =toReturn+ outputWriter.toString();
75        logger.finest("XML is: " + toReturn + "\n **** \n\n");
76  
77      } catch (SAXException e) {
78        logger.log(Level.SEVERE, e.getMessage(), e);
79      } catch (IOException e) {
80        logger.log(Level.SEVERE, e.getMessage(), e);
81      } catch (IntrospectionException e) {
82        logger.log(Level.SEVERE, e.getMessage(), e);
83      } finally {
84        try {
85          writer.close();
86        } catch (IOException e) {
87          logger.log(Level.FINEST, e.getMessage(), e);
88        }
89      }
90  
91      return toReturn;
92    }
93  
94    public <T> T convertToObject(String xml, Class<T> className) {
95      String errorMessage = "Could not convert " + xml + " to " + className;
96      xml=xml.substring(xml.indexOf("?>") + 2);
97      BeanReader reader = new BeanReader();
98      try {
99        reader.registerBeanClass("activity", className);
100       StringReader rd = new StringReader(xml);
101       return (T) reader.parse(rd);
102     } catch (IntrospectionException e) {
103       throw new RuntimeException(errorMessage, e);
104     } catch (IOException e) {
105       throw new RuntimeException(errorMessage, e);
106     } catch (SAXException e) {
107       throw new RuntimeException(errorMessage, e);
108     }
109   }
110 }