1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.xml;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.util.TextFormatter;
28  
29  import java.lang.reflect.Method;
30  
31  import java.util.List;
32  
33  /**
34   * <a href="BeanToXMLUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Charles May
37   *
38   */
39  public class BeanToXMLUtil {
40  
41      public static void addBean(Object obj, Element parentEl) {
42          String classNameWithoutPackage = getClassNameWithoutPackage(
43              obj.getClass().getName());
44  
45          Element el = parentEl.addElement(classNameWithoutPackage);
46  
47          addFields(obj, el);
48      }
49  
50      /**
51       * @deprecated
52       */
53      public static void addBean(Object obj, org.dom4j.Element parentEl) {
54          String classNameWithoutPackage = getClassNameWithoutPackage(
55              obj.getClass().getName());
56  
57          org.dom4j.Element el = parentEl.addElement(classNameWithoutPackage);
58  
59          addFields(obj, el);
60      }
61  
62      public static void addFields(Object obj, Element parentEl) {
63          Method[] methods = obj.getClass().getMethods();
64  
65          for (int i = 0; i < methods.length; i++) {
66              Method method = methods[i];
67  
68              if (method.getName().startsWith("get") &&
69                  !method.getName().equals("getClass")) {
70  
71                  String memberName = StringUtil.replace(
72                      method.getName(), "get", StringPool.BLANK);
73  
74                  memberName = TextFormatter.format(memberName, TextFormatter.I);
75                  memberName = TextFormatter.format(memberName, TextFormatter.K);
76  
77                  try {
78                      Object returnValue = method.invoke(obj, new Object[] {});
79  
80                      if (returnValue instanceof List) {
81                          List<Object> list = (List<Object>)returnValue;
82  
83                          Element listEl = parentEl.addElement(memberName);
84  
85                          for (int j = 0; j < list.size(); j++) {
86                              addBean(list.get(j), listEl);
87                          }
88                      }
89                      else {
90                          DocUtil.add(
91                              parentEl, memberName, returnValue.toString());
92                      }
93                  }
94                  catch (Exception e) {
95                      if (_log.isWarnEnabled()) {
96                          _log.warn(e.getMessage());
97                      }
98                  }
99              }
100         }
101     }
102 
103     /**
104      * @deprecated
105      */
106     public static void addFields(Object obj, org.dom4j.Element parentEl) {
107         Method[] methods = obj.getClass().getMethods();
108 
109         for (int i = 0; i < methods.length; i++) {
110             Method method = methods[i];
111 
112             if (method.getName().startsWith("get") &&
113                 !method.getName().equals("getClass")) {
114 
115                 String memberName = StringUtil.replace(
116                     method.getName(), "get", StringPool.BLANK);
117 
118                 memberName = TextFormatter.format(memberName, TextFormatter.I);
119                 memberName = TextFormatter.format(memberName, TextFormatter.K);
120 
121                 try {
122                     Object returnValue = method.invoke(obj, new Object[] {});
123 
124                     if (returnValue instanceof List) {
125                         List<Object> list = (List<Object>)returnValue;
126 
127                         org.dom4j.Element listEl = parentEl.addElement(
128                             memberName);
129 
130                         for (int j = 0; j < list.size(); j++) {
131                             addBean(list.get(j), listEl);
132                         }
133                     }
134                     else {
135                         DocUtil.add(
136                             parentEl, memberName, returnValue.toString());
137                     }
138                 }
139                 catch (Exception e) {
140                     if (_log.isWarnEnabled()) {
141                         _log.warn(e.getMessage());
142                     }
143                 }
144             }
145         }
146     }
147 
148     public static String getClassNameWithoutPackage(String className) {
149         String[] classNameArray = StringUtil.split(
150             className, StringPool.PERIOD);
151 
152         String classNameWithoutPackage =
153             classNameArray[classNameArray.length - 1];
154 
155         classNameWithoutPackage = TextFormatter.format(
156             classNameWithoutPackage, TextFormatter.I);
157         classNameWithoutPackage = TextFormatter.format(
158             classNameWithoutPackage, TextFormatter.K);
159 
160         return classNameWithoutPackage;
161     }
162 
163     private static Log _log = LogFactoryUtil.getLog(BeanToXMLUtil.class);
164 
165 }