KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > actions > schema > ExtensibilityElementCreatorVisitor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * NodeChildrenCreatorVisitor.java
22  *
23  * Created on March 28, 2006, 6:10 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.wsdl.ui.actions.schema;
30
31 import java.io.IOException JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Stack JavaDoc;
36
37 import javax.xml.namespace.QName JavaDoc;
38
39 import org.netbeans.modules.xml.schema.model.All;
40 import org.netbeans.modules.xml.schema.model.AnyAttribute;
41 import org.netbeans.modules.xml.schema.model.AnyElement;
42 import org.netbeans.modules.xml.schema.model.Attribute;
43 import org.netbeans.modules.xml.schema.model.AttributeGroupReference;
44 import org.netbeans.modules.xml.schema.model.AttributeReference;
45 import org.netbeans.modules.xml.schema.model.Choice;
46 import org.netbeans.modules.xml.schema.model.ElementReference;
47 import org.netbeans.modules.xml.schema.model.Enumeration;
48 import org.netbeans.modules.xml.schema.model.GlobalAttribute;
49 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
50 import org.netbeans.modules.xml.schema.model.GlobalElement;
51 import org.netbeans.modules.xml.schema.model.GlobalGroup;
52 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
53 import org.netbeans.modules.xml.schema.model.GlobalType;
54 import org.netbeans.modules.xml.schema.model.GroupReference;
55 import org.netbeans.modules.xml.schema.model.LocalAttribute;
56 import org.netbeans.modules.xml.schema.model.LocalElement;
57 import org.netbeans.modules.xml.schema.model.LocalType;
58 import org.netbeans.modules.xml.schema.model.SchemaComponent;
59 import org.netbeans.modules.xml.schema.model.SchemaModel;
60 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
61 import org.netbeans.modules.xml.schema.model.Sequence;
62 import org.netbeans.modules.xml.schema.model.SimpleContent;
63 import org.netbeans.modules.xml.schema.model.SimpleContentRestriction;
64 import org.netbeans.modules.xml.schema.model.SimpleExtension;
65 import org.netbeans.modules.xml.schema.model.SimpleType;
66 import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction;
67 import org.netbeans.modules.xml.schema.model.TypeContainer;
68 import org.netbeans.modules.xml.wsdl.model.Definitions;
69 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
70 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
71 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
72 import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
73 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
74 import org.netbeans.modules.xml.wsdl.ui.schema.visitor.AbstractXSDVisitor;
75 import org.netbeans.modules.xml.wsdl.ui.spi.ExtensibilityElementConfigurator;
76 import org.netbeans.modules.xml.wsdl.ui.spi.ExtensibilityElementConfiguratorFactory;
77 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
78 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
79 import org.openide.ErrorManager;
80 import org.openide.util.NbBundle;
81
82 /**
83  *
84  * @author radval
85  */

86 public class ExtensibilityElementCreatorVisitor extends AbstractXSDVisitor {
87     
88     private WSDLComponent mComponent;
89     
90     private WSDLModel mModel;
91     
92     private Definitions mDefinitions;
93     
94     private Stack JavaDoc<WSDLComponent> mStack = new Stack JavaDoc<WSDLComponent>();
95     
96     private ExtensibilityElement mRootExtensibilityElement;
97     
98     
99     /** Creates a new instance of ExtensibilityElementCreatorVisitor */
100     public ExtensibilityElementCreatorVisitor(WSDLComponent component) {
101         this.mComponent = component;
102         //push the component to stack so when we visit
103
//schema element we can add extensibilty element
104
//crated for schema element to it
105
push(this.mComponent);
106
107         this.mModel = component.getModel();
108         this.mDefinitions = this.mModel.getDefinitions();
109         
110     }
111     
112     public ExtensibilityElement getRootExtensibilittElement() {
113         return this.mRootExtensibilityElement;
114     }
115     
116     @Override JavaDoc
117     public void visit(LocalAttribute la) {
118         Attribute.Use use = la.getUseEffective();
119         if(use.equals(Attribute.Use.REQUIRED)) {
120             NamedComponentReference<GlobalSimpleType> lstRef = la.getType();
121             if(lstRef != null) {
122                 visitAttribute(la, la.getName(), lstRef.get());
123             } else {
124                 visitAttribute(la, la.getName(), la.getInlineType());
125             }
126         }
127     }
128     
129     @Override JavaDoc
130     public void visit(AttributeReference reference) {
131         Attribute.Use use = reference.getUseEffective();
132         if(use.equals(Attribute.Use.REQUIRED)) {
133             NamedComponentReference<GlobalAttribute> ga = reference.getRef();
134             if(ga != null) {
135                 visit(ga.get());
136             }
137         }
138     }
139     
140     @Override JavaDoc
141     public void visit(GlobalAttribute ga) {
142         NamedComponentReference<GlobalSimpleType> gstRef = ga.getType();
143         if(gstRef != null) {
144             visitAttribute(ga, ga.getName(), gstRef.get());
145         } else {
146             visitAttribute(ga, ga.getName(), ga.getInlineType());
147         }
148     }
149     
150     @Override JavaDoc
151     public void visit(AttributeGroupReference agr) {
152         NamedComponentReference<GlobalAttributeGroup> aGroup = agr.getGroup();
153         if(aGroup != null) {
154             visit(aGroup.get());
155         }
156     }
157     
158     
159     @Override JavaDoc
160     public void visit(GlobalAttributeGroup gag) {
161         List JavaDoc<SchemaComponent> children = gag.getChildren();
162         Iterator JavaDoc<SchemaComponent> it = children.iterator();
163         
164         while(it.hasNext()) {
165             SchemaComponent sc = it.next();
166             if(sc instanceof LocalAttribute) {
167                 visit((LocalAttribute) sc);
168             } else if(sc instanceof AttributeReference) {
169                 visit((AttributeReference) sc );
170             } else if(sc instanceof AttributeGroupReference) {
171                 visit((AttributeGroupReference) sc);
172             }
173         }
174         
175     }
176     
177 // public void visit(AllElement ae) {
178
// Occur.ZeroOne oc = ae.getMinOccursEffective();
179
// if(oc != null && oc.equals(Occur.ZeroOne.ONE)) {
180
// String namespace = ae.getSchemaModel().getSchema().getTargetNamespace();
181
// visit(ae, ae.getName(), namespace);
182
// }
183
// }
184
//
185
// public void visit(AllElementReference allElementReference) {
186
// Occur.ZeroOne oc = allElementReference.getMinOccursEffective();
187
// if(oc != null && oc.equals(Occur.ZeroOne.ONE)) {
188
// NamedComponentReference<GlobalElement> geRef = allElementReference.getRef();
189
// if(geRef != null && geRef.get() != null) {
190
// visit(geRef.get());
191
// }
192
// }
193
// }
194

195     @Override JavaDoc
196     public void visit(ElementReference er) {
197         int minOccurs = er.getMinOccursEffective();
198         //if this is top level object or min occur is > 0 then visit
199
//top leaving meaning this is the entry point for
200
//visitor and stack have the wsdl component for
201
//which we are creating extensibility element
202
if(mStack.size() == 1 || minOccurs > 0) {
203             NamedComponentReference<GlobalElement> geRef = er.getRef();
204             if(geRef != null && geRef.get() != null) {
205                 visit(geRef.get());
206             }
207         }
208     }
209     
210     @Override JavaDoc
211     public void visit(LocalElement le) {
212         int minOccurs = le.getMinOccursEffective();
213         //if this is top level object or min occur is > 0 then visit
214
if(mStack.size() == 1 || minOccurs > 0) {
215             String JavaDoc namespace = le.getModel().getSchema().getTargetNamespace();
216             visit(le, le.getName(), namespace);
217         }
218     }
219     
220     @Override JavaDoc
221     public void visit(GlobalElement ge) {
222         String JavaDoc namespace = ge.getModel().getSchema().getTargetNamespace();
223         visit(ge, ge.getName(), namespace);
224     }
225     
226     @Override JavaDoc
227     public void visit(All all) {
228         Collection JavaDoc<LocalElement> allElements = all.getElements();
229         Iterator JavaDoc<LocalElement> it = allElements.iterator();
230         while(it.hasNext()) {
231             LocalElement element = it.next();
232             visit(element);
233         }
234         
235         Collection JavaDoc<ElementReference> elementRefs = all.getElementReferences();
236         Iterator JavaDoc<ElementReference> itER = elementRefs.iterator();
237         while(itER.hasNext()) {
238             ElementReference element = itER.next();
239             visit(element);
240         }
241     }
242     
243     @Override JavaDoc
244     public void visit(AnyAttribute anyAttr) {
245 // Node node = NodeFactory.getInstance().createNode(anyAttr);
246
// if(node != null) {
247
// addChild((TreeNode) node);
248
// }
249
}
250     
251     @Override JavaDoc
252     public void visit(AnyElement any) {
253 // Node node = NodeFactory.getInstance().createNode(any);
254
// if(node != null) {
255
// addChild((TreeNode) node);
256
// }
257
}
258     
259     
260     
261     @Override JavaDoc
262     public void visit(Choice choice) {
263         List JavaDoc<SchemaComponent> children = choice.getChildren();
264         Iterator JavaDoc<SchemaComponent> it = children.iterator();
265         
266         while(it.hasNext()) {
267             SchemaComponent comp = it.next();
268             if(comp instanceof AnyElement) {
269                 visit((AnyElement) comp);
270             } else if(comp instanceof Choice) {
271                 visit((Choice) comp);
272             } else if(comp instanceof ElementReference) {
273                 visit((ElementReference) comp);
274             } else if(comp instanceof GroupReference) {
275                 visit((GroupReference) comp);
276             } else if(comp instanceof LocalElement) {
277                 visit((LocalElement) comp);
278             } else if(comp instanceof Sequence) {
279                 visit((Sequence) comp);
280             }
281         }
282     }
283     
284     
285 // public void visit(GroupAll ga) {
286
// List<SchemaComponent> children = ga.getChildren();
287
// Iterator<SchemaComponent> it = children.iterator();
288
//
289
// while(it.hasNext()) {
290
// SchemaComponent sc = it.next();
291
// if(sc instanceof AllElement) {
292
// visit((AllElement) sc);
293
// } else if(sc instanceof AllElementReference) {
294
// visit((AllElementReference) sc);
295
// }
296
// }
297
// }
298
//
299
// public void visit(GroupChoice gc) {
300
// List<SchemaComponent> children = gc.getChildren();
301
// Iterator<SchemaComponent> it = children.iterator();
302
//
303
// while(it.hasNext()) {
304
// SchemaComponent sc = it.next();
305
// if(sc instanceof AnyElement) {
306
// visit((AnyElement) sc);
307
// } else if(sc instanceof Choice) {
308
// visit((Choice) sc);
309
// } else if(sc instanceof ElementReference) {
310
// visit((ElementReference) sc);
311
// } else if(sc instanceof GroupReference) {
312
// visit((GroupReference) sc);
313
// } else if(sc instanceof LocalElement) {
314
// visit((LocalElement) sc);
315
// } else if(sc instanceof Sequence) {
316
// visit((Sequence)sc);
317
// }
318
//
319
// }
320
// }
321

322     @Override JavaDoc
323     public void visit(GroupReference gr) {
324         NamedComponentReference<GlobalGroup> gg = gr.getRef();
325         if(gg != null) {
326             visit(gg.get());
327         }
328     }
329     
330 // public void visit(GroupSequence gs) {
331
// visit(gs.getContent());
332
// }
333

334     
335     @Override JavaDoc
336     public void visit(SimpleContent sc) {
337         
338     }
339     
340     @Override JavaDoc
341     public void visit(SimpleContentRestriction scr) {
342         
343     }
344     
345     @Override JavaDoc
346     public void visit(SimpleExtension se) {
347         
348     }
349     
350     @Override JavaDoc
351     public void visit(SimpleTypeRestriction str) {
352         
353     }
354     
355     private void visit(TypeContainer ge, String JavaDoc elementName, String JavaDoc namespace) {
356         String JavaDoc prefix = createNamespacePrefix(namespace);
357         
358         ExtensibilityElement element = createExtensibilityElement(elementName, prefix, namespace, peek());
359         addExtensibilityElement(peek(), element);
360         ExtensibilityElementConfigurator configurator = new ExtensibilityElementConfiguratorFactory().getExtensibilityElementConfigurator(new QName JavaDoc(namespace, elementName));
361         if (configurator != null) {
362             String JavaDoc attributeName = configurator.getDisplayAttributeName(element, element.getQName());
363             if (attributeName != null && element.getAttribute(attributeName) == null) {
364                 String JavaDoc keyValuePrefix = null;
365                 if ((keyValuePrefix = configurator.getAttributeUniqueValuePrefix(element, element.getQName(), attributeName)) != null) {
366                     
367                     boolean isInTransaction = Utility.startTransaction(element.getModel());
368                     element.setAttribute(attributeName,
369                             NameGenerator.generateUniqueValueForKeyAttribute(element, attributeName, element.getQName(), keyValuePrefix));
370                     Utility.endTransaction(element.getModel(), isInTransaction);
371                 }
372             }
373         }
374         if(mRootExtensibilityElement == null) {
375             mRootExtensibilityElement = element;
376         }
377         push(element);
378         if(ge.getType() != null) {
379             GlobalType gt = ge.getType().get();
380             if(gt != null) {
381                 visit(gt);
382             }
383         } else {
384             LocalType lt = ge.getInlineType();
385             visit(lt);
386         }
387         
388         pop();
389     }
390     
391     private void visitAttribute(Attribute attr, String JavaDoc attrName, SimpleType simpleType) {
392         String JavaDoc defaultValue = attr.getDefault();
393         String JavaDoc fixedValue = attr.getFixed();
394         ExtensibilityElement exElement = (ExtensibilityElement) peek();
395         String JavaDoc attrVal = null;
396         
397         //Already has a value.
398
if (exElement.getAttribute(attrName) != null)
399             return;
400         
401         if(defaultValue != null) {
402             attrVal = defaultValue;
403         } else if(fixedValue != null) {
404             attrVal = fixedValue;
405         } else {
406             QName JavaDoc elementQName = new QName JavaDoc(attr.getModel().getSchema().getTargetNamespace(), exElement.getQName().getLocalPart());
407             ExtensibilityElementConfigurator configurator =
408                 new ExtensibilityElementConfiguratorFactory().getExtensibilityElementConfigurator(elementQName);
409             if(simpleType != null) {
410                 //for boolean type we show true/false drop down
411
String JavaDoc simpleTypeName = null;
412                 if (simpleType instanceof GlobalSimpleType) {
413                     simpleTypeName = ((GlobalSimpleType) simpleType).getName();
414                 }
415                 
416                 String JavaDoc namesapce = simpleType.getModel().getSchema().getTargetNamespace();
417                 SchemaModel primitiveTypesModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel();
418                 String JavaDoc primitiveTypeNamesapce = primitiveTypesModel.getSchema().getTargetNamespace();
419                 if(namesapce != null
420                         && namesapce.equals(primitiveTypeNamesapce)
421                         && simpleTypeName != null && simpleTypeName.equals("boolean")) {//NOI18N
422
if (configurator != null) {
423                         attrVal = configurator.getDefaultValue(exElement, exElement.getQName(), attrName);
424                     }
425                     if (attrVal == null) {
426                         attrVal = "true";//NOI18N
427
addAttributeToExtensibilityElement(exElement, attrName, attrVal);
428                         return;
429                     }
430                 } else if(simpleType.getDefinition() instanceof SimpleTypeRestriction) {
431                     //if attribute has enumeration facet
432
//then use the first enumeration value
433
SimpleTypeRestriction sr = (SimpleTypeRestriction) simpleType.getDefinition();
434                     Collection JavaDoc enumerations = sr.getEnumerations();
435                     if(enumerations != null && enumerations != null) {
436                         if (configurator != null) {
437                             attrVal = configurator.getDefaultValue(exElement, exElement.getQName(), attrName);
438                         }
439                         if (attrVal == null) {
440                             Iterator JavaDoc enuIter = enumerations.iterator();
441                             if(enuIter.hasNext()) {
442                                 Enumeration facet = (Enumeration) enuIter.next();
443                                 attrVal = facet.getValue();
444                                 addAttributeToExtensibilityElement(exElement, attrName, attrVal);
445                                 return;
446                             }
447                         }
448                     }
449                     
450                 }
451             }
452             
453             if(attrVal == null) {
454                 if (configurator == null || (attrVal = configurator.getDefaultValue(exElement, exElement.getQName(), attrName)) == null) {
455                     attrVal = NbBundle.getMessage(ExtensibilityElementCreatorVisitor.class, "REQUIRED_PROPERTY_DEFAULT_VALUE");
456                 }
457             }
458             
459             
460         }
461         addAttributeToExtensibilityElement(exElement, attrName, attrVal);
462         
463         
464     }
465     
466     private String JavaDoc createNamespacePrefix(String JavaDoc targetNamespace) {
467         //(1) see if a prefix for schema targetNamespace
468
//needs to be added
469
String JavaDoc prefix = Utility.getNamespacePrefix(targetNamespace, this.mModel);
470         if(prefix == null) {
471             prefix = NameGenerator.getInstance().generateNamespacePrefix(null, this.mDefinitions);
472             
473             boolean inTransaction = Utility.startTransaction(mModel);
474             ((AbstractDocumentComponent)this.mDefinitions).addPrefix(prefix, targetNamespace);
475             Utility.endTransaction(mModel, inTransaction);
476         }
477         
478         return prefix;
479     }
480     
481     private ExtensibilityElement createExtensibilityElement(String JavaDoc elementName,
482             String JavaDoc prefix,
483             String JavaDoc targetNamespace,
484             WSDLComponent parent) {
485         QName JavaDoc qName = null;
486         
487         if(prefix != null) {
488             qName = new QName JavaDoc(targetNamespace, elementName, prefix);
489         } else {
490             qName = new QName JavaDoc(targetNamespace, elementName);
491         }
492         
493         //create extensibility element
494
//set all its attribute as defined in schema element
495
ExtensibilityElement exElement = (ExtensibilityElement) this.mModel.getFactory().create(parent, qName);
496         
497         return exElement;
498         
499     }
500     
501     private void addExtensibilityElement(WSDLComponent parent, ExtensibilityElement child) {
502         boolean inTransaction = Utility.startTransaction(mModel);
503         
504         parent.addExtensibilityElement(child);
505         
506         Utility.endTransaction(mModel, inTransaction);
507     }
508     
509     private void addAttributeToExtensibilityElement(ExtensibilityElement exElement, String JavaDoc attrName, String JavaDoc attrVal) {
510         boolean inTransaction = Utility.startTransaction(mModel);
511         exElement.setAttribute(attrName, attrVal);
512         Utility.endTransaction(mModel, inTransaction);
513     }
514     
515     private void push(WSDLComponent currentComponent) {
516         mStack.push(currentComponent);
517     }
518     
519     private void pop() {
520         if(!this.mStack.empty()) {
521             this.mStack.pop();
522         }
523     }
524     
525     private WSDLComponent peek() {
526         if(!this.mStack.empty()) {
527             return this.mStack.peek();
528         }
529         
530         return null;
531     }
532 }
533
Popular Tags