001/* 
002 * JKNIV, utils - Helper utilities for jdk code.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.logger;
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.regex.Matcher;
025import java.util.regex.Pattern;
026
027public class FormatterLogger
028{
029    
030    private static final Pattern pattern = Pattern.compile("\\{[w]*\\}");
031    
032    /**
033     * Formatter a string from <code>org.slf4j.Logger</code> format to <code>String.format</code>
034     * Example:
035     * "The user {} cannot make login with password [{}]"
036     * "The user %1$s cannot make login with password [%2$s]"
037     * 
038     * @param format SLF4J formatter message
039     * @return Return a String with new formatter.
040     */
041    public String formatterSlf4j(String format)
042    {
043        StringBuilder sb = new StringBuilder();
044        Matcher matcher = pattern.matcher(format);
045        int index = 1;
046        int initial = 0;
047        List<Integer> groups = new ArrayList<Integer>();
048        while (matcher.find())
049        {
050            groups.add(matcher.start());
051            groups.add(matcher.end());
052            //matcher.group()
053            sb.append( format.substring(initial, matcher.start()));
054            sb.append("%" + (index++) + "$s"); // string format "%1$s";
055            initial = matcher.end();
056        }
057        if (initial > 0)
058            sb.append( format.substring(initial, format.length()));
059        else
060            sb.append(format);
061        return sb.toString();
062    }
063    
064    /**
065     * to String the objects <code>args</code> calling toString method, when
066     * the object is instance of <code>java.lang.Exception</code> the <code>getMessage()</code>
067     * is called.
068     * @param args objects to get message as string
069     * @return Return the string values from objects. <code>null</code> values are converted to <b>"null"</b> string.
070     */
071    public Object[] toString(Object... args)
072    {
073        Object[] newArray = new Object[0];
074        if (args != null)
075        {
076            newArray = new Object[args.length];
077            for (int i=0; i<args.length;i++)
078            {
079                if ( args[i] instanceof Exception)
080                    newArray[i] = String.valueOf(((Exception)args[i]).getMessage());
081                else
082                    newArray[i] = String.valueOf(args[i]);
083            }
084        }
085        return newArray;
086    }
087}