matchutil.java

来自「UCS (Ultra Corba Simulator) is one more 」· Java 代码 · 共 180 行

JAVA
180
字号
package com.corba.mnq.xls;

import com.corba.mnq.main.MNQmainFrame;
import com.corba.mnq.tool.Warehouse;
import com.corba.mnq.ui.ArgsTable;
import com.corba.mnq.ui.MNQMutableTreeNode;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatchUtil {

    // give a op node, return pattern list
    private static Map op2patternList = new Hashtable();

    public static void clearAll() {
        op2patternList.clear();
    }

    public static String displayPatternInfo(MNQMutableTreeNode node) {
        PatternNode pn = getPattern(node);
        if (pn == null)
            return null;
        else
            return "Pattern=" + pn.pattern;
    }

    public static PatternNode getPattern(MNQMutableTreeNode node) {
        List lst = getPatternList(node);
        if (lst == null)
            return null;
        String id = ReplaceUtil.getNodePathId(node, MNQmainFrame.top_CaseTree);
        for (int i = 0; i < lst.size(); i++) {
            PatternNode pn = (PatternNode) lst.get(i);
            if (pn.nodeid.equals(id)) { return pn; }
        }
        return null;
    }

    public static List getPatternList(MNQMutableTreeNode node) {
        MNQMutableTreeNode op = ReplaceUtil.getParaOpNode(node);
        Object obj = op2patternList.get(op);
        if (obj != null) {
            return (List) obj;
        } else {
            return null;
        }
    }

    public static void setPattern(MNQMutableTreeNode node, String pattern) {
        List lst = getPatternList(node);
        String id = ReplaceUtil.getNodePathId(node, MNQmainFrame.top_CaseTree);

        if (lst == null) {
            lst = new Vector();
            PatternNode pn = new PatternNode();
            pn.nodeid = id;
            pn.pattern = pattern;
            lst.add(pn);
            MNQMutableTreeNode op = ReplaceUtil.getParaOpNode(node);
            op2patternList.put(op, lst);
        } else {
            PatternNode pn = getPattern(node);
            if (pn == null) {
                pn = new PatternNode();
                lst.add(pn);
                pn.nodeid = id;
            }
            pn.pattern = pattern;
        }
    }

    public static void removePattern(MNQMutableTreeNode node) {
        List lst = getPatternList(node);
        if (lst == null)
            return;
        String id = ReplaceUtil.getNodePathId(node, MNQmainFrame.top_CaseTree);

        for (int i = 0; i < lst.size(); i++) {
            PatternNode pn = (PatternNode) lst.get(i);
            if (pn.nodeid.equals(id)) {
                lst.remove(pn);
            }
        }
    }

    public static void removePatternList(MNQMutableTreeNode op) {
        List lst = getPatternList(op);
        if (lst == null)
            return;
        op2patternList.remove(lst);
    }

    public static boolean matchOp(MNQMutableTreeNode op) {
        boolean ret = true;
        String info = "";
        try {
            List lst = getPatternList(op);
            if (lst == null || lst.size() <= 0) {
                info = "no rule needs to be matched";
                MNQmainFrame.commonPane.appendln("Result:" + info);
                return ret;
            }
            for (int i = 0; i < lst.size(); i++) {
                PatternNode pn = (PatternNode) lst.get(i);
                MNQMutableTreeNode n = ReplaceUtil
                        .pathId2node(pn.nodeid, MNQmainFrame.top_CaseTree);
                if (n == null) {
                    info = printMatchInfo(info, pn, n, "null");
                    ret = false;
                    continue;
                } else {
                    Pattern p = Pattern.compile(pn.pattern, Pattern.DOTALL);
                    // only basictype,any,union,enum,exception can be
                    // mached

                    ArgsTable table = (ArgsTable) Warehouse.node2table.get(n);
                    String value = table.getInputValue();

                    info = printMatchInfo(info, pn, n, value);
                    Matcher m = p.matcher(value);
                    if (!m.find()) {
                        ret = false;
                    }
                }
            }
        } catch (Exception e) {
            info = e.getMessage();
            ret = false;
        }
        MNQmainFrame.commonPane.appendln("Result:" + info);
        return ret;
    }

    /**
     * Method: "printMatchInfo"
     * 
     * @param info
     * @param pn
     * @param n
     * @param value
     * @return
     */
    private static String printMatchInfo(String info, PatternNode pn, MNQMutableTreeNode n,
            String value) {
        String tmp;
        if (n != null) {
            tmp = ReplaceUtil.getNodePathName(n, MNQmainFrame.top_CaseTree);
        } else {
            tmp = "null";
        }
        info = info + "\n IdlBase=" + tmp;
        info = info + "\n Pattern=" + pn.pattern;
        info = info + "\n Received Value=" + value;
        return info;
    }

    public static String checkPattern(String pattern) {
        try {
            Pattern.compile(pattern, Pattern.DOTALL);
            return null;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    /**
     * @param args
     */
    public static void main_(String[] args) {
        // TODO Auto-generated method stub

    }

}

⌨️ 快捷键说明

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