/*
 * Formatter.java  $Revision: 1.7 $  $Date: 2007/10/03 04:39:39 $
 *
 * 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;

/**
 * @version $Revision: 1.7 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
abstract public class AbstractStringExpander
{
    protected String        mPrefix;
    protected Object        mExpanded;
    private int             mPrefixLen;

    protected AbstractStringExpander(String prefix)
    {
        mPrefix = prefix;
        mPrefixLen = (mPrefix == null) ? 0 : mPrefix.length();
    }

    public Object getExpanded()
    {
        return mExpanded;
    }

    protected boolean expand(String str)
    {
        mExpanded = null;
        boolean replaced = false;
        StringBuilder concat = null;
        Object exp = null;

        if ((str != null) && str.equals(mPrefix + ">"))
        {
            replaced = true;
            str = null;
            exp = expandPart(null, null);
        }
        else
        {
            int idx;
            while ((str != null) && ((idx = str.indexOf(mPrefix)) != -1))
            {
                String lead = null;
                String tail = null;
                if (idx > 0)
                {
                    lead = str.substring(0, idx);
                }

                int cidx = str.indexOf(':', idx + mPrefixLen);
                if (cidx != -1)
                {
                    if (str.length()-1 > cidx)
                    {
                        String end = "";
                        String type = "";
                        if (cidx > idx + mPrefixLen)
                        {
                            end = str.substring(idx + mPrefixLen, cidx);
                            if (end.startsWith("["))
                            {
                                int tend = end.indexOf(']');
                                if (tend != -1)
                                {
                                    type = end.substring(1, tend);
                                    if (tend < end.length()-1)
                                    {
                                        end = end.substring(tend+1);
                                    }
                                    else
                                    {
                                        end = "";
                                    }
                                }
                            }
                        }
                        end += ">";

                        int sidx = str.indexOf(end, cidx+1);
                        if (sidx != -1)
                        {
                            if (sidx < str.length()-end.length())
                            {
                                tail = str.substring(sidx+end.length(), str.length());
                            }

                            replaced = true;
                            String part = str.substring(cidx+1, sidx);
                            if (lead != null)
                            {
                                if (concat == null)
                                {
                                    concat = new StringBuilder();
                                }
                                concat.append(lead);
                            }
                            exp = expandPart(type, part);

                            if (concat != null)
                            {
                                concat.append(exp);
                            }
                        }
                    }
                }

                str = tail;
            }
        }

        if (replaced)
        {
            if (str != null)
            {
                if (concat == null)
                {
                    concat = new StringBuilder();
                    if (exp != null)
                    {
                        concat.append(exp);
                    }
                }
            }

            if (concat == null)
            {
                mExpanded = exp;
            }
            else
            {
                mExpanded = (str == null) ? concat : concat.append(str);
            }
        }

        return replaced;
    }

    abstract protected Object expandPart(String type, String part);
}



