001    /**
002     * Copyright (c) 2000-present 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.portlet.exportimport.xstream;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import com.thoughtworks.xstream.converters.Converter;
021    import com.thoughtworks.xstream.converters.MarshallingContext;
022    import com.thoughtworks.xstream.converters.UnmarshallingContext;
023    import com.thoughtworks.xstream.io.HierarchicalStreamReader;
024    import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
025    
026    /**
027     * @author Daniel Kocsis
028     */
029    public class ConverterAdapter implements Converter {
030    
031            public ConverterAdapter(XStreamConverter xStreamConverter) {
032                    _xStreamConverter = xStreamConverter;
033            }
034    
035            @Override
036            public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
037                    return _xStreamConverter.canConvert(clazz);
038            }
039    
040            @Override
041            public void marshal(
042                    Object object, HierarchicalStreamWriter hierarchicalStreamWriter,
043                    MarshallingContext marshallingContext) {
044    
045                    try {
046                            _xStreamConverter.marshal(
047                                    object,
048                                    new XStreamHierarchicalStreamWriterAdapter(
049                                            hierarchicalStreamWriter),
050                                    new XStreamMarshallingContextAdapter(marshallingContext));
051                    }
052                    catch (Exception e) {
053                            _log.error("Unable to marshal object", e);
054                    }
055            }
056    
057            @Override
058            public Object unmarshal(
059                    HierarchicalStreamReader hierarchicalStreamReader,
060                    UnmarshallingContext unmarshallingContext) {
061    
062                    try {
063                            return _xStreamConverter.unmarshal(
064                                    new XStreamHierarchicalStreamReaderAdapter(
065                                            hierarchicalStreamReader),
066                                    new XStreamUnmarshallingContextAdapter(unmarshallingContext));
067                    }
068                    catch (Exception e) {
069                            _log.error("Unable to un-marshal object", e);
070    
071                            return null;
072                    }
073            }
074    
075            private static final Log _log = LogFactoryUtil.getLog(
076                    ConverterAdapter.class);
077    
078            private final XStreamConverter _xStreamConverter;
079    
080    }