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

📄 matchfunction.java

📁 sunxacml源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            return rfc822Params;
        else if (functionName.equals(NAME_STRING_REGEXP_MATCH))
            return stringRegexpParams;
        else if (functionName.equals(NAME_ANYURI_REGEXP_MATCH))
            return anyURIRegexpParams;
        else if (functionName.equals(NAME_IPADDRESS_REGEXP_MATCH))
            return ipAddressRegexpParams;
        else if (functionName.equals(NAME_DNSNAME_REGEXP_MATCH))
            return dnsNameRegexpParams;
        else if (functionName.equals(NAME_RFC822NAME_REGEXP_MATCH))
            return rfc822NameRegexpParams;
        else if (functionName.equals(NAME_X500NAME_REGEXP_MATCH))
            return x500NameRegexpParams;

        return null;
    }

    /**
     * Returns a <code>Set</code> containing all the function identifiers
     * supported by this class.
     *
     * @return a <code>Set</code> of <code>String</code>s
     */
    public static Set getSupportedIdentifiers() {
        Set set = new HashSet();

        set.add(NAME_REGEXP_STRING_MATCH);
        set.add(NAME_X500NAME_MATCH);
        set.add(NAME_RFC822NAME_MATCH);
        set.add(NAME_STRING_REGEXP_MATCH);
        set.add(NAME_ANYURI_REGEXP_MATCH);
        set.add(NAME_IPADDRESS_REGEXP_MATCH);
        set.add(NAME_DNSNAME_REGEXP_MATCH);
        set.add(NAME_RFC822NAME_MATCH);
        set.add(NAME_X500NAME_MATCH);

        return set;
    }

    /**
     * Evaluate the function, using the specified parameters.
     *
     * @param inputs a <code>List</code> of <code>Evaluatable</code>
     *               objects representing the arguments passed to the function
     * @param context an <code>EvaluationCtx</code> so that the
     *                <code>Evaluatable</code> objects can be evaluated
     * @return an <code>EvaluationResult</code> representing the
     *         function's result
     */
    public EvaluationResult evaluate(List inputs, EvaluationCtx context) {
        
        // Evaluate the arguments
        AttributeValue [] argValues = new AttributeValue[inputs.size()];
        EvaluationResult result = evalArgs(inputs, context, argValues);

        // make sure we didn't get an error in processing the args
        if (result != null)
            return result;
        
        // now that we're setup, we can do the matching operations

        boolean boolResult = false;

        switch (getFunctionId()) {

        case ID_REGEXP_STRING_MATCH:
        case ID_STRING_REGEXP_MATCH: {
            // arg0 is a regular expression; arg1 is a general string
            String arg0 = ((StringAttribute)(argValues[0])).getValue();
            String arg1 = ((StringAttribute)(argValues[1])).getValue();

            boolResult = regexpHelper(arg0, arg1);

            break;
        }

        case ID_X500NAME_MATCH: {
            X500Principal arg0 =
                ((X500NameAttribute)(argValues[0])).getValue();
            X500Principal arg1 =
                ((X500NameAttribute)(argValues[1])).getValue();

            boolResult = arg1.getName(X500Principal.CANONICAL).
                endsWith(arg0.getName(X500Principal.CANONICAL));

            break;
        }

        case ID_RFC822NAME_MATCH: {
            String arg0 = ((StringAttribute)(argValues[0])).getValue();
            String arg1 = ((RFC822NameAttribute)(argValues[1])).getValue();

            if (arg0.indexOf('@') != -1) {
                // this is case #1 : a whole address
                String normalized = (new RFC822NameAttribute(arg0)).getValue();
                boolResult = normalized.equals(arg1);
            } else if (arg0.charAt(0) == '.') {
                // this is case #3 : a sub-domain
                boolResult = arg1.endsWith(arg0.toLowerCase());
            } else {
                // this is case #2 : any mailbox at a specific domain
                String mailDomain = arg1.substring(arg1.indexOf('@') + 1);
                boolResult = arg0.toLowerCase().equals(mailDomain);
            }
            
            break;
        }

        case ID_ANYURI_REGEXP_MATCH:  {
            // arg0 is a regular expression; arg1 is a general string
            String arg0 = ((StringAttribute)(argValues[0])).getValue();
            String arg1 = ((AnyURIAttribute)(argValues[1])).encode();

            boolResult = regexpHelper(arg0, arg1);

            break;
        }

        case ID_IPADDRESS_REGEXP_MATCH:  {
            // arg0 is a regular expression; arg1 is a general string
            String arg0 = ((StringAttribute)(argValues[0])).getValue();
            String arg1 = ((IPAddressAttribute)(argValues[1])).encode();

            boolResult = regexpHelper(arg0, arg1);

            break;
        }  

        case ID_DNSNAME_REGEXP_MATCH:  {
            // arg0 is a regular expression; arg1 is a general string
            String arg0 = ((StringAttribute)(argValues[0])).getValue();
            String arg1 = ((DNSNameAttribute)(argValues[1])).encode();

            boolResult = regexpHelper(arg0, arg1);

            break;
        }

        case ID_RFC822NAME_REGEXP_MATCH:  {
            // arg0 is a regular expression; arg1 is a general string
            String arg0 = ((StringAttribute)(argValues[0])).getValue();
            String arg1 = ((RFC822NameAttribute)(argValues[1])).encode();

            boolResult = regexpHelper(arg0, arg1);

            break;
        }

        case ID_X500NAME_REGEXP_MATCH:  {
            // arg0 is a regular expression; arg1 is a general string
            String arg0 = ((StringAttribute)(argValues[0])).getValue();
            String arg1 = ((X500NameAttribute)(argValues[1])).encode();

            boolResult = regexpHelper(arg0, arg1);

            break;
        }

        }

        // Return the result as a BooleanAttribute.
        return EvaluationResult.getInstance(boolResult);
    }

    /**
     *
     */
    private boolean regexpHelper(String xpr, String str) {
        // the regular expression syntax required by XACML differs
        // from the syntax supported by java.util.regex.Pattern
        // in several ways; the next several code blocks transform
        // the XACML syntax into a semantically equivalent Pattern syntax

        StringBuffer buf = new StringBuffer(xpr);
        
        // in order to handle the requirement that the string is
        // considered to match the pattern if any substring matches
        // the pattern, we prepend ".*" and append ".*" to the reg exp,
        // but only if there isn't an anchor (^ or $) in place

        if (xpr.charAt(0) != '^')
            buf = buf.insert(0, ".*");

        if (xpr.charAt(xpr.length() - 1) != '$')
            buf = buf.insert(buf.length(), ".*");

        // in order to handle Unicode blocks, we replace all 
        // instances of "\p{Is" with "\p{In" in the reg exp

        int idx = -1;
        idx = buf.indexOf("\\p{Is", 0);
        while (idx != -1){
            buf = buf.replace(idx, idx+5, "\\p{In");
            idx = buf.indexOf("\\p{Is", idx);
        }

        // in order to handle Unicode blocks, we replace all instances 
        // of "\P{Is" with "\P{In" in the reg exp

        idx = -1;
        idx = buf.indexOf("\\P{Is", 0);
        while (idx != -1){
            buf = buf.replace(idx, idx+5, "\\P{In");
            idx = buf.indexOf("\\P{Is", idx);
        }
        
        // in order to handle character class subtraction, we
        // replace all instances of "-[" with "&&[^" in the reg exp

        idx = -1;
        idx = buf.indexOf("-[", 0);
        while (idx != -1){
            buf = buf.replace(idx, idx+2, "&&[^");
            idx = buf.indexOf("-[", idx);
        }

        return Pattern.matches(buf.toString(), str);
    }

}

⌨️ 快捷键说明

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