|
Java example source code file (XmlPolicyModelUnmarshaller.java)
The XmlPolicyModelUnmarshaller.java Java example source code/* * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.xml.internal.ws.policy.sourcemodel; import com.sun.xml.internal.ws.policy.PolicyConstants; import com.sun.xml.internal.ws.policy.PolicyException; import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages; import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger; import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.NamespaceVersion; import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.XmlToken; import java.io.Reader; import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.xml.namespace.QName; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.Characters; import javax.xml.stream.events.EndElement; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; /** * Unmarshal XML policy expressions. * * @author Marek Potociar * @author Fabian Ritzmann */ public class XmlPolicyModelUnmarshaller extends PolicyModelUnmarshaller { private static final PolicyLogger LOGGER = PolicyLogger.getLogger(XmlPolicyModelUnmarshaller.class); /** * Creates a new instance of XmlPolicyModelUnmarshaller */ protected XmlPolicyModelUnmarshaller() { // nothing to initialize } /** * See {@link PolicyModelUnmarshaller#unmarshalModel(Object) base method documentation}. */ public PolicySourceModel unmarshalModel(final Object storage) throws PolicyException { 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 (NamespaceVersion.resolveAsToken(event.asStartElement().getName()) == XmlToken.Policy) { StartElement rootElement = reader.nextEvent().asStartElement(); model = initializeNewModel(rootElement); unmarshalNodeContent(model.getNamespaceVersion(), model.getRootNode(), rootElement.getName(), reader); break loop; } else { throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0048_POLICY_ELEMENT_EXPECTED_FIRST())); } 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; } /** * Allow derived classes to pass in a custom instance of PolicySourceModel. * * @param nsVersion * @param id * @param name * @return */ protected PolicySourceModel createSourceModel(NamespaceVersion nsVersion, String id, String name) { return PolicySourceModel.createPolicySourceModel(nsVersion, id, name); } private PolicySourceModel initializeNewModel(final StartElement element) throws PolicyException, XMLStreamException { PolicySourceModel model; final NamespaceVersion nsVersion = NamespaceVersion.resolveVersion(element.getName().getNamespaceURI()); final Attribute policyName = getAttributeByName(element, nsVersion.asQName(XmlToken.Name)); final Attribute xmlId = getAttributeByName(element, PolicyConstants.XML_ID); Attribute policyId = getAttributeByName(element, PolicyConstants.WSU_ID); if (policyId == null) { policyId = xmlId; } else if (xmlId != null) { throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0058_MULTIPLE_POLICY_IDS_NOT_ALLOWED())); } model = createSourceModel(nsVersion, (policyId == null) ? null : policyId.getValue(), (policyName == null) ? null : policyName.getValue()); return model; } private ModelNode addNewChildNode(final NamespaceVersion nsVersion, final ModelNode parentNode, final StartElement childElement) throws PolicyException { ModelNode childNode; final QName childElementName = childElement.getName(); if (parentNode.getType() == ModelNode.Type.ASSERTION_PARAMETER_NODE) { childNode = parentNode.createChildAssertionParameterNode(); } else { XmlToken token = NamespaceVersion.resolveAsToken(childElementName); switch (token) { case Policy: childNode = parentNode.createChildPolicyNode(); break; case All: childNode = parentNode.createChildAllNode(); break; case ExactlyOne: childNode = parentNode.createChildExactlyOneNode(); break; case PolicyReference: final Attribute uri = getAttributeByName(childElement, nsVersion.asQName(XmlToken.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, nsVersion.asQName(XmlToken.Digest)); PolicyReferenceData refData; if (digest == null) { refData = new PolicyReferenceData(reference); } else { final Attribute digestAlgorithm = getAttributeByName(childElement, nsVersion.asQName(XmlToken.DigestAlgorithm)); URI algorithmRef = null; if (digestAlgorithm != null) { algorithmRef = new URI(digestAlgorithm.getValue()); } refData = new PolicyReferenceData(reference, digest.getValue(), algorithmRef); } childNode = parentNode.createChildPolicyReferenceNode(refData); } catch (URISyntaxException e) { throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0012_UNABLE_TO_UNMARSHALL_POLICY_MALFORMED_URI(), e)); } } break; default: if (parentNode.isDomainSpecific()) { childNode = parentNode.createChildAssertionParameterNode(); } else { childNode = parentNode.createChildAssertionNode(); } } } return childNode; } private void parseAssertionData(NamespaceVersion nsVersion, String value, ModelNode childNode, final StartElement childElement) throws IllegalArgumentException, PolicyException { // finish assertion node processing: create and set assertion data... final Map<QName, String> attributeMap = new HashMap Other Java examples (source code examples)Here is a short list of links related to this Java XmlPolicyModelUnmarshaller.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.