001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.policy.sourcemodel;
038:
039: import java.util.Collection;
040: import java.util.Map.Entry;
041: import javax.xml.namespace.QName;
042: import javax.xml.stream.XMLStreamWriter;
043: import com.sun.xml.txw2.TXW;
044: import com.sun.xml.txw2.TypedXmlWriter;
045: import com.sun.xml.txw2.output.StaxSerializer;
046: import com.sun.xml.ws.policy.PolicyConstants;
047: import com.sun.xml.ws.policy.PolicyException;
048: import com.sun.xml.ws.policy.privateutil.LocalizationMessages;
049: import com.sun.xml.ws.policy.privateutil.PolicyLogger;
050: import java.util.Map;
051:
052: public final class XmlPolicyModelMarshaller extends
053: PolicyModelMarshaller {
054: private static final PolicyLogger LOGGER = PolicyLogger
055: .getLogger(XmlPolicyModelMarshaller.class);
056:
057: private final boolean marshallInvisible;
058:
059: XmlPolicyModelMarshaller(boolean marshallInvisible) {
060: this .marshallInvisible = marshallInvisible;
061: }
062:
063: public void marshal(final PolicySourceModel model,
064: final Object storage) throws PolicyException {
065: if (storage instanceof TypedXmlWriter) {
066: marshal(model, (TypedXmlWriter) storage);
067: } else if (storage instanceof XMLStreamWriter) {
068: marshal(model, (XMLStreamWriter) storage);
069: } else {
070: throw LOGGER
071: .logSevereException(new PolicyException(
072: LocalizationMessages
073: .WSP_0022_STORAGE_TYPE_NOT_SUPPORTED(storage
074: .getClass().getName())));
075: }
076: }
077:
078: public void marshal(final Collection<PolicySourceModel> models,
079: final Object storage) throws PolicyException {
080: for (PolicySourceModel model : models) {
081: marshal(model, storage);
082: }
083: }
084:
085: /**
086: * Marshal a policy onto the given TypedXmlWriter.
087: *
088: * @param model A policy source model.
089: * @param writer A typed XML writer.
090: */
091: private void marshal(final PolicySourceModel model,
092: final TypedXmlWriter writer) throws PolicyException {
093: final TypedXmlWriter policy = writer._element(
094: PolicyConstants.POLICY, TypedXmlWriter.class);
095: marshalPolicyAttributes(model, policy);
096: marshal(model.getRootNode(), policy);
097: }
098:
099: /**
100: * Marshal a policy onto the given XMLStreamWriter.
101: *
102: * @param model A policy source model.
103: * @param writer An XML stream writer.
104: */
105: private void marshal(final PolicySourceModel model,
106: final XMLStreamWriter writer) throws PolicyException {
107: final StaxSerializer serializer = new StaxSerializer(writer);
108: final TypedXmlWriter policy = TXW.create(
109: PolicyConstants.POLICY, TypedXmlWriter.class,
110: serializer);
111:
112: final Map<String, String> nsMap = model
113: .getNamespaceToPrefixMapping();
114:
115: if (!marshallInvisible
116: && nsMap
117: .containsKey(PolicyConstants.SUN_POLICY_NAMESPACE_URI)) {
118: nsMap.remove(PolicyConstants.SUN_POLICY_NAMESPACE_URI);
119: }
120:
121: for (Map.Entry<String, String> nsMappingEntry : nsMap
122: .entrySet()) {
123: policy._namespace(nsMappingEntry.getKey(), nsMappingEntry
124: .getValue());
125: }
126:
127: marshalPolicyAttributes(model, policy);
128: marshal(model.getRootNode(), policy);
129: policy.commit();
130: serializer.flush();
131: }
132:
133: /**
134: * Marshal the Policy root element attributes onto the TypedXmlWriter.
135: *
136: * @param model The policy source model.
137: * @param writer The typed XML writer.
138: */
139: private static void marshalPolicyAttributes(
140: final PolicySourceModel model, final TypedXmlWriter writer) {
141: final String policyId = model.getPolicyId();
142: if (policyId != null) {
143: writer._attribute(PolicyConstants.WSU_ID, policyId);
144: }
145:
146: final String policyName = model.getPolicyName();
147: if (policyName != null) {
148: writer._attribute(PolicyConstants.POLICY_NAME, policyName);
149: }
150: }
151:
152: /**
153: * Marshal given ModelNode and child elements on given TypedXmlWriter.
154: *
155: * @param rootNode The ModelNode that is marshalled.
156: * @param writer The TypedXmlWriter onto which the content of the rootNode is marshalled.
157: */
158: private void marshal(final ModelNode rootNode,
159: final TypedXmlWriter writer) {
160: for (ModelNode node : rootNode) {
161: final AssertionData data = node.getNodeData();
162: if (marshallInvisible || data == null
163: || !data.isPrivateAttributeSet()) {
164: TypedXmlWriter child = null;
165: if (data == null) {
166: child = writer._element(node.getType().asQName(),
167: TypedXmlWriter.class);
168: } else {
169: child = writer._element(data.getName(),
170: TypedXmlWriter.class);
171: final String value = data.getValue();
172: if (value != null) {
173: child._pcdata(value);
174: }
175: for (Entry<QName, String> entry : data
176: .getAttributesSet()) {
177: child._attribute(entry.getKey(), entry
178: .getValue());
179: }
180: }
181: marshal(node, child);
182: }
183: }
184: }
185:
186: }
|