1
22
23 package com.liferay.portal.spring.hibernate;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.Converter;
28 import com.liferay.portal.kernel.xml.Document;
29 import com.liferay.portal.kernel.xml.Element;
30 import com.liferay.portal.kernel.xml.SAXReaderUtil;
31
32 import java.util.Iterator;
33 import java.util.Map;
34
35
45 public class HibernateConfigurationConverter implements Converter<String> {
46
47 public String convert(String input) {
48 String output = input;
49
50 try {
51 output = doConvert(input);
52 }
53 catch (Exception e) {
54 _log.error(e, e);
55 }
56
57 return output;
58 }
59
60 public void setClassNames(Map<String, String> classNames) {
61 _classNames = classNames;
62 }
63
64 protected String doConvert(String input) throws Exception {
65 if ((_classNames == null) || _classNames.isEmpty()) {
66 return input;
67 }
68
69 Document document = SAXReaderUtil.read(input);
70
71 Element rootElement = document.getRootElement();
72
73 Iterator<Element> itr = rootElement.elementIterator("class");
74
75 while (itr.hasNext()) {
76 Element classElement = itr.next();
77
78 String oldName = classElement.attributeValue("name");
79
80 String newName = _classNames.get(oldName);
81
82 if (newName != null) {
83 classElement.addAttribute("name", newName);
84 }
85 }
86
87 return document.asXML();
88 }
89
90 private static Log _log =
91 LogFactoryUtil.getLog(HibernateConfigurationConverter.class);
92
93 private Map<String, String> _classNames;
94
95 }