policyset.java

来自「sunxacml源码」· Java 代码 · 共 451 行 · 第 1/2 页

JAVA
451 行
字号
     *                                  contains an object that is not a
     *                                  <code>Rule</code>
     */
    public PolicySet(URI id, String version,
                     PolicyCombiningAlgorithm combiningAlg,
                     String description, Target target, List policyElements,
                     String defaultVersion, Set obligations, List parameters) {
        super(id, version, combiningAlg, description, target, defaultVersion,
              obligations, parameters);

        // check that the list contains only CombinerElements
        if (policyElements != null) {
            Iterator it = policyElements.iterator();
            while (it.hasNext()) {
                Object o = it.next();
                if (! (o instanceof PolicyCombinerElement))
                    throw new IllegalArgumentException("non-AbstractPolicy " +
                                                       "in policies");
            }
        }

        setChildren(policyElements);
    }

    /**
     * Creates a new PolicySet based on the given root node. This is 
     * private since every class is supposed to use a getInstance() method
     * to construct from a Node, but since we want some common code in the
     * parent class, we need this functionality in a constructor.
     */
    private PolicySet(Node root, PolicyFinder finder) throws ParsingException {
        super(root, "PolicySet", "PolicyCombiningAlgId");

        List policies = new ArrayList();
        HashMap policyParameters = new HashMap();
        HashMap policySetParameters = new HashMap();
        PolicyMetaData metaData = getMetaData();

        // collect the PolicySet-specific elements
        NodeList children = root.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            String name = child.getNodeName();

            if (name.equals("PolicySet")) {
                policies.add(PolicySet.getInstance(child, finder));
            } else if (name.equals("Policy")) {
                policies.add(Policy.getInstance(child));
            } else if (name.equals("PolicySetIdReference")) {
                policies.add(PolicyReference.getInstance(child, finder,
                                                         metaData));
            } else if (name.equals("PolicyIdReference")) {
                policies.add(PolicyReference.getInstance(child, finder,
                                                         metaData));
            } else if (name.equals("PolicyCombinerParameters")) {
                paramaterHelper(policyParameters, child, "Policy");
            } else if (name.equals("PolicySetCombinerParameters")) {
                paramaterHelper(policySetParameters, child, "PolicySet");
            }
        }

        // now make sure that we can match up any parameters we may have
        // found to a cooresponding Policy or PolicySet...
        List elements = new ArrayList();
        Iterator it = policies.iterator();

        // right now we have to go though each policy and based on several
        // possible cases figure out what paranmeters might apply...but
        // there should be a better way to do this

        while (it.hasNext()) {
            AbstractPolicy policy = (AbstractPolicy)(it.next());
            List list = null;

            if (policy instanceof Policy) {
                list = (List)(policyParameters.remove(policy.getId().
                                                      toString()));
            } else if (policy instanceof PolicySet) {
                list = (List)(policySetParameters.remove(policy.getId().
                                                         toString()));
            } else {
                PolicyReference ref = (PolicyReference)policy;
                String id = ref.getReference().toString();

                if (ref.getReferenceType() ==
                    PolicyReference.POLICY_REFERENCE)
                    list = (List)(policyParameters.remove(id));
                else
                    list = (List)(policySetParameters.remove(id));
            }

            elements.add(new PolicyCombinerElement(policy, list));
        }

        // ...and that there aren't extra parameters
        if (! policyParameters.isEmpty())
            throw new ParsingException("Unmatched parameters in Policy");
        if (! policySetParameters.isEmpty())
            throw new ParsingException("Unmatched parameters in PolicySet");

        // finally, set the list of Rules
        setChildren(elements);
    }

    /**
     * Private helper method that handles parsing a collection of
     * parameters
     */
    private void paramaterHelper(HashMap parameters, Node root,
                                 String prefix) throws ParsingException {
        String ref = root.getAttributes().getNamedItem(prefix + "IdRef").
            getNodeValue();
        
        if (parameters.containsKey(ref)) {
            List list = (List)(parameters.get(ref));
            parseParameters(list, root);
        } else {
            List list = new ArrayList();
            parseParameters(list, root);
            parameters.put(ref, list);
        }
    }

    /**
     * Private helper method that handles parsing a single parameter.
     */
    private void parseParameters(List parameters, Node root)
        throws ParsingException
    {
        NodeList nodes = root.getChildNodes();

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node.getNodeName().equals("CombinerParameter"))
                parameters.add(CombinerParameter.getInstance(node));
        }
    }

    /**
     * Creates an instance of a <code>PolicySet</code> object based on a
     * DOM node. The node must be the root of PolicySetType XML object,
     * otherwise an exception is thrown. This <code>PolicySet</code> will
     * not support references because it has no <code>PolicyFinder</code>.
     *
     * @param root the DOM root of a PolicySetType XML type
     *
     * @throws ParsingException if the PolicySetType is invalid
     */
    public static PolicySet getInstance(Node root) throws ParsingException {
        return getInstance(root, null);
    }

    /**
     * Creates an instance of a <code>PolicySet</code> object based on a
     * DOM node. The node must be the root of PolicySetType XML object,
     * otherwise an exception is thrown. The finder is used to handle
     * policy references.
     *
     * @param root the DOM root of a PolicySetType XML type
     * @param finder the <code>PolicyFinder</code> used to handle references
     *
     * @throws ParsingException if the PolicySetType is invalid
     */
    public static PolicySet getInstance(Node root, PolicyFinder finder)
        throws ParsingException
    {
        // first off, check that it's the right kind of node
        if (! root.getNodeName().equals("PolicySet")) {
            throw new ParsingException("Cannot create PolicySet from root of" +
                                       " type " + root.getNodeName());
        }

        return new PolicySet(root, finder);
    }

    /**
     * Encodes this <code>PolicySet</code> into its XML representation and
     * writes this encoding to the given <code>OutputStream</code> with no
     * indentation.
     *
     * @param output a stream into which the XML-encoded data is written
     */
    public void encode(OutputStream output) {
        encode(output, new Indenter(0));
    }

    /**
     * Encodes this <code>PolicySet</code> into its XML representation and
     * writes this encoding to the given <code>OutputStream</code> with
     * indentation.
     *
     * @param output a stream into which the XML-encoded data is written
     * @param indenter an object that creates indentation strings
     */
    public void encode(OutputStream output, Indenter indenter) {
        PrintStream out = new PrintStream(output);
        String indent = indenter.makeString();

        out.println(indent + "<PolicySet PolicySetId=\"" + getId().toString() +
                    "\" PolicyCombiningAlgId=\"" +
                    getCombiningAlg().getIdentifier().toString() +
                    "\">");
        
        indenter.in();
        String nextIndent = indenter.makeString();

        String description = getDescription();
        if (description != null)
            out.println(nextIndent + "<Description>" + description +
                        "</Description>");

        String version = getDefaultVersion();
        if (version != null)
            out.println("<PolicySetDefaults><XPathVersion>" + version +
                        "</XPathVersion></PolicySetDefaults>");

        getTarget().encode(output, indenter);
        encodeCommonElements(output, indenter);

        indenter.out();
        out.println(indent + "</PolicySet>");
    }

}

⌨️ 快捷键说明

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