KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > extensibility > model > SchemaBundleGenerator


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 package org.netbeans.modules.xml.wsdl.ui.extensibility.model;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import org.netbeans.modules.xml.schema.model.All;
32 import org.netbeans.modules.xml.schema.model.AnyAttribute;
33 import org.netbeans.modules.xml.schema.model.AnyElement;
34 import org.netbeans.modules.xml.schema.model.AttributeGroupReference;
35 import org.netbeans.modules.xml.schema.model.AttributeReference;
36 import org.netbeans.modules.xml.schema.model.Choice;
37 import org.netbeans.modules.xml.schema.model.ComplexContent;
38 import org.netbeans.modules.xml.schema.model.ComplexContentDefinition;
39 import org.netbeans.modules.xml.schema.model.ComplexContentRestriction;
40 import org.netbeans.modules.xml.schema.model.ComplexExtension;
41 import org.netbeans.modules.xml.schema.model.ComplexExtensionDefinition;
42 import org.netbeans.modules.xml.schema.model.ComplexType;
43 import org.netbeans.modules.xml.schema.model.ComplexTypeDefinition;
44 import org.netbeans.modules.xml.schema.model.Element;
45 import org.netbeans.modules.xml.schema.model.ElementReference;
46 import org.netbeans.modules.xml.schema.model.GlobalAttribute;
47 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
48 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
49 import org.netbeans.modules.xml.schema.model.GlobalElement;
50 import org.netbeans.modules.xml.schema.model.GlobalGroup;
51 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
52 import org.netbeans.modules.xml.schema.model.GlobalType;
53 import org.netbeans.modules.xml.schema.model.GroupReference;
54 import org.netbeans.modules.xml.schema.model.LocalAttribute;
55 import org.netbeans.modules.xml.schema.model.LocalComplexType;
56 import org.netbeans.modules.xml.schema.model.LocalElement;
57 import org.netbeans.modules.xml.schema.model.LocalGroupDefinition;
58 import org.netbeans.modules.xml.schema.model.LocalSimpleType;
59 import org.netbeans.modules.xml.schema.model.LocalType;
60 import org.netbeans.modules.xml.schema.model.NameableSchemaComponent;
61 import org.netbeans.modules.xml.schema.model.Schema;
62 import org.netbeans.modules.xml.schema.model.SchemaComponent;
63 import org.netbeans.modules.xml.schema.model.Sequence;
64 import org.netbeans.modules.xml.schema.model.SequenceDefinition;
65 import org.netbeans.modules.xml.schema.model.SimpleContent;
66 import org.netbeans.modules.xml.schema.model.SimpleContentRestriction;
67 import org.netbeans.modules.xml.schema.model.SimpleExtension;
68 import org.netbeans.modules.xml.schema.model.SimpleType;
69 import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction;
70 import org.netbeans.modules.xml.schema.model.TypeContainer;
71 import org.netbeans.modules.xml.wsdl.ui.property.model.AbstractXSDVisitor;
72 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
73
74 /**
75  *
76  * @author radval
77  */

78 public class SchemaBundleGenerator extends AbstractXSDVisitor {
79     
80     private List JavaDoc mChildren = new ArrayList JavaDoc();
81     
82     private File JavaDoc mBundleFile;
83     
84     private StringBuffer JavaDoc mBuffer = new StringBuffer JavaDoc(200);
85     
86     private NameableSchemaComponent mCurrentElement;
87     
88     private Map JavaDoc<NameableSchemaComponent, ElementToKeyName> elementToKeyNameMap = new HashMap JavaDoc<NameableSchemaComponent, ElementToKeyName>();
89     
90     private Schema mSchema;
91     
92     /** Creates a new instance of NodeChildrenCreatorVisitor */
93     public SchemaBundleGenerator(File JavaDoc bundleFile, Schema schema) {
94         this.mBundleFile = bundleFile;
95         this.mSchema = schema;
96         
97         
98     }
99     
100     public void generate() {
101         Collection JavaDoc<GlobalElement> elements = this.mSchema.getElements();
102         Iterator JavaDoc<GlobalElement> it = elements.iterator();
103         
104         while(it.hasNext()) {
105             GlobalElement ge = it.next();
106             visit(ge);
107         }
108         
109         this.writeToFile();
110     }
111     
112         public void visit(LocalAttribute la) {
113             visitAttribute(la);
114     }
115     
116         public void visit(AttributeReference reference) {
117         NamedComponentReference<GlobalAttribute> ga = reference.getRef();
118                 if(ga != null) {
119                     visit(ga.get());
120                 }
121     }
122
123         public void visit(GlobalAttribute ga) {
124             visitAttribute(ga);
125     }
126         
127         public void visit(AttributeGroupReference agr) {
128         NamedComponentReference<GlobalAttributeGroup> aGroup = agr.getGroup();
129                 if(aGroup != null) {
130                     visit(aGroup.get());
131                 }
132         }
133
134
135     public void visit(GlobalAttributeGroup gag) {
136         List JavaDoc<SchemaComponent> children = gag.getChildren();
137                 Iterator JavaDoc<SchemaComponent> it = children.iterator();
138                 
139                 while(it.hasNext()) {
140                     SchemaComponent sc = it.next();
141                     if(sc instanceof LocalAttribute) {
142                         visit((LocalAttribute) sc);
143                     } else if(sc instanceof AttributeReference) {
144                         visit((AttributeReference) sc );
145                     } else if(sc instanceof AttributeGroupReference) {
146                         visit((AttributeGroupReference) sc);
147                     }
148                 }
149                 
150     }
151
152         public void visit(ElementReference er) {
153             NamedComponentReference<GlobalElement> ge = er.getRef();
154             if(ge != null && ge.get() != null) {
155                 visit(ge.get());
156             }
157         }
158
159         public void visit(GlobalElement ge) {
160             visitElement(ge);
161             
162         }
163                 
164         public void visit(All all) {
165         Collection JavaDoc<LocalElement> allElements = all.getElements();
166                 Iterator JavaDoc<LocalElement> it = allElements.iterator();
167                 while(it.hasNext()) {
168                     LocalElement element = it.next();
169                     visit(element);
170                 }
171     }
172         
173     public void visit(AnyAttribute anyAttr) {
174         
175     }
176
177     public void visit(AnyElement any) {
178             
179     }
180
181     
182
183     public void visit(Choice choice) {
184         List JavaDoc<SchemaComponent> children = choice.getChildren();
185                 Iterator JavaDoc<SchemaComponent> it = children.iterator();
186                 
187                 while(it.hasNext()) {
188                     SchemaComponent comp = it.next();
189                     if(comp instanceof AnyElement) {
190                         visit((AnyElement) comp);
191                     } else if(comp instanceof Choice) {
192                         visit((Choice) comp);
193                     } else if(comp instanceof ElementReference) {
194                         visit((ElementReference) comp);
195                     } else if(comp instanceof GroupReference) {
196                         visit((GroupReference) comp);
197                     } else if(comp instanceof LocalElement) {
198                         visit((LocalElement) comp);
199                     } else if(comp instanceof Sequence) {
200                         visit((Sequence) comp);
201                     }
202                 }
203     }
204
205     public void visit(ComplexContent cc) {
206         
207         ComplexContentDefinition ccd = cc.getLocalDefinition();
208         if(ccd != null) {
209             visit(ccd);
210         }
211     }
212
213     
214
215     
216
217     public void visit(GlobalComplexType gct) {
218         visit((GlobalType) gct);
219     }
220
221
222     public void visit(GlobalGroup gd) {
223         LocalGroupDefinition lgd = gd.getDefinition();
224                 if(lgd != null) {
225                     if(lgd instanceof Choice) {
226                         visit((Choice) lgd);
227                     } else if(lgd instanceof All) {
228                         visit((All) lgd );
229                     } else if(lgd instanceof Sequence) {
230                         visit((Sequence)lgd);
231                     }
232                 }
233     }
234
235     public void visit(GlobalSimpleType gst) {
236         visit((GlobalType) gst);
237     }
238
239     public void visit(GroupReference gr) {
240         NamedComponentReference<GlobalGroup> gg = gr.getRef();
241                 if(gg != null) {
242                     visit(gg.get());
243                 }
244     }
245
246     public void visit(LocalComplexType type) {
247         visit((LocalType) type);
248     }
249
250     public void visit(LocalElement le) {
251             visitElement(le);
252     }
253
254     public void visit(LocalSimpleType type) {
255         visit((LocalType) type);
256     }
257
258     public void visit(Sequence s) {
259             visit(s.getContent());
260     }
261
262     public void visit(SimpleContent sc) {
263         
264     }
265
266     public void visit(SimpleContentRestriction scr) {
267         
268     }
269
270     public void visit(SimpleExtension se) {
271         
272     }
273
274     public void visit(SimpleTypeRestriction str) {
275         
276     }
277         
278         
279         public void visit(GlobalType gt) {
280             if(gt instanceof ComplexType) {
281                 visit((ComplexType) gt);
282             } else if(gt instanceof SimpleType) {
283                 visit((SimpleType) gt);
284             }
285         }
286         
287         public void visit(LocalType lt) {
288             if(lt instanceof ComplexType) {
289                 visit((ComplexType) lt);
290             } else if(lt instanceof SimpleType) {
291                 visit((SimpleType) lt);
292             }
293         }
294         
295         private void visit(SimpleType st) {
296         }
297         
298         private void visit(ComplexType ct) {
299             List JavaDoc<SchemaComponent> children = ct.getChildren();
300             Iterator JavaDoc<SchemaComponent> it = children.iterator();
301             while(it.hasNext()) {
302                 SchemaComponent sc = it.next();
303                 if(sc instanceof AnyAttribute) {
304                     visit((AnyAttribute) sc );
305                 } else if(sc instanceof AttributeGroupReference) {
306                     visit((AttributeGroupReference) sc);
307                 }else if(sc instanceof AttributeReference) {
308                     visit((AttributeReference) sc);
309                 }else if(sc instanceof LocalAttribute) {
310                     visit((LocalAttribute) sc);
311                 }else if(sc instanceof ComplexTypeDefinition) {
312                     visit((ComplexTypeDefinition) sc);
313                 }
314             }
315             
316             //search TypeContainer
317
//getAttributeGroupReferences
318

319         }
320         
321         private void visit(ComplexTypeDefinition ctd) {
322             if (ctd instanceof All) {
323                 visit((All) ctd);
324             } else if (ctd instanceof Choice) {
325                 visit((Choice) ctd);
326             } else if (ctd instanceof Sequence) {
327                 visit((Sequence) ctd);
328             } else if(ctd instanceof ComplexContent) {
329                 visit((ComplexContent) ctd);
330             } else if(ctd instanceof GroupReference) {
331                 visit((GroupReference) ctd);
332             } else if (ctd instanceof SimpleContent) {
333                 visit((SimpleContent) ctd);
334             }
335         }
336         
337         private void visit(ComplexContentDefinition ccd) {
338             
339             if(ccd instanceof ComplexContentRestriction) {
340                 visit((ComplexContentRestriction) ccd);
341             } else if(ccd instanceof ComplexExtension) {
342                 visit((ComplexExtension) ccd);
343             }
344             
345         }
346         
347        
348        public void visit(ComplexContentRestriction ccr) {
349            NamedComponentReference<GlobalComplexType> baseRef = ccr.getBase();
350            if(baseRef != null) {
351                 GlobalComplexType gType = baseRef.get();
352                 if(gType != null) {
353                     visit(gType);
354                 }
355            }
356            
357            List JavaDoc children = ccr.getChildren();
358            Iterator JavaDoc it = children.iterator();
359            while(it.hasNext()) {
360                Object JavaDoc child = it.next();
361                
362                if(child instanceof AnyAttribute) {
363                    visit((AnyAttribute) child);
364                } else if(child instanceof AttributeGroupReference) {
365                    visit((AttributeGroupReference) child);
366                } else if(child instanceof AttributeReference) {
367                    visit((AttributeReference) child);
368                } else if (child instanceof ComplexTypeDefinition) {
369                    visit((ComplexTypeDefinition) child);
370                }
371            }
372        }
373
374        public void visit(ComplexExtension ce) {
375            NamedComponentReference<GlobalType> baseRef = ce.getBase();
376            if(baseRef != null) {
377                 GlobalType gType = baseRef.get();
378                 if(gType != null) {
379                     visit(gType);
380                 }
381            }
382            
383            List JavaDoc children = ce.getChildren();
384            Iterator JavaDoc it = children.iterator();
385            while(it.hasNext()) {
386                Object JavaDoc child = it.next();
387                
388                if(child instanceof LocalAttribute) {
389                    visit((LocalAttribute) child);
390                }else if(child instanceof AnyAttribute) {
391                    visit((AnyAttribute) child);
392                } else if(child instanceof AttributeGroupReference) {
393                    visit((AttributeGroupReference) child);
394                } else if(child instanceof AttributeReference) {
395                    visit((AttributeReference) child);
396                } else if (child instanceof ComplexExtensionDefinition) {
397                    visit((ComplexExtensionDefinition) child);
398                }
399            }
400        }
401         
402         
403        private void visit(ComplexExtensionDefinition ced) {
404            if (ced instanceof All) {
405                 visit((All) ced);
406             } else if (ced instanceof Choice) {
407                 visit((Choice) ced);
408             } else if (ced instanceof Sequence) {
409                 visit((Sequence) ced);
410             } else if(ced instanceof GroupReference) {
411                 visit((GroupReference) ced);
412             }
413        }
414        
415         private void visit(List JavaDoc<SequenceDefinition> sdList) {
416             Iterator JavaDoc<SequenceDefinition> it = sdList.iterator();
417             while(it.hasNext()) {
418                 SequenceDefinition sd = it.next();
419                 if(sd instanceof Sequence) {
420                     visit((Sequence) sd);
421                 } else if(sd instanceof AnyElement) {
422                     visit((AnyElement) sd);
423                 } else if(sd instanceof Choice) {
424                     visit((Choice) sd);
425                 } else if(sd instanceof ElementReference) {
426                     visit((ElementReference) sd);
427                 } else if(sd instanceof GroupReference) {
428                     visit((GroupReference) sd);
429                 } else if(sd instanceof LocalElement) {
430                     visit((LocalElement) sd);
431                 }
432                 
433             }
434         }
435         
436         void visitAttribute(NameableSchemaComponent la) {
437             StringBuffer JavaDoc key = new StringBuffer JavaDoc(20);
438             String JavaDoc elementPrefix = getElementPrefix();
439             if(elementPrefix != null) {
440                 key.append(elementPrefix);
441                 key.append("_");
442             }
443             
444             key.append(la.getName());
445             storeKeyValue(key.toString(), la.getName());
446         }
447         
448         private void visitElement(NameableSchemaComponent ge) {
449             StringBuffer JavaDoc key = new StringBuffer JavaDoc(20);
450             
451             ElementToKeyName ekn = elementToKeyNameMap.get(mCurrentElement);
452         if(ekn != null) {
453                 String JavaDoc prefix = ekn.getKeyPrefix();
454                 key.append(prefix);
455                 key.append("_");
456             }
457             key.append(ge.getName());
458             storeKeyValue(key.toString(), ge.getName());
459             
460             ElementToKeyName newEkn = new ElementToKeyName(ge, key.toString());
461             elementToKeyNameMap.put(ge, newEkn);
462             
463             if(ge instanceof TypeContainer) {
464                 TypeContainer tc = (TypeContainer) ge;
465                 LocalType lt = tc.getInlineType();
466                 NamedComponentReference gtRef = tc.getType();
467                 if(lt != null) {
468                     visit(lt);
469                 } else if(gtRef != null && gtRef.getType() != null) {
470                     visit((GlobalType) gtRef.get());
471                 }
472             }
473         }
474         
475         private String JavaDoc getElementPrefix() {
476             String JavaDoc elementPrefix = null;
477             if(mCurrentElement != null) {
478                 ElementToKeyName ekn = elementToKeyNameMap.get(mCurrentElement);
479                 if(ekn != null) {
480                   elementPrefix = ekn.getKeyPrefix();
481                 }
482                 
483             }
484             
485             return elementPrefix;
486         }
487         private void storeKeyValue(String JavaDoc key, String JavaDoc value) {
488             mBuffer.append(key);
489             mBuffer.append("=");
490             mBuffer.append(value);
491             mBuffer.append("\n");
492         }
493         
494         private void writeToFile() {
495             try {
496                 String JavaDoc content = mBuffer.toString();
497                 FileWriter JavaDoc fWriter = new FileWriter JavaDoc(mBundleFile);
498                 fWriter.write(content);
499                 fWriter.close();
500             } catch(Exception JavaDoc ex) {
501                 ex.printStackTrace();
502             }
503         }
504         
505         
506         class ElementToKeyName {
507             
508             private NameableSchemaComponent mElement;
509             
510             private String JavaDoc mKeyPrefix;
511             
512             
513             ElementToKeyName(NameableSchemaComponent element, String JavaDoc keyPrefix) {
514                 this.mElement = element;
515                 this.mKeyPrefix = keyPrefix;
516                 
517             }
518             
519             
520             public NameableSchemaComponent getElement() {
521                 return this.mElement;
522             }
523             
524             public String JavaDoc getKeyPrefix() {
525                 return this.mKeyPrefix;
526             }
527             
528         }
529             
530 }
531
Popular Tags