001
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
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 }