src/share/jaxws_classes/com/sun/xml/internal/ws/policy/sourcemodel/XmlPolicyModelMarshaller.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/policy/sourcemodel/XmlPolicyModelMarshaller.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,193 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.policy.sourcemodel;
    1.30 +
    1.31 +import com.sun.xml.internal.txw2.TXW;
    1.32 +import com.sun.xml.internal.txw2.TypedXmlWriter;
    1.33 +import com.sun.xml.internal.txw2.output.StaxSerializer;
    1.34 +import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.XmlToken;
    1.35 +import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.NamespaceVersion;
    1.36 +import com.sun.xml.internal.ws.policy.PolicyConstants;
    1.37 +import com.sun.xml.internal.ws.policy.PolicyException;
    1.38 +import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages;
    1.39 +import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger;
    1.40 +
    1.41 +import java.util.Collection;
    1.42 +import java.util.Map;
    1.43 +import java.util.Map.Entry;
    1.44 +import javax.xml.namespace.QName;
    1.45 +import javax.xml.stream.XMLStreamWriter;
    1.46 +
    1.47 +
    1.48 +public final class XmlPolicyModelMarshaller extends PolicyModelMarshaller {
    1.49 +
    1.50 +    private static final PolicyLogger LOGGER = PolicyLogger.getLogger(XmlPolicyModelMarshaller.class);
    1.51 +
    1.52 +    private final boolean marshallInvisible;
    1.53 +
    1.54 +
    1.55 +    XmlPolicyModelMarshaller(boolean marshallInvisible) {
    1.56 +        this.marshallInvisible = marshallInvisible;
    1.57 +    }
    1.58 +
    1.59 +    public void marshal(final PolicySourceModel model, final Object storage) throws PolicyException {
    1.60 +        if (storage instanceof StaxSerializer) {
    1.61 +            marshal(model, (StaxSerializer) storage);
    1.62 +        } else if (storage instanceof TypedXmlWriter) {
    1.63 +            marshal(model, (TypedXmlWriter) storage);
    1.64 +        } else if (storage instanceof XMLStreamWriter) {
    1.65 +            marshal(model, (XMLStreamWriter) storage);
    1.66 +        } else {
    1.67 +            throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0022_STORAGE_TYPE_NOT_SUPPORTED(storage.getClass().getName())));
    1.68 +        }
    1.69 +    }
    1.70 +
    1.71 +    public void marshal(final Collection<PolicySourceModel> models, final Object storage) throws PolicyException {
    1.72 +        for (PolicySourceModel model : models) {
    1.73 +            marshal(model, storage);
    1.74 +        }
    1.75 +    }
    1.76 +
    1.77 +    /**
    1.78 +     * Marshal a policy onto the given StaxSerializer.
    1.79 +     *
    1.80 +     * @param model A policy source model.
    1.81 +     * @param writer A Stax serializer.
    1.82 +     * @throws PolicyException If marshalling failed.
    1.83 +     */
    1.84 +    private void marshal(final PolicySourceModel model, final StaxSerializer writer) throws PolicyException {
    1.85 +        final TypedXmlWriter policy = TXW.create(model.getNamespaceVersion().asQName(XmlToken.Policy), TypedXmlWriter.class, writer);
    1.86 +
    1.87 +        marshalDefaultPrefixes(model, policy);
    1.88 +        marshalPolicyAttributes(model, policy);
    1.89 +        marshal(model.getNamespaceVersion(), model.getRootNode(), policy);
    1.90 +        policy.commit();
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * Marshal a policy onto the given TypedXmlWriter.
    1.95 +     *
    1.96 +     * @param model A policy source model.
    1.97 +     * @param writer A typed XML writer.
    1.98 +     * @throws PolicyException If marshalling failed.
    1.99 +     */
   1.100 +    private void marshal(final PolicySourceModel model, final TypedXmlWriter writer) throws PolicyException {
   1.101 +        final TypedXmlWriter policy = writer._element(model.getNamespaceVersion().asQName(XmlToken.Policy), TypedXmlWriter.class);
   1.102 +
   1.103 +        marshalDefaultPrefixes(model, policy);
   1.104 +        marshalPolicyAttributes(model, policy);
   1.105 +        marshal(model.getNamespaceVersion(), model.getRootNode(), policy);
   1.106 +    }
   1.107 +
   1.108 +    /**
   1.109 +     * Marshal a policy onto the given XMLStreamWriter.
   1.110 +     *
   1.111 +     * @param model A policy source model.
   1.112 +     * @param writer An XML stream writer.
   1.113 +     * @throws PolicyException If marshalling failed.
   1.114 +     */
   1.115 +    private void marshal(final PolicySourceModel model, final XMLStreamWriter writer) throws PolicyException {
   1.116 +        final StaxSerializer serializer = new StaxSerializer(writer);
   1.117 +        final TypedXmlWriter policy = TXW.create(model.getNamespaceVersion().asQName(XmlToken.Policy), TypedXmlWriter.class, serializer);
   1.118 +
   1.119 +        marshalDefaultPrefixes(model, policy);
   1.120 +        marshalPolicyAttributes(model, policy);
   1.121 +        marshal(model.getNamespaceVersion(), model.getRootNode(), policy);
   1.122 +        policy.commit();
   1.123 +        serializer.flush();
   1.124 +    }
   1.125 +
   1.126 +    /**
   1.127 +     * Marshal the Policy root element attributes onto the TypedXmlWriter.
   1.128 +     *
   1.129 +     * @param model The policy source model.
   1.130 +     * @param writer The typed XML writer.
   1.131 +     */
   1.132 +    private static void marshalPolicyAttributes(final PolicySourceModel model, final TypedXmlWriter writer) {
   1.133 +        final String policyId = model.getPolicyId();
   1.134 +        if (policyId != null) {
   1.135 +            writer._attribute(PolicyConstants.WSU_ID, policyId);
   1.136 +        }
   1.137 +
   1.138 +        final String policyName = model.getPolicyName();
   1.139 +        if (policyName != null) {
   1.140 +            writer._attribute(model.getNamespaceVersion().asQName(XmlToken.Name), policyName);
   1.141 +        }
   1.142 +    }
   1.143 +
   1.144 +    /**
   1.145 +     * Marshal given ModelNode and child elements on given TypedXmlWriter.
   1.146 +     *
   1.147 +     * @param nsVersion The WS-Policy version.
   1.148 +     * @param rootNode The ModelNode that is marshalled.
   1.149 +     * @param writer The TypedXmlWriter onto which the content of the rootNode is marshalled.
   1.150 +     */
   1.151 +    private void marshal(final NamespaceVersion nsVersion, final ModelNode rootNode, final TypedXmlWriter writer) {
   1.152 +        for (ModelNode node : rootNode) {
   1.153 +            final AssertionData data = node.getNodeData();
   1.154 +            if (marshallInvisible || data == null || !data.isPrivateAttributeSet()) {
   1.155 +                TypedXmlWriter child = null;
   1.156 +                if (data == null) {
   1.157 +                    child = writer._element(nsVersion.asQName(node.getType().getXmlToken()), TypedXmlWriter.class);
   1.158 +                } else {
   1.159 +                    child = writer._element(data.getName(), TypedXmlWriter.class);
   1.160 +                    final String value = data.getValue();
   1.161 +                    if (value != null) {
   1.162 +                        child._pcdata(value);
   1.163 +                    }
   1.164 +                    if (data.isOptionalAttributeSet()) {
   1.165 +                        child._attribute(nsVersion.asQName(XmlToken.Optional), Boolean.TRUE);
   1.166 +                    }
   1.167 +                    if (data.isIgnorableAttributeSet()) {
   1.168 +                        child._attribute(nsVersion.asQName(XmlToken.Ignorable), Boolean.TRUE);
   1.169 +                    }
   1.170 +                    for (Entry<QName, String> entry : data.getAttributesSet()) {
   1.171 +                        child._attribute(entry.getKey(), entry.getValue());
   1.172 +                    }
   1.173 +                }
   1.174 +                marshal(nsVersion, node, child);
   1.175 +            }
   1.176 +        }
   1.177 +    }
   1.178 +
   1.179 +    /**
   1.180 +     * Write default prefixes onto the given TypedXmlWriter
   1.181 +     *
   1.182 +     * @param model The policy source model. May not be null.
   1.183 +     * @param writer The TypedXmlWriter. May not be null.
   1.184 +     * @throws PolicyException If the creation of the prefix to namespace URI map failed.
   1.185 +     */
   1.186 +    private void marshalDefaultPrefixes(final PolicySourceModel model, final TypedXmlWriter writer) throws PolicyException {
   1.187 +        final Map<String, String> nsMap = model.getNamespaceToPrefixMapping();
   1.188 +        if (!marshallInvisible && nsMap.containsKey(PolicyConstants.SUN_POLICY_NAMESPACE_URI)) {
   1.189 +            nsMap.remove(PolicyConstants.SUN_POLICY_NAMESPACE_URI);
   1.190 +        }
   1.191 +        for (Map.Entry<String, String> nsMappingEntry : nsMap.entrySet()) {
   1.192 +            writer._namespace(nsMappingEntry.getKey(), nsMappingEntry.getValue());
   1.193 +        }
   1.194 +    }
   1.195 +
   1.196 +}

mercurial