1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util.xml;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.kernel.xml.Element;
22  import com.liferay.util.TextFormatter;
23  
24  import java.lang.reflect.Method;
25  
26  import java.util.List;
27  
28  /**
29   * <a href="BeanToXMLUtil.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Charles May
32   */
33  public class BeanToXMLUtil {
34  
35      public static void addBean(Object obj, Element parentEl) {
36          String classNameWithoutPackage = getClassNameWithoutPackage(
37              obj.getClass().getName());
38  
39          Element el = parentEl.addElement(classNameWithoutPackage);
40  
41          addFields(obj, el);
42      }
43  
44      public static void addFields(Object obj, Element parentEl) {
45          Method[] methods = obj.getClass().getMethods();
46  
47          for (int i = 0; i < methods.length; i++) {
48              Method method = methods[i];
49  
50              if (method.getName().startsWith("get") &&
51                  !method.getName().equals("getClass")) {
52  
53                  String memberName = StringUtil.replace(
54                      method.getName(), "get", StringPool.BLANK);
55  
56                  memberName = TextFormatter.format(memberName, TextFormatter.I);
57                  memberName = TextFormatter.format(memberName, TextFormatter.K);
58  
59                  try {
60                      Object returnValue = method.invoke(obj, new Object[] {});
61  
62                      if (returnValue instanceof List<?>) {
63                          List<Object> list = (List<Object>)returnValue;
64  
65                          Element listEl = parentEl.addElement(memberName);
66  
67                          for (int j = 0; j < list.size(); j++) {
68                              addBean(list.get(j), listEl);
69                          }
70                      }
71                      else {
72                          DocUtil.add(
73                              parentEl, memberName, returnValue.toString());
74                      }
75                  }
76                  catch (Exception e) {
77                      if (_log.isWarnEnabled()) {
78                          _log.warn(e.getMessage());
79                      }
80                  }
81              }
82          }
83      }
84  
85      public static String getClassNameWithoutPackage(String className) {
86          String[] classNameArray = StringUtil.split(
87              className, StringPool.PERIOD);
88  
89          String classNameWithoutPackage =
90              classNameArray[classNameArray.length - 1];
91  
92          classNameWithoutPackage = TextFormatter.format(
93              classNameWithoutPackage, TextFormatter.I);
94          classNameWithoutPackage = TextFormatter.format(
95              classNameWithoutPackage, TextFormatter.K);
96  
97          return classNameWithoutPackage;
98      }
99  
100     private static Log _log = LogFactoryUtil.getLog(BeanToXMLUtil.class);
101 
102 }