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.validator.internal;
019:
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import javax.wsdl.Definition;
025: import javax.wsdl.Message;
026: import javax.wsdl.Operation;
027: import javax.wsdl.Part;
028: import javax.wsdl.PortType;
029: import javax.xml.namespace.QName;
030:
031: public class UniqueBodyPartsValidator extends
032: AbstractDefinitionValidator {
033: private Map<QName, String> uniqueBodyPartsMap;
034:
035: public UniqueBodyPartsValidator(Definition def) {
036: super (def);
037:
038: }
039:
040: public boolean isValid() {
041: Iterator ite = def.getPortTypes().values().iterator();
042: while (ite.hasNext()) {
043: //
044: // Only check for unique body parts per portType.
045: // (Create a new Map for each portType.)
046: //
047: uniqueBodyPartsMap = new HashMap<QName, String>();
048: PortType portType = (PortType) ite.next();
049: Iterator ite2 = portType.getOperations().iterator();
050: while (ite2.hasNext()) {
051: Operation operation = (Operation) ite2.next();
052: if (operation.getInput() != null) {
053: Message inMessage = operation.getInput()
054: .getMessage();
055: if (inMessage != null
056: && !isUniqueBodyPart(operation.getName(),
057: inMessage)) {
058: return false;
059: }
060: }
061: }
062: }
063: return true;
064:
065: }
066:
067: private boolean isUniqueBodyPart(String operationName, Message msg) {
068: Map partsMap = msg.getParts();
069: Iterator ite = partsMap.values().iterator();
070: if (ite.hasNext()) {
071: Part part = (Part) ite.next();
072: if (part.getElementName() == null) {
073: return true;
074: }
075: String opName = getOperationNameWithSamePart(operationName,
076: part);
077: if (opName != null) {
078: addErrorMessage("Non unique body parts, operation "
079: + "[ " + opName + " ] " + "and operation [ "
080: + operationName
081: + " ] have the same body block "
082: + part.getElementName());
083: return false;
084: }
085: }
086: return true;
087: }
088:
089: private String getOperationNameWithSamePart(String operationName,
090: Part part) {
091: QName partQN = part.getElementName();
092: String opName = uniqueBodyPartsMap.get(partQN);
093: if (opName == null) {
094: uniqueBodyPartsMap.put(partQN, operationName);
095: return null;
096: } else {
097: return opName;
098: }
099: }
100:
101: }
|