KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > processors > wsdl2 > validators > XMLFormatValidator


1 package org.objectweb.celtix.tools.processors.wsdl2.validators;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import javax.wsdl.Binding;
7 import javax.wsdl.BindingOperation;
8 import javax.wsdl.Definition;
9 import javax.wsdl.Operation;
10 import javax.xml.namespace.QName JavaDoc;
11
12 import org.objectweb.celtix.tools.extensions.xmlformat.XMLFormat;
13 import org.objectweb.celtix.tools.extensions.xmlformat.XMLFormatBinding;
14
15 public class XMLFormatValidator
16     extends AbstractValidator {
17
18     public XMLFormatValidator(Definition def) {
19         super(def);
20     }
21
22     @Override JavaDoc
23     public boolean isValid() {
24         // TODO Auto-generated method stub
25
return checkXMLBindingFormat();
26     }
27
28     @SuppressWarnings JavaDoc("unchecked")
29     private boolean checkXMLBindingFormat() {
30         Collection JavaDoc<Binding> bindings = def.getBindings().values();
31         if (bindings != null) {
32             for (Binding binding : bindings) {
33                 Iterator JavaDoc it = binding.getExtensibilityElements().iterator();
34                 while (it.hasNext()) {
35                     if (it.next() instanceof XMLFormatBinding) {
36                         if (!checkXMLFormat(binding)) {
37                             return false;
38                         }
39                     } else {
40                         break;
41                     }
42                 }
43             }
44         }
45         return true;
46     }
47
48     @SuppressWarnings JavaDoc("unchecked")
49     private boolean checkXMLFormat(Binding binding) {
50         List JavaDoc<BindingOperation> bos = binding.getBindingOperations();
51         boolean result = true;
52         boolean needRootNode = false;
53         for (BindingOperation bo : bos) {
54             Operation op = binding.getPortType().getOperation(bo.getName(), null, null);
55             needRootNode = false;
56             if (op.getInput().getMessage().getParts().size() == 0
57                 || op.getInput().getMessage().getParts().size() > 1) {
58                 needRootNode = true;
59             }
60             if (needRootNode) {
61                 String JavaDoc path = "Binding(" + binding.getQName().getLocalPart()
62                               + "):BindingOperation(" + bo.getName() + ")";
63                 Iterator JavaDoc itIn = bo.getBindingInput().getExtensibilityElements().iterator();
64                 if (findXMLFormatRootNode(itIn, bo, path + "-input")) {
65                     // Input check correct, continue to check output binding
66
needRootNode = false;
67                     if (op.getOutput().getMessage().getParts().size() == 0
68                         || op.getOutput().getMessage().getParts().size() > 1) {
69                         needRootNode = true;
70                     }
71                     if (needRootNode) {
72                         Iterator JavaDoc itOut = bo.getBindingInput().getExtensibilityElements().iterator();
73                         result = result
74                                  && findXMLFormatRootNode(itOut, bo, path + "-Output");
75                         if (!result) {
76                             return false;
77                         }
78                     }
79                 } else {
80                     return false;
81                 }
82             }
83         }
84         return true;
85     }
86
87     private boolean findXMLFormatRootNode(Iterator JavaDoc it, BindingOperation bo, String JavaDoc errorPath) {
88         while (it.hasNext()) {
89             Object JavaDoc ext = it.next();
90             if (ext instanceof XMLFormat) {
91                 XMLFormat xmlFormat = (XMLFormat)ext;
92                 // String rootNodeValue = def.getPrefix(def.getTargetNamespace()) + ":" + bo.getName();
93
QName JavaDoc rootNodeName = new QName JavaDoc(def.getTargetNamespace(), bo.getName());
94                 if (xmlFormat.getRootNode() != null) {
95                     if (xmlFormat.getRootNode().equals(rootNodeName)) {
96                         return true;
97                     } else {
98                         addErrorMessage(errorPath
99                                         + ": wrong value of rootNode attribute, the value should be "
100                                         + rootNodeName);
101                         return false;
102                     }
103                 } else {
104                     addErrorMessage(errorPath
105                                     + ": empty value of rootNode attribute, the value should be "
106                                     + rootNodeName);
107                     return false;
108                 }
109             }
110         }
111         addErrorMessage(errorPath + ": missing xml format body element");
112         return false;
113     }
114 }
115
Popular Tags