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 addFields(Object obj, Element parentEl) {
043                    Method[] methods = obj.getClass().getMethods();
044    
045                    for (int i = 0; i < methods.length; i++) {
046                            Method method = methods[i];
047    
048                            if (method.getName().startsWith("get") &&
049                                    !method.getName().equals("getClass")) {
050    
051                                    String memberName = StringUtil.replace(
052                                            method.getName(), "get", StringPool.BLANK);
053    
054                                    memberName = TextFormatter.format(memberName, TextFormatter.I);
055                                    memberName = TextFormatter.format(memberName, TextFormatter.K);
056    
057                                    try {
058                                            Object returnValue = method.invoke(obj, new Object[] {});
059    
060                                            if (returnValue instanceof List<?>) {
061                                                    List<Object> list = (List<Object>)returnValue;
062    
063                                                    Element listEl = parentEl.addElement(memberName);
064    
065                                                    for (int j = 0; j < list.size(); j++) {
066                                                            addBean(list.get(j), listEl);
067                                                    }
068                                            }
069                                            else {
070                                                    DocUtil.add(
071                                                            parentEl, memberName, returnValue.toString());
072                                            }
073                                    }
074                                    catch (Exception e) {
075                                            if (_log.isWarnEnabled()) {
076                                                    _log.warn(e.getMessage());
077                                            }
078                                    }
079                            }
080                    }
081            }
082    
083            public static String getClassNameWithoutPackage(String className) {
084                    String[] classNameArray = StringUtil.split(
085                            className, StringPool.PERIOD);
086    
087                    String classNameWithoutPackage =
088                            classNameArray[classNameArray.length - 1];
089    
090                    classNameWithoutPackage = TextFormatter.format(
091                            classNameWithoutPackage, TextFormatter.I);
092                    classNameWithoutPackage = TextFormatter.format(
093                            classNameWithoutPackage, TextFormatter.K);
094    
095                    return classNameWithoutPackage;
096            }
097    
098            private static Log _log = LogFactoryUtil.getLog(BeanToXMLUtil.class);
099    
100    }