public PolicySet read()

in modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicySetProcessor.java [121:250]


    public PolicySet read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException,
        XMLStreamException {
        PolicySet policySet = null;
        Monitor monitor = context.getMonitor();
        String policySetName = reader.getAttributeValue(null, NAME);
        String appliesTo = reader.getAttributeValue(null, APPLIES_TO);
        if (policySetName == null || appliesTo == null) {
            if (policySetName == null)
                error(monitor, "PolicySetNameMissing", reader);
            if (appliesTo == null)
                error(monitor, "PolicySetAppliesToMissing", reader);
            return policySet;
        }

        policySet = policyFactory.createPolicySet();
        policySet.setName(new QName(policySetName));

        //TODO: with 1.0 version of specs the applies to xpath is given related to the immediate
        //parent whereas the runtime evaluates the xpath aginst the composite element.  What the runtime
        //is doing is what the future version of the specs could be tending towards.  When that happens
        //this 'if' must be deleted
        if (appliesTo != null && !appliesTo.startsWith("/")) {
            appliesTo = "//" + appliesTo;
        }

        policySet.setAppliesTo(appliesTo);

        if (appliesTo != null) {
            try {
                XPath path = xpathHelper.newXPath();
                NamespaceContext nsContext = xpathHelper.getNamespaceContext(appliesTo, reader.getNamespaceContext());
                // path.setXPathFunctionResolver(new PolicyXPathFunctionResolver(context));
                XPathExpression expression = xpathHelper.compile(path, nsContext, appliesTo);
                policySet.setAppliesToXPathExpression(expression);
            } catch (XPathExpressionException e) {
                ContributionReadException ce = new ContributionReadException(e);
                error(monitor, "ContributionReadException", policySet, ce);
                //throw ce;
            }
        }

        String attachTo = reader.getAttributeValue(null, ATTACH_TO);
        if (attachTo != null) {
            try {
                XPath path = xpathHelper.newXPath();
                NamespaceContext nsContext = xpathHelper.getNamespaceContext(attachTo, reader.getNamespaceContext());
                path.setXPathFunctionResolver(new PolicyXPathFunctionResolver(nsContext));                
                                           
                attachTo = PolicyXPathFunction.normalize(attachTo,getSCAPrefix(nsContext));
                XPathExpression expression = xpathHelper.compile(path, nsContext, attachTo);
                policySet.setAttachTo(attachTo);
                policySet.setAttachToXPathExpression(expression);
            } catch (XPathExpressionException e) {
                ContributionReadException ce = new ContributionReadException(e);
                error(monitor, "ContributionReadException", policySet, ce);
                //throw ce;
            }

        }

        readProvidedIntents(policySet, reader);

        int event = reader.getEventType();
        QName name = null;
        reader.next();
        while (reader.hasNext()) {
            event = reader.getEventType();
            switch (event) {
                case START_ELEMENT: {
                    name = reader.getName();
                    if (POLICY_INTENT_MAP_QNAME.equals(name)) {
                        Intent mappedIntent = policyFactory.createIntent();
                        String provides = reader.getAttributeValue(null, PROVIDES);
                        if (provides != null) {
                            mappedIntent.setName(getQName(reader, PROVIDES));
                            if (policySet.getProvidedIntents().contains(mappedIntent)) {
                                readIntentMap(reader, policySet, mappedIntent, context);
                            } else {
                                error(monitor, "IntentNotSpecified", policySet, policySetName);
                            }
                        } else {
                            error(monitor, "IntentMapProvidesMissing", reader, policySetName);
                        }
                    } else if (POLICY_SET_REFERENCE_QNAME.equals(name)) {
                        PolicySet referredPolicySet = policyFactory.createPolicySet();
                        String referencename = reader.getAttributeValue(null, NAME);
                        if (referencename != null) {
                            referredPolicySet.setName(getQName(reader, NAME));
                            policySet.getReferencedPolicySets().add(referredPolicySet);
                        } else {
                            error(monitor, "PolicySetReferenceNameMissing", reader, policySetName);
                        }
                    } /*else if ( WS_POLICY_QNAME.equals(name) )  {
                        OMElement policyElement = loadElement(reader);
                        org.apache.neethi.Policy wsPolicy = PolicyEngine.getPolicy(policyElement);
                        policySet.getPolicies().add(wsPolicy);
                      } */else {
                        Object extension = extensionProcessor.read(reader, context);
                        if (extension != null) {
                            PolicyExpression exp = policyFactory.createPolicyExpression();
                            exp.setName(name);
                            exp.setPolicy(extension);
                            // check that all the policies in the policy set are 
                            // expressed in the same language. Compare against the 
                            // first expression we added
                            if ((policySet.getPolicies().size() > 0) && 
                                (!policySet.getPolicies().get(0).getName().getNamespaceURI().equals(name.getNamespaceURI()))) {
                                error(monitor, "PolicyLanguageMissmatch", reader, policySet.getName(), policySet
                                    .getPolicies().get(0).getName(), name);
                            } else {
                                policySet.getPolicies().add(exp);
                            }
                        }
                    }
                    break;
                }
            }
            if (event == END_ELEMENT) {
                if (POLICY_SET_QNAME.equals(reader.getName())) {
                    break;
                }
            }

            //Read the next element
            if (reader.hasNext()) {
                reader.next();
            }
        }
        return policySet;
    }