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.binding.xml;
019:
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import javax.wsdl.extensions.ExtensibilityElement;
025: import javax.xml.namespace.QName;
026:
027: import org.apache.cxf.bindings.xformat.XMLBindingMessageFormat;
028:
029: import org.apache.cxf.service.model.BindingInfo;
030: import org.apache.cxf.service.model.BindingOperationInfo;
031: import org.apache.cxf.service.model.OperationInfo;
032: import org.apache.cxf.service.model.ServiceInfo;
033: import org.apache.cxf.tools.validator.ServiceValidator;
034: import org.apache.cxf.wsdl.WSDLConstants;
035:
036: public class XMLFormatValidator extends ServiceValidator {
037:
038: public XMLFormatValidator() {
039: }
040:
041: public XMLFormatValidator(ServiceInfo s) {
042: this .service = s;
043: }
044:
045: @Override
046: public boolean isValid() {
047: return checkXMLBindingFormat();
048: }
049:
050: @SuppressWarnings("unchecked")
051: private boolean checkXMLBindingFormat() {
052: Collection<BindingInfo> bindings = service.getBindings();
053: if (bindings != null) {
054: for (BindingInfo binding : bindings) {
055: System.err.println(binding.getBindingId());
056: System.err.println(WSDLConstants.XML_BINDING_NS);
057: if (WSDLConstants.XML_BINDING_NS
058: .equalsIgnoreCase(binding.getBindingId())
059: && !checkXMLFormat(binding)) {
060: return false;
061: }
062: }
063: }
064: return true;
065: }
066:
067: @SuppressWarnings("unchecked")
068: private boolean checkXMLFormat(BindingInfo binding) {
069: Collection<BindingOperationInfo> bos = binding.getOperations();
070: boolean result = true;
071: boolean needRootNode = false;
072: for (BindingOperationInfo bo : bos) {
073: OperationInfo op = binding.getInterface().getOperation(
074: bo.getName());
075: needRootNode = false;
076: if (op.getInput().getMessageParts().size() == 0
077: || op.getInput().getMessageParts().size() > 1) {
078: needRootNode = true;
079: }
080: if (needRootNode) {
081: String path = "Binding("
082: + binding.getName().getLocalPart()
083: + "):BindingOperation(" + bo.getName() + ")";
084: List<ExtensibilityElement> inExtensors = bo.getInput()
085: .getExtensors(ExtensibilityElement.class);
086: Iterator itIn = null;
087: if (inExtensors != null) {
088: itIn = inExtensors.iterator();
089: }
090: if (findXMLFormatRootNode(itIn, bo, path + "-input")) {
091: // Input check correct, continue to check output binding
092: needRootNode = false;
093: if (op.getOutput().getMessageParts().size() == 0
094: || op.getOutput().getMessageParts().size() > 1) {
095: needRootNode = true;
096: }
097: if (needRootNode) {
098: List<ExtensibilityElement> outExtensors = bo
099: .getOutput().getExtensors(
100: ExtensibilityElement.class);
101: Iterator itOut = null;
102: if (outExtensors != null) {
103: itOut = outExtensors.iterator();
104: }
105: result = result
106: && findXMLFormatRootNode(itOut, bo,
107: path + "-Output");
108: if (!result) {
109: return false;
110: }
111: }
112: } else {
113: return false;
114: }
115: }
116: }
117: return true;
118: }
119:
120: private boolean findXMLFormatRootNode(Iterator it,
121: BindingOperationInfo bo, String errorPath) {
122: while (it != null && it.hasNext()) {
123: Object ext = it.next();
124: if (ext instanceof XMLBindingMessageFormat) {
125: XMLBindingMessageFormat xmlFormat = (XMLBindingMessageFormat) ext;
126: QName rootNodeName = bo.getName();
127: if (xmlFormat.getRootNode() != null) {
128: if (xmlFormat.getRootNode().equals(rootNodeName)) {
129: return true;
130: } else {
131: addErrorMessage(errorPath
132: + ": wrong value of rootNode attribute, the value should be "
133: + rootNodeName);
134: return false;
135: }
136: } else {
137: addErrorMessage(errorPath
138: + ": empty value of rootNode attribute, the value should be "
139: + rootNodeName);
140: return false;
141: }
142: }
143: }
144: addErrorMessage(errorPath + ": missing xml format body element");
145: return false;
146: }
147: }
|