aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.policy.sourcemodel; aoqi@0: aoqi@0: import com.sun.xml.internal.txw2.TXW; aoqi@0: import com.sun.xml.internal.txw2.TypedXmlWriter; aoqi@0: import com.sun.xml.internal.txw2.output.StaxSerializer; aoqi@0: import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.XmlToken; aoqi@0: import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.NamespaceVersion; aoqi@0: import com.sun.xml.internal.ws.policy.PolicyConstants; aoqi@0: import com.sun.xml.internal.ws.policy.PolicyException; aoqi@0: import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages; aoqi@0: import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger; aoqi@0: aoqi@0: import java.util.Collection; aoqi@0: import java.util.Map; aoqi@0: import java.util.Map.Entry; aoqi@0: import javax.xml.namespace.QName; aoqi@0: import javax.xml.stream.XMLStreamWriter; aoqi@0: aoqi@0: aoqi@0: public final class XmlPolicyModelMarshaller extends PolicyModelMarshaller { aoqi@0: aoqi@0: private static final PolicyLogger LOGGER = PolicyLogger.getLogger(XmlPolicyModelMarshaller.class); aoqi@0: aoqi@0: private final boolean marshallInvisible; aoqi@0: aoqi@0: aoqi@0: XmlPolicyModelMarshaller(boolean marshallInvisible) { aoqi@0: this.marshallInvisible = marshallInvisible; aoqi@0: } aoqi@0: aoqi@0: public void marshal(final PolicySourceModel model, final Object storage) throws PolicyException { aoqi@0: if (storage instanceof StaxSerializer) { aoqi@0: marshal(model, (StaxSerializer) storage); aoqi@0: } else if (storage instanceof TypedXmlWriter) { aoqi@0: marshal(model, (TypedXmlWriter) storage); aoqi@0: } else if (storage instanceof XMLStreamWriter) { aoqi@0: marshal(model, (XMLStreamWriter) storage); aoqi@0: } else { aoqi@0: throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0022_STORAGE_TYPE_NOT_SUPPORTED(storage.getClass().getName()))); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void marshal(final Collection models, final Object storage) throws PolicyException { aoqi@0: for (PolicySourceModel model : models) { aoqi@0: marshal(model, storage); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Marshal a policy onto the given StaxSerializer. aoqi@0: * aoqi@0: * @param model A policy source model. aoqi@0: * @param writer A Stax serializer. aoqi@0: * @throws PolicyException If marshalling failed. aoqi@0: */ aoqi@0: private void marshal(final PolicySourceModel model, final StaxSerializer writer) throws PolicyException { aoqi@0: final TypedXmlWriter policy = TXW.create(model.getNamespaceVersion().asQName(XmlToken.Policy), TypedXmlWriter.class, writer); aoqi@0: aoqi@0: marshalDefaultPrefixes(model, policy); aoqi@0: marshalPolicyAttributes(model, policy); aoqi@0: marshal(model.getNamespaceVersion(), model.getRootNode(), policy); aoqi@0: policy.commit(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Marshal a policy onto the given TypedXmlWriter. aoqi@0: * aoqi@0: * @param model A policy source model. aoqi@0: * @param writer A typed XML writer. aoqi@0: * @throws PolicyException If marshalling failed. aoqi@0: */ aoqi@0: private void marshal(final PolicySourceModel model, final TypedXmlWriter writer) throws PolicyException { aoqi@0: final TypedXmlWriter policy = writer._element(model.getNamespaceVersion().asQName(XmlToken.Policy), TypedXmlWriter.class); aoqi@0: aoqi@0: marshalDefaultPrefixes(model, policy); aoqi@0: marshalPolicyAttributes(model, policy); aoqi@0: marshal(model.getNamespaceVersion(), model.getRootNode(), policy); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Marshal a policy onto the given XMLStreamWriter. aoqi@0: * aoqi@0: * @param model A policy source model. aoqi@0: * @param writer An XML stream writer. aoqi@0: * @throws PolicyException If marshalling failed. aoqi@0: */ aoqi@0: private void marshal(final PolicySourceModel model, final XMLStreamWriter writer) throws PolicyException { aoqi@0: final StaxSerializer serializer = new StaxSerializer(writer); aoqi@0: final TypedXmlWriter policy = TXW.create(model.getNamespaceVersion().asQName(XmlToken.Policy), TypedXmlWriter.class, serializer); aoqi@0: aoqi@0: marshalDefaultPrefixes(model, policy); aoqi@0: marshalPolicyAttributes(model, policy); aoqi@0: marshal(model.getNamespaceVersion(), model.getRootNode(), policy); aoqi@0: policy.commit(); aoqi@0: serializer.flush(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Marshal the Policy root element attributes onto the TypedXmlWriter. aoqi@0: * aoqi@0: * @param model The policy source model. aoqi@0: * @param writer The typed XML writer. aoqi@0: */ aoqi@0: private static void marshalPolicyAttributes(final PolicySourceModel model, final TypedXmlWriter writer) { aoqi@0: final String policyId = model.getPolicyId(); aoqi@0: if (policyId != null) { aoqi@0: writer._attribute(PolicyConstants.WSU_ID, policyId); aoqi@0: } aoqi@0: aoqi@0: final String policyName = model.getPolicyName(); aoqi@0: if (policyName != null) { aoqi@0: writer._attribute(model.getNamespaceVersion().asQName(XmlToken.Name), policyName); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Marshal given ModelNode and child elements on given TypedXmlWriter. aoqi@0: * aoqi@0: * @param nsVersion The WS-Policy version. aoqi@0: * @param rootNode The ModelNode that is marshalled. aoqi@0: * @param writer The TypedXmlWriter onto which the content of the rootNode is marshalled. aoqi@0: */ aoqi@0: private void marshal(final NamespaceVersion nsVersion, final ModelNode rootNode, final TypedXmlWriter writer) { aoqi@0: for (ModelNode node : rootNode) { aoqi@0: final AssertionData data = node.getNodeData(); aoqi@0: if (marshallInvisible || data == null || !data.isPrivateAttributeSet()) { aoqi@0: TypedXmlWriter child = null; aoqi@0: if (data == null) { aoqi@0: child = writer._element(nsVersion.asQName(node.getType().getXmlToken()), TypedXmlWriter.class); aoqi@0: } else { aoqi@0: child = writer._element(data.getName(), TypedXmlWriter.class); aoqi@0: final String value = data.getValue(); aoqi@0: if (value != null) { aoqi@0: child._pcdata(value); aoqi@0: } aoqi@0: if (data.isOptionalAttributeSet()) { aoqi@0: child._attribute(nsVersion.asQName(XmlToken.Optional), Boolean.TRUE); aoqi@0: } aoqi@0: if (data.isIgnorableAttributeSet()) { aoqi@0: child._attribute(nsVersion.asQName(XmlToken.Ignorable), Boolean.TRUE); aoqi@0: } aoqi@0: for (Entry entry : data.getAttributesSet()) { aoqi@0: child._attribute(entry.getKey(), entry.getValue()); aoqi@0: } aoqi@0: } aoqi@0: marshal(nsVersion, node, child); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Write default prefixes onto the given TypedXmlWriter aoqi@0: * aoqi@0: * @param model The policy source model. May not be null. aoqi@0: * @param writer The TypedXmlWriter. May not be null. aoqi@0: * @throws PolicyException If the creation of the prefix to namespace URI map failed. aoqi@0: */ aoqi@0: private void marshalDefaultPrefixes(final PolicySourceModel model, final TypedXmlWriter writer) throws PolicyException { aoqi@0: final Map nsMap = model.getNamespaceToPrefixMapping(); aoqi@0: if (!marshallInvisible && nsMap.containsKey(PolicyConstants.SUN_POLICY_NAMESPACE_URI)) { aoqi@0: nsMap.remove(PolicyConstants.SUN_POLICY_NAMESPACE_URI); aoqi@0: } aoqi@0: for (Map.Entry nsMappingEntry : nsMap.entrySet()) { aoqi@0: writer._namespace(nsMappingEntry.getKey(), nsMappingEntry.getValue()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: }