1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.util.TextFormatter;
31  
32  import java.lang.reflect.Method;
33  
34  import java.util.List;
35  
36  /**
37   * <a href="BeanToXMLUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Charles May
40   *
41   */
42  public class BeanToXMLUtil {
43  
44      public static void addBean(Object obj, Element parentEl) {
45          String classNameWithoutPackage = getClassNameWithoutPackage(
46              obj.getClass().getName());
47  
48          Element el = parentEl.addElement(classNameWithoutPackage);
49  
50          addFields(obj, el);
51      }
52  
53      /**
54       * @deprecated
55       */
56      public static void addBean(Object obj, org.dom4j.Element parentEl) {
57          String classNameWithoutPackage = getClassNameWithoutPackage(
58              obj.getClass().getName());
59  
60          org.dom4j.Element el = parentEl.addElement(classNameWithoutPackage);
61  
62          addFields(obj, el);
63      }
64  
65      public static void addFields(Object obj, Element parentEl) {
66          Method[] methods = obj.getClass().getMethods();
67  
68          for (int i = 0; i < methods.length; i++) {
69              Method method = methods[i];
70  
71              if (method.getName().startsWith("get") &&
72                  !method.getName().equals("getClass")) {
73  
74                  String memberName = StringUtil.replace(
75                      method.getName(), "get", StringPool.BLANK);
76  
77                  memberName = TextFormatter.format(memberName, TextFormatter.I);
78                  memberName = TextFormatter.format(memberName, TextFormatter.K);
79  
80                  try {
81                      Object returnValue = method.invoke(obj, new Object[] {});
82  
83                      if (returnValue instanceof List) {
84                          List<Object> list = (List<Object>)returnValue;
85  
86                          Element listEl = parentEl.addElement(memberName);
87  
88                          for (int j = 0; j < list.size(); j++) {
89                              addBean(list.get(j), listEl);
90                          }
91                      }
92                      else {
93                          DocUtil.add(
94                              parentEl, memberName, returnValue.toString());
95                      }
96                  }
97                  catch (Exception e) {
98                      if (_log.isWarnEnabled()) {
99                          _log.warn(e.getMessage());
100                     }
101                 }
102             }
103         }
104     }
105 
106     /**
107      * @deprecated
108      */
109     public static void addFields(Object obj, org.dom4j.Element parentEl) {
110         Method[] methods = obj.getClass().getMethods();
111 
112         for (int i = 0; i < methods.length; i++) {
113             Method method = methods[i];
114 
115             if (method.getName().startsWith("get") &&
116                 !method.getName().equals("getClass")) {
117 
118                 String memberName = StringUtil.replace(
119                     method.getName(), "get", StringPool.BLANK);
120 
121                 memberName = TextFormatter.format(memberName, TextFormatter.I);
122                 memberName = TextFormatter.format(memberName, TextFormatter.K);
123 
124                 try {
125                     Object returnValue = method.invoke(obj, new Object[] {});
126 
127                     if (returnValue instanceof List) {
128                         List<Object> list = (List<Object>)returnValue;
129 
130                         org.dom4j.Element listEl = parentEl.addElement(
131                             memberName);
132 
133                         for (int j = 0; j < list.size(); j++) {
134                             addBean(list.get(j), listEl);
135                         }
136                     }
137                     else {
138                         DocUtil.add(
139                             parentEl, memberName, returnValue.toString());
140                     }
141                 }
142                 catch (Exception e) {
143                     if (_log.isWarnEnabled()) {
144                         _log.warn(e.getMessage());
145                     }
146                 }
147             }
148         }
149     }
150 
151     public static String getClassNameWithoutPackage(String className) {
152         String[] classNameArray = StringUtil.split(
153             className, StringPool.PERIOD);
154 
155         String classNameWithoutPackage =
156             classNameArray[classNameArray.length - 1];
157 
158         classNameWithoutPackage = TextFormatter.format(
159             classNameWithoutPackage, TextFormatter.I);
160         classNameWithoutPackage = TextFormatter.format(
161             classNameWithoutPackage, TextFormatter.K);
162 
163         return classNameWithoutPackage;
164     }
165 
166     private static Log _log = LogFactoryUtil.getLog(BeanToXMLUtil.class);
167 
168 }