1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class BeanToXMLUtil {
42  
43      public static void addBean(Object obj, Element parentEl) {
44          String classNameWithoutPackage = getClassNameWithoutPackage(
45              obj.getClass().getName());
46  
47          Element el = parentEl.addElement(classNameWithoutPackage);
48  
49          addFields(obj, el);
50      }
51  
52      public static void addBean(Object obj, org.dom4j.Element parentEl) {
53          String classNameWithoutPackage = getClassNameWithoutPackage(
54              obj.getClass().getName());
55  
56          org.dom4j.Element el = parentEl.addElement(classNameWithoutPackage);
57  
58          addFields(obj, el);
59      }
60  
61      public static void addFields(Object obj, Element parentEl) {
62          Method[] methods = obj.getClass().getMethods();
63  
64          for (int i = 0; i < methods.length; i++) {
65              Method method = methods[i];
66  
67              if (method.getName().startsWith("get") &&
68                  !method.getName().equals("getClass")) {
69  
70                  String memberName = StringUtil.replace(
71                      method.getName(), "get", StringPool.BLANK);
72  
73                  memberName = TextFormatter.format(memberName, TextFormatter.I);
74                  memberName = TextFormatter.format(memberName, TextFormatter.K);
75  
76                  try {
77                      Object returnValue = method.invoke(obj, new Object[] {});
78  
79                      if (returnValue instanceof List<?>) {
80                          List<Object> list = (List<Object>)returnValue;
81  
82                          Element listEl = parentEl.addElement(memberName);
83  
84                          for (int j = 0; j < list.size(); j++) {
85                              addBean(list.get(j), listEl);
86                          }
87                      }
88                      else {
89                          DocUtil.add(
90                              parentEl, memberName, returnValue.toString());
91                      }
92                  }
93                  catch (Exception e) {
94                      if (_log.isWarnEnabled()) {
95                          _log.warn(e.getMessage());
96                      }
97                  }
98              }
99          }
100     }
101 
102     /**
103      * @deprecated
104      */
105     public static void addFields(Object obj, org.dom4j.Element parentEl) {
106         Method[] methods = obj.getClass().getMethods();
107 
108         for (int i = 0; i < methods.length; i++) {
109             Method method = methods[i];
110 
111             if (method.getName().startsWith("get") &&
112                 !method.getName().equals("getClass")) {
113 
114                 String memberName = StringUtil.replace(
115                     method.getName(), "get", StringPool.BLANK);
116 
117                 memberName = TextFormatter.format(memberName, TextFormatter.I);
118                 memberName = TextFormatter.format(memberName, TextFormatter.K);
119 
120                 try {
121                     Object returnValue = method.invoke(obj, new Object[] {});
122 
123                     if (returnValue instanceof List<?>) {
124                         List<Object> list = (List<Object>)returnValue;
125 
126                         org.dom4j.Element listEl = parentEl.addElement(
127                             memberName);
128 
129                         for (int j = 0; j < list.size(); j++) {
130                             addBean(list.get(j), listEl);
131                         }
132                     }
133                     else {
134                         DocUtil.add(
135                             parentEl, memberName, returnValue.toString());
136                     }
137                 }
138                 catch (Exception e) {
139                     if (_log.isWarnEnabled()) {
140                         _log.warn(e.getMessage());
141                     }
142                 }
143             }
144         }
145     }
146 
147     public static String getClassNameWithoutPackage(String className) {
148         String[] classNameArray = StringUtil.split(
149             className, StringPool.PERIOD);
150 
151         String classNameWithoutPackage =
152             classNameArray[classNameArray.length - 1];
153 
154         classNameWithoutPackage = TextFormatter.format(
155             classNameWithoutPackage, TextFormatter.I);
156         classNameWithoutPackage = TextFormatter.format(
157             classNameWithoutPackage, TextFormatter.K);
158 
159         return classNameWithoutPackage;
160     }
161 
162     private static Log _log = LogFactoryUtil.getLog(BeanToXMLUtil.class);
163 
164 }