001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.wsdlto.frontend.jaxws.customization;
019:
020: import java.util.Iterator;
021: import java.util.logging.Logger;
022:
023: import javax.wsdl.WSDLException;
024: import javax.wsdl.extensions.ExtensionRegistry;
025: import javax.xml.namespace.NamespaceContext;
026: import javax.xml.xpath.XPath;
027: import javax.xml.xpath.XPathConstants;
028: import javax.xml.xpath.XPathExpressionException;
029: import javax.xml.xpath.XPathFactory;
030:
031: import org.w3c.dom.Element;
032: import org.w3c.dom.Node;
033: import org.w3c.dom.NodeList;
034:
035: import org.apache.cxf.common.i18n.Message;
036: import org.apache.cxf.common.logging.LogUtils;
037: import org.apache.cxf.helpers.DOMUtils;
038: import org.apache.cxf.tools.common.ToolConstants;
039: import org.apache.cxf.tools.common.ToolException;
040:
041: public class JAXWSBindingParser {
042: private static final Logger LOG = LogUtils
043: .getL7dLogger(CustomizationParser.class);
044: private ExtensionRegistry extReg;
045:
046: public JAXWSBindingParser(ExtensionRegistry ext) {
047: extReg = ext;
048: }
049:
050: public JAXWSBinding parse(Class parentType, Element element,
051: String namespace) throws WSDLException {
052: JAXWSBinding jaxwsBinding = (JAXWSBinding) extReg
053: .createExtension(parentType,
054: ToolConstants.JAXWS_BINDINGS);
055:
056: jaxwsBinding.setElementType(ToolConstants.JAXWS_BINDINGS);
057: jaxwsBinding.setElement(element);
058: jaxwsBinding.setDocumentBaseURI(namespace);
059:
060: parseElement(jaxwsBinding, element);
061:
062: return jaxwsBinding;
063: }
064:
065: void parseElement(JAXWSBinding jaxwsBinding, Element element) {
066: NodeList children = element.getChildNodes();
067: if (children != null && children.getLength() == 0) {
068: // global binding
069: if (isAsyncElement(element)) {
070: jaxwsBinding
071: .setEnableAsyncMapping(getNodeValue(element));
072: }
073: if (isMIMEElement(element)) {
074: jaxwsBinding.setEnableMime(getNodeValue(element));
075: }
076: if (isPackageElement(element)) {
077: jaxwsBinding.setPackage(getPackageName(element));
078: }
079:
080: if (isWrapperStyle(element)) {
081: jaxwsBinding
082: .setEnableWrapperStyle(getNodeValue(element));
083: }
084: }
085:
086: if (children != null && children.getLength() > 0) {
087: // other binding
088: for (int i = 0; i < children.getLength(); i++) {
089: Node child = children.item(i);
090:
091: if (isAsyncElement(child)) {
092: jaxwsBinding
093: .setEnableAsyncMapping(getNodeValue(child));
094: }
095: if (isMIMEElement(child)) {
096: jaxwsBinding.setEnableMime(getNodeValue(child));
097: }
098:
099: if (isWrapperStyle(child)) {
100: jaxwsBinding
101: .setEnableWrapperStyle(getNodeValue(child));
102: }
103:
104: if (isPackageElement(child)) {
105: jaxwsBinding.setPackage(getPackageName(child));
106: }
107:
108: if (isJAXWSMethodElement(child)) {
109: jaxwsBinding.setMethodName(getMethodName(child));
110: }
111:
112: if (isJAXWSParameterElement(child)) {
113: Element childElement = (Element) child;
114: String partPath = "//"
115: + childElement.getAttribute("part");
116: Node node = queryXPathNode(element
117: .getOwnerDocument().getDocumentElement(),
118: partPath);
119: String messageName = "";
120: if (node != null) {
121: Node messageNode = node.getParentNode();
122: if (messageNode != null) {
123: Element messageEle = (Element) messageNode;
124: messageName = messageEle
125: .getAttribute("name");
126: }
127: }
128:
129: String name = childElement.getAttribute("name");
130: String elementName = childElement
131: .getAttribute("childElementName");
132: JAXWSParameter jpara = new JAXWSParameter(
133: messageName, elementName, name);
134: jaxwsBinding.setJaxwsPara(jpara);
135: }
136:
137: if (isJAXWSClass(child)) {
138: Element childElement = (Element) child;
139: String clzName = childElement.getAttribute("name");
140: String javadoc = "";
141: Node docChild = DOMUtils.getChild(child,
142: Element.ELEMENT_NODE);
143:
144: if (docChild != null
145: && this .isJAXWSClassDoc(docChild)) {
146: javadoc = DOMUtils.getContent(docChild);
147: }
148:
149: JAXWSClass jaxwsClass = new JAXWSClass(clzName,
150: javadoc);
151: jaxwsBinding.setJaxwsClass(jaxwsClass);
152: }
153: }
154: }
155:
156: }
157:
158: private boolean isJAXWSMethodElement(Node node) {
159: return ToolConstants.NS_JAXWS_BINDINGS.equals(node
160: .getNamespaceURI())
161: && "method".equals(node.getLocalName());
162: }
163:
164: private String getMethodName(Node node) {
165: Element ele = (Element) node;
166: return ele.getAttribute("name");
167: }
168:
169: private boolean isPackageElement(Node node) {
170: if (ToolConstants.NS_JAXWS_BINDINGS.equals(node
171: .getNamespaceURI())
172: && "package".equals(node.getLocalName())) {
173: return true;
174: }
175: return false;
176:
177: }
178:
179: private boolean isJAXWSParameterElement(Node node) {
180: return (ToolConstants.NS_JAXWS_BINDINGS.equals(node
181: .getNamespaceURI()))
182: && "parameter".equals(node.getLocalName());
183:
184: }
185:
186: private boolean isJAXWSClass(Node node) {
187: return (ToolConstants.NS_JAXWS_BINDINGS.equals(node
188: .getNamespaceURI()))
189: && "class".equals(node.getLocalName());
190: }
191:
192: private boolean isJAXWSClassDoc(Node node) {
193: return (ToolConstants.NS_JAXWS_BINDINGS.equals(node
194: .getNamespaceURI()))
195: && "javadoc".equals(node.getLocalName());
196: }
197:
198: private String getPackageName(Node node) {
199: Element ele = (Element) node;
200: return ele.getAttribute("name");
201: }
202:
203: private Boolean isAsyncElement(Node node) {
204: return "enableAsyncMapping".equals(node.getLocalName())
205: && ToolConstants.NS_JAXWS_BINDINGS.equals(node
206: .getNamespaceURI());
207: }
208:
209: private Boolean isWrapperStyle(Node node) {
210: return "enableWrapperStyle".equals(node.getLocalName())
211: && ToolConstants.NS_JAXWS_BINDINGS.equals(node
212: .getNamespaceURI());
213: }
214:
215: private Boolean getNodeValue(Node node) {
216: return Boolean.valueOf(node.getTextContent());
217: }
218:
219: private Boolean isMIMEElement(Node node) {
220: return "enableMIMEContent".equals(node.getLocalName())
221: && ToolConstants.NS_JAXWS_BINDINGS.equals(node
222: .getNamespaceURI());
223: }
224:
225: private Node queryXPathNode(Node target, String expression) {
226: NodeList nlst;
227: try {
228: ContextImpl contextImpl = new ContextImpl(target);
229: XPath xpath = XPathFactory.newInstance().newXPath();
230: xpath.setNamespaceContext(contextImpl);
231: nlst = (NodeList) xpath.evaluate(expression, target,
232: XPathConstants.NODESET);
233: } catch (XPathExpressionException e) {
234: Message msg = new Message("XPATH_ERROR", LOG,
235: new Object[] { expression });
236: throw new ToolException(msg, e);
237: }
238:
239: if (nlst.getLength() != 1) {
240: Message msg = new Message("ERROR_TARGETNODE_WITH_XPATH",
241: LOG, new Object[] { expression });
242: throw new ToolException(msg);
243: }
244:
245: Node rnode = nlst.item(0);
246: if (!(rnode instanceof Element)) {
247: return null;
248: }
249: return (Element) rnode;
250: }
251:
252: class ContextImpl implements NamespaceContext {
253: private Node targetNode;
254:
255: public ContextImpl(Node node) {
256: targetNode = node;
257: }
258:
259: public String getNamespaceURI(String prefix) {
260: return targetNode.getOwnerDocument().lookupNamespaceURI(
261: prefix);
262: }
263:
264: public String getPrefix(String nsURI) {
265: throw new UnsupportedOperationException();
266: }
267:
268: public Iterator getPrefixes(String namespaceURI) {
269: throw new UnsupportedOperationException();
270: }
271: }
272: }
|