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