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

📄 jacsonregistry.java

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

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

import de.spieleck.config.ConfigNode;

import de.spieleck.app.jacson.util.RegExpUtil;

/**
 * Registry for id/idref setup and everything else needed at config time.
 * Hold a RegExpUtil instance also.
 * <br/>
 * XXX Consider moving that in the {@link de.spieleck.config} package.
 * @author fsn
 */
public class JacsonRegistry
{
    public final static String ID_NODE = "id";
    public final static String IDREF_NODE = "idref";

    protected Map registered;

    protected RegExpUtil rutil = null;

    public JacsonRegistry(ConfigNode config)
        throws JacsonConfigException
    {
        registered = new HashMap();
        recurse(config);
    }

    public static boolean acceptId(ConfigNode node)
    {
        String name = node.getName();
        return name.equals(ID_NODE) || name.equals(IDREF_NODE);
    }

    public void recurse(ConfigNode cn)
        throws JacsonConfigException
    {
        String id = cn.getString(ID_NODE, null);
        if ( id != null )
        {
            String idref = cn.getString(IDREF_NODE, null);
            if ( id.equals(idref) )
                throw new JacsonConfigException("idref points to itself.");
            if ( registered.containsKey(id) )
                throw new JacsonConfigException("Duplicate id: "+id);
            registered.put(id, cn);
        }
        Iterator iter = cn.children();
        while ( iter.hasNext() )
            recurse((ConfigNode) iter.next());
    }

    public ConfigNode lookup(ConfigNode cn, ConfigNode anchor)
    {
        String idref = cn.getString(IDREF_NODE, null);
        if ( idref == null )
            return null;
        ConfigNode o = (ConfigNode) registered.get(idref);
        if ( o == null )
            return null;
        if ( o == anchor )
        {
            System.err.println("WARNING: circular lookup with "+idref
                              +", try to proceed by ignoring.");
            return null;
        }
        return o;
    }

    public Iterator scanFor(String name, ConfigNode cn)
    {
        Iterator iter = cn.childrenNamed(name);
        ConfigNode superBlock = lookup(cn, cn);
        while ( !iter.hasNext() && superBlock != null )
        {
            iter = superBlock.childrenNamed(name);
            superBlock = lookup(superBlock, cn);
        }
        return iter;
    }

    public String scanForString(String name, ConfigNode cn)
    {
        Iterator iter = scanFor(name, cn);
        if ( !iter.hasNext() )
            return null;
        ConfigNode cns = (ConfigNode) iter.next();
        if ( cns == null )
            return null;
        return cns.getString();
    }

    public String scanForValue(ConfigNode cn)
    {
        String ret = cn.getString();
        ConfigNode cn2 = cn;
        while ( ret == null )
        {
            cn2 = lookup(cn2, cn);
            if ( cn2 == null )
                return null;
            ret = cn2.getString();
        }
        return ret;
    }

    public Iterator find(ConfigNode node, String nodeName)
    {
        Iterator iter = node.childrenNamed(nodeName);
        if ( !iter.hasNext() )
            iter = scanFor(nodeName, node);
        return iter;
    }

    public RegExpUtil getRegExpUtil()
    {
        if ( rutil == null )
            rutil = new RegExpUtil();
        return rutil;
    }
}

//
//    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 + -