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

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

     1 /*
     2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.ws.policy.sourcemodel;
    28 import com.sun.xml.internal.txw2.TXW;
    29 import com.sun.xml.internal.txw2.TypedXmlWriter;
    30 import com.sun.xml.internal.txw2.output.StaxSerializer;
    31 import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.XmlToken;
    32 import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.NamespaceVersion;
    33 import com.sun.xml.internal.ws.policy.PolicyConstants;
    34 import com.sun.xml.internal.ws.policy.PolicyException;
    35 import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages;
    36 import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger;
    38 import java.util.Collection;
    39 import java.util.Map;
    40 import java.util.Map.Entry;
    41 import javax.xml.namespace.QName;
    42 import javax.xml.stream.XMLStreamWriter;
    45 public final class XmlPolicyModelMarshaller extends PolicyModelMarshaller {
    47     private static final PolicyLogger LOGGER = PolicyLogger.getLogger(XmlPolicyModelMarshaller.class);
    49     private final boolean marshallInvisible;
    52     XmlPolicyModelMarshaller(boolean marshallInvisible) {
    53         this.marshallInvisible = marshallInvisible;
    54     }
    56     public void marshal(final PolicySourceModel model, final Object storage) throws PolicyException {
    57         if (storage instanceof StaxSerializer) {
    58             marshal(model, (StaxSerializer) storage);
    59         } else if (storage instanceof TypedXmlWriter) {
    60             marshal(model, (TypedXmlWriter) storage);
    61         } else if (storage instanceof XMLStreamWriter) {
    62             marshal(model, (XMLStreamWriter) storage);
    63         } else {
    64             throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0022_STORAGE_TYPE_NOT_SUPPORTED(storage.getClass().getName())));
    65         }
    66     }
    68     public void marshal(final Collection<PolicySourceModel> models, final Object storage) throws PolicyException {
    69         for (PolicySourceModel model : models) {
    70             marshal(model, storage);
    71         }
    72     }
    74     /**
    75      * Marshal a policy onto the given StaxSerializer.
    76      *
    77      * @param model A policy source model.
    78      * @param writer A Stax serializer.
    79      * @throws PolicyException If marshalling failed.
    80      */
    81     private void marshal(final PolicySourceModel model, final StaxSerializer writer) throws PolicyException {
    82         final TypedXmlWriter policy = TXW.create(model.getNamespaceVersion().asQName(XmlToken.Policy), TypedXmlWriter.class, writer);
    84         marshalDefaultPrefixes(model, policy);
    85         marshalPolicyAttributes(model, policy);
    86         marshal(model.getNamespaceVersion(), model.getRootNode(), policy);
    87         policy.commit();
    88     }
    90     /**
    91      * Marshal a policy onto the given TypedXmlWriter.
    92      *
    93      * @param model A policy source model.
    94      * @param writer A typed XML writer.
    95      * @throws PolicyException If marshalling failed.
    96      */
    97     private void marshal(final PolicySourceModel model, final TypedXmlWriter writer) throws PolicyException {
    98         final TypedXmlWriter policy = writer._element(model.getNamespaceVersion().asQName(XmlToken.Policy), TypedXmlWriter.class);
   100         marshalDefaultPrefixes(model, policy);
   101         marshalPolicyAttributes(model, policy);
   102         marshal(model.getNamespaceVersion(), model.getRootNode(), policy);
   103     }
   105     /**
   106      * Marshal a policy onto the given XMLStreamWriter.
   107      *
   108      * @param model A policy source model.
   109      * @param writer An XML stream writer.
   110      * @throws PolicyException If marshalling failed.
   111      */
   112     private void marshal(final PolicySourceModel model, final XMLStreamWriter writer) throws PolicyException {
   113         final StaxSerializer serializer = new StaxSerializer(writer);
   114         final TypedXmlWriter policy = TXW.create(model.getNamespaceVersion().asQName(XmlToken.Policy), TypedXmlWriter.class, serializer);
   116         marshalDefaultPrefixes(model, policy);
   117         marshalPolicyAttributes(model, policy);
   118         marshal(model.getNamespaceVersion(), model.getRootNode(), policy);
   119         policy.commit();
   120         serializer.flush();
   121     }
   123     /**
   124      * Marshal the Policy root element attributes onto the TypedXmlWriter.
   125      *
   126      * @param model The policy source model.
   127      * @param writer The typed XML writer.
   128      */
   129     private static void marshalPolicyAttributes(final PolicySourceModel model, final TypedXmlWriter writer) {
   130         final String policyId = model.getPolicyId();
   131         if (policyId != null) {
   132             writer._attribute(PolicyConstants.WSU_ID, policyId);
   133         }
   135         final String policyName = model.getPolicyName();
   136         if (policyName != null) {
   137             writer._attribute(model.getNamespaceVersion().asQName(XmlToken.Name), policyName);
   138         }
   139     }
   141     /**
   142      * Marshal given ModelNode and child elements on given TypedXmlWriter.
   143      *
   144      * @param nsVersion The WS-Policy version.
   145      * @param rootNode The ModelNode that is marshalled.
   146      * @param writer The TypedXmlWriter onto which the content of the rootNode is marshalled.
   147      */
   148     private void marshal(final NamespaceVersion nsVersion, final ModelNode rootNode, final TypedXmlWriter writer) {
   149         for (ModelNode node : rootNode) {
   150             final AssertionData data = node.getNodeData();
   151             if (marshallInvisible || data == null || !data.isPrivateAttributeSet()) {
   152                 TypedXmlWriter child = null;
   153                 if (data == null) {
   154                     child = writer._element(nsVersion.asQName(node.getType().getXmlToken()), TypedXmlWriter.class);
   155                 } else {
   156                     child = writer._element(data.getName(), TypedXmlWriter.class);
   157                     final String value = data.getValue();
   158                     if (value != null) {
   159                         child._pcdata(value);
   160                     }
   161                     if (data.isOptionalAttributeSet()) {
   162                         child._attribute(nsVersion.asQName(XmlToken.Optional), Boolean.TRUE);
   163                     }
   164                     if (data.isIgnorableAttributeSet()) {
   165                         child._attribute(nsVersion.asQName(XmlToken.Ignorable), Boolean.TRUE);
   166                     }
   167                     for (Entry<QName, String> entry : data.getAttributesSet()) {
   168                         child._attribute(entry.getKey(), entry.getValue());
   169                     }
   170                 }
   171                 marshal(nsVersion, node, child);
   172             }
   173         }
   174     }
   176     /**
   177      * Write default prefixes onto the given TypedXmlWriter
   178      *
   179      * @param model The policy source model. May not be null.
   180      * @param writer The TypedXmlWriter. May not be null.
   181      * @throws PolicyException If the creation of the prefix to namespace URI map failed.
   182      */
   183     private void marshalDefaultPrefixes(final PolicySourceModel model, final TypedXmlWriter writer) throws PolicyException {
   184         final Map<String, String> nsMap = model.getNamespaceToPrefixMapping();
   185         if (!marshallInvisible && nsMap.containsKey(PolicyConstants.SUN_POLICY_NAMESPACE_URI)) {
   186             nsMap.remove(PolicyConstants.SUN_POLICY_NAMESPACE_URI);
   187         }
   188         for (Map.Entry<String, String> nsMappingEntry : nsMap.entrySet()) {
   189             writer._namespace(nsMappingEntry.getKey(), nsMappingEntry.getValue());
   190         }
   191     }
   193 }

mercurial