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

📄 engine.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            String[] values = this.union(operands);
            for (int i=0; i<values.length; i++) {
                list.add(values[i]);
            }
        } else if (id.equals("urn:oasis:names:tc:xacml:1.0:function:string-union")) {
            String[] values = this.union(operands);
            for (int i=0; i<values.length; i++) {
                list.add(values[i]);
            }
        } else if (id.equals("urn:oasis:names:tc:xacml:1.0:function:integer-one-and-only")) {
            int value = this.getIntegerOneAndOnly(operands);
            list.add(new Integer(value).toString());
        } else if (id.equals("urn:oasis:names:tc:xacml:1.0:function:double-one-and-only")) {
            int value = this.getIntegerOneAndOnly(operands);
            list.add(new Integer(value).toString());
        } else if (id.equals("urn:oasis:names:tc:xacml:1.0:function:string-one-and-only")) {
            int value = this.getIntegerOneAndOnly(operands);
            list.add(new Integer(value).toString());
        } else throw new EngineException("unknown function:"+id);
        String[] res = new String[list.size()];
        res = (String[])list.toArray(res);
        return res;
    }
    
    /**
     * get a set of values in String[] from context 
     * according to attribute name and its data type
     */
    
    private String[] getValues(String name,String data,Element context,int type) throws EngineException {
        logger.debug("to catch "+name+" for "+data);
        ArrayList list = new ArrayList();
        int attributeType = type;
        NodeList nodeList = null;
        if (attributeType==this.SUBJECT) nodeList = context.getElementsByTagName("Subject");
        else if (attributeType==this.RESOURCE) nodeList = context.getElementsByTagName("Resource");
        else if (attributeType==this.ACTION) nodeList = context.getElementsByTagName("Action");
        else if (attributeType==this.ENVIRONMENT) nodeList = context.getElementsByTagName("Environment");
        else throw new EngineException("invalid attribute type in the request context");
        
        for (int i=0; i<nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            NodeList list1 = node.getChildNodes();
            for (int j=0; j<list1.getLength(); j++) {
                Node attribute = list1.item(j);
                if (Text.class.isAssignableFrom(attribute.getClass())) continue;
                if (!attribute.getNodeName().equals("Attribute")) throw new EngineException("unexpected XML element (should be Attribute)");
                Element attr = (Element)attribute;
                String attributeId = attr.getAttribute("AttributeId");
                String dataType = attr.getAttribute("DataType");
                if (name.equals(attributeId) && data.equals(dataType)) {
                    NodeList list2 = attribute.getChildNodes();
                    for (int k=0; k<list2.getLength(); k++) {
                        Node attributeValue = list2.item(k);
                        if (Text.class.isAssignableFrom(attributeValue.getClass())) continue;
                        if (!attributeValue.getNodeName().equals("AttributeValue"))
                            throw new EngineException("unexpected XML element (should be AttributeValue)");
                        NodeList list3 = attributeValue.getChildNodes();
                        if (list3.getLength()!=1)
                            throw new EngineException("unexpected value (should be text)");
                        Node val = list3.item(0);
                        if (Text.class.isAssignableFrom(val.getClass())) {
                            list.add(val.getNodeValue());
                        } else throw new EngineException("unexpected value (should be text)");
                    }
                }
            }
        }
        String[] res = new String[list.size()];
        res = (String[])list.toArray(res);
        return res;
    }
    
    private int integerAdd(ArrayList oprands) throws EngineException {
        if (oprands.size()<2) throw new EngineException("integer-add needs at least two operands");
        int result = 0;
        for (Iterator i=oprands.iterator();i.hasNext();) {
            String[] values = (String[])i.next();
            int x = new Integer(values[0]).intValue();
            result += x;
        }
        return result;
    }
    
    private double doubleAdd(ArrayList oprands) throws EngineException {
        if (oprands.size()<2) throw new EngineException("double-add needs at least two operands");
        double result = 0;
        for (Iterator i=oprands.iterator();i.hasNext();) {
            String[] values = (String[])i.next();
            double x = new Float(values[0]).doubleValue();
            result += x;
        }
        return result;
    }
    
    private int integerSubtract(ArrayList oprands) throws EngineException {
        if (oprands.size()<2) throw new EngineException("int-substract needs at least two operands");
        String[] values = (String[])oprands.get(0);
        int result = new Integer(values[0]).intValue();;
        for (int i=1; i<oprands.size();i++) {
            values = (String[])oprands.get(i);
            int x = new Integer(values[0]).intValue();
            result -= x;
        }
        return result;
    }
    
    private double doubleSubtract(ArrayList oprands) throws EngineException {
        if (oprands.size()<2) throw new EngineException("double-substract needs at least two operands");
        String[] values = (String[])oprands.get(0);
        double result = new Float(values[0]).doubleValue();;
        for (int i=1; i<oprands.size();i++) {
            values = (String[])oprands.get(i);
            double x = new Float(values[0]).doubleValue();
            result -= x;
        }
        return result;
    }

    private int integerMultiply(ArrayList oprands) throws EngineException {
        if (oprands.size()<2) throw new EngineException("integer-multiply needs at least two operands");
        int result = 1;
        for (Iterator i=oprands.iterator();i.hasNext();) {
            String[] values = (String[])i.next();
            int x = new Integer(values[0]).intValue();
            result *= x;
        }
        return result;
    }
    
    private double doubleMultiply(ArrayList oprands) throws EngineException {
        if (oprands.size()<2) throw new EngineException("double-multiply needs at least two operands");
        double result = 1;
        for (Iterator i=oprands.iterator();i.hasNext();) {
            String[] values = (String[])i.next();
            double x = new Float(values[0]).doubleValue();
            result *= x;
        }
        return result;
    }
    
    private int integerDivide(ArrayList oprands) throws EngineException {
        if (oprands.size()<2) throw new EngineException("int-divide needs at least two operands");
        String[] values = (String[])oprands.get(0);
        int result = new Integer(values[0]).intValue();;
        for (int i=1; i<oprands.size();i++) {
            values = (String[])oprands.get(i);
            int x = new Integer(values[0]).intValue();
            result /= x;
        }
        return result;
    }
    
    private double doubleDivide(ArrayList oprands) throws EngineException {
        if (oprands.size()<2) throw new EngineException("double-divide needs at least two operands");
        String[] values = (String[])oprands.get(0);
        double result = new Float(values[0]).doubleValue();;
        for (int i=1; i<oprands.size();i++) {
            values = (String[])oprands.get(i);
            double x = new Float(values[0]).doubleValue();
            result /= x;
        }
        return result;
    }

    private String concate(ArrayList oprands) throws EngineException {
        if (oprands.size()<2) throw new EngineException("string-concatenation needs at least two operands");
        String result = new String();
        for (Iterator i=oprands.iterator();i.hasNext();) {
            String[] values = (String[])i.next();
            result += values[0];
        }
        return result;
    }

    private String[] intersection(ArrayList oprands) throws EngineException {
        if (oprands.size()<2) throw new EngineException("set intersection needs at least two operands");
        String[] values = (String[])oprands.get(0);
        HashSet tmp1 = new HashSet();
        for (int i=0; i<values.length; i++) {
            tmp1.add(values[i]);
        }
        HashSet tmp = null;
        for (int i=1; i<oprands.size();i++) {
            values = (String[])oprands.get(i);
            HashSet tmp2 = new HashSet();
            for (int j=0; j<values.length; j++) {
            tmp2.add(values[j]);
            }
            tmp = new HashSet();
            for (Iterator j=tmp1.iterator();j.hasNext();) {
                String obj = (String)j.next();
                if (tmp2.contains(obj)) tmp.add(obj);
            }
            tmp1=tmp;
        }
        String[] result = new String[tmp.size()];
        result = (String[])tmp.toArray(result);
        return result;
    }
    
    private String[] union(ArrayList oprands) throws EngineException {
        if (oprands.size()<2) throw new EngineException("set intersection needs at least two operands");
        String[] values = (String[])oprands.get(0);
        HashSet tmp = new HashSet();
        for (int i=0; i<values.length; i++) {
            tmp.add(values[i]);
        }
        for (int i=1; i<oprands.size();i++) {
            values = (String[])oprands.get(i);
            for (int j=0; j<values.length;j++) {
                if (tmp.contains(values[j])) continue;
                else tmp.add(values[j]);
            }
        }
        String[] result = new String[tmp.size()];
        result = (String[])tmp.toArray(result);
        return result;
    }
    
    private int getIntegerOneAndOnly(ArrayList oprands) throws EngineException {
        if (oprands.size()!=1) throw new EngineException("integer-one-and-only limits the number of integers to 1");
        String[] arg = (String[])oprands.get(0);      
        return new Integer(arg[0]).intValue();
    }
    
    private double getDoubleOneAndOnly(ArrayList oprands) throws EngineException {
        if (oprands.size()!=1) throw new EngineException("double-one-and-only limits the number of integers to 1");
        String[] arg = (String[])oprands.get(0);      
        return new Float(arg[0]).doubleValue();
    }
    
    private String getStringOneAndOnly(ArrayList oprands) throws EngineException {
        if (oprands.size()!=1) throw new EngineException("string-one-and-only limits the number of integers to 1");
        String[] arg = (String[])oprands.get(0);      
        return arg[0];
    }
}

⌨️ 快捷键说明

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