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.util.TextFormatter;
28  
29  import java.lang.reflect.Method;
30  
31  import java.util.List;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import org.dom4j.Element;
37  
38  /**
39   * <a href="BeanToXMLUtil.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Charles May
42   *
43   */
44  public class BeanToXMLUtil {
45  
46      public static void addBean(Object obj, Element parentEl) {
47          String classNameWithoutPackage = getClassNameWithoutPackage(
48              obj.getClass().getName());
49  
50          Element el = parentEl.addElement(classNameWithoutPackage);
51  
52          addFields(obj, el);
53      }
54  
55      public static void addFields(Object obj, Element parentEl) {
56          Method[] methods = obj.getClass().getMethods();
57  
58          for (int i = 0; i < methods.length; i++) {
59              Method method = methods[i];
60  
61              if (method.getName().startsWith("get") &&
62                  !method.getName().equals("getClass")) {
63  
64                  String memberName = StringUtil.replace(
65                      method.getName(), "get", StringPool.BLANK);
66  
67                  memberName = TextFormatter.format(memberName, TextFormatter.I);
68                  memberName = TextFormatter.format(memberName, TextFormatter.K);
69  
70                  try {
71                      Object returnValue = method.invoke(obj, new Object[] {});
72  
73                      if (returnValue instanceof List) {
74                          List<Object> list = (List<Object>)returnValue;
75  
76                          Element listEl = parentEl.addElement(memberName);
77  
78                          for (int j = 0; j < list.size(); j++) {
79                              addBean(list.get(j), listEl);
80                          }
81                      }
82                      else {
83                          DocUtil.add(
84                              parentEl, memberName, returnValue.toString());
85                      }
86                  }
87                  catch (Exception e) {
88                      if (_log.isWarnEnabled()) {
89                          _log.warn(e.getMessage());
90                      }
91                  }
92              }
93          }
94      }
95  
96      public static String getClassNameWithoutPackage(String className) {
97          String[] classNameArray = StringUtil.split(
98              className, StringPool.PERIOD);
99  
100         String classNameWithoutPackage =
101             classNameArray[classNameArray.length - 1];
102 
103         classNameWithoutPackage = TextFormatter.format(
104             classNameWithoutPackage, TextFormatter.I);
105         classNameWithoutPackage = TextFormatter.format(
106             classNameWithoutPackage, TextFormatter.K);
107 
108         return classNameWithoutPackage;
109     }
110 
111     private static Log _log = LogFactory.getLog(BeanToXMLUtil.class);
112 
113 }