1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.xml;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.xml.Element;
28  import com.liferay.util.TextFormatter;
29  
30  import java.lang.reflect.Method;
31  
32  import java.util.List;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * <a href="BeanToXMLUtil.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Charles May
41   *
42   */
43  public class BeanToXMLUtil {
44  
45      public static void addBean(Object obj, Element parentEl) {
46          String classNameWithoutPackage = getClassNameWithoutPackage(
47              obj.getClass().getName());
48  
49          Element el = parentEl.addElement(classNameWithoutPackage);
50  
51          addFields(obj, el);
52      }
53  
54      public static void addFields(Object obj, Element parentEl) {
55          Method[] methods = obj.getClass().getMethods();
56  
57          for (int i = 0; i < methods.length; i++) {
58              Method method = methods[i];
59  
60              if (method.getName().startsWith("get") &&
61                  !method.getName().equals("getClass")) {
62  
63                  String memberName = StringUtil.replace(
64                      method.getName(), "get", StringPool.BLANK);
65  
66                  memberName = TextFormatter.format(memberName, TextFormatter.I);
67                  memberName = TextFormatter.format(memberName, TextFormatter.K);
68  
69                  try {
70                      Object returnValue = method.invoke(obj, new Object[] {});
71  
72                      if (returnValue instanceof List) {
73                          List<Object> list = (List<Object>)returnValue;
74  
75                          Element listEl = parentEl.addElement(memberName);
76  
77                          for (int j = 0; j < list.size(); j++) {
78                              addBean(list.get(j), listEl);
79                          }
80                      }
81                      else {
82                          DocUtil.add(
83                              parentEl, memberName, returnValue.toString());
84                      }
85                  }
86                  catch (Exception e) {
87                      if (_log.isWarnEnabled()) {
88                          _log.warn(e.getMessage());
89                      }
90                  }
91              }
92          }
93      }
94  
95      public static String getClassNameWithoutPackage(String className) {
96          String[] classNameArray = StringUtil.split(
97              className, StringPool.PERIOD);
98  
99          String classNameWithoutPackage =
100             classNameArray[classNameArray.length - 1];
101 
102         classNameWithoutPackage = TextFormatter.format(
103             classNameWithoutPackage, TextFormatter.I);
104         classNameWithoutPackage = TextFormatter.format(
105             classNameWithoutPackage, TextFormatter.K);
106 
107         return classNameWithoutPackage;
108     }
109 
110     private static Log _log = LogFactory.getLog(BeanToXMLUtil.class);
111 
112 }