// Decompiled by DJ v2.6.6.51 Copyright 2000 Atanas Neshkov  Date: 6/15/2001 17:41:55
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   com/macromedia/generator/api/Context

package com.macromedia.generator.api;

import com.macromedia.generator.api.peer.ContextPeer;

// Referenced classes of package com.macromedia.generator.api:
//            GenObject, GeneratorApiRuntimeException

public class Context extends GenObject
{

    public Context()
    {
        peer = ContextPeer.New();
        if(peer == 0)
            throw new GeneratorApiRuntimeException("unable to create peer");
        else
            return;
    }

    protected Context(int i)
    {
        peer = i;
        if(peer == 0)
            throw new GeneratorApiRuntimeException("invalid peer");
        else
            return;
    }

    public String apply(String s)
    {
        if(s.indexOf(123) == -1)
            return s;
        StringBuffer stringbuffer = new StringBuffer(256);
        StringBuffer stringbuffer1 = new StringBuffer(256);
        boolean flag = false;
        for(int i = 0; i < s.length(); i++)
        {
            char c = s.charAt(i);
            if(flag)
            {
                if(c == '}')
                {
                    if(i + 1 < s.length() && s.charAt(i + 1) == '}')
                    {
                        stringbuffer1.append(c);
                        i++;
                    } else
                    {
                        flag = false;
                        String s2 = getValue(stringbuffer1.toString());
                        if(s2 != null)
                            stringbuffer.append(s2);
                        stringbuffer1.setLength(0);
                    }
                } else
                {
                    stringbuffer1.append(c);
                }
            } else
            if(c == '{')
            {
                if(i + 1 < s.length() && s.charAt(i + 1) == '{')
                {
                    stringbuffer.append(c);
                    i++;
                } else
                {
                    stringbuffer1.setLength(0);
                    flag = true;
                }
            } else
            if(c == '}' && i + 1 < s.length() && s.charAt(i + 1) == '}')
            {
                stringbuffer.append(c);
                i++;
            } else
            {
                stringbuffer.append(c);
            }
        }

        String s1 = stringbuffer.toString();
        if(stringbuffer1.length() > 0)
            s1 = s1 + "{" + stringbuffer1.toString();
        return s1;
    }

    protected void finalize()
        throws Throwable
    {
        if(peer != 0)
            ContextPeer.Release(peer);
        peer = 0;
    }

    public Context getParent()
    {
        int ai[] = new int[1];
        Context context = null;
        if(peer == 0)
            throw new GeneratorApiRuntimeException("invalid peer");
        int i = ContextPeer.GetParent(peer, ai);
        if(i != 0)
            context = new Context(ai[0]);
        return context;
    }

    public String getValue(String s)
    {
        String as[] = new String[1];
        if(peer == 0)
            throw new GeneratorApiRuntimeException("invalid peer");
        if(s != null)
        {
            int i = ContextPeer.GetValue(peer, s, as);
            if(i == 0)
                as[0] = null;
        }
        return as[0];
    }

    public void release()
    {
        if(peer != 0)
            ContextPeer.Release(peer);
        peer = 0;
    }

    public void setParent(Context context)
    {
        int i = 0;
        if(peer == 0)
            throw new GeneratorApiRuntimeException("invalid peer");
        if(context != null)
            i = context.peer;
        int j = ContextPeer.SetParent(peer, i);
        if(j == 0)
            throw new GeneratorApiRuntimeException("error setting parent");
        else
            return;
    }

    public void setValue(String s, String s1)
    {
        if(peer == 0)
            throw new GeneratorApiRuntimeException("invalid peer");
        if(s != null)
        {
            if(s1 == null)
                s1 = "";
            int i = ContextPeer.SetValue(peer, s, s1);
            if(i == 0)
                throw new GeneratorApiRuntimeException("error setting value");
        }
    }
}