001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)WrapperUtil.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.wsdl11wrapper.util;
030:
031: import javax.xml.namespace.QName;
032: import javax.xml.parsers.DocumentBuilder;
033: import javax.xml.parsers.DocumentBuilderFactory;
034: import javax.xml.parsers.ParserConfigurationException;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.NodeList;
039: import com.sun.jbi.wsdl11wrapper.*;
040:
041: /*
042: * WrapperUtil.java
043: *
044: * Created on August 12, 2005, 2:00 PM
045: *
046: * @author blu
047: */
048: public class WrapperUtil {
049:
050: /**
051: * Constants to build wsdl 1.1 wrapper, e.g. along the lines of
052: * <jbi:message version="1.0" type="wsdl:input wsdl:output or wsdl:fault message attribute value QNAME" name="optional name attribute from wsdl:input etc." xmlns:jbi="http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper">
053: * <jbi:part>
054: * </jbi:part>
055: * <jbi:part>
056: * </jbi:part>
057: * </jbi:message>
058: */
059: public static final String WRAPPER_DEFAULT_NAMESPACE_PREFIX = "jbi";
060: public static final String WRAPPER_DEFAULT_NAMESPACE = "http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper";
061: public static final String WRAPPER_MESSAGE = "jbi:message";
062: public static final String WRAPPER_ATTRIBUTE_VERSION = "version";
063: public static final String WRAPPER_ATTRIBUTE_VERSION_VALUE = "1.0";
064: public static final String WRAPPER_ATTRIBUTE_TYPE = "type";
065: public static final String WRAPPER_ATTRIBUTE_NAME = "name";
066: public static final String WRAPPER_PART = "jbi:part";
067:
068: /**
069: * Creates and returns a JBI message wrapper element. Does NOT add the created element to the normalDoc
070: * - nor does it popluate the wrapper element with a payload
071: * @param normalDoc The target document of the normalization
072: * @param type qualified message name defined in the portmap definition for a given message
073: * @param name optional name attribute defined in the portmap definition for a given message
074: * @return the jbi message wrapper element
075: */
076: public static Element createJBIMessageWrapper(Document normalDoc,
077: QName type, String name) {
078: Element msgWrapper = normalDoc.createElementNS(
079: WRAPPER_DEFAULT_NAMESPACE, WRAPPER_MESSAGE);
080: msgWrapper.setAttribute(WRAPPER_ATTRIBUTE_VERSION,
081: WRAPPER_ATTRIBUTE_VERSION_VALUE);
082: String prefix = type.getPrefix();
083: if (prefix == null || prefix.length() == 0) {
084: prefix = "msgns";
085: }
086: msgWrapper.setAttribute(WRAPPER_ATTRIBUTE_TYPE, prefix + ":"
087: + type.getLocalPart());
088: msgWrapper.setAttribute("xmlns:" + prefix, type
089: .getNamespaceURI());
090:
091: if (name != null) {
092: msgWrapper.setAttribute(WRAPPER_ATTRIBUTE_NAME, name);
093: }
094:
095: return msgWrapper;
096: }
097:
098: /**
099: * Creates and returns a JBI part wrapper element. Does NOT add the created element to the normalDoc.
100: * @param normalDoc The target document of the normalization
101: * @param partName the name of the part
102: * @param part the part payload which must be created by normalDoc
103: */
104: public static Element createJBIWrappedPart(Document normalDoc,
105: NodeList part) {
106: Element wrapperElem = normalDoc.createElement(WRAPPER_PART);
107: if (part != null) {
108: int noOfNodes = part.getLength();
109: for (int nodeCount = 0; nodeCount < noOfNodes; nodeCount++) {
110: wrapperElem.appendChild(part.item(nodeCount));
111: }
112: }
113: return wrapperElem;
114: }
115:
116: /**
117: * Creates and returns a JBI part wrapper element. Does NOT add the created element to the normalDoc.
118: * @param normalDoc The target document of the normalization
119: * @param partName the name of the part
120: * @param part the part payload which must be created by normalDoc
121: */
122: public static Element createJBIWrappedPart(Document normalDoc,
123: Node part) {
124: Element wrapperElem = normalDoc.createElement(WRAPPER_PART);
125: if (part != null) {
126: wrapperElem.appendChild(part);
127: }
128: return wrapperElem;
129: }
130:
131: /**
132: * Creates and returns a JBI part wrapper element. Does NOT add the created element to the normalDoc.
133: * @param normalDoc The target document of the normalization
134: * @param partName the name of the part
135: * @param part the part payload which need not be created by normalDoc
136: */
137: public static Element importJBIWrappedPart(Document normalDoc,
138: NodeList part) {
139: Element wrapperElem = normalDoc.createElement(WRAPPER_PART);
140: if (part != null) {
141: int noOfNodes = part.getLength();
142: for (int nodeCount = 0; nodeCount < noOfNodes; nodeCount++) {
143: Node aNode = part.item(nodeCount);
144: if (aNode != null) {
145: Node importedPartNode = normalDoc.importNode(aNode,
146: true);
147: wrapperElem.appendChild(importedPartNode);
148: }
149: }
150: }
151: return wrapperElem;
152: }
153:
154: /**
155: * Creates and returns a JBI part wrapper element. Does NOT add the created element to the normalDoc.
156: * @param normalDoc The target document of the normalization
157: * @param partName the name of the part
158: * @param part the part payload which need not be created by normalDoc
159: */
160: public static Element importJBIWrappedPart(Document normalDoc,
161: Node part) {
162: Element wrapperElem = normalDoc.createElement(WRAPPER_PART);
163: if (part != null) {
164: Node importedPartNode = normalDoc.importNode(part, true);
165: wrapperElem.appendChild(importedPartNode);
166: }
167: return wrapperElem;
168: }
169:
170: /**
171: * Returns only the first Element inside the part wrapper
172: * Legally, the jbi:part may contain multiple Elements, or text - but
173: * in many cases the WSDL will limit this to one element.
174: *
175: * (a jbi:part element may legally contain multiple Elements, or text)
176: * @param wrappedDoc the wrapped document
177: * @return the first Element in the normalized message part, null if no element is present
178: * @throws WrapperProcessingException if the part could not be returned
179: */
180: public static Element getPartElement(Document wrappedDoc)
181: throws WrapperProcessingException {
182: // Get the jbi message wrapper element
183: Element jbiMessageWrapper = wrappedDoc.getDocumentElement();
184: if (jbiMessageWrapper.getTagName().equalsIgnoreCase(
185: WRAPPER_MESSAGE)) {
186: // The JBI wsdl 1.1 wrapper does not contain part names, but all parts
187: // have to appear in the exact order defined in the WSDL
188: NodeList childNodesI = jbiMessageWrapper.getChildNodes();
189: for (int i = 0, I = childNodesI.getLength(); i < I; i++) {
190: Node nodeI = childNodesI.item(i);
191: if (nodeI.getNodeType() != Node.ELEMENT_NODE) {
192: continue;
193: }
194: Element jbiPartWrapper = (Element) nodeI;
195: if (!jbiPartWrapper.getTagName().equalsIgnoreCase(
196: WRAPPER_PART)) {
197: continue;
198: }
199: NodeList childNodesJ = jbiPartWrapper.getChildNodes();
200: for (int j = 0, J = childNodesJ.getLength(); j < J; j++) {
201: Node nodeJ = childNodesJ.item(j);
202: if (nodeJ.getNodeType() == Node.ELEMENT_NODE) {
203: return (Element) nodeJ;
204: }
205: }
206: }
207: }
208: return null;
209: }
210:
211: }
|