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