/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.riotfamily.components.xstream;
import org.riotfamily.components.model.ComponentList;
import org.riotfamily.components.model.Content;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.CollectionConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
public class ComponentListConverter extends CollectionConverter {
public ComponentListConverter(Mapper mapper) {
super(mapper);
}
@Override
@SuppressWarnings("unchecked")
public boolean canConvert(Class type) {
return ComponentList.class.isAssignableFrom(type);
}
@Override
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
ComponentList list = (ComponentList) source;
writer.addAttribute("id", list.getFragmentId());
super.marshal(list, writer, context);
}
@Override
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
Content owner = (Content) context.get("content");
String path = (String) context.get("path");
String id = reader.getAttribute("id");
ComponentList list = new ComponentList(owner, id, path);
ComponentList parentList = (ComponentList) context.get("list");
context.put("list", list);
populateCollection(reader, context, list);
context.put("list", parentList);
return list;
}
}