FileDocCategorySizeDatePackage
XmlPolicyModelUnmarshaller.javaAPI DocExample16696Tue May 29 16:56:40 BST 2007com.sun.xml.ws.policy.sourcemodel

XmlPolicyModelUnmarshaller

public final class XmlPolicyModelUnmarshaller extends PolicyModelUnmarshaller
author
Marek Potociar

Fields Summary
private static final com.sun.xml.ws.policy.privateutil.PolicyLogger
LOGGER
Constructors Summary
XmlPolicyModelUnmarshaller()
Creates a new instance of XmlPolicyModelUnmarshaller

    
                 
     
        // nothing to initialize
    
Methods Summary
private voidcheckEndTagName(javax.xml.namespace.QName expected, javax.xml.stream.events.EndElement element)
Method checks whether the actual name of the end tag is equal to the expected name - the name of currently unmarshalled XML policy model element. Throws exception, if the two FQNs are not equal as expected.

        final QName actual = element.getName();
        if (!expected.equals(actual)) {
            throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0003_UNMARSHALLING_FAILED_END_TAG_DOES_NOT_MATCH(expected, actual)));
        }
    
private javax.xml.stream.XMLEventReadercreateXMLEventReader(java.lang.Object storage)
Method checks if the storage type is supported and transforms it to XMLEventReader instance which is then returned. Throws PolicyException if the transformation is not succesfull or if the storage type is not supported.

        if (!(storage instanceof Reader)) {
            throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0022_STORAGE_TYPE_NOT_SUPPORTED(storage.getClass().getName())));
        }
        
        try {
            return XMLInputFactory.newInstance().createXMLEventReader((Reader) storage);
        } catch (XMLStreamException e) {
            throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0014_UNABLE_TO_INSTANTIATE_READER_FOR_STORAGE(), e));
        }
    
private javax.xml.stream.events.AttributegetAttributeByName(javax.xml.stream.events.StartElement element, javax.xml.namespace.QName attributeName)

        // call standard API method to retrieve the attribute by name
        Attribute attribute = element.getAttributeByName(attributeName);
        
        // try to find the attribute without a prefix.
        if (attribute == null) {
            final String localAttributeName = attributeName.getLocalPart();
            final Iterator iterator = element.getAttributes();
            while (iterator.hasNext()) {
                final Attribute nextAttribute = (Attribute) iterator.next();
                final QName aName = nextAttribute.getName();
                final boolean attributeFoundByWorkaround = aName.equals(attributeName) || (aName.getLocalPart().equals(localAttributeName) && (aName.getPrefix() == null || "".equals(aName.getPrefix())));
                if (attributeFoundByWorkaround) {
                    attribute = nextAttribute;
                    break;
                }
            }
        }
        
        return attribute;
    
private java.lang.StringBufferprocessCharacters(ModelNode.Type currentNodeType, javax.xml.stream.events.Characters characters, java.lang.StringBuffer currentValueBuffer)

        if (characters.isWhiteSpace()) {
            return currentValueBuffer;
        } else {
            final StringBuffer buffer = (currentValueBuffer == null) ? new StringBuffer() : currentValueBuffer;
            final String data = characters.getData();
            if (currentNodeType == ModelNode.Type.ASSERTION || currentNodeType == ModelNode.Type.ASSERTION_PARAMETER_NODE) {
                return buffer.append(data);
            } else {
                throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0009_UNEXPECTED_CDATA_ON_SOURCE_MODEL_NODE(currentNodeType, data)));
            }
        }
    
public PolicySourceModelunmarshalModel(java.lang.Object storage)
See {@link PolicyModelUnmarshaller#unmarshalModel(Object) base method documentation}.

        final XMLEventReader reader = createXMLEventReader(storage);
        PolicySourceModel model = null;
        
        loop : while (reader.hasNext()) {
            try {
                final XMLEvent event = reader.peek();
                switch (event.getEventType()) {
                    case XMLStreamConstants.START_DOCUMENT:
                    case XMLStreamConstants.COMMENT:
                        reader.nextEvent();
                        break; // skipping the comments and start document events
                    case XMLStreamConstants.CHARACTERS:
                        processCharacters(ModelNode.Type.POLICY, event.asCharacters(), null);
                        // we advance the reader only if there is no exception thrown from 
                        // the processCharacters() call. Otherwise we don't modify the stream
                        reader.nextEvent(); 
                        break;
                    case XMLStreamConstants.START_ELEMENT :
                        if (ModelNode.Type.POLICY.asQName().equals(event.asStartElement().getName())) {
                            final StartElement element = reader.nextEvent().asStartElement();
                            
                            Attribute policyId = getAttributeByName(element, PolicyConstants.WSU_ID);
                            final Attribute xmlId = getAttributeByName(element, PolicyConstants.XML_ID);
                            
                            if (policyId == null) {
                                policyId = xmlId;
                            } else if (xmlId != null) {
                                throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0058_MULTIPLE_POLICY_IDS_NOT_ALLOWED()));
                            }
                            
                            final Attribute policyName = getAttributeByName(element, PolicyConstants.POLICY_NAME);
                            
                            model = PolicySourceModel.createPolicySourceModel((policyId == null) ? null : policyId.getValue(), (policyName == null) ? null : policyName.getValue());
                            
                            unmarshalNodeContent(model.getRootNode(), event.asStartElement(), reader);
                            break loop;
                        }
                        // else (this is not a policy tag) -> go to default => throw exception
                    default:
                        throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0048_POLICY_ELEMENT_EXPECTED_FIRST()));
                }
            } catch (XMLStreamException e) {
                throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0068_FAILED_TO_UNMARSHALL_POLICY_EXPRESSION(), e));
            }
        }
        return model;
    
private voidunmarshalNodeContent(ModelNode lastNode, javax.xml.stream.events.StartElement lastStartElement, javax.xml.stream.XMLEventReader reader)

        final QName lastElementName = lastStartElement.getName();
        StringBuffer valueBuffer = null;
        
        loop : while (reader.hasNext()) {
            try {
                final XMLEvent xmlParserEvent = reader.nextEvent();
                switch (xmlParserEvent.getEventType()) {
                    case XMLStreamConstants.COMMENT:
                        break; // skipping the comments
                        
                    case XMLStreamConstants.CHARACTERS:
                        valueBuffer = processCharacters(lastNode.getType(), xmlParserEvent.asCharacters(), valueBuffer);
                        break;
                        
                    case XMLStreamConstants.END_ELEMENT:
                        checkEndTagName(lastElementName, xmlParserEvent.asEndElement());
                        break loop; // data exctraction for currently processed policy node is done
                        
                    case XMLStreamConstants.START_ELEMENT:
                        final StartElement childElement = xmlParserEvent.asStartElement();
                        final QName childElementName = childElement.getName();
                        
                        ModelNode childNode;
                        if (lastNode.getType() == ModelNode.Type.ASSERTION_PARAMETER_NODE) {
                            childNode = lastNode.createChildAssertionParameterNode();
                        } else {
                            if (ModelNode.Type.POLICY.asQName().equals(childElementName)) {
                                childNode = lastNode.createChildPolicyNode();
                            } else if (ModelNode.Type.ALL.asQName().equals(childElementName)) {
                                childNode = lastNode.createChildAllNode();
                            } else if (ModelNode.Type.EXACTLY_ONE.asQName().equals(childElementName)) {
                                childNode = lastNode.createChildExactlyOneNode();
                            } else if (ModelNode.Type.POLICY_REFERENCE.asQName().equals(childElementName)) {
                                final Attribute uri = getAttributeByName(childElement, PolicyReferenceData.ATTRIBUTE_URI);
                                if (uri == null) {
                                    throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0040_POLICY_REFERENCE_URI_ATTR_NOT_FOUND()));
                                } else {
                                    try {
                                        final URI reference = new URI(uri.getValue());
                                        final Attribute digest = getAttributeByName(childElement, PolicyReferenceData.ATTRIBUTE_DIGEST);
                                        PolicyReferenceData refData;
                                        if (digest == null) {
                                            refData = new PolicyReferenceData(reference);
                                        } else {
                                            final Attribute digestAlgorithm = getAttributeByName(childElement, PolicyReferenceData.ATTRIBUTE_DIGEST_ALGORITHM);
                                            URI algorithmRef = null;
                                            if (digestAlgorithm != null) {
                                                algorithmRef = new URI(digestAlgorithm.getValue());
                                            }
                                            refData = new PolicyReferenceData(reference, digest.getValue(), algorithmRef);
                                        }
                                        childNode = lastNode.createChildPolicyReferenceNode(refData);
                                    } catch (URISyntaxException e) {
                                        throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0012_UNABLE_TO_UNMARSHALL_POLICY_MALFORMED_URI(), e));
                                    }
                                }
                            } else {
                                if (lastNode.isAssertionRelatedNode()) {
                                    childNode = lastNode.createChildAssertionParameterNode();
                                } else {
                                    childNode = lastNode.createChildAssertionNode();
                                }
                            }
                        }
                        
                        unmarshalNodeContent(childNode, childElement, reader);
                        break;
                    default:
                        throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0011_UNABLE_TO_UNMARSHALL_POLICY_XML_ELEM_EXPECTED()));
                }
            } catch (XMLStreamException e) {
                throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0068_FAILED_TO_UNMARSHALL_POLICY_EXPRESSION(), e));
            }
        }
        
        if (lastNode.isAssertionRelatedNode()) {
            // finish assertion node processing: create and set assertion data...
            final Map<QName, String> attributeMap = new HashMap<QName, String>();
            final Iterator iterator = lastStartElement.getAttributes();
            while (iterator.hasNext()) {
                final Attribute nextAttribute = (Attribute) iterator.next();
                final QName name = nextAttribute.getName();
                if (attributeMap.containsKey(name)) {
                    throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0059_MULTIPLE_ATTRS_WITH_SAME_NAME_DETECTED_FOR_ASSERTION(nextAttribute.getName(), lastElementName)));
                } else {
                    attributeMap.put(name , nextAttribute.getValue());
                }
            }
            final AssertionData nodeData = new AssertionData(lastElementName, (valueBuffer == null) ? null : valueBuffer.toString(), attributeMap, lastNode.getType());
            
            // check visibility value syntax if present...
            if (nodeData.containsAttribute(PolicyConstants.VISIBILITY_ATTRIBUTE)) {
                final String visibilityValue = nodeData.getAttributeValue(PolicyConstants.VISIBILITY_ATTRIBUTE);
                if (!PolicyConstants.VISIBILITY_VALUE_PRIVATE.equals(visibilityValue)) {
                    throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0004_UNEXPECTED_VISIBILITY_ATTR_VALUE(visibilityValue)));
                }
            }
            
            lastNode.setOrReplaceNodeData(nodeData);
        }