⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jacsonstate.java

📁 java编写的OCR软件
💻 JAVA
字号:
package de.spieleck.app.jacson;

import java.util.*;

import de.spieleck.config.ConfigParamMap;
import de.spieleck.config.SimpleParamMap;
import de.spieleck.config.SimpleParamGetter;

/**
 * The state of the Jacson Engine. A parameter map, a 
 * chunk counter. JacsonBlock puts some life scoping arround this.
 * @author fsn
 */
public class JacsonState
    implements ConfigParamMap, SimpleParamGetter
{
    /** The chunk Counter */
    protected int chunkNo;

    /** The parent state (for attributes inheritance) */
    protected JacsonState parent = null;

    /** Storage for (local) attributes */
    protected Map attributes = null;

    public JacsonState()
    {
        chunkNo = 0;
    }

    /** Nested constructor for scoping. */
    public JacsonState(JacsonState parent)
    {
        this();
        setParent(parent);
    }

    /**
     * Register the parent state of this state.
     * States form a hierarchy with value inheritance.
     */
    public void setParent(JacsonState parent)
    {
        this.parent = parent;
        this.chunkNo = parent.getChunkNo();
        attributes = null;
    }

    /** Increment the chunk count. Usually done by the driver program. */
    public void incChunkNo()
    {
        chunkNo++;
    }

    /** Get the current chunk count. */
    public int getChunkNo()
    {
        return chunkNo;
    }

    /** Get a value to a given key. */
    public Object get(String key)
    {
        return getContextual(key);
    }

    /** Get the value to a given key only search the key locally. */
    public Object getLocal(String key)
    {
        if ( attributes == null )
            return null;
        return attributes.get(key);
    }

    /** Get the value to a given key, searching locally and then
     the parent. */
    public Object getContextual(String key)
    {
        if ( attributes != null && attributes.containsKey(key) )
        {
            return attributes.get(key);
        }
        else if ( getParent() != null )
        {
            return getParent().get(key);
        }
        return null;
    }

    /** Get the value from the root (no parent) state.
     * Depending on the driver program the root state is often a special
     * one: It is the ant environment within ant tasks and it is the 
     * JJacket environment within jjacket.
     */
    public Object getGlobal(String key)
    {
        return getRoot().getLocal(key);
    }

    /** Store a key,value pair in the state.
     * @param key the name of the object
     * @param value the object to be remembered
     */
    public void put(String key, Object value)
    {
        putContext(key, value);
    }

    /** Store a key/value pair in the local memory.
     * @param key the name of the object
     * @param value the object to be remembered
     */
    public void putLocal(String key, Object value)
    {
        if ( attributes == null )
            attributes = new HashMap(8);
        attributes.put(key, value);
    }

    /** Store the key/value pair in the most local context containing
     * this key. This will be root if the key isn't existing yet.
     * @param key the name of the object
     * @param value the object to be remembered
     */
    public void putContext(String key, Object value)
    {
        if ( ( attributes != null && attributes.containsKey(key) )
             || getParent() == null )
        {
            putLocal(key, value);
        }
        else
        {
            getParent().putContext(key, value);
        }
        // XXX we could consider here, going back the chain to
        // top, when the attribute is new (making it local not
        // global per default)
    }

    /**
     * Store key/value pair in the root state.
     * Depending on the driver program the root state is often a special
     * one: It is the ant environment within ant tasks and it is the 
     * JJacket environment within jjacket.
     * @param key the name of the object
     * @param value the object to be remembered
     */
    public void putGlobal(String key, Object value)
    {
        getRoot().putLocal(key, value);
    }

    /**
     * What keys do we have locally ?
     */
    public Set getLocalKeySet()
    {
        if ( attributes == null )
            return Collections.EMPTY_SET;
        else
            return attributes.keySet();
    }

    /**
     * What keys do we have locally or in the inheritance hierarchy.
     */
    public Set getKeySet()
    {
        Set keys = new HashSet();
        JacsonState curState = this;
        do
        {
            keys.add(curState.getLocalKeySet());
            curState = curState.getParent();
        }
        while ( curState != null );
        return keys;
    }

    protected JacsonState getRoot()
    {
        JacsonState h = getParent();
        if ( h == null )
            return this;
        JacsonState h2 = h;
        h = h.getParent();
        while ( h != null )
        {
            h2 = h;
            h = h.getParent();
        }
        return h2;
    }
        
    protected JacsonState getParent()
    {
        return parent;
    }

    // ----- The ConfigParamMap interface  -----
    /** Wrap {@link #put} as
     * {@link de.spieleck.config.ConfigParamMap#set} for compatibility */
    public void set(String key, String value)
    {
        // Currently the policy is, either the
        // value is new, or we keep the previous one!
        if ( get(key) == null )
            put(key, value);
    }

    // ----- SimpleParamGetter -----
    /** Wrap {@link #get} to {@link de.spieleck.config.SimpleParamGetter#getParam} for compatibility */
    public String getParam(String key)
    {
        Object o = get(key);
        if ( o == null )
            return null;
        else
            return o.toString();
    }

    public String expand(String input)
    {
        return SimpleParamMap.expand(input, this);
    }

}
//
//    Jacson - Text Filtering with Java.
//    Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
//    This library 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 2.1 of the License, or (at your option) any later version.
//
//    This library 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 this library; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -