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.xstream;
19  
20  import com.thoughtworks.xstream.converters.MarshallingContext;
21  import com.thoughtworks.xstream.converters.UnmarshallingContext;
22  import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
23  import com.thoughtworks.xstream.io.HierarchicalStreamReader;
24  import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
25  import com.thoughtworks.xstream.mapper.Mapper;
26  
27  import org.apache.shindig.social.opensocial.spi.RestfulCollection;
28  
29  /***
30   * This converter changes the way in which a collection is serialized
31   * 
32   */
33  public class RestfullCollectionConverter extends AbstractCollectionConverter {
34  
35    /***
36     * @param mapper
37     */
38    public RestfullCollectionConverter(Mapper mapper) {
39      super(mapper);
40    }
41  
42    /***
43     * {@inheritDoc}
44     * 
45     * @see com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter#canConvert(java.lang.Class)
46     */
47    @Override
48    public boolean canConvert(Class clazz) {
49      boolean convert = (RestfulCollection.class.isAssignableFrom(clazz));
50      return convert;
51    }
52  
53    /***
54     * {@inheritDoc}
55     * 
56     * @see com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter#marshal(java.lang.Object,
57     *      com.thoughtworks.xstream.io.HierarchicalStreamWriter,
58     *      com.thoughtworks.xstream.converters.MarshallingContext)
59     */
60    @Override
61    public void marshal(Object source, HierarchicalStreamWriter writer,
62        MarshallingContext context) {
63  
64      RestfulCollection<?> collection = (RestfulCollection<?>) source;
65      writer.startNode("startIndex");
66      writer.setValue(String.valueOf(collection.getStartIndex()));
67      writer.endNode();
68      writer.startNode("totalResults");
69      writer.setValue(String.valueOf(collection.getTotalResults()));
70      writer.endNode();
71      writer.startNode("isFiltered");
72      writer.setValue(String.valueOf(collection.isFiltered()));
73      writer.endNode();
74      writer.startNode("isSorted");
75      writer.setValue(String.valueOf(collection.isSorted()));
76      writer.endNode();
77      writer.startNode("isUpdatedSince");
78      writer.setValue(String.valueOf(collection.isUpdatedSince()));
79      writer.endNode();
80      // TODO: resolve if entry is the container or the name of the object.
81      for (Object o : collection.getEntry()) {
82        writer.startNode("entry");
83        writeItem(o, context, writer);
84        writer.endNode();
85      }
86    }
87  
88    /***
89     * {@inheritDoc}
90     * 
91     * @see com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,
92     *      com.thoughtworks.xstream.converters.UnmarshallingContext)
93     */
94    @Override
95    public Object unmarshal(HierarchicalStreamReader arg0,
96        UnmarshallingContext arg1) {
97      // TODO Auto-generated method stub
98      return null;
99    }
100 
101 }