001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.lar.xstream;
016    
017    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018    
019    import com.thoughtworks.xstream.converters.Converter;
020    import com.thoughtworks.xstream.converters.MarshallingContext;
021    import com.thoughtworks.xstream.converters.UnmarshallingContext;
022    import com.thoughtworks.xstream.io.HierarchicalStreamReader;
023    import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
024    
025    import java.util.List;
026    
027    /**
028     * @author Akos Thurzo
029     */
030    public abstract class BaseXStreamConverter implements Converter {
031    
032            @Override
033            public abstract boolean canConvert(Class clazz);
034    
035            @Override
036            public void marshal(
037                    Object object, HierarchicalStreamWriter hierarchicalStreamWriter,
038                    MarshallingContext marshallingContext) {
039    
040                    for (String field : getFields()) {
041                            hierarchicalStreamWriter.startNode(field);
042    
043                            Object value = BeanPropertiesUtil.getObject(object, field);
044    
045                            if (value != null) {
046                                    marshallingContext.convertAnother(value);
047                            }
048    
049                            hierarchicalStreamWriter.endNode();
050                    }
051            }
052    
053            @Override
054            public abstract Object unmarshal(
055                    HierarchicalStreamReader hierarchicalStreamReader,
056                    UnmarshallingContext unmarshallingContext);
057    
058            protected abstract List<String> getFields();
059    
060    }