/*
 * FormatterExpander.java  $Revision: 1.6 $  $Date: 2007/10/03 04:59:32 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.meldcraft.util;

import java.util.Formatter;

/**
 * @version $Revision: 1.6 $ $Date: 2007/10/03 04:59:32 $
 * @author Rob Richards
 */
public class FormatterExpander extends AbstractStringExpander
{
    private Object  mValue;

    public FormatterExpander()
    {
        super("<format");
    }

    public boolean expandFormat(String str, Object value)
    {
        mValue = value;
        return expand(str);
    }

    protected Object expandPart(String type, String fmt)
    {
        Object ret = "";

        Object value = mValue;
        Boolean qempty = null;
        Boolean qnull = null;
        if (fmt != null)
        {
            if (type == null)
            {
                type = "";
            }

            StringBuilder typeb = new StringBuilder(type.toLowerCase());
            int idx = -1;
            while ((idx = typeb.indexOf("!empty")) != -1)
            {
                qempty = Boolean.FALSE;
                typeb.delete(idx, idx+6);
            }
            while ((idx = typeb.indexOf("empty")) != -1)
            {
                qempty = Boolean.TRUE;
                typeb.delete(idx, idx+5);
            }
            while ((idx = typeb.indexOf("!null")) != -1)
            {
                qnull = Boolean.FALSE;
                typeb.delete(idx, idx+5);
            }
            while ((idx = typeb.indexOf("null")) != -1)
            {
                qnull = Boolean.TRUE;
                typeb.delete(idx, idx+4);
            }

            boolean doit = true;
            if (qempty != null)
            {
                if (qempty.booleanValue())
                {
                    doit = (value == null) || ((value instanceof String) && (((String)value).length() == 0));
                }
                else
                {
                    if (value == null)
                    {
                        doit = false;
                    }
                    else
                    {
                        if (value instanceof String)
                        {
                            doit = ((String)value).length() > 0;
                        }
                    }
                }
            }
            if (qnull != null)
            {
                if (qnull.booleanValue())
                {
                    doit = (value == null);
                }
                else
                {
                    doit = (value != null);
                }
            }

            if (doit)
            {
                StringBuilder out = new StringBuilder();
                Formatter formatter = new Formatter(out);
                Object[] args = (value instanceof Object[]) ? (Object[])value : new Object[]{value};
                formatter.format(fmt, args);
                ret = out.toString();
            }
        }
        return ret;
    }
}



