1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
28   * <a href="HibernateConfigurationConverter.java.html"><b><i>View Source</i></b>
29   * </a>
30   *
31   * <p>
32   * See http://issues.liferay.com/browse/LPS-5363.
33   * </p>
34   *
35   * @author Brian Wing Shun Chan
36   */
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  }