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