001    /**
002     * Copyright (c) 2000-present 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.CharPool;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.TextFormatter;
023    import com.liferay.portal.kernel.xml.Element;
024    
025    import java.lang.reflect.Method;
026    
027    import java.util.List;
028    
029    /**
030     * @author Charles May
031     */
032    public class BeanToXMLUtil {
033    
034            public static void addBean(Object obj, Element parentEl) {
035                    Class<?> clazz = obj.getClass();
036    
037                    String classNameWithoutPackage = getClassNameWithoutPackage(
038                            clazz.getName());
039    
040                    Element el = parentEl.addElement(classNameWithoutPackage);
041    
042                    addFields(obj, el);
043            }
044    
045            public static void addFields(Object obj, Element parentEl) {
046                    Class<?> clazz = obj.getClass();
047    
048                    Method[] methods = clazz.getMethods();
049    
050                    for (int i = 0; i < methods.length; i++) {
051                            Method method = methods[i];
052    
053                            if (method.getName().startsWith("get") &&
054                                    !method.getName().equals("getClass")) {
055    
056                                    String memberName = StringUtil.replace(
057                                            method.getName(), "get", StringPool.BLANK);
058    
059                                    memberName = TextFormatter.format(memberName, TextFormatter.I);
060                                    memberName = TextFormatter.format(memberName, TextFormatter.K);
061    
062                                    try {
063                                            Object returnValue = method.invoke(obj, new Object[0]);
064    
065                                            if (returnValue instanceof List<?>) {
066                                                    List<Object> list = (List<Object>)returnValue;
067    
068                                                    Element listEl = parentEl.addElement(memberName);
069    
070                                                    for (int j = 0; j < list.size(); j++) {
071                                                            addBean(list.get(j), listEl);
072                                                    }
073                                            }
074                                            else {
075                                                    DocUtil.add(
076                                                            parentEl, memberName, returnValue.toString());
077                                            }
078                                    }
079                                    catch (Exception e) {
080                                            if (_log.isWarnEnabled()) {
081                                                    _log.warn(e.getMessage());
082                                            }
083                                    }
084                            }
085                    }
086            }
087    
088            public static String getClassNameWithoutPackage(String className) {
089                    String[] classNameArray = StringUtil.split(className, CharPool.PERIOD);
090    
091                    String classNameWithoutPackage =
092                            classNameArray[classNameArray.length - 1];
093    
094                    classNameWithoutPackage = TextFormatter.format(
095                            classNameWithoutPackage, TextFormatter.I);
096                    classNameWithoutPackage = TextFormatter.format(
097                            classNameWithoutPackage, TextFormatter.K);
098    
099                    return classNameWithoutPackage;
100            }
101    
102            private static final Log _log = LogFactoryUtil.getLog(BeanToXMLUtil.class);
103    
104    }