KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > impl > SchemaGeneratorUtil


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.axi.impl;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import org.netbeans.modules.xml.axi.AXIComponent;
29 import org.netbeans.modules.xml.axi.AXIDocument;
30 import org.netbeans.modules.xml.axi.AXIModel;
31 import org.netbeans.modules.xml.axi.Attribute;
32 import org.netbeans.modules.xml.axi.Compositor;
33 import org.netbeans.modules.xml.axi.Compositor.CompositorType;
34 import org.netbeans.modules.xml.axi.ContentModel;
35 import org.netbeans.modules.xml.axi.Element;
36 import org.netbeans.modules.xml.axi.AnyElement;
37 import org.netbeans.modules.xml.axi.SchemaGenerator;
38 import org.netbeans.modules.xml.axi.datatype.CustomDatatype;
39 import org.netbeans.modules.xml.axi.datatype.Datatype;
40 import org.netbeans.modules.xml.axi.datatype.NumberBase;
41 import org.netbeans.modules.xml.axi.datatype.UnionType;
42 import org.netbeans.modules.xml.axi.visitor.AXINonCyclicVisitor;
43 import org.netbeans.modules.xml.schema.model.*;
44 import org.netbeans.modules.xml.schema.model.Attribute.Use;
45 import org.netbeans.modules.xml.axi.visitor.FindUsageVisitor;
46 import org.netbeans.modules.xml.axi.visitor.Preview;
47 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
48 import org.w3c.dom.NamedNodeMap JavaDoc;
49 import org.w3c.dom.Node JavaDoc;
50
51 /**
52  * SchemaGenerator Util class.
53  *
54  * @author Ayub Khan
55  */

56 public class SchemaGeneratorUtil {
57     
58     public static NamedComponentReference<GlobalSimpleType> createPrimitiveType(
59             final Datatype d, SchemaComponent referer, SchemaGenerator.PrimitiveCart pc){
60         pc.add(d, referer);
61         String JavaDoc typeName = d.getName();
62         if(d instanceof CustomDatatype)
63             typeName = ((CustomDatatype)d).getBase().getName();
64         return referer.createReferenceTo(pc.getPrimitiveType(typeName), GlobalSimpleType.class);
65     }
66     
67     public static boolean isPrimitiveType(Datatype d) {
68         return !d.hasFacets() && !(d instanceof UnionType || d instanceof CustomDatatype);
69     }
70     
71     public static void createGlobalSimpleType(
72             final Datatype d, final SchemaModel sm, final SchemaComponent sc,
73             final SchemaGenerator.UniqueId id, SchemaGenerator.PrimitiveCart pc) {
74         if(d != null) {
75             NamedComponentReference<GlobalSimpleType> ref =null;
76             if(isPrimitiveType(d)) {
77                 ref = SchemaGeneratorUtil.createPrimitiveType(d, sc, pc);
78             } else {
79                 GlobalSimpleType gst = SchemaGeneratorUtil.createGlobalSimpleType(sm);
80                 String JavaDoc typeName = d.getName();
81                 StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
82                 buf.append("New"+typeName.substring(0, 1).toUpperCase()+
83                         typeName.substring(1)+"Type"+String.valueOf(id.nextId()));
84                 String JavaDoc gstName = findUniqueGlobalName(GlobalSimpleType.class, buf.toString(), sm);
85                 gst.setName(gstName);
86                 sm.getSchema().addSimpleType(gst);
87                 if(d instanceof CustomDatatype)
88                     SchemaGeneratorUtil.populateSimpleType(
89                             ((CustomDatatype)d).getBase(), sm, gst, pc);
90                 else
91                     SchemaGeneratorUtil.populateSimpleType(d, sm, gst, pc);
92                 ref = sc.createReferenceTo(gst, GlobalSimpleType.class);
93             }
94             SchemaGeneratorUtil.setSimpleType(sc, ref);
95         }
96     }
97     
98     public static GlobalSimpleType createGlobalSimpleType(final SchemaModel m) {
99         GlobalSimpleType t = m.getFactory().createGlobalSimpleType();
100         return t;
101     }
102     
103     public static void setSimpleType(final SchemaComponent e, final NamedComponentReference<GlobalSimpleType> ref) {
104         if(e instanceof GlobalElement) {
105             if(((GlobalElement)e).getInlineType() != null)
106                 ((GlobalElement)e).setInlineType(null);
107             ((GlobalElement)e).setType(ref);
108         } else if(e instanceof LocalElement) {
109             if(((LocalElement)e).getInlineType() != null)
110                 ((LocalElement)e).setInlineType(null);
111             ((LocalElement)e).setType(ref);
112         } else if(e instanceof GlobalAttribute) {
113             if(((GlobalAttribute)e).getInlineType() != null)
114                 ((GlobalAttribute)e).setInlineType(null);
115             ((GlobalAttribute)e).setType(ref);
116         } else if(e instanceof LocalAttribute) {
117             if(((LocalAttribute)e).getInlineType() != null)
118                 ((LocalAttribute)e).setInlineType(null);
119             ((LocalAttribute)e).setType(ref);
120         } else if(e instanceof SimpleTypeRestriction) {
121             if(((SimpleTypeRestriction)e).getInlineType() != null) {
122                 ((SimpleTypeRestriction)e).setInlineType(null);
123             }
124             ((SimpleTypeRestriction)e).setBase(ref);
125         } else if(e instanceof Union) {
126             ((Union)e).removeMemberType(ref);
127             ((Union)e).addMemberType(ref);
128         }
129     }
130     
131     public static GlobalSimpleType findGlobalSimpleType(final SchemaModel sm, String JavaDoc name) {
132         for(GlobalSimpleType gst: sm.getSchema().getSimpleTypes()) {
133             String JavaDoc tmp = gst.getName();
134             if(tmp.equals(name))
135                 return gst;
136         }
137         return null;
138     }
139     
140     public static NamedComponentReference<? extends GlobalSimpleType> createInlineSimpleType(
141             final Datatype d, final SchemaModel sm,
142             final SchemaComponent sc, SchemaGenerator.PrimitiveCart pc) {
143         if(sc instanceof org.netbeans.modules.xml.schema.model.Element)
144             return createInlineSimpleType(d, sm, ((org.netbeans.modules.xml.schema.model.Element)sc), pc);
145         else if(sc instanceof org.netbeans.modules.xml.schema.model.Attribute)
146             return createInlineSimpleType(d, sm, ((org.netbeans.modules.xml.schema.model.Attribute)sc), pc);
147         return null;
148     }
149     
150     public static NamedComponentReference<? extends GlobalSimpleType> createInlineSimpleType(
151             final Datatype d, final SchemaModel sm,
152             final org.netbeans.modules.xml.schema.model.Element e, SchemaGenerator.PrimitiveCart pc) {
153         NamedComponentReference ref = null;
154         if(isPrimitiveType(d)) {
155             ref = createPrimitiveType(d, e, pc);
156             if(e instanceof TypeContainer) {
157                 if(((TypeContainer)e).getInlineType() != null)
158                     ((TypeContainer)e).setInlineType(null);
159                 ((TypeContainer)e).setType(ref);
160             }
161         } else {
162             LocalSimpleType lst = createLocalSimpleType(sm, e);
163             if(d instanceof CustomDatatype)
164                 ref = populateSimpleType(((CustomDatatype)d).getBase(), sm, lst, pc);
165             else
166                 ref = populateSimpleType(d, sm, lst, pc);
167         }
168         return ref;
169     }
170     
171     public static NamedComponentReference<? extends GlobalSimpleType> createInlineSimpleType(
172             final Datatype d, final SchemaModel sm,
173             final org.netbeans.modules.xml.schema.model.Attribute attr, SchemaGenerator.PrimitiveCart pc) {
174         NamedComponentReference ref = null;
175         if(isPrimitiveType(d)) {
176             ref = createPrimitiveType(d, attr, pc);
177             if(attr instanceof GlobalAttribute) {
178                 if(((GlobalAttribute)attr).getInlineType() != null)
179                     ((GlobalAttribute)attr).setInlineType(null);
180                 ((GlobalAttribute)attr).setType(ref);
181             } else if(attr instanceof LocalAttribute) {
182                 if(((LocalAttribute)attr).getInlineType() != null)
183                     ((LocalAttribute)attr).setInlineType(null);
184                 ((LocalAttribute)attr).setType(ref);
185             }
186         } else {
187             LocalSimpleType lst = createLocalSimpleType(sm, attr);
188             if(d instanceof CustomDatatype)
189                 ref = populateSimpleType(((CustomDatatype)d).getBase(), sm, lst, pc);
190             else
191                 ref = populateSimpleType(d, sm, lst, pc);
192         }
193         return ref;
194     }
195     
196     public static LocalSimpleType createLocalSimpleType(final SchemaModel m,
197             final org.netbeans.modules.xml.schema.model.Element e) {
198         LocalSimpleType t = m.getFactory().createLocalSimpleType();
199         if(e instanceof TypeContainer) {
200             if(((TypeContainer)e).getType() != null)
201                 ((TypeContainer)e).setType(null);//reset type if any
202
((TypeContainer)e).setInlineType(t);
203         }
204         return t;
205     }
206     
207     public static LocalSimpleType createLocalSimpleType(final SchemaModel m,
208             final org.netbeans.modules.xml.schema.model.Attribute attr) {
209         LocalSimpleType t = m.getFactory().createLocalSimpleType();
210         if(attr instanceof GlobalAttribute) {
211             if(((GlobalAttribute)attr).getType() != null)
212                 ((GlobalAttribute)attr).setType(null);
213             ((GlobalAttribute)attr).setInlineType(t);
214         } else if(attr instanceof LocalAttribute) {
215             if(((LocalAttribute)attr).getType() != null)
216                 ((LocalAttribute)attr).setType(null);
217             ((LocalAttribute)attr).setInlineType(t);
218         }
219         return t;
220     }
221     
222     public static LocalSimpleType createLocalSimpleType(final SchemaModel m,
223             final Union u) {
224         LocalSimpleType lst = m.getFactory().createLocalSimpleType();
225         u.addInlineType(lst);
226         return lst;
227     }
228     
229     public static NamedComponentReference<? extends GlobalSimpleType> populateSimpleType(
230             final Datatype d, final SchemaModel sm, final SchemaComponent st,
231             SchemaGenerator.PrimitiveCart pc) {
232         NamedComponentReference ref = null;
233         if(!d.hasFacets()) {
234             if(d instanceof UnionType) {
235 /*
236  * take care of
237         <attribute name="remarks" use="optional">
238           <simpleType>
239             <union memberTypes="nonNegativeInteger positiveInteger"/>
240           </simpleType>
241         </attribute>
242  */

243                 Union u = createUnion(sm, st);
244                 for(Datatype m:((UnionType)d).getMemberTypes()) {
245                     ref = createPrimitiveType(m, u, pc);
246                     u.addMemberType(ref);
247                 }
248             } else {
249                 ref = createFacets(d, sm, st, pc);
250             }
251         } else {
252             if(d instanceof UnionType) {
253 /*
254  *take care of
255         <attribute name="remarks2" use="optional">
256           <simpleType>
257             <union>
258               <simpleType>
259                 <restriction base='nonNegativeInteger'/>
260               </simpleType>
261               <simpleType>
262                 <restriction base='string'>
263                   <enumeration value='unbounded'/>
264                 </restriction>
265               </simpleType>
266             </union>
267           </simpleType>
268         </attribute>
269  */

270                 Union u = createUnion(sm, st);
271                 
272                 for(Datatype m:((UnionType)d).getMemberTypes()) {
273                     LocalSimpleType lst2 = createLocalSimpleType(sm, u);
274                     //ref = createPrimitiveType(m, lst2, pc);
275
ref = createFacets(m, sm, lst2, pc);
276                 }
277             } else {
278                 //ref = createPrimitiveType(d, st, pc);
279
ref = createFacets(d, sm, st, pc);
280             }
281         }
282         return ref;
283     }
284     
285     public static NamedComponentReference createFacets(final Datatype d, final SchemaModel sm,
286             final SchemaComponent lst, SchemaGenerator.PrimitiveCart pc) {
287         SimpleTypeRestriction def = createSimpleRestriction(sm, (SimpleType)lst);
288         NamedComponentReference ref = createPrimitiveType(d, def, pc);
289         def.setBase(ref);
290         createLength(d, sm, def);
291         createMinLength(d, sm, def);
292         createMaxLength(d, sm, def);
293         createPattern(d, sm, def);
294         createEnumeration(d, sm, def);
295         createWhitespace(d, sm, def);
296         createTotalDigits(d, sm, def);
297         createFractionDigits(d, sm, def);
298         createMaxInclusive(d, sm, def);
299         createMaxExclusive(d, sm, def);
300         createMinInclusive(d, sm, def);
301         createMinExclusive(d, sm, def);
302         
303         return ref;
304     }
305     
306     public static void createMinExclusive(final Datatype d, final SchemaModel sm,
307             final SimpleTypeRestriction def) {
308         List JavaDoc<? extends Object JavaDoc> minExclusiveList = d.getMinExclusives();
309         if(minExclusiveList != null) {
310             for(Object JavaDoc minExclusive:minExclusiveList) {
311                 MinExclusive mnex = createMinExclusive(sm, def);
312                 mnex.setValue(String.valueOf(minExclusive));
313             }
314         }
315     }
316     
317     public static void createMinInclusive(final Datatype d, final SchemaModel sm,
318             final SimpleTypeRestriction def) {
319         List JavaDoc<? extends Object JavaDoc> minInclusiveList = d.getMinInclusives();
320         if(minInclusiveList != null) {
321             for(Object JavaDoc minInclusive:minInclusiveList) {
322                 MinInclusive mnic = createMinInclusive(sm, def);
323                 mnic.setValue(String.valueOf(minInclusive));
324             }
325         }
326     }
327     
328     public static void createMaxExclusive(final Datatype d, final SchemaModel sm,
329             final SimpleTypeRestriction def) {
330         List JavaDoc<? extends Object JavaDoc> maxExclusiveList = d.getMaxExclusives();
331         if(maxExclusiveList != null) {
332             for(Object JavaDoc maxExclusive:maxExclusiveList) {
333                 MaxExclusive mxex = createMaxExclusive(sm, def);
334                 mxex.setValue(String.valueOf(maxExclusive));
335             }
336         }
337     }
338     
339     public static void createMaxInclusive(final Datatype d, final SchemaModel sm,
340             final SimpleTypeRestriction def) {
341         List JavaDoc<? extends Object JavaDoc> maxInclusiveList = d.getMaxInclusives();
342         if(maxInclusiveList != null) {
343             for(Object JavaDoc maxInclusive:maxInclusiveList) {
344                 MaxInclusive mxic = createMaxInclusive(sm, def);
345                 mxic.setValue(String.valueOf(maxInclusive));
346             }
347         }
348     }
349     
350     public static void createFractionDigits(final Datatype d, final SchemaModel sm,
351             final SimpleTypeRestriction def) {
352         List JavaDoc<? extends Object JavaDoc> fractionDigitsList = d.getFractionDigits();
353         if(fractionDigitsList != null) {
354             for(Object JavaDoc fractionDigits:fractionDigitsList) {
355                 FractionDigits fdg = createFractionDigits(sm, def);
356                 fdg.setValue(Integer.parseInt(String.valueOf(fractionDigits)));
357             }
358         }
359     }
360     
361     public static void createTotalDigits(final Datatype d, final SchemaModel sm,
362             final SimpleTypeRestriction def) {
363         List JavaDoc<? extends Object JavaDoc> totalDigitsList = d.getTotalDigits();
364         if(totalDigitsList != null) {
365             for(Object JavaDoc totalDigits:totalDigitsList) {
366                 TotalDigits tdg = createTotalDigits(sm, def);
367                 tdg.setValue(Integer.parseInt(String.valueOf(totalDigits)));
368             }
369         }
370     }
371     
372     public static void createWhitespace(final Datatype d, final SchemaModel sm,
373             final SimpleTypeRestriction def) {
374         List JavaDoc<Whitespace.Treatment> wsList = d.getWhiteSpaces();
375         if(wsList != null) {
376             for(Whitespace.Treatment ws:wsList) {
377                 Whitespace w = createWhitespace(sm, def);
378                 w.setValue(ws);
379             }
380         }
381     }
382     
383     public static void createEnumeration(final Datatype d, final SchemaModel sm,
384             final SimpleTypeRestriction def) {
385         List JavaDoc<? extends Object JavaDoc> enumList = d.getEnumerations();
386         if(enumList != null) {
387             for(Object JavaDoc en:enumList) {
388                 Enumeration e = createEnumeration(sm, def);
389                 e.setValue(String.valueOf(en));
390             }
391         }
392     }
393     
394     public static void createPattern(final Datatype d, final SchemaModel sm,
395             final SimpleTypeRestriction def) {
396         List JavaDoc<? extends String JavaDoc> patternList = d.getPatterns();
397         if(patternList != null) {
398             for(String JavaDoc pattern:patternList) {
399                 Pattern p = createPattern(sm, def);
400                 p.setValue(pattern);
401             }
402         }
403     }
404     
405     public static void createMaxLength(final Datatype d, final SchemaModel sm,
406             final SimpleTypeRestriction def) {
407         List JavaDoc<? extends Number JavaDoc> maxLengthList = d.getMaxLengths();
408         if(maxLengthList != null) {
409             for(Number JavaDoc maxLength:maxLengthList) {
410                 MaxLength maxLen = createMaxLength(sm, def);
411                 maxLen.setValue(Integer.parseInt(NumberBase.toXMLString(maxLength)));
412             }
413         }
414     }
415     
416     public static void createMinLength(final Datatype d, final SchemaModel sm,
417             final SimpleTypeRestriction def) {
418         List JavaDoc<? extends Number JavaDoc> minLengthList = d.getMinLengths();
419         if(minLengthList != null) {
420             for(Number JavaDoc minLength:minLengthList) {
421                 MinLength minLen = createMinLength(sm, def);
422                 minLen.setValue(Integer.parseInt(NumberBase.toXMLString(minLength)));
423             }
424         }
425     }
426     
427     public static void createLength(final Datatype d, final SchemaModel sm,
428             final SimpleTypeRestriction def) {
429         List JavaDoc<? extends Number JavaDoc> lengthsList = d.getLengths();
430         if(lengthsList != null) {
431             for(Number JavaDoc length:lengthsList) {
432                 Length l = createLength(sm, def);
433                 l.setValue(Integer.parseInt(NumberBase.toXMLString(length)));
434             }
435         }
436     }
437     
438     public static Sequence createSequence(
439             final SchemaModel m, final ComplexContentDefinition ccd) {
440         Sequence s = m.getFactory().createSequence();
441         if(ccd instanceof ComplexContentRestriction)
442             ((ComplexContentRestriction)ccd).setDefinition(s);
443         else if(ccd instanceof ComplexExtension)
444             ((ComplexExtension)ccd).setLocalDefinition(s);
445         return s;
446     }
447     
448     public static Sequence createSequence(
449             final SchemaModel m, final ComplexTypeDefinition ctd, int index) {
450         Sequence s = m.getFactory().createSequence();
451         if(index != -1)
452             addChildComponent(m, ctd, s, index);
453         else {
454             if(ctd instanceof Choice)
455                 ((Choice)ctd).addSequence(s);
456             else if(ctd instanceof Sequence)
457                 ((Sequence)ctd).appendContent(s);
458         }
459         return s;
460     }
461     
462     public static Sequence createSequence(
463             final SchemaModel m, final ComplexType gct) {
464         Sequence s = m.getFactory().createSequence();
465         gct.setDefinition(s);
466         return s;
467     }
468     
469     public static Choice createChoice(
470             final SchemaModel m, final ComplexContentDefinition ccd) {
471         Choice c = m.getFactory().createChoice();
472         if(ccd instanceof ComplexContentRestriction)
473             ((ComplexContentRestriction)ccd).setDefinition(c);
474         else if(ccd instanceof ComplexExtension)
475             ((ComplexExtension)ccd).setLocalDefinition(c);
476         return c;
477     }
478     
479     public static Choice createChoice(
480             final SchemaModel m, final ComplexTypeDefinition ctd, int index) {
481         Choice c = m.getFactory().createChoice();
482         if(index != -1)
483             addChildComponent(m, ctd, c, index);
484         else {
485             if(ctd instanceof Choice)
486                 ((Choice)ctd).addChoice(c);
487             else if(ctd instanceof Sequence)
488                 ((Sequence)ctd).appendContent(c);
489         }
490         return c;
491     }
492     
493     public static Choice createChoice(
494             final SchemaModel m, final ComplexType gct) {
495         Choice c = m.getFactory().createChoice();
496         gct.setDefinition(c);
497         return c;
498     }
499     
500     public static All createAll(
501             final SchemaModel m, final ComplexContentDefinition ccd) {
502         All c = m.getFactory().createAll();
503         if(ccd instanceof ComplexContentRestriction)
504             ((ComplexContentRestriction)ccd).setDefinition(c);
505         else if(ccd instanceof ComplexExtension)
506             ((ComplexExtension)ccd).setLocalDefinition(c);
507         return c;
508     }
509     
510     public static All createAll(
511             final SchemaModel m, final ComplexType ct) {
512         All a = m.getFactory().createAll();
513         ct.setDefinition(a);
514         return a;
515     }
516     
517     public static GlobalElement findGlobalElement(final SchemaModel sm, final String JavaDoc eName) {
518         for(GlobalElement ge:sm.getSchema().getElements())
519             if(ge.getName().equals(eName))
520                 return ge;
521         return null;
522     }
523     
524     public static GlobalElement createGlobalElement(final SchemaModel m) {
525         GlobalElement ge = m.getFactory().createGlobalElement();
526         return ge;
527     }
528     
529     public static LocalElement createLocalElement(final SchemaModel m,
530             final ComplexTypeDefinition ctd, final String JavaDoc name, int index) {
531         LocalElement le = m.getFactory().createLocalElement();
532         le.setName(name);
533         if(index != -1)
534             addChildComponent(m, ctd, le, index);
535         else {
536             if(ctd instanceof Choice)
537                 ((Choice)ctd).addLocalElement(le);
538             else if(ctd instanceof Sequence)
539                 ((Sequence)ctd).appendContent(le);
540             else if(ctd instanceof All)
541                 ((All)ctd).addElement(le);
542         }
543         return le;
544     }
545     
546     public static ElementReference createElementReference(SchemaModel m, SchemaComponent sc,
547             GlobalElement e, int index) {
548         ElementReference ref = m.getFactory().createElementReference();
549         if(index != -1)
550             addChildComponent(m, sc, ref, index);
551         else {
552             if(sc instanceof Choice)
553                 ((Choice)sc).addElementReference(ref);
554             else if(sc instanceof Sequence)
555                 ((Sequence)sc).appendContent(ref);
556             else if(sc instanceof All)
557                 ((All)sc).addElementReference(ref);
558         }
559         ref.setRef(ref.createReferenceTo(e, GlobalElement.class));
560         return ref;
561     }
562     
563     public static LocalComplexType createLocalComplexType(final SchemaModel m,
564             final SchemaComponent e) {
565         LocalComplexType t = m.getFactory().createLocalComplexType();
566         if(e instanceof TypeContainer) {
567             if(((TypeContainer)e).getType() != null)
568                 ((TypeContainer)e).setType(null);//reset type if any
569
((TypeContainer)e).setInlineType(t);
570         }
571         return t;
572     }
573     
574     public static Annotation createAnnotation(final SchemaModel m,
575             final SchemaComponent p, final String JavaDoc s) {
576         Annotation a = m.getFactory().createAnnotation();
577         Documentation d = m.getFactory().createDocumentation();
578         a.addDocumentation(d); p.setAnnotation(a);
579         org.w3c.dom.Element JavaDoc e = d.getDocumentationElement();
580         m.getDocument().createTextNode(s);
581         d.setDocumentationElement(e);
582         return a;
583     }
584     
585     public static SimpleTypeRestriction createSimpleRestriction(final SchemaModel m,
586             final SimpleType st) {
587         SimpleTypeRestriction csr = m.getFactory().createSimpleTypeRestriction();
588         st.setDefinition(csr);
589         return csr;
590     }
591     
592     public static Union createUnion(final SchemaModel m, final SchemaComponent st) {
593         Union u = m.getFactory().createUnion();
594         ((SimpleType)st).setDefinition(u);
595         return u;
596     }
597     
598     public static LocalAttribute createLocalAttribute(final SchemaModel m,
599             final String JavaDoc name, final SchemaComponent sc, int index) {
600         LocalAttribute attr = m.getFactory().createLocalAttribute();
601         attr.setName(name);
602         if(index != -1)
603             addChildComponent(m, sc, attr, index);
604         else {
605             if(sc instanceof LocalAttributeContainer)
606                 ((LocalAttributeContainer)sc).addLocalAttribute(attr);
607         }
608         return attr;
609     }
610     
611     public static AttributeReference createAttributeReference(SchemaModel m,
612             SchemaComponent sc,
613             GlobalAttribute a, int index) {
614         AttributeReference ref = m.getFactory().createAttributeReference();
615         if(index != -1)
616             m.addChildComponent(sc, ref, index);
617         else {
618             if(sc instanceof LocalAttributeContainer)
619                 ((LocalAttributeContainer)sc).addAttributeReference(ref);
620         }
621         ref.setRef(ref.createReferenceTo(a, GlobalAttribute.class));
622         return ref;
623     }
624     
625     public static LocalAttribute getLocalAttribute(final SchemaModel m,
626             final String JavaDoc name, final SchemaComponent sc) {
627         Collection JavaDoc<LocalAttribute> attrs = null;
628         if(sc instanceof LocalAttributeContainer)
629             attrs = ((LocalAttributeContainer)sc).getLocalAttributes();
630         if(attrs != null && !attrs.isEmpty()) {
631             Iterator JavaDoc it = attrs.iterator();
632             while(it.hasNext()) {
633                 LocalAttribute attr = (LocalAttribute) it.next();
634                 if(attr.getName().equals(name))
635                     return attr;
636             }
637         }
638         return null;
639     }
640
641     public static int findSchemaComponentIndex(final SchemaComponent parent,
642             final SchemaComponent child, int axiCompIndex) {
643         List JavaDoc<SchemaComponent> allChilds = parent.getChildren();
644         List JavaDoc<SchemaComponent> similarChilds =
645                 parent.getChildren((Class JavaDoc<SchemaComponent>)child.getClass());
646         int absIndex = axiCompIndex;
647         if(axiCompIndex < 0 || similarChilds.size() == 0)
648             absIndex = allChilds.size();
649         else if(axiCompIndex == 0) {
650             if(similarChilds.size() == 0)
651                 absIndex = allChilds.size();
652             else {
653                 SchemaComponent prev = similarChilds.get(0);
654                 for(int i=0;i<allChilds.size();i++) {
655                     SchemaComponent c = allChilds.get(i);
656                     if(c == prev)
657                         absIndex = i;
658                 }
659             }
660         }
661         else if(axiCompIndex > 0) {
662             if(axiCompIndex > similarChilds.size())
663                 axiCompIndex = similarChilds.size();
664             SchemaComponent prev = similarChilds.get(axiCompIndex-1);
665             for(int i=0;i<allChilds.size();i++) {
666                 SchemaComponent c = allChilds.get(i);
667                 if(c == prev)
668                     absIndex = i+1;
669             }
670         }
671         return absIndex;
672     }
673     
674     public static void addChildComponent(
675             final SchemaModel sm, final SchemaComponent parent,
676             final SchemaComponent child, int axiCompIndex) {
677         sm.addChildComponent(parent, child,
678                 findSchemaComponentIndex(parent, child, axiCompIndex));
679     }
680     
681     public static TotalDigits createTotalDigits(final SchemaModel schemaModel,
682             final SimpleTypeRestriction def) {
683         TotalDigits tdg = schemaModel.getFactory().createTotalDigits();
684         def.addTotalDigit(tdg);
685         return tdg;
686     }
687     
688     public static FractionDigits createFractionDigits(final SchemaModel schemaModel,
689             final SimpleTypeRestriction def) {
690         FractionDigits fdg = schemaModel.getFactory().createFractionDigits();
691         def.addFractionDigits(fdg);
692         return fdg;
693     }
694     
695     public static Pattern createPattern(final SchemaModel schemaModel,
696             final SimpleTypeRestriction def) {
697         Pattern p = schemaModel.getFactory().createPattern();
698         def.addPattern(p);
699         return p;
700     }
701     
702     public static Whitespace createWhitespace(final SchemaModel schemaModel,
703             final SimpleTypeRestriction def) {
704         Whitespace w = schemaModel.getFactory().createWhitespace();
705         def.addWhitespace(w);
706         return w;
707     }
708     
709     public static Length createLength(final SchemaModel sm,
710             final SimpleTypeRestriction def) {
711         Length l = sm.getFactory().createLength();
712         def.addLength(l);
713         return l;
714     }
715     
716     public static MinLength createMinLength(final SchemaModel sm,
717             final SimpleTypeRestriction def) {
718         MinLength minLen = sm.getFactory().createMinLength();
719         def.addMinLength(minLen);
720         return minLen;
721     }
722     
723     public static MaxLength createMaxLength(final SchemaModel sm,
724             final SimpleTypeRestriction def) {
725         MaxLength maxLen = sm.getFactory().createMaxLength();
726         def.addMaxLength(maxLen);
727         return maxLen;
728     }
729     
730     public static Enumeration createEnumeration(final SchemaModel sm,
731             final SimpleTypeRestriction def) {
732         Enumeration e = sm.getFactory().createEnumeration();
733         def.addEnumeration(e);
734         return e;
735     }
736     
737     public static MaxInclusive createMaxInclusive(final SchemaModel sm,
738             final SimpleTypeRestriction def) {
739         MaxInclusive mxic = sm.getFactory().createMaxInclusive();
740         def.addMaxInclusive(mxic);
741         return mxic;
742     }
743     
744     public static MaxExclusive createMaxExclusive(final SchemaModel sm,
745             final SimpleTypeRestriction def) {
746         MaxExclusive mxex = sm.getFactory().createMaxExclusive();
747         def.addMaxExclusive(mxex);
748         return mxex;
749     }
750     
751     public static MinInclusive createMinInclusive(final SchemaModel sm,
752             final SimpleTypeRestriction def) {
753         MinInclusive mnic = sm.getFactory().createMinInclusive();
754         def.addMinInclusive(mnic);
755         return mnic;
756     }
757     
758     public static MinExclusive createMinExclusive(final SchemaModel sm,
759             final SimpleTypeRestriction def) {
760         MinExclusive mnex = sm.getFactory().createMinExclusive();
761         def.addMinExclusive(mnex);
762         return mnex;
763     }
764     
765     public static SchemaUpdate getSchemaUpdate(final AXIModel am) {
766         SchemaUpdate su = new SchemaUpdate();
767         
768         List JavaDoc<PropertyChangeEvent JavaDoc> pcEvents =
769                 ((AXIModelImpl)am).getPropertyChangeListener().getEvents();
770         for(PropertyChangeEvent JavaDoc ev:pcEvents) {
771             SchemaUpdate.UpdateUnit.Type type = null;
772             if(ev.getOldValue() != null) {
773                 if(ev.getNewValue() != null)
774                     type = SchemaUpdate.UpdateUnit.Type.CHILD_MODIFIED;
775                 else {
776                     //if axi components, then it has to be a delete
777
if(ev.getOldValue() instanceof AXIComponent)
778                         type = SchemaUpdate.UpdateUnit.Type.CHILD_DELETED;
779                     else
780                         type = SchemaUpdate.UpdateUnit.Type.CHILD_MODIFIED;
781                 }
782             } else if(ev.getNewValue() != null) {
783                 //if axi components, then it has to be a add
784
if(!(ev.getNewValue() instanceof AXIComponent) ||
785                         (ev.getSource() instanceof Element &&
786                         ev.getNewValue() instanceof ContentModel))
787                     type = SchemaUpdate.UpdateUnit.Type.CHILD_MODIFIED;
788                 else
789                     type = SchemaUpdate.UpdateUnit.Type.CHILD_ADDED;
790             }
791             assert type != null;
792             
793             SchemaUpdate.UpdateUnit unit =
794                     su.createUpdateUnit(type, (AXIComponent) ev.getSource(),
795                     ev.getOldValue(), ev.getNewValue(), ev.getPropertyName());
796             if(unit != null)
797                 su.addUpdateUnit(unit);
798         }
799         return su;
800     }
801     
802     public static void replacePeer(final Element ae, final SchemaModel sm,
803             final GlobalElement ge) {
804         GlobalElement oldPeer = (GlobalElement) ae.getPeer();
805         ae.setPeer(ge);
806         
807         if(oldPeer != null) {
808             int count = sm.getSchema().getElements().size();
809             sm.getSchema().removeElement((GlobalElement) oldPeer);
810             assert count == (sm.getSchema().getElements().size() + 1);
811         }
812     }
813     
814     public static void removeSchemaComponent(final AXIComponent source,
815             final SchemaUpdate.UpdateUnit u, final SchemaModel sm) {
816         if(source instanceof AXIDocument) {
817             if(u.getOldValue() instanceof Element) {
818                 removeGlobalElement(u, sm);//remove global element
819
} else if(u.getOldValue() instanceof ContentModel) {
820                 removeContentModel(u, sm);//remove Content Model
821
}
822         } else if(source instanceof Element) {
823             checkPopulate(source);
824             ComplexType ct = (LocalComplexType)getLocalComplexType(
825                     ((Element)source).getPeer());
826             if(ct == null)
827                 ct = (GlobalComplexType)getGlobalComplexType(
828                         ((Element)source).getPeer());
829             if(ct == null) return;
830             if(u.getOldValue() instanceof Attribute) {
831                 if(((Attribute)u.getOldValue()).getPeer() instanceof LocalAttribute)
832                     removeLocalAttribute(((Attribute)u.getOldValue()), sm, ct);
833                 else if(((Attribute)u.getOldValue()).getPeer() instanceof AttributeReference)
834                     removeAttributeRef(((Attribute)u.getOldValue()), sm, ct);
835             } else if(u.getOldValue() instanceof Compositor)
836                 removeComplexTypeDefinition(u, sm, ct);
837         } else if(source instanceof ContentModel) {
838             checkPopulate(source);
839             if(source.getPeer() instanceof GlobalComplexType) {
840                 GlobalComplexType gct =
841                         (GlobalComplexType) ((ContentModel)source).getPeer();
842                 if(u.getOldValue() instanceof Compositor)
843                     removeComplexTypeDefinition(u, sm, gct);
844                 else if(u.getOldValue() instanceof Attribute) {
845                     if(u.getOldValue() instanceof Attribute) {
846                         if(((Attribute)u.getOldValue()).getPeer() instanceof LocalAttribute)
847                             removeLocalAttribute(((Attribute)u.getOldValue()), sm, gct);
848                         else if(((Attribute)u.getOldValue()).getPeer() instanceof AttributeReference)
849                             removeAttributeRef(((Attribute)u.getOldValue()), sm, gct);
850                     }
851                 }
852             } else if(source.getPeer() instanceof GlobalAttributeGroup) {
853                 if(u.getOldValue() instanceof Attribute) {
854                     if(((Attribute)u.getOldValue()).getPeer() instanceof LocalAttribute)
855                         removeLocalAttribute(((Attribute)u.getOldValue()), sm,
856                                 (GlobalAttributeGroup)source.getPeer());
857                     else if(((Attribute)u.getOldValue()).getPeer() instanceof AttributeReference)
858                         removeAttributeRef(((Attribute)u.getOldValue()), sm,
859                                 (GlobalAttributeGroup)source.getPeer());
860                 }
861             }
862         } else if(source instanceof Compositor) {
863             ComplexTypeDefinition ctd = (ComplexTypeDefinition) ((Compositor)source).getPeer();
864             if(ctd == null) return;
865             if(u.getOldValue() instanceof Element) {
866                 if(((Element)u.getOldValue()).getPeer() instanceof LocalElement)
867                     removeLocalElement(u, sm, ctd);
868                 else if(((Element)u.getOldValue()).getPeer() instanceof ElementReference)
869                     removeElementRef(u, sm, ctd);
870             } else if(u.getOldValue() instanceof Compositor)
871                 removeComplexTypeDefinition(u, sm, ctd);
872         }
873     }
874     
875     public static void removeContentModel(final SchemaUpdate.UpdateUnit u,
876             final SchemaModel sm) {
877         ContentModel cm = (ContentModel) u.getOldValue();
878         assert cm != null;
879         if(cm.getPeer() instanceof GlobalComplexType) {
880             GlobalComplexType gct = (GlobalComplexType) cm.getPeer();
881             Schema s = sm.getSchema();
882             if(s.getComplexTypes().contains(gct))
883                 s.removeComplexType(gct);
884         }
885     }
886     
887     public static void removeGlobalElement(final SchemaUpdate.UpdateUnit u,
888             final SchemaModel sm) {
889         Element ae = (Element) u.getOldValue();
890         assert ae != null;
891         if(ae.getPeer() instanceof GlobalElement) {
892             GlobalElement ge = (GlobalElement) ae.getPeer();
893             Schema s = sm.getSchema();
894             if(s.getElements().contains(ge))
895                 s.removeElement(ge);
896         }
897     }
898     
899     public static void removeLocalElement(final SchemaUpdate.UpdateUnit u,
900             final SchemaModel sm, final ComplexTypeDefinition ctd) {
901         if(ctd instanceof Choice)
902             ((Choice)ctd).removeLocalElement(
903                     (LocalElement) ((Element)u.getOldValue()).getPeer());
904         else if(ctd instanceof Sequence)
905             ((Sequence)ctd).removeContent(
906                     (LocalElement) ((Element)u.getOldValue()).getPeer());
907         else if(ctd instanceof All)
908             ((All)ctd).removeElement(
909                     (LocalElement) ((Element)u.getOldValue()).getPeer());
910     }
911     
912     public static void removeElementRef(final SchemaUpdate.UpdateUnit u,
913             final SchemaModel sm, final ComplexTypeDefinition ctd) {
914         if(ctd instanceof Choice)
915             ((Choice)ctd).removeElementReference(
916                     (ElementReference) ((Element)u.getOldValue()).getPeer());
917         else if(ctd instanceof Sequence)
918             ((Sequence)ctd).removeContent(
919                     (ElementReference) ((Element)u.getOldValue()).getPeer());
920         else if(ctd instanceof All)
921             ((All)ctd).removeElementReference(
922                     (ElementReference) ((Element)u.getOldValue()).getPeer());
923     }
924     
925     public static void removeLocalAttribute(final Attribute attribute,
926             final SchemaModel sm, final SchemaComponent sc) {
927         if(!(attribute.getPeer() instanceof LocalAttribute)) return;
928         LocalAttribute attr = (LocalAttribute) attribute.getPeer();
929         SchemaComponent attrParent = attr.getParent();
930         if(attrParent instanceof LocalAttributeContainer)
931             ((LocalAttributeContainer)attrParent).removeLocalAttribute(attr);
932         else if(attrParent instanceof SimpleExtension)
933             ((SimpleExtension)attrParent).removeLocalAttribute(attr);
934     }
935     
936     public static void removeAttributeRef(final Attribute attribute,
937             final SchemaModel sm, final SchemaComponent sc) {
938         if(!(attribute.getPeer() instanceof AttributeReference)) return;
939         AttributeReference ref = (AttributeReference) attribute.getPeer();
940         SchemaComponent attrParent = ref.getParent();
941         if(attrParent instanceof LocalAttributeContainer)
942             ((LocalAttributeContainer)attrParent).removeAttributeReference(ref);
943         else if(attrParent instanceof SimpleExtension)
944             ((SimpleExtension)attrParent).removeAttributeReference(ref);
945     }
946     
947     public static void removeComplexTypeDefinition(final SchemaUpdate.UpdateUnit u,
948             final SchemaModel sm, final ComplexType ct) {
949         if(!(u.getOldValue() instanceof Compositor)) return;
950         Compositor c = (Compositor) u.getOldValue();
951         ComplexTypeDefinition ctd = (ComplexTypeDefinition) ((Compositor)u.getOldValue()).getPeer();
952         if(c.getType() == Compositor.CompositorType.CHOICE &&
953                 ctd instanceof Choice) {
954             ct.setDefinition(null);
955         } else if(c.getType() == Compositor.CompositorType.SEQUENCE &&
956                 ctd instanceof Sequence) {
957             ct.setDefinition(null);
958         } else if(c.getType() == Compositor.CompositorType.ALL &&
959                 ctd instanceof All) {
960             ct.setDefinition(null);
961         }
962     }
963     
964     public static void removeComplexTypeDefinition(final SchemaUpdate.UpdateUnit u,
965             final SchemaModel sm, final ComplexTypeDefinition ctd) {
966         if(!(u.getOldValue() instanceof Compositor)) return;
967         Compositor c = (Compositor) u.getOldValue();
968         if(ctd instanceof Choice) {
969             if(c.getType() == Compositor.CompositorType.CHOICE)
970                 ((Choice)ctd).removeChoice(
971                         (Choice) ((Compositor)u.getOldValue()).getPeer());
972             else if(c.getType() == Compositor.CompositorType.SEQUENCE)
973                 ((Choice)ctd).removeSequence(
974                         (Sequence) ((Compositor)u.getOldValue()).getPeer());
975         } else if(ctd instanceof Sequence) {
976             if(c.getType() == Compositor.CompositorType.CHOICE)
977                 ((Sequence)ctd).removeContent(
978                         (Choice) ((Compositor)u.getOldValue()).getPeer());
979             else if(c.getType() == Compositor.CompositorType.SEQUENCE)
980                 ((Sequence)ctd).removeContent(
981                         (Sequence) ((Compositor)u.getOldValue()).getPeer());
982         } else if(ctd instanceof All) {
983             if(u.getOldValue() instanceof LocalElement)
984                 ((All)ctd).removeElement(
985                         (LocalElement) ((Compositor)u.getOldValue()).getPeer());
986             else if(u.getOldValue() instanceof ElementReference)
987                 ((All)ctd).removeElementReference(
988                         (ElementReference) ((Compositor)u.getOldValue()).getPeer());
989         }
990     }
991     
992     public static void removeComplexContentDefinition(final SchemaUpdate.UpdateUnit u,
993             final SchemaModel sm, final ComplexContentDefinition ctd) {
994         if(!(u.getOldValue() instanceof Compositor)) return;
995         Compositor c = (Compositor) u.getOldValue();
996         if(ctd instanceof Choice) {
997             if(c.getType() == Compositor.CompositorType.CHOICE)
998                 ((Choice)ctd).removeChoice(
999                         (Choice) ((Compositor)u.getOldValue()).getPeer());
1000            else if(c.getType() == Compositor.CompositorType.SEQUENCE)
1001                ((Choice)ctd).removeSequence(
1002                        (Sequence) ((Compositor)u.getOldValue()).getPeer());
1003        } else if(ctd instanceof Sequence) {
1004            if(c.getType() == Compositor.CompositorType.CHOICE)
1005                ((Sequence)ctd).removeContent(
1006                        (Choice) ((Compositor)u.getOldValue()).getPeer());
1007            else if(c.getType() == Compositor.CompositorType.SEQUENCE)
1008                ((Sequence)ctd).removeContent(
1009                        (Sequence) ((Compositor)u.getOldValue()).getPeer());
1010        }
1011    }
1012    
1013    public static void modifySchemaComponent(final AXIComponent source,
1014            final SchemaUpdate.UpdateUnit u, final SchemaModel sm, SchemaGenerator.PrimitiveCart pc) {
1015        if(source instanceof AXIDocument) {
1016            modifyAXIDocument((AXIDocument)source, u, sm);
1017        } else if(source instanceof Element) {
1018            if(((Element)source).getPeer() instanceof LocalElement)
1019                modifyLocalElement(source, u, sm, pc);
1020            else if(((Element)source).getPeer() instanceof GlobalElement)
1021                modifyGlobalElement(((Element)source), u, sm, pc);
1022            else if(source instanceof ElementRef &&
1023                    ((Element)source).getPeer() instanceof ElementReference)
1024                modifyElementRef(((ElementRef)source), u, sm);
1025        } else if(source instanceof ContentModel) {
1026            //Schema All, Choice, Sequence
1027
modifyContentModel((ContentModel) source, u, sm);
1028        } else if(source instanceof Compositor) {
1029            //Schema All, Choice, Sequence
1030
modifyCompositor((Compositor) source, u, sm);
1031        } else if(source instanceof Attribute) {
1032            if(((Attribute)source).getPeer() instanceof LocalAttribute)
1033                modifyLocalAttribute(source, u, sm, pc);
1034            else if(((Attribute)source).getPeer() instanceof GlobalAttribute)
1035                modifyGlobalAttribute(source, u, sm, pc);
1036            else if(((Attribute)source).getPeer() instanceof AttributeReference)
1037                modifyAttributeRef(source, u, sm);
1038        } else if(source instanceof AnyElement) {
1039            if(((AnyElement)source).getPeer() instanceof
1040                    org.netbeans.modules.xml.schema.model.AnyElement)
1041                modifyAnyElement(((AnyElement)source), u, sm);
1042        }
1043    }
1044    
1045    public static void modifyAttributeProperties(
1046            final org.netbeans.modules.xml.schema.model.Attribute a,
1047            final String JavaDoc property, final Object JavaDoc newValue) {
1048        if(property.equals(Element.PROP_DEFAULT)) {
1049            a.setDefault((String JavaDoc) newValue);
1050        } else if(property.equals(Element.PROP_FIXED)) {
1051            a.setFixed((String JavaDoc) newValue);
1052        }
1053    }
1054    
1055    public static void modifyLocalAttribute(final AXIComponent source,
1056            final SchemaUpdate.UpdateUnit u, final SchemaModel sm, SchemaGenerator.PrimitiveCart pc) {
1057        LocalAttribute la = (LocalAttribute) ((Attribute)source).getPeer();
1058        String JavaDoc propertyName = u.getPropertyName();
1059        Object JavaDoc newValue = u.getNewValue();
1060        
1061        //check, if any modify fixed, default, nillable properties
1062
modifyAttributeProperties(la, propertyName, newValue);
1063        
1064        if(propertyName.equals(Attribute.PROP_NAME)) {
1065            la.setName((String JavaDoc)u.getNewValue());//change local attribute name
1066
} else if(propertyName.equals(Attribute.PROP_FORM)) {
1067            la.setForm((Form) newValue);
1068        } else if(propertyName.equals(Attribute.PROP_USE)) {
1069            la.setUse((Use) newValue);
1070        } else if(propertyName.equals(Attribute.PROP_TYPE)) {
1071            modifyDatatype(sm, la, (Datatype) u.getNewValue(), pc);
1072        }
1073    }
1074    
1075    public static void modifyAttributeRef(final AXIComponent source,
1076            final SchemaUpdate.UpdateUnit u, final SchemaModel sm) {
1077        String JavaDoc propertyName = u.getPropertyName();
1078        Object JavaDoc newValue = u.getNewValue();
1079        AttributeReference la = (AttributeReference) ((Attribute)source).getPeer();
1080        if(la.getRef().get().getName().equals(u.getOldValue())) {
1081            String JavaDoc attrName = findUniqueGlobalName(GlobalAttribute.class, (String JavaDoc)newValue, sm);
1082            la.getRef().get().setName(attrName);//change local attribute name
1083
}
1084        
1085        //check, if any modify fixed, default, nillable properties
1086
modifyAttributeProperties(la, propertyName, newValue);
1087        
1088        if(propertyName.equals(Attribute.PROP_FORM)) {
1089            la.setForm((Form) newValue);
1090        } else if(propertyName.equals(Attribute.PROP_USE)) {
1091            la.setUse((Use) newValue);
1092        } else if(propertyName.equals(AttributeRef.PROP_ATTRIBUTE_REF)) {
1093            GlobalAttribute ga = (GlobalAttribute)((Attribute)newValue).getPeer();
1094            NamedComponentReference ncr = la.getModel().getFactory().
1095                    createGlobalReference(ga, GlobalAttribute.class, la);
1096            la.setRef(ncr);
1097        }
1098    }
1099    
1100    public static void modifyGlobalAttribute(final AXIComponent source,
1101            final SchemaUpdate.UpdateUnit u, final SchemaModel sm, SchemaGenerator.PrimitiveCart pc) {
1102        GlobalAttribute ga = (GlobalAttribute) ((Attribute)source).getPeer();
1103        String JavaDoc propertyName = u.getPropertyName();
1104        Object JavaDoc newValue = u.getNewValue();
1105        
1106        //check, if any modify fixed, default, nillable properties
1107
modifyAttributeProperties(ga, propertyName, newValue);
1108        
1109        if(propertyName.equals(Attribute.PROP_NAME)) {
1110            String JavaDoc attrName = findUniqueGlobalName(GlobalAttribute.class, (String JavaDoc)newValue, sm);
1111            ga.setName(attrName);
1112        } else if(propertyName.equals(Attribute.PROP_TYPE)) {
1113            modifyDatatype(sm, ga, (Datatype) u.getNewValue(), pc);
1114        }
1115    }
1116    
1117    private static void modifyDatatype(final SchemaModel sm,
1118            final SchemaComponent component, final Datatype d, SchemaGenerator.PrimitiveCart pc) {
1119        if(d == null)
1120            return;
1121        String JavaDoc typeName = d.getName();
1122        if(d instanceof CustomDatatype)
1123            typeName = ((CustomDatatype)d).getName();
1124        if(typeName != null) {
1125            if(component instanceof GlobalAttribute &&
1126                    ((GlobalAttribute)component).getInlineType() != null) {
1127                createInlineSimpleType(d, sm, ((GlobalAttribute)component), pc);
1128                return;
1129            } else if(component instanceof LocalAttribute &&
1130                    ((LocalAttribute)component).getInlineType() != null) {
1131                createInlineSimpleType(d, sm, ((LocalAttribute)component), pc);
1132                return;
1133            } else if(component instanceof GlobalElement &&
1134                    ((GlobalElement)component).getInlineType() != null) {
1135                createInlineSimpleType(d, sm, ((GlobalElement)component), pc);
1136                return;
1137            } else if(component instanceof LocalElement &&
1138                    ((LocalElement)component).getInlineType() != null) {
1139                createInlineSimpleType(d, sm, ((LocalElement)component), pc);
1140                return;
1141            } else {
1142                NamedComponentReference<GlobalSimpleType> ref = null;
1143                for(GlobalSimpleType gst: sm.getSchema().getSimpleTypes())
1144                    if(gst.getName().equals(typeName)) {
1145                    ref = component.createReferenceTo(gst, GlobalSimpleType.class);
1146                    break;
1147                    }
1148                if(ref == null) {
1149                    SchemaGenerator.UniqueId id =
1150                            new SchemaGenerator.UniqueId() {
1151                        private int lastId = -1;
1152                        public int nextId() {
1153                            return ++lastId;
1154                        }
1155                    };
1156                    createGlobalSimpleType(d, sm, component, id, pc);
1157                } else
1158                    setSimpleType(component, ref);
1159            }
1160        }
1161    }
1162    
1163    public static void modifyElementProperties(
1164            final org.netbeans.modules.xml.schema.model.Element e,
1165            final String JavaDoc property, final Object JavaDoc newValue) {
1166        if(property.equals(Element.PROP_DEFAULT)) {
1167            e.setDefault((String JavaDoc) newValue);
1168        } else if(property.equals(Element.PROP_FIXED)) {
1169            e.setFixed((String JavaDoc) newValue);
1170        } else if(property.equals(Element.PROP_NILLABLE)) {
1171            e.setNillable((Boolean JavaDoc) newValue);
1172        }
1173    }
1174    
1175    public static void modifyCardinality(final SchemaComponent sc,
1176            final String JavaDoc property, final Object JavaDoc newValue) {
1177        if(property.equals(Element.PROP_MINOCCURS)) {
1178            if(sc instanceof All) {
1179                if(newValue == null)//restore default
1180
((All)sc).setMinOccurs(Occur.ZeroOne.ONE);
1181                else if(newValue instanceof Occur.ZeroOne)
1182                    ((All)sc).setMinOccurs((Occur.ZeroOne) newValue);
1183            } else {
1184                try {
1185                    int minOccurs = 1;//default value
1186
if(newValue != null)
1187                        minOccurs = Integer.parseInt((String JavaDoc) newValue);
1188                    if(sc instanceof LocalElement)
1189                        ((LocalElement)sc).setMinOccurs(minOccurs);
1190                    else if(sc instanceof ElementReference)
1191                        ((ElementReference)sc).setMinOccurs(minOccurs);
1192                    else if(sc instanceof Sequence)
1193                        ((Sequence)sc).getCardinality().setMinOccurs(minOccurs);
1194                    else if(sc instanceof Choice)
1195                        ((Choice)sc).getCardinality().setMinOccurs(minOccurs);
1196                    else if(sc instanceof
1197                            org.netbeans.modules.xml.schema.model.AnyElement)
1198                        ((org.netbeans.modules.xml.schema.model.AnyElement)sc).
1199                                setMinOccurs(minOccurs);
1200                } catch(Throwable JavaDoc th) {
1201                }
1202            }
1203        } else if(property.equals(Element.PROP_MAXOCCURS)) {
1204            if(sc instanceof LocalElement)
1205                ((LocalElement)sc).setMaxOccurs((String JavaDoc) newValue);
1206            else if(sc instanceof ElementReference)
1207                ((ElementReference)sc).setMaxOccurs((String JavaDoc) newValue);
1208            else if(sc instanceof Sequence)
1209                ((Sequence)sc).getCardinality().setMaxOccurs((String JavaDoc) newValue);
1210            else if(sc instanceof Choice)
1211                ((Choice)sc).getCardinality().setMaxOccurs((String JavaDoc) newValue);
1212            else if(sc instanceof org.netbeans.modules.xml.schema.model.AnyElement)
1213                ((org.netbeans.modules.xml.schema.model.AnyElement)sc).
1214                        setMaxOccurs((String JavaDoc) newValue);
1215        }
1216    }
1217    
1218    public static void modifyLocalElement(final AXIComponent source,
1219            final SchemaUpdate.UpdateUnit u, final SchemaModel sm, SchemaGenerator.PrimitiveCart pc) {
1220        checkPopulate(source);
1221        LocalElement le = (LocalElement) ((Element)source).getPeer();
1222        String JavaDoc propertyName = u.getPropertyName();
1223        Object JavaDoc newValue = u.getNewValue();
1224        
1225        //check, if any modify fixed, default, nillable properties
1226
modifyElementProperties(le, propertyName, newValue);
1227        
1228        //check, if any modify minoccurs and maxoccurs
1229
modifyCardinality(le, propertyName, newValue);
1230        
1231        if(propertyName.equals(Element.PROP_NAME)) {
1232            le.setName((String JavaDoc)newValue);//change local element name
1233
} else if(propertyName.equals(Element.PROP_FORM)) {
1234            le.setForm((Form) newValue);
1235        } else if(propertyName.equals(Element.PROP_TYPE)) {
1236            if(newValue instanceof ContentModel) {
1237                //remove any inline type
1238
LocalComplexType lct = (LocalComplexType) le.getInlineType();
1239                if(lct != null)
1240                    le.setInlineType(null);
1241                
1242                //now set type
1243
le.setType(le.createReferenceTo(
1244                        (GlobalComplexType)((ContentModel)newValue).getPeer(),
1245                        GlobalComplexType.class));
1246            } else if(newValue instanceof Datatype)
1247                modifyDatatype(sm, le, (Datatype) newValue, pc);
1248        }
1249    }
1250    
1251    public static void modifyElementRef(final ElementRef element,
1252            final SchemaUpdate.UpdateUnit u, final SchemaModel sm) {
1253        checkPopulate(element);
1254        ElementReference eref = (ElementReference) element.getPeer();
1255        assert eref != null;
1256        String JavaDoc propertyName = u.getPropertyName();
1257        Object JavaDoc newValue = u.getNewValue();
1258        Object JavaDoc oldValue = u.getOldValue();
1259        
1260        //check, if any modify minoccurs and maxoccurs
1261
modifyCardinality(eref, propertyName, newValue);
1262        
1263        if(propertyName.equals(ElementRef.PROP_ELEMENT_REF)) {
1264            GlobalElement ge = (GlobalElement)((Element)newValue).getPeer();
1265            NamedComponentReference ncr = eref.getModel().getFactory().
1266                    createGlobalReference(ge, GlobalElement.class, eref);
1267            eref.setRef(ncr);
1268        }
1269    }
1270    
1271    public static void modifyGlobalElement(final Element element,
1272            final SchemaUpdate.UpdateUnit u, final SchemaModel sm, SchemaGenerator.PrimitiveCart pc) {
1273        checkPopulate(element);
1274        if(u.getPropertyName().equals(Element.PROP_MINOCCURS) ||
1275                u.getPropertyName().equals(Element.PROP_MAXOCCURS)) {
1276            return;//cannot set min or max occurs for global
1277
}
1278        GlobalElement ge = (GlobalElement) element.getPeer();
1279        assert ge != null;
1280        String JavaDoc propertyName = u.getPropertyName();
1281        Object JavaDoc newValue = u.getNewValue();
1282        
1283        //check, if any modify fixed, default, nillable properties
1284
modifyElementProperties(ge, propertyName, newValue);
1285        
1286        if(propertyName.equals(Element.PROP_NAME)) {
1287            String JavaDoc eName = findUniqueGlobalName(GlobalElement.class, (String JavaDoc)newValue, sm);
1288            ge.setName(eName);
1289            refactorRenameElement(element, (String JavaDoc)newValue, ge);
1290        } else if(propertyName.equals(Element.PROP_TYPE)) {
1291            if(newValue instanceof ContentModel) {
1292                //remove any inline type
1293
LocalComplexType lct = (LocalComplexType) ge.getInlineType();
1294                if(lct != null)
1295                    ge.setInlineType(null);
1296                
1297                //now set type
1298
ge.setType(ge.createReferenceTo(
1299                        (GlobalComplexType)((ContentModel)newValue).getPeer(),
1300                        GlobalComplexType.class));
1301            } else if(newValue instanceof Datatype)
1302                modifyDatatype(sm, ge, (Datatype) newValue, pc);
1303        }
1304    }
1305    
1306    public static void refactorRenameElement(final Element element,
1307            final String JavaDoc newValue, GlobalElement ge) {
1308        //Now expand the AXI tree deep for some global elements from the list
1309
AXINonCyclicVisitor visitor = new AXINonCyclicVisitor(element.getModel());
1310        visitor.expand(element.getModel().getRoot());
1311        
1312        if(element.getRefSet() == null)
1313            return;
1314        assert ge != null;
1315        for(AXIComponent ref : element.getRefSet()) {
1316            if(ref instanceof Element) {
1317                if(ref instanceof ElementRef) {
1318                    ElementReference eref = (ElementReference) ref.getPeer();
1319                    assert eref != null;
1320                    eref.setRef(eref.createReferenceTo(ge, GlobalElement.class));
1321                } else
1322                    ((Element)ref).setName((String JavaDoc)newValue);
1323            }
1324        }
1325    }
1326    
1327    public static void refactorRenameType(final ContentModel cm,
1328            final String JavaDoc newValue, GlobalComplexType gct) {
1329        //Now expand the AXI tree deep for some global elements from the list
1330
AXINonCyclicVisitor visitor = new AXINonCyclicVisitor(cm.getModel());
1331        visitor.expand(cm.getModel().getRoot());
1332        
1333        if(cm.getRefSet() == null)
1334            return;
1335        assert gct != null;
1336        for(AXIComponent ref : cm.getRefSet()) {
1337            if(ref instanceof Element) {
1338                org.netbeans.modules.xml.schema.model.Element eref =
1339                        (org.netbeans.modules.xml.schema.model.Element) ref.getPeer();
1340                assert eref != null;
1341                setType(eref, gct);
1342            }
1343        }
1344    }
1345    
1346    public static <T extends NameableSchemaComponent>String JavaDoc
1347            findUniqueGlobalName(Class JavaDoc<T> type, final String JavaDoc seed,
1348            final SchemaModel sm) {
1349        int count = 0;
1350        boolean found = true;
1351        while(found) {
1352            found = false;
1353            for(T sc:sm.getSchema().getChildren(type)) {
1354                if(sc.getName().equals(count>0?(seed + String.valueOf(count)):seed)) {
1355                    count++;
1356                    found = true;
1357                }
1358            }
1359        }
1360        return count>0?(seed + String.valueOf(count)):seed;
1361    }
1362    
1363    public static void modifyAnyElement(final AnyElement element,
1364            final SchemaUpdate.UpdateUnit u, final SchemaModel sm) {
1365        checkPopulate(element);
1366        org.netbeans.modules.xml.schema.model.AnyElement ae =
1367                (org.netbeans.modules.xml.schema.model.AnyElement) element.getPeer();
1368        String JavaDoc propertyName = u.getPropertyName();
1369        Object JavaDoc newValue = u.getNewValue();
1370        
1371        //check, if any modify minoccurs and maxoccurs
1372
modifyCardinality(ae, propertyName, newValue);
1373        
1374        if(propertyName.equals(AnyElement.PROP_PROCESSCONTENTS)) {
1375            ae.setProcessContents((org.netbeans.modules.xml.schema.model.AnyElement.ProcessContents)newValue);
1376        }
1377    }
1378    
1379    public static void modifyAXIDocument(final AXIDocument document,
1380            final SchemaUpdate.UpdateUnit u, final SchemaModel sm) {
1381        Schema schema = (Schema)document.getPeer();
1382        if(u.getPropertyName().equals(AXIDocument.PROP_TARGET_NAMESPACE)) {
1383            schema.setTargetNamespace((String JavaDoc)u.getNewValue());//change name
1384
} else if(u.getPropertyName().equals(AXIDocument.PROP_ATTRIBUTE_FORM_DEFAULT)) {
1385            schema.setAttributeFormDefault((Form)u.getNewValue());//change attr form default
1386
} else if(u.getPropertyName().equals(AXIDocument.PROP_ELEMENT_FORM_DEFAULT)) {
1387            schema.setElementFormDefault((Form)u.getNewValue());//change element form default
1388
} else if(u.getPropertyName().equals(AXIDocument.PROP_VERSION)) {
1389            schema.setVersion((String JavaDoc)u.getNewValue());//change version
1390
}
1391    }
1392    
1393    public static void modifyContentModel(final ContentModel conentModel,
1394            final SchemaUpdate.UpdateUnit u, final SchemaModel sm) {
1395        checkPopulate(conentModel);
1396        GlobalComplexType gct = (GlobalComplexType) ((ContentModel)conentModel).getPeer();
1397        if(u.getPropertyName().equals(Element.PROP_NAME)) {
1398            String JavaDoc typeName = findUniqueGlobalName(GlobalComplexType.class,
1399                    (String JavaDoc)u.getNewValue(), sm);
1400            gct.setName(typeName);//change name
1401
refactorRenameType(conentModel, typeName, gct);
1402        }
1403    }
1404    
1405    public static void modifyCompositor(final Compositor compositor,
1406            final SchemaUpdate.UpdateUnit u, final SchemaModel sm) {
1407        checkPopulate(compositor);
1408        ComplexTypeDefinition oldc = (ComplexTypeDefinition) ((Compositor)compositor).getPeer();
1409        String JavaDoc propertyName = u.getPropertyName();
1410        Object JavaDoc newValue = u.getNewValue();
1411        
1412        //check, if any modify minoccurs and maxoccurs
1413
modifyCardinality(oldc, propertyName, newValue);
1414        
1415        //removing old schema compositor and add new one
1416
if(propertyName.equals(Compositor.PROP_TYPE)) {
1417            int index = compositor.getIndex();
1418            if(index == -1) index = 0;
1419            SchemaComponent ctd = compositor.getPeer();
1420            SchemaComponent ctdParent = ctd.getParent();
1421            SchemaComponent ctdCopy = (SchemaComponent) ctd.copy(ctd.getParent());
1422            ComplexTypeDefinition newctd = null;
1423            if(newValue.equals(CompositorType.SEQUENCE))
1424                newctd = sm.getFactory().createSequence();
1425            else if(newValue.equals(CompositorType.CHOICE))
1426                newctd = sm.getFactory().createChoice();
1427            else if(newValue.equals(CompositorType.ALL))
1428                newctd = sm.getFactory().createAll();
1429            //populate min, max occurs, annotation etc., from old compositor
1430
SchemaGeneratorUtil.populateCompositor(newctd, compositor);
1431            int count = 0;
1432            for(SchemaComponent sc: ctdCopy.getChildren()) {
1433                sm.addChildComponent(newctd, sc, count++);
1434            }
1435            sm.removeChildComponent(ctd);
1436            sm.addChildComponent(ctdParent, newctd, index);
1437        }
1438    }
1439    
1440    public static GlobalAttributeGroup createGlobalAttributeGroup(final SchemaModel m) {
1441        GlobalAttributeGroup t = m.getFactory().createGlobalAttributeGroup();
1442// m.getSchema().addAttributeGroup(t);
1443
return t;
1444    }
1445    
1446    public static GlobalAttributeGroup createGlobalAttributeGroup(final SchemaModel sm,
1447            final String JavaDoc name) {
1448        GlobalAttributeGroup gag = createGlobalAttributeGroup(sm);
1449        if(gag != null) {
1450            String JavaDoc agName = findUniqueGlobalName(GlobalAttributeGroup.class, name, sm);
1451            gag.setName(agName);//NoI18n
1452
}
1453        return gag;
1454    }
1455
1456    public static GlobalComplexType findTypeFromOtherModel(
1457            org.netbeans.modules.xml.schema.model.Element e,
1458            Element element, SchemaModel sm) {
1459        //check type from another schema model
1460
GlobalComplexType gct = null;
1461        org.netbeans.modules.xml.schema.model.Element oldE =
1462                (org.netbeans.modules.xml.schema.model.Element) element.getPeer();
1463        NamedComponentReference ref = null;
1464        if(oldE instanceof GlobalElement)
1465            ref = ((GlobalElement)oldE).getType();
1466        else if(oldE instanceof LocalElement)
1467            ref = ((LocalElement)oldE).getType();
1468        else if(oldE instanceof ElementReference)
1469            ref = ((ElementReference)oldE).getRef();
1470        if(ref != null && ref.get() instanceof GlobalComplexType &&
1471                !fromSameSchemaModel((GlobalComplexType)ref.get(), sm)) {
1472            gct = (GlobalComplexType) ref.get();
1473            if(e instanceof LocalElement) {//overwrite type if present
1474
if(((LocalElement)e).getInlineType() != null)
1475                    ((LocalElement)e).setInlineType(null);
1476                ((LocalElement)e).setType(e.createReferenceTo(gct,
1477                        GlobalComplexType.class));
1478            } else if(e instanceof GlobalElement) {//overwrite type if present
1479
if(((GlobalElement)e).getInlineType() != null)
1480                    ((GlobalElement)e).setInlineType(null);
1481                ((GlobalElement)e).setType(e.createReferenceTo(gct,
1482                        GlobalComplexType.class));
1483            }
1484        }
1485        return gct;
1486    }
1487    
1488    public static GlobalType getGlobalComplexType(final SchemaComponent sc) {
1489        GlobalComplexType gct = null;
1490        if(sc instanceof TypeContainer && ((TypeContainer)sc).getType() != null &&
1491                ((TypeContainer)sc).getType().get() instanceof GlobalComplexType)
1492            gct = (GlobalComplexType) ((TypeContainer)sc).getType().get();
1493        else if(sc instanceof GlobalComplexType)
1494            return (GlobalType) sc;
1495        return gct;
1496    }
1497    
1498    public static GlobalComplexType createGlobalComplexType(final SchemaModel m) {
1499        GlobalComplexType t = m.getFactory().createGlobalComplexType();
1500        return t;
1501    }
1502    
1503    public static void setType(
1504            final org.netbeans.modules.xml.schema.model.Element e,
1505            final GlobalComplexType gct) {
1506        if(e instanceof TypeContainer) {//overwrite type if present
1507
if(((TypeContainer)e).getInlineType() != null)
1508                ((TypeContainer)e).setInlineType(null);
1509            ((TypeContainer)e).setType(e.createReferenceTo(gct, GlobalComplexType.class));
1510        }
1511    }
1512    
1513    public static LocalType getLocalComplexType(final SchemaComponent sc) {
1514        LocalType lct = null;
1515        if(sc instanceof TypeContainer)
1516            lct = ((TypeContainer)sc).getInlineType();
1517        else if(sc instanceof LocalComplexType)
1518            lct = (LocalType) sc;
1519        return lct;
1520    }
1521    
1522    public static NamedComponentReference<? extends GlobalType> getType(
1523            final SchemaComponent sc) {
1524        if(sc instanceof TypeContainer)
1525            return ((TypeContainer)sc).getType();
1526        return null;
1527    }
1528    
1529    public static Element findGlobalElement(final AXIComponent c) {
1530        AXIComponent parent = c.getParent();
1531        if(parent == null || parent instanceof AXIDocument) {
1532            //true only for an AXIDocument.
1533
return (Element)c;
1534        }
1535        
1536        return findGlobalElement(c.getParent());
1537    }
1538    
1539    public static SchemaGenerator.Pattern inferDesignPattern(AXIModel am) {
1540        SchemaGenerator.Pattern dp = null;
1541// java.util.List<Element> lrges = findMasterGlobalElements(am);
1542
SchemaModel sm = am.getSchemaModel();
1543        int geCount = sm.getSchema().getElements().size();
1544        int ctCount = sm.getSchema().getComplexTypes().size();
1545        if(ctCount > 0)
1546            if(geCount > 1)
1547                dp = SchemaGenerator.Pattern.GARDEN_OF_EDEN;
1548            else {
1549            GlobalElement ge = null;
1550            Iterator JavaDoc it = sm.getSchema().getElements().iterator();
1551            if(it.hasNext())
1552                ge = (GlobalElement) it.next();
1553            if(ge != null && ge.getType() != null &&
1554                    ge.getType().get() instanceof GlobalComplexType)
1555                dp = SchemaGenerator.Pattern.GARDEN_OF_EDEN;
1556            else
1557                dp = SchemaGenerator.Pattern.VENITIAN_BLIND;
1558            } else
1559// if(lrges.size() > 1 || geCount > 1)
1560
if(geCount > 1)
1561                    dp = SchemaGenerator.Pattern.SALAMI_SLICE;
1562                else if(geCount == 1)
1563                    dp = SchemaGenerator.Pattern.RUSSIAN_DOLL;
1564        return dp;
1565    }
1566    
1567    public static java.util.List JavaDoc<Element> findMasterGlobalElements(final AXIModel am) {
1568        //least referenced global elements
1569
java.util.List JavaDoc<Element> lrges = new ArrayList JavaDoc<Element>();
1570        
1571        Preview p = new FindUsageVisitor(am).findUsages(am.getRoot());
1572        if(p == null) return lrges;
1573        //get the reverse usage map (ie., B usedBy A) for all global elements
1574
Map JavaDoc<AXIComponent, java.util.List JavaDoc<AXIComponent>> revmap = p.getReverseUsages();
1575        
1576        List JavaDoc<GlobalElement> refges = new ArrayList JavaDoc<GlobalElement>();
1577        List JavaDoc<Element> ges = am.getRoot().getElements();
1578        for(Element e : ges) {
1579            java.util.List JavaDoc<AXIComponent> useList = revmap.get(e);
1580            if(useList.size() > 1) {
1581                for(int i=1;i<useList.size();i++) {
1582                    AXIComponent c = useList.get(i);
1583                    if(c instanceof Element) {
1584                        SchemaComponent peer = c.getPeer();
1585                        if(peer instanceof ElementReference) {
1586                            peer = ((ElementReference)peer).getRef().get();
1587                            for(Element e1 : ges) {
1588                                if(e1.getPeer() == peer &&
1589                                        peer != e.getPeer()) {
1590                                    refges.add((GlobalElement)peer);
1591                                    break;
1592                                }
1593                            }
1594                        }
1595                    }
1596                }
1597                lrges.add(e);
1598            }
1599        }
1600        //add
1601
for(Element e : am.getRoot().getElements()) {
1602            if(!refges.contains(e.getPeer()) && !lrges.contains(e))
1603                lrges.add(e);
1604        }
1605        //find remove global elements
1606
List JavaDoc<Integer JavaDoc> removeList = new ArrayList JavaDoc<Integer JavaDoc>();
1607        for(int i=0;i<lrges.size();i++) {
1608            Element e = lrges.get(i);
1609            if(refges.contains(e.getPeer())) {
1610                removeList.add(new Integer JavaDoc(i));
1611            } else if(!SchemaGeneratorUtil.fromSameSchemaModel(
1612                    e.getPeer(), am.getSchemaModel()))
1613                removeList.add(new Integer JavaDoc(i));
1614        }
1615        //finally remove unnecessary global elements from visit list
1616
for(int i=removeList.size()-1;i>=0;i--) {
1617            lrges.remove(removeList.get(i).intValue());
1618        }
1619        return lrges;
1620    }
1621    
1622    public static Element findOriginalElement(Element e) {
1623        if(e instanceof ElementRef)
1624            return ((ElementRef)e).getReferent();
1625        else if(e instanceof ElementProxy) {
1626            return findOriginalElement((Element) (((ElementProxy)e).getOriginal()));
1627        } else
1628            return e;
1629    }
1630    
1631    public static void checkPopulate(final AXIComponent source)
1632    throws IllegalArgumentException JavaDoc {
1633        if(source instanceof Attribute) return;
1634        if((source).getPeer() == null) {
1635            String JavaDoc name = (source instanceof Element)?((Element)source).getName():
1636                ((source instanceof ContentModel)?((ContentModel)source).getName():source.toString());
1637            throw new IllegalArgumentException JavaDoc("Component "+name+
1638                    " needs to be added to its parent, before its children can be populated");
1639        }
1640    }
1641    
1642    public static void populateElement(
1643            org.netbeans.modules.xml.schema.model.Element e, Element element) {
1644        if(e instanceof GlobalElement) {
1645            if(element.getAbstract())
1646                ((GlobalElement)e).setAbstract(Boolean.valueOf(element.getAbstract()));
1647            if(element.getPeer() instanceof GlobalElement &&
1648                    ((GlobalElement)element.getPeer()).getFinalEffective() != null &&
1649                    !((GlobalElement)element.getPeer()).getFinalEffective().isEmpty())
1650                ((GlobalElement)e).setFinal(((GlobalElement)element.getPeer()).getFinalEffective());
1651        }
1652        if(e instanceof LocalElement) {
1653            if(element.getForm() != null &&
1654                    element.getModel().getRoot().getElementFormDefault() !=
1655                    element.getForm())
1656                ((LocalElement)e).setForm(element.getForm());
1657        }
1658        if(e instanceof LocalElement ||
1659                e instanceof ElementReference) {
1660            if(element.getMinOccurs() != null &&
1661                    Integer.parseInt(element.getMinOccurs()) != 1) {
1662                if(e instanceof LocalElement)
1663                    ((LocalElement)e).setMinOccurs(
1664                            Integer.parseInt(element.getMinOccurs()));
1665                else if(e instanceof ElementReference)
1666                    ((ElementReference)e).setMinOccurs(
1667                            Integer.parseInt(element.getMinOccurs()));
1668            }
1669            if(element.getMaxOccurs() != null) {
1670                String JavaDoc maxValue = element.getMaxOccurs();
1671                if(maxValue.equals(NumberBase.UNBOUNDED_STRING) ||
1672                        Integer.parseInt(element.getMaxOccurs()) != 1) {
1673                    if(e instanceof LocalElement)
1674                        ((LocalElement)e).setMaxOccurs(element.getMaxOccurs());
1675                    else if(e instanceof ElementReference)
1676                        ((ElementReference)e).setMaxOccurs(element.getMaxOccurs());
1677                }
1678            }
1679        }
1680        if(e instanceof LocalElement ||
1681                e instanceof GlobalElement) {
1682            if(element.getFixed() != null)
1683                e.setFixed(element.getFixed());
1684            if(element.getDefault() != null)
1685                e.setDefault(element.getDefault());
1686            if(element.getNillable())
1687                e.setNillable(element.getNillable());
1688            if(element.getPeer() instanceof GlobalElement)
1689                if(e instanceof GlobalElement &&
1690                    ((GlobalElement)element.getPeer()).getBlockEffective() != null &&
1691                    !((GlobalElement)element.getPeer()).getBlockEffective().isEmpty())
1692                    ((GlobalElement)e).setBlock(((GlobalElement)element.getPeer()).getBlockEffective());
1693                else if(e instanceof LocalElement &&
1694                    ((GlobalElement)element.getPeer()).getBlockEffective() != null &&
1695                    !((GlobalElement)element.getPeer()).getBlockEffective().isEmpty())
1696                    ((LocalElement)e).setBlock(((GlobalElement)element.getPeer()).getBlockEffective());
1697                else if(element.getPeer() instanceof LocalElement)
1698                    if(e instanceof GlobalElement &&
1699                    ((LocalElement)element.getPeer()).getBlockEffective() != null &&
1700                    !((LocalElement)element.getPeer()).getBlockEffective().isEmpty())
1701                        ((GlobalElement)e).setBlock(((LocalElement)element.getPeer()).getBlockEffective());
1702                    else if(e instanceof LocalElement &&
1703                    ((LocalElement)element.getPeer()).getBlockEffective() != null &&
1704                    !((LocalElement)element.getPeer()).getBlockEffective().isEmpty())
1705                        ((LocalElement)e).setBlock(((LocalElement)element.getPeer()).getBlockEffective());
1706        }
1707        org.netbeans.modules.xml.schema.model.Element old =
1708                (org.netbeans.modules.xml.schema.model.Element) element.getPeer();
1709        if(old != null) {
1710            if(old.getAnnotation() != null) {
1711                Annotation a = copyAnnotation(old.getAnnotation());
1712                e.setAnnotation(a);
1713            }
1714            Collection JavaDoc<Constraint> constraints = old.getConstraints();//key, keyref, unique
1715
if(constraints != null)
1716                for(Constraint c:constraints) {
1717                    Constraint copy = copyConstraint((Constraint)c);
1718                    e.addConstraint(copy);
1719                }
1720        }
1721    }
1722    
1723    public static Annotation copyAnnotation(final Annotation old) {
1724        //workaround for pretty print
1725
List JavaDoc<SchemaComponent> childs = old.getChildren();
1726        List JavaDoc<SchemaComponent> copies = new ArrayList JavaDoc<SchemaComponent>();
1727        Annotation a = old.getModel().getFactory().createAnnotation();
1728        for(SchemaComponent child:childs) {
1729            copies.add((SchemaComponent)child.copy(a));
1730        }
1731        for(SchemaComponent child:copies) {
1732            if(child instanceof AppInfo)
1733                a.addAppInfo((AppInfo) child);
1734            else if(child instanceof Documentation)
1735                a.addDocumentation((Documentation) child);
1736        }
1737        return a;
1738    }
1739    
1740    public static Constraint copyConstraint(final Constraint old) {
1741        //workaround for pretty print
1742
if(old == null) return null;
1743        List JavaDoc<SchemaComponent> childs = old.getChildren();
1744        List JavaDoc<SchemaComponent> copies = new ArrayList JavaDoc<SchemaComponent>();
1745        Constraint c = null;
1746        if(old instanceof Unique)
1747            c = old.getModel().getFactory().createUnique();
1748        else if(old instanceof Key)
1749            c = old.getModel().getFactory().createKey();
1750        else if(old instanceof KeyRef)
1751            c = old.getModel().getFactory().createKeyRef();
1752        assert c != null;
1753        for(SchemaComponent child:childs) {
1754            copies.add((SchemaComponent)child.copy(c));
1755        }
1756        for(SchemaComponent child:copies) {
1757            if(child instanceof Selector)
1758                c.setSelector((Selector)child);
1759            else if(child instanceof Field)
1760                c.addField((Field) child);
1761        }
1762        return c;
1763    }
1764    
1765    public static void populateAttribute(
1766            org.netbeans.modules.xml.schema.model.Attribute attr, Attribute attribute) {
1767        if(attr instanceof LocalAttribute) {
1768            if(attribute.getForm() != null &&
1769                    attribute.getModel().getRoot().getAttributeFormDefault() !=
1770                    attribute.getForm())
1771                ((LocalAttribute)attr).setForm(attribute.getForm());
1772            if(attribute.getUse() != null &&
1773                    attribute.getUse() !=
1774                    org.netbeans.modules.xml.schema.model.Attribute.Use.OPTIONAL)
1775                ((LocalAttribute)attr).setUse(attribute.getUse());
1776        }
1777        if(attribute.getFixed() != null)
1778            attr.setFixed(attribute.getFixed());
1779        if(attribute.getDefault() != null)
1780            attr.setDefault(attribute.getDefault());
1781        org.netbeans.modules.xml.schema.model.Attribute old =
1782                (org.netbeans.modules.xml.schema.model.Attribute) attribute.getPeer();
1783        if(old != null && old.getAnnotation() != null) {
1784            Annotation a = copyAnnotation(old.getAnnotation());
1785            attr.setAnnotation(a);
1786        }
1787    }
1788    
1789    public static void populateCompositor(ComplexTypeDefinition ctd,
1790            Compositor compositor) {
1791        //set minoccurs
1792
if(compositor.getMinOccurs() != null &&
1793                Integer.parseInt(compositor.getMinOccurs()) != 1) {
1794            if(ctd instanceof Sequence) {
1795                Sequence seq = (Sequence) ctd;
1796                if(seq.getCardinality() != null)
1797                    seq.getCardinality().setMinOccurs(
1798                            Integer.valueOf(compositor.getMinOccurs()));
1799            } else if(ctd instanceof Choice) {
1800                Choice c = (Choice) ctd;
1801                if(c.getCardinality() != null)
1802                    c.getCardinality().setMinOccurs(Integer.valueOf(compositor.getMinOccurs()));
1803            } else if(ctd instanceof All) {
1804                All a = (All) ctd;
1805                a.setMinOccurs(Occur.ZeroOne.valueOfNumeric(
1806                        a.toString(), compositor.getMinOccurs()));
1807            }
1808        }
1809        
1810        //set maxoccurs
1811
if(compositor.getMaxOccurs() != null &&
1812                (compositor.getMaxOccurs().equals(NumberBase.UNBOUNDED_STRING) ||
1813                Integer.parseInt(compositor.getMaxOccurs()) != 1)) {
1814            if(ctd instanceof Sequence) {
1815                Sequence seq = (Sequence) ctd;
1816                if(seq.getCardinality() != null)
1817                    seq.getCardinality().setMaxOccurs(compositor.getMaxOccurs());
1818            } else if(ctd instanceof Choice) {
1819                Choice c = (Choice) ctd;
1820                if(c.getCardinality() != null)
1821                    c.getCardinality().setMaxOccurs(compositor.getMaxOccurs());
1822            }
1823        }
1824        ComplexTypeDefinition old = (ComplexTypeDefinition) compositor.getPeer();
1825        if(old != null && old.getAnnotation() != null) {
1826            Annotation a = copyAnnotation(old.getAnnotation());
1827            ctd.setAnnotation(a);
1828        }
1829    }
1830    
1831    public static void populateContentModel(SchemaComponent sc, ContentModel cm) {
1832        SchemaComponent old = cm.getPeer();
1833        if(old != null && old.getAnnotation() != null) {
1834            Annotation a = copyAnnotation(old.getAnnotation());
1835            sc.setAnnotation(a);
1836        }
1837    }
1838    
1839    public static boolean isSimpleElement(Element element) {
1840        return element.getType() instanceof Datatype ||
1841                (element.getType() == null && element.getChildren().size() == 0);
1842    }
1843    
1844    public static boolean isSimpleElementStructure(Element e) {
1845        if(e.getAttributes().size() > 0 ||
1846                e.getChildren(Compositor.class).size() > 1 ||
1847                (e.getCompositor() != null &&
1848                e.getCompositor().getChildren().size() > 1))
1849            return false;
1850        else
1851            return true;
1852    }
1853    
1854    public static boolean isGlobalElement(AXIComponent axiparent) {
1855        return axiparent instanceof Element &&
1856                axiparent.getPeer() instanceof GlobalElement;
1857    }
1858    
1859    public static boolean isIdentical(SchemaComponent sc1, SchemaComponent sc2) {
1860        return compareElement(sc1.getPeer(), sc2.getPeer(), true) &&
1861                sc1.getChildren().size() == 0 &&
1862                (sc1.getChildren().size() == sc2.getChildren().size());
1863    }
1864    
1865    public static boolean hasProxyChild(Element element) {
1866        return element.getType() instanceof ContentModel;
1867    }
1868    
1869    public static boolean compareElement(org.w3c.dom.Element JavaDoc n1,
1870            org.w3c.dom.Element JavaDoc n2, boolean identical) {
1871        String JavaDoc qName1 = n1.getLocalName();
1872        String JavaDoc qName2 = n2.getLocalName();
1873        String JavaDoc ns1 = ((Node JavaDoc)n1).getNamespaceURI();
1874        String JavaDoc ns2 = ((Node JavaDoc)n2).getNamespaceURI();
1875        
1876        if (qName1.intern() != qName2.intern())
1877            return false;
1878        if (!(ns1 == null && ns2 == null) &&
1879                !(ns1 != null && ns2 != null && ns1.intern() == ns2.intern()))
1880            return false;
1881        
1882        return compareAttr(n1, n2, identical);
1883    }
1884    
1885    public static boolean compareAttr(org.w3c.dom.Element JavaDoc n1,
1886            org.w3c.dom.Element JavaDoc n2, boolean identical) {
1887        NamedNodeMap JavaDoc attrs1 = n1.getAttributes();
1888        NamedNodeMap JavaDoc attrs2 = n2.getAttributes();
1889        
1890        List JavaDoc<String JavaDoc> nameSet = new ArrayList JavaDoc<String JavaDoc>();
1891        nameSet.add( "id" );
1892        nameSet.add( "name" );
1893        nameSet.add( "ref" );
1894        
1895        if(nameSet.isEmpty())
1896            return true;
1897        else if(attrs1.getLength() == 0 && attrs2.getLength() == 0)
1898            return true;
1899        else if(identical && attrs1.getLength() != attrs2.getLength())
1900            return false;
1901        
1902        int matchCount = 0;
1903        int unmatchCount = 0;
1904        for(String JavaDoc name:nameSet) {
1905            Node JavaDoc attr1 = (Node JavaDoc) attrs1.getNamedItem(name);
1906            Node JavaDoc attr2 = (Node JavaDoc) attrs2.getNamedItem(name);
1907            if(attr1 == null && attr2 == null)
1908                continue;
1909            else if(attr1 != null && attr2 != null){
1910                if(attr2.getNodeValue().intern() != attr1.getNodeValue().intern())
1911                    unmatchCount++;
1912                else
1913                    matchCount++;
1914            } else
1915                unmatchCount++;
1916            //check for exact match
1917
if(matchCount == 1)
1918                return true;
1919            
1920            //check for rename
1921
if(unmatchCount == 1 && attrs1.getLength() == attrs2.getLength())
1922                return false;
1923        }
1924        
1925        //no attributes in attrs1 and attrs2 that match nameSet
1926
if ( matchCount == 0 && unmatchCount == 0 )
1927            return true;
1928        
1929        return false;
1930    }
1931    
1932    public static boolean fromSameSchemaModel(AXIComponent c, SchemaModel sm) {
1933        return c.getPeer()!=null?
1934            fromSameSchemaModel(c.getPeer(), sm):
1935            c.getModel().getSchemaModel() == sm;
1936    }
1937    
1938    public static boolean fromSameSchemaModel(SchemaComponent c, SchemaModel sm) {
1939        return c.getModel() == sm;
1940    }
1941}
1942
Popular Tags