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