001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.xml;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.util.TextFormatter;
023    
024    import java.lang.reflect.Method;
025    
026    import java.util.List;
027    
028    /**
029     * @author Charles May
030     */
031    public class BeanToXMLUtil {
032    
033            public static void addBean(Object obj, Element parentEl) {
034                    String classNameWithoutPackage = getClassNameWithoutPackage(
035                            obj.getClass().getName());
036    
037                    Element el = parentEl.addElement(classNameWithoutPackage);
038    
039                    addFields(obj, el);
040            }
041    
042            public static void addBean(Object obj, org.dom4j.Element parentEl) {
043                    String classNameWithoutPackage = getClassNameWithoutPackage(
044                            obj.getClass().getName());
045    
046                    org.dom4j.Element el = parentEl.addElement(classNameWithoutPackage);
047    
048                    addFields(obj, el);
049            }
050    
051            public static void addFields(Object obj, Element parentEl) {
052                    Method[] methods = obj.getClass().getMethods();
053    
054                    for (int i = 0; i < methods.length; i++) {
055                            Method method = methods[i];
056    
057                            if (method.getName().startsWith("get") &&
058                                    !method.getName().equals("getClass")) {
059    
060                                    String memberName = StringUtil.replace(
061                                            method.getName(), "get", StringPool.BLANK);
062    
063                                    memberName = TextFormatter.format(memberName, TextFormatter.I);
064                                    memberName = TextFormatter.format(memberName, TextFormatter.K);
065    
066                                    try {
067                                            Object returnValue = method.invoke(obj, new Object[] {});
068    
069                                            if (returnValue instanceof List<?>) {
070                                                    List<Object> list = (List<Object>)returnValue;
071    
072                                                    Element listEl = parentEl.addElement(memberName);
073    
074                                                    for (int j = 0; j < list.size(); j++) {
075                                                            addBean(list.get(j), listEl);
076                                                    }
077                                            }
078                                            else {
079                                                    DocUtil.add(
080                                                            parentEl, memberName, returnValue.toString());
081                                            }
082                                    }
083                                    catch (Exception e) {
084                                            if (_log.isWarnEnabled()) {
085                                                    _log.warn(e.getMessage());
086                                            }
087                                    }
088                            }
089                    }
090            }
091    
092            /**
093             * @deprecated
094             */
095            public static void addFields(Object obj, org.dom4j.Element parentEl) {
096                    Method[] methods = obj.getClass().getMethods();
097    
098                    for (int i = 0; i < methods.length; i++) {
099                            Method method = methods[i];
100    
101                            if (method.getName().startsWith("get") &&
102                                    !method.getName().equals("getClass")) {
103    
104                                    String memberName = StringUtil.replace(
105                                            method.getName(), "get", StringPool.BLANK);
106    
107                                    memberName = TextFormatter.format(memberName, TextFormatter.I);
108                                    memberName = TextFormatter.format(memberName, TextFormatter.K);
109    
110                                    try {
111                                            Object returnValue = method.invoke(obj, new Object[] {});
112    
113                                            if (returnValue instanceof List<?>) {
114                                                    List<Object> list = (List<Object>)returnValue;
115    
116                                                    org.dom4j.Element listEl = parentEl.addElement(
117                                                            memberName);
118    
119                                                    for (int j = 0; j < list.size(); j++) {
120                                                            addBean(list.get(j), listEl);
121                                                    }
122                                            }
123                                            else {
124                                                    DocUtil.add(
125                                                            parentEl, memberName, returnValue.toString());
126                                            }
127                                    }
128                                    catch (Exception e) {
129                                            if (_log.isWarnEnabled()) {
130                                                    _log.warn(e.getMessage());
131                                            }
132                                    }
133                            }
134                    }
135            }
136    
137            public static String getClassNameWithoutPackage(String className) {
138                    String[] classNameArray = StringUtil.split(
139                            className, StringPool.PERIOD);
140    
141                    String classNameWithoutPackage =
142                            classNameArray[classNameArray.length - 1];
143    
144                    classNameWithoutPackage = TextFormatter.format(
145                            classNameWithoutPackage, TextFormatter.I);
146                    classNameWithoutPackage = TextFormatter.format(
147                            classNameWithoutPackage, TextFormatter.K);
148    
149                    return classNameWithoutPackage;
150            }
151    
152            private static Log _log = LogFactoryUtil.getLog(BeanToXMLUtil.class);
153    
154    }