001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public String convert(String input) {
037                    String output = input;
038    
039                    try {
040                            output = doConvert(input);
041                    }
042                    catch (Exception e) {
043                            _log.error(e, e);
044                    }
045    
046                    return output;
047            }
048    
049            public void setClassNames(Map<String, String> classNames) {
050                    _classNames = classNames;
051            }
052    
053            protected String doConvert(String input) throws Exception {
054                    if ((_classNames == null) || _classNames.isEmpty()) {
055                            return input;
056                    }
057    
058                    Document document = SAXReaderUtil.read(input);
059    
060                    Element rootElement = document.getRootElement();
061    
062                    Iterator<Element> itr = rootElement.elementIterator("class");
063    
064                    while (itr.hasNext()) {
065                            Element classElement = itr.next();
066    
067                            String oldName = classElement.attributeValue("name");
068    
069                            String newName = _classNames.get(oldName);
070    
071                            if (newName != null) {
072                                    classElement.addAttribute("name", newName);
073                            }
074                    }
075    
076                    return document.asXML();
077            }
078    
079            private static Log _log = LogFactoryUtil.getLog(
080                    HibernateConfigurationConverter.class);
081    
082            private Map<String, String> _classNames;
083    
084    }