001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.jmx;
023    
024    /**
025     * Utility class for String formatting.
026     * 
027     * @author Franck WOLFF
028     */
029    public class MBeanUtil {
030    
031            public static String format(String s) {
032                    if (s == null)
033                            return null;
034                    if (shouldEscape())
035                            return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
036                    return s;
037            }
038            public static String format(String[] sArr) {
039                    return format(sArr, false);
040            }
041    
042            public static String format(String[] sArr, boolean asSet) {
043                    if (sArr == null)
044                            return null;
045                    
046                    StringBuilder sb = new StringBuilder();
047                    if (asSet)
048                            sb.append("{\n");
049                    else
050                            sb.append("[\n");
051                    
052                    boolean shouldEscape = shouldEscape();
053                    for (String s : sArr) {
054                            if (shouldEscape)
055                                    s = format(s);
056                            sb.append("  ").append(s).append(",\n");
057                    }
058                    
059                    if (asSet)
060                            sb.append("}");
061                    else
062                            sb.append("]");
063                    
064                    return sb.toString();
065            }
066            
067            private static boolean shouldEscape() {
068                    return "jboss".equals(MBeanServerLocator.getInstance().getMBeanServer().getDefaultDomain());
069            }
070    }