KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > validator > visitor > schema > SchemaSemanticsVisitor


1 /*
2  * SchemaSemanticsVisitor.java
3  *
4  * Created on February 9, 2007, 5:11 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.xml.wsdl.validator.visitor.schema;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import org.netbeans.modules.xml.schema.model.Import;
17 import org.netbeans.modules.xml.schema.model.Schema;
18 import org.netbeans.modules.xml.schema.model.SchemaComponent;
19 import org.netbeans.modules.xml.schema.model.SchemaModel;
20 import org.netbeans.modules.xml.schema.model.visitor.DefaultSchemaVisitor;
21 import org.netbeans.modules.xml.wsdl.model.Definitions;
22 import org.netbeans.modules.xml.wsdl.model.Types;
23 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
24 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
25 import org.netbeans.modules.xml.xam.Component;
26 import org.netbeans.modules.xml.xam.Model;
27 import org.netbeans.modules.xml.xam.spi.Validation;
28 import org.netbeans.modules.xml.xam.spi.Validation.ValidationType;
29 import org.netbeans.modules.xml.xam.spi.Validator;
30 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
31 import org.openide.util.NbBundle;
32
33 /**
34  *
35  * @author radval
36  */

37 public class SchemaSemanticsVisitor extends DefaultSchemaVisitor {
38     
39      /**Import does not have imported document object */
40     public static final String JavaDoc VAL_MISSING_IMPORTED_DOCUMENT = "VAL_MISSING_IMPORTED_DOCUMENT";
41     public static final String JavaDoc FIX_MISSING_IMPORTED_DOCUMENT = "FIX_MISSING_IMPORTED_DOCUMENT";
42   
43     
44     /** Creates a new instance of SchemaSemanticsVisitor */
45     public List JavaDoc<ResultItem> mResultItems = new ArrayList JavaDoc<ResultItem>();
46     private WSDLModel mParentModel;
47     private Validator mValidator;
48     private Validation mValidation;
49     private List JavaDoc<Model> mValidatedModels;
50     
51     /** Creates a new instance of SchemaSemanticsVisitor */
52     public SchemaSemanticsVisitor(WSDLModel parentModel,
53                                   Validator validator,
54                                   Validation validation,
55                                   List JavaDoc<Model> validatedModels) {
56         mParentModel = parentModel;
57         mValidation = validation;
58         mValidatedModels = validatedModels;
59     }
60     
61     public List JavaDoc<ResultItem> getResultItems() {
62         return mResultItems;
63     }
64     
65     public void visit(Schema s) {
66         if(s != null) {
67             visitChildren(s);
68         }
69     }
70     
71     public void visit(Import im) {
72         
73         //verify if imported document is available
74
Collection JavaDoc<Schema> schemas = im.getModel().findSchemas(im.getNamespace());
75         
76         if(schemas == null || schemas.isEmpty()) {
77                 // it can be an inline xsd in wsdl types section
78
WSDLSchema schema = findInlineSchema(im.getNamespace());
79                 if(schema == null) {
80                     logValidation
81                         (Validator.ResultType.ERROR, im,
82                         NbBundle.getMessage(SchemaSemanticsVisitor.class,
83                                             VAL_MISSING_IMPORTED_DOCUMENT,
84                                              im.getNamespace(),
85                                              im.getSchemaLocation()),
86                         NbBundle.getMessage(SchemaSemanticsVisitor.class, FIX_MISSING_IMPORTED_DOCUMENT)
87                         );
88                 }
89             
90         }
91         
92 // for (Schema s : schemas) {
93
// mValidation.validate(s.getModel(), ValidationType.COMPLETE);
94
// }
95
}
96
97     private void visitChildren(SchemaComponent w) {
98         Collection JavaDoc coll = w.getChildren();
99         if (coll != null) {
100             Iterator JavaDoc iter = coll.iterator();
101             while (iter.hasNext()) {
102                 SchemaComponent component = (SchemaComponent) iter.next();
103                 component.accept(this);
104             }
105         }
106     }
107     
108     private void logValidation(Validator.ResultType type,
109                                  Component component,
110                                  String JavaDoc desc,
111                                  String JavaDoc correction) {
112         String JavaDoc message = desc;
113         if (correction != null) {
114             message = desc + " : " + correction;
115         }
116         ResultItem item = new Validator.ResultItem(mValidator, type, component, message);
117         mResultItems.add(item);
118         
119     }
120     
121     private WSDLSchema findInlineSchema(String JavaDoc namespace) {
122         if(namespace == null) {
123             return null;
124         }
125
126         WSDLSchema matchingSchema = null;
127         
128         Definitions def = mParentModel.getDefinitions();
129         if(def != null) {
130             Types types = def.getTypes();
131             if(types != null) {
132                 Collection JavaDoc<WSDLSchema> schemas = types.getExtensibilityElements(WSDLSchema.class);
133                 if(schemas != null) {
134                     Iterator JavaDoc<WSDLSchema> sIt = schemas.iterator();
135                     while(sIt.hasNext()) {
136                         WSDLSchema wSchema = sIt.next();
137                         SchemaModel sModel = wSchema.getSchemaModel();
138                         if(sModel != null && sModel.getSchema() != null) {
139                             String JavaDoc targetNamespace = sModel.getSchema().getTargetNamespace();
140                             if(namespace.equals(targetNamespace)) {
141                                 matchingSchema = wSchema;
142                                 break;
143                             }
144                         }
145                     }
146                 }
147             }
148         }
149         
150         return matchingSchema;
151     }
152 }
153
Popular Tags