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.spring.hibernate;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.Converter;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.kernel.xml.SAXReaderUtil;
023    
024    import java.util.Iterator;
025    import java.util.Map;
026    
027    /**
028     * <p>
029     * See http://issues.liferay.com/browse/LPS-5363.
030     * </p>
031     *
032     * @author Brian Wing Shun Chan
033     */
034    public class HibernateConfigurationConverter implements Converter<String> {
035    
036            @Override
037            public String convert(String input) {
038                    String output = input;
039    
040                    try {
041                            output = doConvert(input);
042                    }
043                    catch (Exception e) {
044                            _log.error(e, e);
045                    }
046    
047                    return output;
048            }
049    
050            public void setClassNames(Map<String, String> classNames) {
051                    _classNames = classNames;
052            }
053    
054            protected String doConvert(String input) throws Exception {
055                    if ((_classNames == null) || _classNames.isEmpty()) {
056                            return input;
057                    }
058    
059                    Document document = SAXReaderUtil.read(input);
060    
061                    Element rootElement = document.getRootElement();
062    
063                    Iterator<Element> itr = rootElement.elementIterator("class");
064    
065                    while (itr.hasNext()) {
066                            Element classElement = itr.next();
067    
068                            String oldName = classElement.attributeValue("name");
069    
070                            String newName = _classNames.get(oldName);
071    
072                            if (newName != null) {
073                                    classElement.addAttribute("name", newName);
074                            }
075                    }
076    
077                    return document.asXML();
078            }
079    
080            private static Log _log = LogFactoryUtil.getLog(
081                    HibernateConfigurationConverter.class);
082    
083            private Map<String, String> _classNames;
084    
085    }