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