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.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 addBean(Object obj, org.dom4j.Element parentEl) {
45          String classNameWithoutPackage = getClassNameWithoutPackage(
46              obj.getClass().getName());
47  
48          org.dom4j.Element el = parentEl.addElement(classNameWithoutPackage);
49  
50          addFields(obj, el);
51      }
52  
53      public static void addFields(Object obj, Element parentEl) {
54          Method[] methods = obj.getClass().getMethods();
55  
56          for (int i = 0; i < methods.length; i++) {
57              Method method = methods[i];
58  
59              if (method.getName().startsWith("get") &&
60                  !method.getName().equals("getClass")) {
61  
62                  String memberName = StringUtil.replace(
63                      method.getName(), "get", StringPool.BLANK);
64  
65                  memberName = TextFormatter.format(memberName, TextFormatter.I);
66                  memberName = TextFormatter.format(memberName, TextFormatter.K);
67  
68                  try {
69                      Object returnValue = method.invoke(obj, new Object[] {});
70  
71                      if (returnValue instanceof List<?>) {
72                          List<Object> list = (List<Object>)returnValue;
73  
74                          Element listEl = parentEl.addElement(memberName);
75  
76                          for (int j = 0; j < list.size(); j++) {
77                              addBean(list.get(j), listEl);
78                          }
79                      }
80                      else {
81                          DocUtil.add(
82                              parentEl, memberName, returnValue.toString());
83                      }
84                  }
85                  catch (Exception e) {
86                      if (_log.isWarnEnabled()) {
87                          _log.warn(e.getMessage());
88                      }
89                  }
90              }
91          }
92      }
93  
94      /**
95       * @deprecated
96       */
97      public static void addFields(Object obj, org.dom4j.Element parentEl) {
98          Method[] methods = obj.getClass().getMethods();
99  
100         for (int i = 0; i < methods.length; i++) {
101             Method method = methods[i];
102 
103             if (method.getName().startsWith("get") &&
104                 !method.getName().equals("getClass")) {
105 
106                 String memberName = StringUtil.replace(
107                     method.getName(), "get", StringPool.BLANK);
108 
109                 memberName = TextFormatter.format(memberName, TextFormatter.I);
110                 memberName = TextFormatter.format(memberName, TextFormatter.K);
111 
112                 try {
113                     Object returnValue = method.invoke(obj, new Object[] {});
114 
115                     if (returnValue instanceof List<?>) {
116                         List<Object> list = (List<Object>)returnValue;
117 
118                         org.dom4j.Element listEl = parentEl.addElement(
119                             memberName);
120 
121                         for (int j = 0; j < list.size(); j++) {
122                             addBean(list.get(j), listEl);
123                         }
124                     }
125                     else {
126                         DocUtil.add(
127                             parentEl, memberName, returnValue.toString());
128                     }
129                 }
130                 catch (Exception e) {
131                     if (_log.isWarnEnabled()) {
132                         _log.warn(e.getMessage());
133                     }
134                 }
135             }
136         }
137     }
138 
139     public static String getClassNameWithoutPackage(String className) {
140         String[] classNameArray = StringUtil.split(
141             className, StringPool.PERIOD);
142 
143         String classNameWithoutPackage =
144             classNameArray[classNameArray.length - 1];
145 
146         classNameWithoutPackage = TextFormatter.format(
147             classNameWithoutPackage, TextFormatter.I);
148         classNameWithoutPackage = TextFormatter.format(
149             classNameWithoutPackage, TextFormatter.K);
150 
151         return classNameWithoutPackage;
152     }
153 
154     private static Log _log = LogFactoryUtil.getLog(BeanToXMLUtil.class);
155 
156 }