KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beansdev > SchemaRep


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.schema2beansdev;
21
22 import org.netbeans.modules.schema2beansdev.gen.JavaUtil;
23 import org.netbeans.modules.schema2beansdev.gen.XMLWriter;
24
25 import java.io.*;
26 import java.util.*;
27
28 import org.w3c.dom.*;
29 import org.xml.sax.*;
30
31 import org.netbeans.modules.schema2beans.*;
32
33 /**
34  * This class will represent a schema for XML as a Java object.
35  * (This implementation isn't complete, but does enough for my purposes
36  * for now.)
37  *
38  * @author cliffwd
39  */

40 public class SchemaRep implements PrefixGuesser {
41     public static boolean debug = false;
42
43     public static final String JavaDoc XSD_NS = "http://www.w3.org/2001/XMLSchema";
44
45     /*
46      * The Schema Representation is internally made up of a tree of
47      * Element Expressions (ElementExpr).
48      */

49     ////////////////////////////////////////////////////////////////
50
public abstract class ElementExpr {
51         public abstract String JavaDoc getName();
52         public abstract void writeDTD(StringBuffer JavaDoc out);
53
54         // writeDTDName returns true if there were any named elements
55
public abstract boolean writeDTDName(StringBuffer JavaDoc out);
56         public abstract void writeXMLSchema(XMLWriter out) throws IOException;
57
58         // validate checks to make sure everything is okay
59
public abstract void validate();
60
61         // optimize returns a replacement node. null means delete.
62
public abstract ElementExpr optimize();
63         public abstract void readSchema(org.w3c.dom.Element JavaDoc node);
64
65         // Return null for getContentName if this expr doesn't have one.
66
public abstract String JavaDoc getContentName();
67         protected ElementExpr parentExpr;
68         protected void setParentExpr(ElementExpr ee) {
69             parentExpr = ee;
70             fullContentName = null;
71         }
72         public ElementExpr getParentExpr() {
73             return parentExpr;
74         }
75         private String JavaDoc fullContentName = null;
76         public String JavaDoc getFullContentName() {
77             if (fullContentName == null) {
78                 String JavaDoc contentName = getContentName();
79                 if (parentExpr == null) {
80                     if (contentName == null)
81                         fullContentName = "/";
82                     else
83                         fullContentName = ("/" + contentName).intern();
84                 } else {
85                     String JavaDoc parentFullContentName = parentExpr.getFullContentName();
86                     if (contentName == null)
87                         fullContentName = parentFullContentName;
88                     else if (parentFullContentName == "/")
89                         fullContentName = (parentFullContentName + contentName).intern();
90                     else
91                         fullContentName = (parentFullContentName + "/" + contentName).intern();
92                 }
93             }
94             return fullContentName;
95         }
96         protected String JavaDoc uniquifyFullContentName() {
97             fullContentName = (getFullContentName()+"/#").intern();
98             return fullContentName;
99         }
100     }
101
102     ////////////////////////////////////////////////////////////////
103
// This class represents all elements (or nodes in the metadata)
104
// that can have subelements.
105
public abstract class ContainsSubElements extends ElementExpr {
106         protected List subElements; // List<ElementExpr>
107

108         public ContainsSubElements() {
109             subElements = new LinkedList();
110         }
111
112         public void addSubElement(ElementExpr subElement) {
113             //System.out.println("Adding:"+subElement);
114
subElement.setParentExpr(this);
115             if (subElement instanceof Element) {
116                 String JavaDoc subElementFullContentName;
117                 boolean checkAgain;
118                 do {
119                     checkAgain = false;
120                     subElementFullContentName = subElement.getFullContentName();
121                     Iterator it = subElements.iterator();
122                     while (it.hasNext()) {
123                         ElementExpr otherElement = (ElementExpr) it.next();
124                         String JavaDoc otherElementFullContentName = otherElement.getFullContentName();
125                         if (subElementFullContentName == otherElementFullContentName) {
126                             if (debug)
127                                 System.out.println("Found duplicate fullContentName for "+otherElement.getName()+" : "+subElementFullContentName);
128                             subElement.uniquifyFullContentName();
129                             checkAgain = true;
130                         }
131                     }
132                 } while (checkAgain);
133             }
134             subElements.add(subElement);
135         }
136
137         public void addSubElement(List se) {
138             //System.out.println("Adding: "+se+" to:"+toString());
139
Iterator it = se.iterator();
140             while (it.hasNext()) {
141                 addSubElement((ElementExpr) it.next());
142             }
143         }
144
145         public Iterator subElementsIterator() {
146             return subElements.iterator();
147         }
148
149         public ElementExpr findSubElement(String JavaDoc nodeName) {
150             //System.out.println("Looking for subelement "+nodeName);
151
Iterator it = subElements.iterator();
152             while (it.hasNext()) {
153                 ElementExpr el = (ElementExpr) it.next();
154                 if (el.getName().equals(nodeName))
155                     return el;
156             }
157             //System.out.println("Did not find it");
158
return null;
159         }
160
161         public ElementExpr findSubElement(Class JavaDoc type) {
162             Iterator it = subElements.iterator();
163             while (it.hasNext()) {
164                 ElementExpr el = (ElementExpr) it.next();
165                 if (type.isAssignableFrom(el.getClass()))
166                     return el;
167             }
168             return null;
169         }
170
171         /**
172          * This will do an xpath like search.
173          */

174         public ElementExpr findSubElement(String JavaDoc[] nodeNames) {
175             ContainsSubElements current = this;
176             for (int i = 0; i < nodeNames.length; ++i) {
177                 ElementExpr ee = current.findSubElement(nodeNames[i]);
178                 if (ee == null)
179                     return null;
180                 if (ee instanceof ContainsSubElements)
181                     current = (ContainsSubElements) ee;
182                 else if (i+1 != nodeNames.length)
183                     return null;
184             }
185             return current;
186         }
187
188         /**
189          * Find all matching sub elements and put them into lst.
190          */

191         public void findAllSubElements(String JavaDoc name, List lst) {
192             Iterator it = subElements.iterator();
193             while (it.hasNext()) {
194                 ElementExpr el = (ElementExpr) it.next();
195                 if (el.getName().equals(name))
196                     lst.add(el);
197                 if (el instanceof ContainsSubElements)
198                     ((ContainsSubElements)el).findAllSubElements(name, lst);
199             }
200         }
201
202         /**
203          * Find all matching sub elements and put them into lst.
204          */

205         public void findAllSubElements(Class JavaDoc type, List lst) {
206             Iterator it = subElements.iterator();
207             while (it.hasNext()) {
208                 ElementExpr el = (ElementExpr) it.next();
209                 if (type.isAssignableFrom(el.getClass()))
210                     lst.add(el);
211                 if (el instanceof ContainsSubElements)
212                     ((ContainsSubElements)el).findAllSubElements(type, lst);
213             }
214         }
215
216         public void validate() {
217             //System.out.println("** validate: "+this);
218
Map possibleSubElements = validSubElementTypeMap();
219             Iterator it = subElements.iterator();
220             while (it.hasNext()) {
221                 ElementExpr ee = (ElementExpr) it.next();
222                 if (!possibleSubElements.containsKey(ee.getClass())) {
223                     throw new IllegalStateException JavaDoc(Common.getMessage("MSG_InvalidContents", toString(), ee.getName(), getFullContentName()));
224                 }
225                 ee.validate();
226             }
227         }
228
229         public abstract Map validSubElementTypeMap();
230
231         public void writeDTD(StringBuffer JavaDoc out) {
232             Iterator it = subElements.iterator();
233             while (it.hasNext()) {
234                 ElementExpr el = (ElementExpr) it.next();
235                 el.writeDTD(out);
236             }
237         }
238
239         /**
240          * Return the attributes as a string for the XML Schema.
241          * null means that we have no attributes to add.
242          */

243         public String JavaDoc getAttributeString() {
244             return null;
245         }
246
247         /**
248          * If whitespace is used on the outside of this element.
249          */

250         public boolean compressWhiteSpaceOuter() {
251             return false;
252         }
253
254         /**
255          * If whitespace is used on the inside of this element.
256          */

257         public boolean compressWhiteSpaceInner() {
258             return false;
259         }
260
261         public void writeXMLSchema(XMLWriter out) throws IOException {
262             writeXMLSchema(out, true);
263         }
264
265         /**
266          * If @printMyElement is false, then we only print subelements.
267          */

268         public void writeXMLSchema(XMLWriter out, boolean printMyElement) throws IOException {
269             if (printMyElement) {
270                 //if (!compressWhiteSpaceOuter())
271
// out.indentRight();
272
out.startTag(getXSDNamespace(), getName(), false);
273                 String JavaDoc attrs = getAttributeString();
274                 if (attrs != null)
275                     out.write(attrs);
276                 out.finishStartTag(subElements.size() > 0, !compressWhiteSpaceInner());
277                 if (subElements.size() == 0)
278                     return;
279             }
280             Iterator it = subElements.iterator();
281             while (it.hasNext()) {
282                 ElementExpr el = (ElementExpr) it.next();
283                 el.writeXMLSchema(out);
284             }
285             if (printMyElement) {
286                 //if (!compressWhiteSpaceInner())
287
// out.indentLeft();
288
out.endTag(!compressWhiteSpaceOuter());
289             }
290         }
291
292         protected boolean writeDTDSubElementNames(StringBuffer JavaDoc out) {
293             return writeDTDSubElementNames(out, true);
294         }
295         protected boolean writeDTDSubElementNames(StringBuffer JavaDoc out, boolean writeParens) {
296             if (subElements.size() == 0)
297                 return false;
298             boolean first = true;
299             Iterator it = subElements.iterator();
300             boolean hasNamedSubElements = false;
301             // Put all of the DTD names of our subelements into subOut
302
StringBuffer JavaDoc subOut = new StringBuffer JavaDoc();
303             // Let each individual subelement do it's work in freshOut
304
// (it's always empty at the top of the loop).
305
StringBuffer JavaDoc freshOut = new StringBuffer JavaDoc();
306             while (it.hasNext()) {
307                 ElementExpr el = (ElementExpr) it.next();
308                 boolean anyNamed = el.writeDTDName(freshOut);
309                 if (anyNamed) {
310                     hasNamedSubElements = true;
311                     if (first)
312                         first = false;
313                     else
314                         subOut.append(", "); // NOI18N
315
subOut.append(freshOut.toString());
316                     freshOut = new StringBuffer JavaDoc();
317                 }
318             }
319             if (!hasNamedSubElements)
320                 return false;
321             if (writeParens && subElements.size() >= 2)
322                 out.append("("); // NOI18N
323
out.append(subOut);
324             if (writeParens && subElements.size() >= 2)
325                 out.append(")"); // NOI18N
326
return hasNamedSubElements;
327         }
328
329         // Inserts String's into @param list.
330
public void findSubElementNames(List list) {
331             Iterator it = subElements.iterator();
332             StringBuffer JavaDoc freshOut = new StringBuffer JavaDoc();
333             while (it.hasNext()) {
334                 ElementExpr el = (ElementExpr) it.next();
335                 boolean anyNamed = el.writeDTDName(freshOut);
336             }
337         }
338
339         public String JavaDoc toString() {
340             String JavaDoc attributeString = getAttributeString();
341             if (attributeString == null)
342                 return getName();
343             else
344                 return getName()+attributeString;
345         }
346
347         public boolean equals(Object JavaDoc o) {
348             if (!(o instanceof ContainsSubElements))
349                 return false;
350             ContainsSubElements e = (ContainsSubElements) o;
351             //System.out.println("Checking for equals of ContainsSubElements");
352
if (subElements.size() != e.subElements.size())
353                 return false;
354             Iterator it = subElements.iterator();
355             Iterator ite = e.subElements.iterator();
356             while (it.hasNext()) {
357                 ElementExpr el = (ElementExpr) it.next();
358                 ElementExpr ele = (ElementExpr) ite.next();
359                 //System.out.println("el="+el);
360
//System.out.println("ele="+ele);
361
if (!el.equals(ele))
362                     return false;
363             }
364             return true;
365         }
366
367         public int hashCode() {
368             int result = 17;
369             Iterator it = subElements.iterator();
370             while (it.hasNext()) {
371                 ElementExpr el = (ElementExpr) it.next();
372                 result = 37*result + el.hashCode();
373             }
374             return result;
375         }
376
377         public ElementExpr optimize() {
378             ListIterator it = subElements.listIterator();
379             while (it.hasNext()) {
380                 ElementExpr el = (ElementExpr) it.next();
381                 ElementExpr result = el.optimize();
382                 //System.out.println("optimize: result="+result);
383
if (result == null) {
384                     el.setParentExpr(null);
385                     it.remove();
386                 } else if (el != result)
387                     it.set(result);
388             }
389             return this;
390         }
391
392         public String JavaDoc getJavaTypeName() {
393             Iterator it = subElements.iterator();
394             while (it.hasNext()) {
395                 Object JavaDoc o = it.next();
396                 if (o instanceof HasJavaTypeName) {
397                     //System.out.println("Container: getJavaTypeName: o="+o);
398
return ((HasJavaTypeName)o).getJavaTypeName();
399                 }
400             }
401             return null;
402         }
403     }
404
405     public interface HasJavaTypeName {
406         public String JavaDoc getJavaTypeName();
407     }
408
409     public interface MinMaxOccurs {
410         public String JavaDoc getMinOccurs();
411         public String JavaDoc getMaxOccurs();
412     }
413
414     public interface CanRef {
415         public boolean hasRef();
416         /**
417          * @return the name of the ref or null if !hasRef()
418          */

419         public String JavaDoc getRef();
420         public ElementExpr getRefElementExpr();
421     }
422
423     ////
424
public abstract class HasTypeName extends ContainsSubElements {
425         private String JavaDoc typeNameLocalPart = null;
426         private String JavaDoc typeNameNamespace = null;
427         
428         protected String JavaDoc setTypeName(String JavaDoc typeName) {
429             if (typeName == null) {
430                 typeNameLocalPart = null;
431                 typeNameNamespace = null;
432                 return null;
433             }
434             typeName = normalizeTargetNamespace(typeName).intern();
435             typeNameLocalPart = removePrefix(typeName).intern();
436             String JavaDoc prefix = prefixOf(typeName);
437             typeNameNamespace = getNamespaceURI(prefix);
438             if (typeNameNamespace != null)
439                 typeNameNamespace = typeNameNamespace.intern();
440             return typeName;
441         }
442
443         public String JavaDoc getTypeNameLocalPart() {
444             return typeNameLocalPart;
445         }
446         
447         public String JavaDoc getTypeNameNamespace() {
448             return typeNameNamespace;
449         }
450
451         /**
452          * Can return null.
453          */

454         public String JavaDoc getTypeName() {
455             if (typeNameLocalPart == null)
456                 return null;
457             String JavaDoc result = normalizeNamespace(typeNameNamespace, typeNameLocalPart);
458             return result;
459         }
460
461     }
462     
463     ////////////////////////////////////////////////////////////////
464
// Top of an XML Schema
465
public class SchemaNode extends ContainsSubElements {
466         protected String JavaDoc targetNamespace;
467         private Boolean JavaDoc elementFormQualifiedDefault = null;
468         private Boolean JavaDoc attributeFormQualifiedDefault = null;
469
470         public SchemaNode() {
471         }
472
473         public String JavaDoc getName() {
474             return "schema";
475         }
476
477         public String JavaDoc getContentName() {
478             return null;
479         }
480
481         public void setTargetNamespace(String JavaDoc tn) {
482             targetNamespace = tn;
483         }
484
485         public String JavaDoc getTargetNamespace() {
486             return targetNamespace;
487         }
488
489         public boolean isElementFormQualified() {
490             if (elementFormQualifiedDefault == null)
491                 return false;
492             return elementFormQualifiedDefault.booleanValue();
493         }
494
495         public boolean isAttributeFormQualified() {
496             if (attributeFormQualifiedDefault == null)
497                 return false;
498             return attributeFormQualifiedDefault.booleanValue();
499         }
500
501         public void readSchema(org.w3c.dom.Element JavaDoc node) {
502             String JavaDoc tns = node.getAttribute("targetNamespace"); // NOI18N
503
String JavaDoc efd = node.getAttribute("elementFormDefault"); // NOI18N
504
String JavaDoc afd = node.getAttribute("attributeFormDefault"); // NOI18N
505
if (tns != null && !"".equals(tns))
506                 targetNamespace = tns;
507             if (efd != null)
508                 elementFormQualifiedDefault = Boolean.valueOf("qualified".equals(efd));
509             SchemaRep.this.elementFormQualifiedDefault = isElementFormQualified();
510             if (afd != null)
511                 attributeFormQualifiedDefault = Boolean.valueOf("qualified".equals(afd));
512             SchemaRep.this.attributeFormQualifiedDefault = isAttributeFormQualified();
513         }
514
515         void merge(org.w3c.dom.Element JavaDoc node) {
516             //System.out.println("merge: "+node);
517
String JavaDoc tns = node.getAttribute("targetNamespace"); // NOI18N
518
String JavaDoc efd = node.getAttribute("elementFormDefault"); // NOI18N
519
String JavaDoc afd = node.getAttribute("attributeFormDefault"); // NOI18N
520
if (targetNamespace == null)
521                 targetNamespace = tns;
522             
523             boolean value = "qualified".equals(efd);
524             if (efd != null) {
525                 if (elementFormQualifiedDefault == null || elementFormQualifiedDefault.booleanValue())
526                     elementFormQualifiedDefault = Boolean.valueOf(value);
527             }
528             SchemaRep.this.elementFormQualifiedDefault = value;
529             
530             value = "qualified".equals(afd);
531             if (afd != null) {
532                 if (attributeFormQualifiedDefault == null || attributeFormQualifiedDefault.booleanValue())
533                     attributeFormQualifiedDefault = Boolean.valueOf(value);
534             }
535             SchemaRep.this.attributeFormQualifiedDefault = value;
536         }
537
538         public void addSubElement(ElementExpr subElement) {
539             if (subElement instanceof Element) {
540                 Element el = (Element) subElement;
541                 //System.out.println("SchemaNode.addSubElement: el="+el);
542
if (el.isDefiningNewType()) {
543                     if (debug)
544                         System.out.println("SchemaNode new element type: name="+el.getElementName());
545                     definedTypes.put(el.getElementName(), el);
546                     definedTypesFull.put(canonicalQName(el.getElementNamespace(), el.getElementName()), el);
547                 }
548             }
549             if (subElement instanceof Attribute) {
550                 Attribute attr = (Attribute) subElement;
551                 //System.out.println("SchemaNode.addSubElement: attr="+attr);
552
if (attr.isDefiningNewType()) {
553                     if (debug)
554                         System.out.println("SchemaNode new element type: attr="+attr);
555                     definedAttributes.put(attr.getAttributeName(), attr);
556                 }
557             }
558             super.addSubElement(subElement);
559         }
560
561         public String JavaDoc getAttributeString() {
562             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(" xmlns:"+getXSDNamespace()+"='"+getNamespaceURI(getXSDNamespace())+"'"); // NOI18N
563
if (targetNamespace != null)
564                 sb.append(" targetNamespace='"+getTargetNamespace()+"'"); // NOI18N
565
if (documentNamespace != null) {
566                 sb.append(" xmlns='"+documentNamespace+"'"); // NOI18N
567
}
568             if (isElementFormQualified())
569                 sb.append(" elementFormDefault='qualified'");
570             if (isAttributeFormQualified())
571                 sb.append(" attributeFormDefault='qualified'");
572             return sb.toString();
573         }
574
575         public void writeXMLSchema(XMLWriter out) throws IOException {
576             out.startTag(getXSDNamespace(), getName(), getAttributeString()); // NOI18N
577
out.cr();
578             Iterator it = requiredPredefinedTypes.keySet().iterator();
579             while (it.hasNext()) {
580                 String JavaDoc type = (String JavaDoc) it.next();
581                 //System.out.println("Required predefined type "+type);
582
ElementExpr el = (ElementExpr) optionallyDefinedTypes.get(type);
583                 el.writeXMLSchema(out);
584             }
585
586             super.writeXMLSchema(out, false);
587             out.endTag(); // NOI18N
588
}
589
590         public boolean writeDTDName(StringBuffer JavaDoc out) {
591             return writeDTDSubElementNames(out, false);
592         }
593
594         public Map validSubElementTypeMap() {
595             return schemaValidSubElementTypeMap;
596         }
597
598         public boolean equals(Object JavaDoc o) {
599             if (!(o instanceof SchemaNode))
600                 return false;
601             SchemaNode el = (SchemaNode) o;
602             if (targetNamespace == null) {
603                 if (el.targetNamespace != null)
604                     return false;
605             } else {
606                 if (!targetNamespace.equals(el.targetNamespace))
607                     return false;
608             }
609             if (elementFormQualifiedDefault != el.elementFormQualifiedDefault)
610                 return false;
611             if (attributeFormQualifiedDefault != el.attributeFormQualifiedDefault)
612                 return false;
613
614             return super.equals(el);
615         }
616
617         public int hashCode() {
618             int result = 17;
619             result = 37*result + ((targetNamespace == null) ? 0 : targetNamespace.hashCode());
620             result = 37*result + (elementFormQualifiedDefault.booleanValue() ? 1 : 0);
621             result = 37*result + (attributeFormQualifiedDefault.booleanValue() ? 1 : 0);
622             result = 37*result + super.hashCode();
623             return result;
624         }
625     }
626     static private Map schemaValidSubElementTypeMap = null;
627     static {
628         schemaValidSubElementTypeMap = new HashMap();
629         schemaValidSubElementTypeMap.put(Annotation.class, null);
630         schemaValidSubElementTypeMap.put(SimpleType.class, null);
631         schemaValidSubElementTypeMap.put(ComplexType.class, null);
632         schemaValidSubElementTypeMap.put(Element.class, null);
633         schemaValidSubElementTypeMap.put(Attribute.class, null);
634         schemaValidSubElementTypeMap.put(AttributeGroup.class, null);
635         schemaValidSubElementTypeMap.put(Include.class, null);
636         schemaValidSubElementTypeMap.put(Import.class, null);
637         schemaValidSubElementTypeMap.put(Group.class, null);
638     }
639
640     ////////////////////////////////////////////////////////////////
641
// See XML Schema complexType
642
public class ComplexType extends HasTypeName implements HasJavaTypeName {
643         //protected String typeName;
644
private boolean mixed = false;
645         private boolean abstractType = false;
646
647         public ComplexType() {
648         }
649
650         public ComplexType(String JavaDoc typeName) {
651             if (typeName != null && !typeName.equals("")) {
652                 typeName = setTypeName(typeName);
653                 putSchemaTypeDef(typeName, this);
654             }
655         }
656
657         public String JavaDoc getName() {
658             return "complexType";
659         }
660
661         public String JavaDoc getContentName() {
662             return getTypeName();
663         }
664
665         public boolean isMixed() {
666             return mixed;
667         }
668
669         public boolean isAbstract() {
670             return abstractType;
671         }
672
673         public boolean writeDTDName(StringBuffer JavaDoc out) {
674             return writeDTDSubElementNames(out, false);
675         }
676
677         public String JavaDoc getAttributeString() {
678             String JavaDoc result = "";
679             if (getTypeName() != null)
680                 result += " name='"+getTypeName()+"'";
681             if (mixed)
682                 result += " mixed='true'";
683             if (abstractType)
684                 result += " abstract='true'";
685             return result;
686         }
687
688         public Map validSubElementTypeMap() {
689             return complexTypeValidSubElementTypeMap;
690         }
691
692         public void readSchema(org.w3c.dom.Element JavaDoc node) {
693             String JavaDoc elementType = node.getAttribute("name"); // NOI18N
694
String JavaDoc myMixed = node.getAttribute("mixed"); // NOI18N
695
String JavaDoc myAbstract = node.getAttribute("abstract"); // NOI18N
696
ComplexType el = new ComplexType(elementType);
697             if (myMixed != null && (myMixed.equals("true") || myMixed.equals("yes") || myMixed.equals("on")))
698                 el.mixed = true;
699             if (myAbstract != null && (myAbstract.equals("true") || myAbstract.equals("yes") || myAbstract.equals("on")))
700                 el.abstractType = true;
701             if (debug)
702                 System.out.println("Created complexType "+elementType);
703             pushCurrent(el);
704             read(node);
705             popCurrent();
706         }
707
708         public boolean equals(Object JavaDoc o) {
709             if (!(o instanceof ComplexType))
710                 return false;
711             ComplexType el = (ComplexType) o;
712             if ((getTypeName() == null) ? (el.getTypeName() == null) : getTypeName().equals(el.getTypeName()))
713                 return false;
714             if (mixed != el.mixed)
715                 return false;
716             if (abstractType != el.abstractType)
717                 return false;
718
719             return super.equals(el);
720         }
721
722         public int hashCode() {
723             int result = 17;
724             result = 37*result + ((getTypeName() == null) ? 0 : getTypeName().hashCode());
725             result = 37*result + (mixed ? 1 : 0);
726             result = 37*result + (abstractType ? 1 : 0);
727             result = 37*result + super.hashCode();
728             return result;
729         }
730     }
731     static private Map complexTypeValidSubElementTypeMap = null;
732     static {
733         complexTypeValidSubElementTypeMap = new HashMap();
734         complexTypeValidSubElementTypeMap.put(Annotation.class, null);
735         complexTypeValidSubElementTypeMap.put(Choice.class, null);
736         complexTypeValidSubElementTypeMap.put(Group.class, null);
737         complexTypeValidSubElementTypeMap.put(Sequence.class, null);
738         complexTypeValidSubElementTypeMap.put(All.class, null);
739         complexTypeValidSubElementTypeMap.put(Attribute.class, null);
740         complexTypeValidSubElementTypeMap.put(AttributeGroup.class, null);
741         complexTypeValidSubElementTypeMap.put(AnyAttribute.class, null);
742         complexTypeValidSubElementTypeMap.put(SimpleContent.class, null);
743         complexTypeValidSubElementTypeMap.put(ComplexContent.class, null);
744     }
745
746     ////////////////////////////////////////////////////////////////
747
public class SimpleContent extends ContainsSubElements implements HasJavaTypeName {
748         public SimpleContent() {
749         }
750
751         public String JavaDoc getName() {
752             return "simpleContent";
753         }
754
755         public String JavaDoc getContentName() {
756             return null;
757         }
758
759         public void validate() {
760         }
761
762         public ElementExpr optimize() {
763             return this;
764         }
765
766         public void readSchema(org.w3c.dom.Element JavaDoc node) {
767             SimpleContent el = new SimpleContent();
768             pushCurrent(el);
769             read(node);
770             popCurrent();
771         }
772
773         public boolean writeDTDName(StringBuffer JavaDoc out) {
774             return writeDTDSubElementNames(out, false);
775         }
776
777         public Map validSubElementTypeMap() {
778             return simpleContentValidSubElementTypeMap;
779         }
780     }
781     static private Map simpleContentValidSubElementTypeMap = null;
782     static {
783         simpleContentValidSubElementTypeMap = new HashMap();
784         simpleContentValidSubElementTypeMap.put(Annotation.class, null);
785         simpleContentValidSubElementTypeMap.put(Restriction.class, null);
786         simpleContentValidSubElementTypeMap.put(Extension.class, null);
787     }
788
789
790     ////////////////////////////////////////////////////////////////
791
public class ComplexContent extends ContainsSubElements implements HasJavaTypeName {
792         private boolean mixed;
793
794         public ComplexContent() {
795         }
796
797         public String JavaDoc getName() {
798             return "complexContent";
799         }
800
801         public String JavaDoc getContentName() {
802             return null;
803         }
804
805         public void validate() {
806         }
807
808         public ElementExpr optimize() {
809             return this;
810         }
811
812         public boolean isMixed() {
813             return mixed;
814         }
815
816         public void readSchema(org.w3c.dom.Element JavaDoc node) {
817             ComplexContent el = new ComplexContent();
818             String JavaDoc myMixed = node.getAttribute("mixed"); // NOI18N
819
if (myMixed != null && (myMixed.equals("true") || myMixed.equals("yes") || myMixed.equals("on")))
820                 el.mixed = true;
821             pushCurrent(el);
822             read(node);
823             popCurrent();
824         }
825
826         public boolean writeDTDName(StringBuffer JavaDoc out) {
827             return writeDTDSubElementNames(out, false);
828         }
829
830         public Map validSubElementTypeMap() {
831             return complexContentValidSubElementTypeMap;
832         }
833
834         public boolean equals(Object JavaDoc o) {
835             if (!(o instanceof ComplexContent))
836                 return false;
837             ComplexContent el = (ComplexContent) o;
838             if (mixed != el.mixed)
839                 return false;
840             return super.equals(el);
841         }
842
843         public int hashCode() {
844             int result = 17;
845             result = 37*result + (mixed ? 0 : 1);
846             result = 37*result + super.hashCode();
847             return result;
848         }
849     }
850     static private Map complexContentValidSubElementTypeMap = null;
851     static {
852         complexContentValidSubElementTypeMap = new HashMap();
853         complexContentValidSubElementTypeMap.put(Annotation.class, null);
854         complexContentValidSubElementTypeMap.put(Restriction.class, null);
855         complexContentValidSubElementTypeMap.put(Extension.class, null);
856     }
857
858     ////////////////////////////////////////////////////////////////
859
// See XML Schema simpleType
860
public class SimpleType extends HasTypeName implements HasJavaTypeName {
861         //private String typeName = null;
862
private String JavaDoc javaTypeName = null;
863
864         public SimpleType(String JavaDoc tn) {
865             //System.out.println("SimpleType1: tn="+tn);
866
if (tn != null && !tn.equals("")) {
867                 String JavaDoc typeName = normalizeTargetNamespace(tn).intern();
868                 ElementExpr previousDef = getSchemaTypeDef(typeName);
869                 if (previousDef instanceof SimpleType && (((SimpleType)previousDef).javaTypeName != null || ((SimpleType)previousDef).subElements.size() > 0)) {
870                     // The previous definition has more information. Keep it.
871
System.out.println("Keeping schemaTypeDefs1 for "+previousDef);
872                 } else {
873                     if (previousDef != null)
874                         System.out.println("!!! Overwriting schemaTypeDefs1 ("+previousDef+") for "+this);
875                     typeName = setTypeName(typeName);
876                     putSchemaTypeDef(typeName, this);
877                 }
878             }
879         }
880         
881         /**
882          * @param tn is the type
883          * @param javaTypeName is a java type to associate with @param tn
884          * and should be linearly dependent.
885          */

886         public SimpleType(String JavaDoc tn, String JavaDoc javaTypeName) {
887             super();
888             //System.out.println("SimpleType2: tn="+tn+" javaTypeName="+javaTypeName);
889
String JavaDoc typeName = setTypeName(tn);
890             this.javaTypeName = (javaTypeName == null) ? null : javaTypeName.intern();
891             /*
892             if (schemaTypeDefs.get(typeName) != null)
893                 System.out.println("!!! Overwriting schemaTypeDefs2 for "+this);
894             */

895             putSchemaTypeDef(typeName, this);
896         }
897
898         public SimpleType(String JavaDoc tn, Restriction restrict) {
899             super();
900             //System.out.println("SimpleType3: tn="+tn+" restrict="+restrict);
901
String JavaDoc typeName = setTypeName(tn);
902             this.addSubElement(restrict);
903             /*
904             if (schemaTypeDefs.get(typeName) != null)
905                 System.out.println("!!! Overwriting schemaTypeDefs3 for "+this);
906             */

907             putSchemaTypeDef(typeName, this);
908         }
909
910         public String JavaDoc getName() {
911             return "simpleType";
912         }
913
914         public String JavaDoc getContentName() {
915             return getTypeName();
916         }
917
918         // This may return null
919
public String JavaDoc getJavaTypeName() {
920             if (javaTypeName != null)
921                 return javaTypeName;
922             return super.getJavaTypeName();
923         }
924
925         public String JavaDoc getAttributeString() {
926             if (getTypeName() == null)
927                 return null;
928             return (" name='"+getTypeName()+"'");
929         }
930
931         public boolean compressWhiteSpaceInner() {
932             if (subElements.size() == 0)
933                 return true;
934             ElementExpr subElement = (ElementExpr) subElementsIterator().next();
935             if (subElement instanceof ContainsSubElements)
936                 return ((ContainsSubElements)subElement).compressWhiteSpaceOuter();
937             return true;
938         }
939
940         public boolean writeDTDName(StringBuffer JavaDoc out) {
941             return writeDTDSubElementNames(out, false);
942         }
943
944         public void readSchema(org.w3c.dom.Element JavaDoc node) {
945             String JavaDoc elementType = node.getAttribute("name"); // NOI18N
946
SimpleType el = new SimpleType(elementType);
947             pushCurrent(el);
948             read(node);
949             popCurrent();
950         }
951
952         public Map validSubElementTypeMap() {
953             return simpleTypeValidSubElementTypeMap;
954         }
955
956         public String JavaDoc toString() {
957             if (javaTypeName == null) {
958                 if (getTypeName() == null)
959                     return "simpleType (not named)"; // NOI18N
960
else
961                     return "simpleType "+getTypeName(); // NOI18N
962
} else
963                 return "simpleType "+getTypeName()+" "+javaTypeName; // NOI18N
964
}
965
966         public boolean equals(Object JavaDoc o) {
967             if (!(o instanceof SimpleType))
968                 return false;
969             SimpleType el = (SimpleType) o;
970             if ((getTypeName() == null) ? (el.getTypeName() == null) : getTypeName().equals(el.getTypeName()))
971                 return false;
972             // javaTypeName ought to be redundant to typeName
973
return super.equals(el);
974         }
975
976         public int hashCode() {
977             int result = 17;
978             if (getTypeName() != null)
979                 result = 37*result + getTypeName().hashCode();
980             result = 37*result + super.hashCode();
981             return result;
982         }
983     }
984     static private Map simpleTypeValidSubElementTypeMap = null;
985     static {
986         simpleTypeValidSubElementTypeMap = new HashMap();
987         simpleTypeValidSubElementTypeMap.put(Annotation.class, null);
988         simpleTypeValidSubElementTypeMap.put(Restriction.class, null);
989         simpleTypeValidSubElementTypeMap.put(ListElement.class, null);
990         simpleTypeValidSubElementTypeMap.put(UnionType.class, null);
991     }
992
993     ////////////////////////////////////////////////////////////////
994
// See XML Schema simpleType
995
public class UnionType extends ContainsSubElements {
996         private String JavaDoc typeName = null;
997         private String JavaDoc memberTypes = null;
998
999         public UnionType(String JavaDoc tn, String JavaDoc memberTypes) {
1000            //System.out.println("UnionType1: tn="+tn);
1001
if (tn != null && !tn.equals("")) {
1002                typeName = normalizeTargetNamespace(tn).intern();
1003            }
1004            this.memberTypes = memberTypes;
1005        }
1006
1007        public String JavaDoc getMemberTypes() {
1008            return memberTypes;
1009        }
1010
1011        public ElementExpr[] getMemberTypeElements() {
1012            if (memberTypes == null || memberTypes.trim().length() == 0)
1013                return null;
1014            ArrayList mlist = new ArrayList();
1015            String JavaDoc[] members = memberTypes.trim().split(" ");
1016            for (int i=0; i < members.length; i++) {
1017                if (members[i].length() == 0)
1018                    continue;
1019                SchemaRep.ElementExpr ee = getSchemaTypeDef(members[i]);
1020                if (ee != null)
1021                    mlist.add(ee);
1022            }
1023
1024            ElementExpr[] memberList = new ElementExpr[mlist.size()];
1025            memberList = (ElementExpr[]) mlist.toArray(memberList);
1026            return memberList;
1027        }
1028
1029        public String JavaDoc getName() {
1030            return "union";
1031        }
1032
1033        public String JavaDoc getContentName() {
1034            return typeName;
1035        }
1036
1037        public String JavaDoc getTypeName() {
1038            return typeName;
1039        }
1040
1041        public String JavaDoc getAttributeString() {
1042            if (memberTypes == null)
1043                return null;
1044            return (" memberTypes='"+memberTypes+"'");
1045        }
1046
1047        public boolean compressWhiteSpaceInner() {
1048            if (subElements.size() == 0)
1049                return true;
1050            ElementExpr subElement = (ElementExpr) subElementsIterator().next();
1051            if (subElement instanceof ContainsSubElements)
1052                return ((ContainsSubElements)subElement).compressWhiteSpaceOuter();
1053            return true;
1054        }
1055
1056        public boolean writeDTDName(StringBuffer JavaDoc out) {
1057            return writeDTDSubElementNames(out, false);
1058        }
1059
1060        public void readSchema(org.w3c.dom.Element JavaDoc node) {
1061            String JavaDoc memberTypes = node.getAttribute("memberTypes"); // NOI18N
1062
String JavaDoc typeName = "_union";
1063            org.w3c.dom.Node JavaDoc parent = node.getParentNode();
1064            if (parent instanceof org.w3c.dom.Element JavaDoc) {
1065                do {
1066                    org.w3c.dom.Element JavaDoc parentEl = (org.w3c.dom.Element JavaDoc) parent;
1067                    if (parent != null) {
1068                        String JavaDoc name = parentEl.getAttribute("name");
1069                        if (name != null) {
1070                            typeName = name + typeName;
1071                            break;
1072                        } else {
1073                            // Only simpletypes have unions..So
1074
typeName = "_simpleType" + typeName;
1075                        }
1076                        parent = parent.getParentNode();
1077                    } else
1078                        break;
1079                } while (parent != null);
1080            }
1081            UnionType el = new UnionType(typeName, memberTypes);
1082            pushCurrent(el);
1083            read(node);
1084            popCurrent();
1085        }
1086
1087        public Map validSubElementTypeMap() {
1088            return unionTypeValidSubElementTypeMap;
1089        }
1090
1091        public String JavaDoc toString() {
1092            if (typeName == null)
1093                return "unionType (not named)"; // NOI18N
1094
else
1095                return "unionType "+typeName; // NOI18N
1096
}
1097
1098        public boolean equals(Object JavaDoc o) {
1099            if (!(o instanceof UnionType))
1100                return false;
1101            UnionType el = (UnionType) o;
1102            if (typeName != el.typeName)
1103                return false;
1104            if (memberTypes != el.memberTypes)
1105                return false;
1106            // javaTypeName ought to be redundant to typeName
1107
return super.equals(el);
1108        }
1109
1110        public int hashCode() {
1111            int result = 17;
1112            result = 37*result + memberTypes.hashCode();
1113            result = 37*result + typeName.hashCode();
1114            result = 37*result + super.hashCode();
1115            return result;
1116        }
1117    }
1118    static private Map unionTypeValidSubElementTypeMap = null;
1119    static {
1120        unionTypeValidSubElementTypeMap = new HashMap();
1121        unionTypeValidSubElementTypeMap.put(Annotation.class, null);
1122        unionTypeValidSubElementTypeMap.put(SimpleType.class, null);
1123    }
1124
1125    public interface EncodingStyle {
1126    }
1127
1128    public class HexBinary extends SimpleType implements EncodingStyle {
1129        public HexBinary() {
1130            super(getXSDNamespace()+":hexBinary", "byte[]");
1131        }
1132
1133        public String JavaDoc toString() {
1134            return "hexBinary";
1135        }
1136    }
1137
1138    public class Base64Binary extends SimpleType implements EncodingStyle {
1139        public Base64Binary() {
1140            super(getXSDNamespace()+":base64Binary", "byte[]");
1141        }
1142
1143        public String JavaDoc toString() {
1144            return "base64Binary";
1145        }
1146    }
1147
1148    ////////////////////////////////////////////////////////////////
1149
// See XML Schema restriction (usually underneath simpleType)
1150
public class Restriction extends ContainsSubElements implements HasJavaTypeName {
1151        protected String JavaDoc base;
1152        public Restriction() {
1153        }
1154
1155        public Restriction(String JavaDoc base) {
1156            setBase(base);
1157        }
1158
1159        public String JavaDoc getName() {
1160            return "restriction";
1161        }
1162
1163        public String JavaDoc getContentName() {
1164            return null;
1165        }
1166
1167        public void setBase(String JavaDoc b) {
1168            if (b == null) {
1169                base = null;
1170                return;
1171            }
1172            base = normalizeDocumentNamespace(b).intern();
1173        }
1174
1175        public String JavaDoc getBase() {
1176            return base;
1177        }
1178
1179        public String JavaDoc getJavaTypeName() {
1180            return schemaTypeToJavaType(base);
1181        }
1182
1183        public boolean compressWhiteSpaceOuter() {
1184            return subElements.size() == 0;
1185        }
1186
1187        public boolean compressWhiteSpaceInner() {
1188            return subElements.size() == 0;
1189        }
1190
1191        public boolean writeDTDName(StringBuffer JavaDoc out) {
1192            return writeDTDSubElementNames(out, false);
1193        }
1194
1195        public String JavaDoc getAttributeString() {
1196            if (base == null)
1197                return null;
1198            return (" base='"+getBase()+"'");
1199        }
1200
1201        public void readSchema(org.w3c.dom.Element JavaDoc node) {
1202            String JavaDoc base = node.getAttribute("base"); // NOI18N
1203
Restriction el = new Restriction(base);
1204            pushCurrent(el);
1205            read(node);
1206            popCurrent();
1207        }
1208
1209        public Map validSubElementTypeMap() {
1210            return restrictionValidSubElementTypeMap;
1211        }
1212
1213        public boolean equals(Object JavaDoc o) {
1214            if (!(o instanceof Restriction))
1215                return false;
1216            Restriction el = (Restriction) o;
1217            if (base == null) {
1218                if (el.base != null)
1219                    return false;
1220            } else {
1221                if (!base.equals(el.base))
1222                    return false;
1223            }
1224
1225            return super.equals(el);
1226        }
1227
1228        public String JavaDoc toString() {
1229            Iterator subElements = subElementsIterator();
1230            StringBuffer JavaDoc sb = null;
1231            while (subElements.hasNext()) {
1232                ElementExpr ee = (ElementExpr) subElements.next();
1233                if (ee instanceof RestrictionType) {
1234                    if (sb == null)
1235                        sb = new StringBuffer JavaDoc();
1236                    else
1237                        sb.append(", ");
1238                    RestrictionType restrictionType = (RestrictionType) ee;
1239                    sb.append(restrictionType.toString());
1240                }
1241            }
1242/*
1243            if (sb == null)
1244                return super.toString();
1245            else
1246*/

1247            if (sb != null)
1248                return sb.toString();
1249            else
1250                return "";
1251        }
1252
1253        public int hashCode() {
1254            int result = 17;
1255            result = 37*result + ((base == null) ? 0: base.hashCode());
1256            result = 37*result + super.hashCode();
1257            return result;
1258        }
1259    }
1260    static private Map restrictionValidSubElementTypeMap = null;
1261    static {
1262        restrictionValidSubElementTypeMap = new HashMap();
1263        restrictionValidSubElementTypeMap.put(Annotation.class, null);
1264        restrictionValidSubElementTypeMap.put(SimpleType.class, null);
1265        restrictionValidSubElementTypeMap.put(All.class, null);
1266        restrictionValidSubElementTypeMap.put(Choice.class, null);
1267        restrictionValidSubElementTypeMap.put(Sequence.class, null);
1268        restrictionValidSubElementTypeMap.put(Group.class, null);
1269        restrictionValidSubElementTypeMap.put(Attribute.class, null);
1270        restrictionValidSubElementTypeMap.put(AttributeGroup.class, null);
1271        restrictionValidSubElementTypeMap.put(AnyAttribute.class, null);
1272        restrictionValidSubElementTypeMap.put(MinExclusive.class, null);
1273        restrictionValidSubElementTypeMap.put(MaxExclusive.class, null);
1274        restrictionValidSubElementTypeMap.put(Enumeration.class, null);
1275        restrictionValidSubElementTypeMap.put(Pattern.class, null);
1276        restrictionValidSubElementTypeMap.put(MinLength.class, null);
1277        restrictionValidSubElementTypeMap.put(MaxLength.class, null);
1278        restrictionValidSubElementTypeMap.put(TotalDigits.class, null);
1279        restrictionValidSubElementTypeMap.put(MinInclusive.class, null);
1280        restrictionValidSubElementTypeMap.put(MaxInclusive.class, null);
1281        restrictionValidSubElementTypeMap.put(FractionDigits.class, null);
1282        restrictionValidSubElementTypeMap.put(Length.class, null);
1283        restrictionValidSubElementTypeMap.put(WhiteSpace.class, null);
1284    }
1285
1286    ////////////////////////////////////////////////////////////////
1287
public class Extension extends ContainsSubElements implements HasJavaTypeName {
1288        protected String JavaDoc base;
1289        public Extension() {
1290        }
1291
1292        public Extension(String JavaDoc base) {
1293            setBase(base);
1294        }
1295
1296        public String JavaDoc getContentName() {
1297            return null;
1298        }
1299
1300        public String JavaDoc getName() {
1301            return "extension";
1302        }
1303
1304        public void setBase(String JavaDoc b) {
1305            base = normalizeDocumentNamespace(b);
1306        }
1307
1308        public String JavaDoc getBase() {
1309            return base;
1310        }
1311
1312        public String JavaDoc getJavaTypeName() {
1313            return schemaTypeToJavaType(base);
1314        }
1315
1316        public boolean writeDTDName(StringBuffer JavaDoc out) {
1317            return writeDTDSubElementNames(out, false);
1318        }
1319
1320        public String JavaDoc getAttributeString() {
1321            if (base == null)
1322                return null;
1323            return (" base='"+getBase()+"'");
1324        }
1325
1326        public void readSchema(org.w3c.dom.Element JavaDoc node) {
1327            String JavaDoc base = node.getAttribute("base"); // NOI18N
1328
Extension el = new Extension(base);
1329            pushCurrent(el);
1330            read(node);
1331            popCurrent();
1332        }
1333
1334        public Map validSubElementTypeMap() {
1335            return extensionValidSubElementTypeMap;
1336        }
1337
1338        public boolean equals(Object JavaDoc o) {
1339            if (!(o instanceof Extension))
1340                return false;
1341            Extension el = (Extension) o;
1342            if (base == null) {
1343                if (el.base != null)
1344                    return false;
1345            } else {
1346                if (!base.equals(el.base))
1347                    return false;
1348            }
1349
1350            return super.equals(el);
1351        }
1352
1353        public int hashCode() {
1354            int result = 17;
1355            result = 37*result + ((base == null) ? 0: base.hashCode());
1356            result = 37*result + super.hashCode();
1357            return result;
1358        }
1359    }
1360    static private Map extensionValidSubElementTypeMap = null;
1361    static {
1362        extensionValidSubElementTypeMap = new HashMap();
1363        extensionValidSubElementTypeMap.put(Annotation.class, null);
1364        extensionValidSubElementTypeMap.put(Attribute.class, null);
1365        extensionValidSubElementTypeMap.put(AttributeGroup.class, null);
1366        extensionValidSubElementTypeMap.put(AnyAttribute.class, null);
1367        extensionValidSubElementTypeMap.put(Choice.class, null);
1368        extensionValidSubElementTypeMap.put(Group.class, null);
1369        extensionValidSubElementTypeMap.put(Sequence.class, null);
1370        extensionValidSubElementTypeMap.put(All.class, null);
1371    }
1372
1373    ////////////////////////////////////////////////////////////////
1374
public class ListElement extends ContainsSubElements {
1375        protected String JavaDoc itemType;
1376        public ListElement() {
1377        }
1378
1379        public ListElement(String JavaDoc itemType) {
1380            this.itemType = itemType;
1381        }
1382
1383        public String JavaDoc getContentName() {
1384            return null;
1385        }
1386
1387        public String JavaDoc getName() {
1388            return "list";
1389        }
1390
1391        public void setItemType(String JavaDoc b) {
1392            itemType = b;
1393        }
1394
1395        public String JavaDoc getItemType() {
1396            return itemType;
1397        }
1398
1399        public String JavaDoc getJavaTypeName() {
1400            return schemaTypeToJavaType(itemType);
1401        }
1402
1403        public boolean writeDTDName(StringBuffer JavaDoc out) {
1404            return writeDTDSubElementNames(out, false);
1405        }
1406
1407        public String JavaDoc getAttributeString() {
1408            if (itemType == null)
1409                return null;
1410            return (" itemType='"+getItemType()+"'");
1411        }
1412
1413        public void readSchema(org.w3c.dom.Element JavaDoc node) {
1414            String JavaDoc itemType = node.getAttribute("itemType"); // NOI18N
1415
ListElement el = new ListElement(itemType);
1416            pushCurrent(el);
1417            read(node);
1418            popCurrent();
1419        }
1420
1421        public Map validSubElementTypeMap() {
1422            return listValidSubElementTypeMap;
1423        }
1424
1425        public boolean equals(Object JavaDoc o) {
1426            if (!(o instanceof ListElement))
1427                return false;
1428            ListElement el = (ListElement) o;
1429            if (itemType == null) {
1430                if (el.itemType != null)
1431                    return false;
1432            } else {
1433                if (!itemType.equals(el.itemType))
1434                    return false;
1435            }
1436
1437            return super.equals(el);
1438        }
1439
1440        public int hashCode() {
1441            int result = 17;
1442            result = 37*result + ((itemType == null) ? 0: itemType.hashCode());
1443            result = 37*result + super.hashCode();
1444            return result;
1445        }
1446    }
1447    static private Map listValidSubElementTypeMap = null;
1448    static {
1449        listValidSubElementTypeMap = new HashMap();
1450        listValidSubElementTypeMap.put(Annotation.class, null);
1451    }
1452
1453    ////////////////////////////////////////////////////////////////
1454
public abstract class RestrictionType extends ContainsSubElements {
1455        protected String JavaDoc value;
1456
1457        public RestrictionType(String JavaDoc value) {
1458            super();
1459            this.value = (value == null) ? null : value.intern();
1460        }
1461
1462        public String JavaDoc getContentName() {
1463            return null;
1464        }
1465
1466        public String JavaDoc getValue() {
1467            return value;
1468        }
1469
1470        public void writeDTD(StringBuffer JavaDoc out) {
1471            // Has no bearing on a DTD
1472
}
1473        public boolean writeDTDName(StringBuffer JavaDoc out) {
1474            // Has no bearing on a DTD
1475
return false;
1476        }
1477
1478        public void validate() {
1479        }
1480
1481        public ElementExpr optimize() {
1482            return this;
1483        }
1484
1485        public Map validSubElementTypeMap() {
1486            return restrictionTypeValidSubElementTypeMap;
1487        }
1488
1489        public void writeXMLSchema(XMLWriter out) throws IOException {
1490            out.startTag(getXSDNamespace(), getName(), false);
1491            out.write(" value='");
1492            XMLUtil.printXML(out, value);
1493            out.write("'");
1494            out.finishStartTag(false, true);
1495        }
1496
1497        public void readSchema(org.w3c.dom.Element JavaDoc node) {
1498            String JavaDoc myValue = node.getAttribute("value"); // NOI18N
1499
RestrictionType el = newInstance(myValue);
1500            pushCurrent(el);
1501            read(node);
1502            popCurrent();
1503        }
1504
1505        protected abstract RestrictionType newInstance(String JavaDoc value);
1506
1507        /*
1508        public void genRestriction(Writer out, String var, String type, String failVar) throws IOException {
1509            out.write("// FIXME "+getClass()+"\n");
1510        }
1511        */

1512
1513        public String JavaDoc toString() {
1514            return getName()+" ("+value+")";
1515        }
1516    }
1517    static private Map restrictionTypeValidSubElementTypeMap = null;
1518    static {
1519        restrictionTypeValidSubElementTypeMap = new HashMap();
1520        restrictionTypeValidSubElementTypeMap.put(Annotation.class, null);
1521    }
1522
1523    ////////////////////////////////////////////////////////////////
1524
public class MaxExclusive extends RestrictionType implements DataTypeRestriction {
1525        public MaxExclusive(String JavaDoc value) {
1526            super(value);
1527        }
1528
1529        public String JavaDoc getName() {
1530            return "maxExclusive";
1531        }
1532
1533        protected RestrictionType newInstance(String JavaDoc value) {
1534            return new MaxExclusive(value);
1535        }
1536
1537        public void genRestriction(Writer out, String JavaDoc var, String JavaDoc type, String JavaDoc failVar, boolean passCheck) throws IOException {
1538            if (!passCheck) {
1539                out.write("if ("+JavaUtil.compareToText(var, type, value)+" >= 0) {\n");
1540                out.write(failVar+" = true;\n");
1541                out.write("}\n");
1542            } else {
1543                out.write("if ("+JavaUtil.compareToText(var, type, value)+" < 0) {\n");
1544                out.write(failVar+" = true;\n");
1545                out.write("}\n");
1546            }
1547        }
1548    }
1549
1550    ////////////////////////////////////////////////////////////////
1551
public class MinExclusive extends RestrictionType implements DataTypeRestriction {
1552        public MinExclusive(String JavaDoc value) {
1553            super(value);
1554        }
1555
1556        public String JavaDoc getName() {
1557            return "minExclusive";
1558        }
1559
1560        protected RestrictionType newInstance(String JavaDoc value) {
1561            return new MinExclusive(value);
1562        }
1563
1564        public void genRestriction(Writer out, String JavaDoc var, String JavaDoc type, String JavaDoc failVar, boolean passCheck) throws IOException {
1565            if (!passCheck) {
1566                out.write("if ("+JavaUtil.compareToText(var, type, value)+" <= 0) {\n");
1567                out.write(failVar+" = true;\n");
1568                out.write("}\n");
1569            } else {
1570                out.write("if ("+JavaUtil.compareToText(var, type, value)+" > 0) {\n");
1571                out.write(failVar+" = true;\n");
1572                out.write("}\n");
1573            }
1574        }
1575    }
1576
1577    ////////////////////////////////////////////////////////////////
1578
public class Enumeration extends RestrictionType implements DataEnumRestriction {
1579        public Enumeration(String JavaDoc value) {
1580            super(value);
1581        }
1582
1583        public String JavaDoc getName() {
1584            return "enumeration";
1585        }
1586
1587        protected RestrictionType newInstance(String JavaDoc value) {
1588            return new Enumeration(value);
1589        }
1590
1591        public void genRestriction(Writer out, String JavaDoc type) throws IOException {
1592            out.write(JavaUtil.instanceFrom(type, value));
1593        }
1594    }
1595
1596    ////////////////////////////////////////////////////////////////
1597
public class Pattern extends RestrictionType implements DataTypeRestriction {
1598        public Pattern(String JavaDoc value) {
1599            super(value);
1600        }
1601
1602        public String JavaDoc getName() {
1603            return "pattern";
1604        }
1605
1606        protected RestrictionType newInstance(String JavaDoc value) {
1607            return new Pattern(value);
1608        }
1609
1610        public void genRestriction(Writer out, String JavaDoc var, String JavaDoc type, String JavaDoc failVar, boolean passCheck) throws IOException {
1611            if (!passCheck) {
1612                out.write("if (!("+JavaUtil.typeToString(type, var)+").matches("+JavaUtil.instanceFrom("java.lang.String", value)+")) {\n");
1613                out.write(failVar+" = true;\n");
1614                out.write("}\n");
1615            } else {
1616                out.write("if (("+JavaUtil.typeToString(type, var)+").matches("+JavaUtil.instanceFrom("java.lang.String", value)+")) {\n");
1617                out.write(failVar+" = true;\n");
1618                out.write("}\n");
1619            }
1620        }
1621    }
1622
1623    ////////////////////////////////////////////////////////////////
1624
public class MinLength extends RestrictionType implements DataTypeRestriction {
1625        public MinLength(String JavaDoc value) {
1626            super(value);
1627        }
1628
1629        public String JavaDoc getName() {
1630            return "minLength";
1631        }
1632
1633        protected RestrictionType newInstance(String JavaDoc value) {
1634            return new MinLength(value);
1635        }
1636
1637        public void genRestriction(Writer out, String JavaDoc var, String JavaDoc type, String JavaDoc failVar, boolean passCheck) throws IOException {
1638            if (!passCheck) {
1639                out.write("if (("+JavaUtil.typeToString(type, var)+").length() < "+value+") {\n");
1640                out.write(failVar+" = true;\n");
1641                out.write("}\n");
1642            } else {
1643                out.write("if (("+JavaUtil.typeToString(type, var)+").length() >= "+value+") {\n");
1644                out.write(failVar+" = true;\n");
1645                out.write("}\n");
1646            }
1647        }
1648    }
1649
1650    ////////////////////////////////////////////////////////////////
1651
public class MaxLength extends RestrictionType implements DataTypeRestriction {
1652        public MaxLength(String JavaDoc value) {
1653            super(value);
1654        }
1655
1656        public String JavaDoc getName() {
1657            return "maxLength";
1658        }
1659
1660        protected RestrictionType newInstance(String JavaDoc value) {
1661            return new MaxLength(value);
1662        }
1663
1664        public void genRestriction(Writer out, String JavaDoc var, String JavaDoc type, String JavaDoc failVar, boolean passCheck) throws IOException {
1665            if (!passCheck) {
1666                out.write("if (("+JavaUtil.typeToString(type, var)+").length() > "+value+") {\n");
1667                out.write(failVar+" = true;\n");
1668                out.write("}\n");
1669            } else {
1670                out.write("if (("+JavaUtil.typeToString(type, var)+").length() <= "+value+") {\n");
1671                out.write(failVar+" = true;\n");
1672                out.write("}\n");
1673            }
1674        }
1675    }
1676
1677    ////////////////////////////////////////////////////////////////
1678
public class TotalDigits extends RestrictionType implements DataTypeRestriction {
1679        public TotalDigits(String JavaDoc value) {
1680            super(value);
1681        }
1682
1683        public String JavaDoc getName() {
1684            return "totalDigits";
1685        }
1686
1687        protected RestrictionType newInstance(String JavaDoc value) {
1688            return new TotalDigits(value);
1689        }
1690
1691        public void genRestriction(Writer out, String JavaDoc var, String JavaDoc type, String JavaDoc failVar, boolean passCheck) throws IOException {
1692            if (!passCheck) {
1693                out.write("{\n");
1694                out.write("String _tmp = "+JavaUtil.typeToString(type, var)+";\n");
1695                out.write("int digitCount = 0;\n");
1696                out.write("for (int _index1 = 0; _index1 < _tmp.length(); ++_index1) {\n");
1697                out.write("if (Character.isDigit(_tmp.charAt(_index1))) {\n");
1698                out.write("++digitCount;\n");
1699                out.write("if (digitCount > "+value+") {\n");
1700                out.write(failVar+" = true;\n");
1701                out.write("break;\n");
1702                out.write("}\n");
1703                out.write("}\n");
1704                out.write("}\n");
1705                out.write("}\n");
1706            } else {
1707                out.write("{\n");
1708                out.write("String _tmp = "+JavaUtil.typeToString(type, var)+";\n");
1709                out.write("int digitCount = 0;\n");
1710                out.write("for (int _index1 = 0; _index1 < _tmp.length(); ++_index1) {\n");
1711                out.write("if (Character.isDigit(_tmp.charAt(_index1))) {\n");
1712                out.write("++digitCount;\n");
1713                out.write("}\n");
1714                out.write("}\n");
1715                out.write("if (digitCount <= "+value+") {\n");
1716                out.write(failVar+" = true;\n");
1717                out.write("}\n");
1718                out.write("}\n");
1719            }
1720        }
1721    }
1722
1723    ////////////////////////////////////////////////////////////////
1724
public class MinInclusive extends RestrictionType implements DataTypeRestriction {
1725        public MinInclusive(String JavaDoc value) {
1726            super(value);
1727        }
1728
1729        public String JavaDoc getName() {
1730            return "minInclusive";
1731        }
1732
1733        protected RestrictionType newInstance(String JavaDoc value) {
1734            return new MinInclusive(value);
1735        }
1736
1737        public void genRestriction(Writer out, String JavaDoc var, String JavaDoc type, String JavaDoc failVar, boolean passCheck) throws IOException {
1738            if (!passCheck) {
1739                out.write("if ("+JavaUtil.compareToText(var, type, value)+" < 0) {\n");
1740                out.write(failVar+" = true;\n");
1741                out.write("}\n");
1742            } else {
1743                out.write("if ("+JavaUtil.compareToText(var, type, value)+" >= 0) {\n");
1744                out.write(failVar+" = true;\n");
1745                out.write("}\n");
1746            }
1747        }
1748    }
1749
1750    ////////////////////////////////////////////////////////////////
1751
public class MaxInclusive extends RestrictionType implements DataTypeRestriction {
1752        public MaxInclusive(String JavaDoc value) {
1753            super(value);
1754        }
1755
1756        public String JavaDoc getName() {
1757            return "maxInclusive";
1758        }
1759
1760        protected RestrictionType newInstance(String JavaDoc value) {
1761            return new MaxInclusive(value);
1762        }
1763
1764        public void genRestriction(Writer out, String JavaDoc var, String JavaDoc type, String JavaDoc failVar, boolean passCheck) throws IOException {
1765            if (!passCheck) {
1766                out.write("if ("+JavaUtil.compareToText(var, type, value)+" > 0) {\n");
1767                out.write(failVar+" = true;\n");
1768                out.write("}\n");
1769            } else {
1770                out.write("if ("+JavaUtil.compareToText(var, type, value)+" <= 0) {\n");
1771                out.write(failVar+" = true;\n");
1772                out.write("}\n");
1773            }
1774        }
1775    }
1776
1777    ////////////////////////////////////////////////////////////////
1778
public class FractionDigits extends RestrictionType implements DataTypeRestriction {
1779        public FractionDigits(String JavaDoc value) {
1780            super(value);
1781        }
1782
1783        public String JavaDoc getName() {
1784            return "fractionDigits";
1785        }
1786
1787        protected RestrictionType newInstance(String JavaDoc value) {
1788            return new FractionDigits(value);
1789        }
1790
1791        public void genRestriction(Writer out, String JavaDoc var, String JavaDoc type, String JavaDoc failVar, boolean passCheck) throws IOException {
1792            if (!passCheck) {
1793                out.write("{\n");
1794                out.write("String _tmp = "+JavaUtil.typeToString(type, var)+";\n");
1795                out.write("int dotPos = _tmp.indexOf('.');\n");
1796                out.write("if (dotPos >= 0) {\n");
1797                out.write("_tmp = _tmp.substring(dotPos+1, _tmp.length());\n");
1798                out.write("int digitCount = 0;\n");
1799                out.write("for (int _index1 = 0; _index1 < _tmp.length(); ++_index1) {\n");
1800                out.write("if (Character.isDigit(_tmp.charAt(_index1))) {\n");
1801                out.write("++digitCount;\n");
1802                out.write("if (digitCount > "+value+") {\n");
1803                out.write(failVar+" = true;\n");
1804                out.write("break;\n");
1805                out.write("}\n");
1806                out.write("}\n");
1807                out.write("}\n");
1808                out.write("}\n");
1809                out.write("}\n");
1810            } else {
1811                out.write("{\n");
1812                out.write("String _tmp = "+JavaUtil.typeToString(type, var)+";\n");
1813                out.write("int dotPos = _tmp.indexOf('.');\n");
1814                out.write("if (dotPos >= 0) {\n");
1815                out.write("_tmp = _tmp.substring(dotPos+1, _tmp.length());\n");
1816                out.write("int digitCount = 0;\n");
1817                out.write("for (int _index1 = 0; _index1 < _tmp.length(); ++_index1) {\n");
1818                out.write("if (Character.isDigit(_tmp.charAt(_index1))) {\n");
1819                out.write("++digitCount;\n");
1820                out.write("}\n");
1821                out.write("}\n");
1822                out.write("if (digitCount <= "+value+") {\n");
1823                out.write(failVar+" = true;\n");
1824                out.write("}\n");
1825                out.write("}\n");
1826                out.write("else\n");
1827                out.write(failVar+" = true;\n");
1828                out.write("}\n");
1829            }
1830        }
1831    }
1832
1833    ////////////////////////////////////////////////////////////////
1834
public class Length extends RestrictionType implements DataTypeRestriction {
1835        public Length(String JavaDoc value) {
1836            super(value);
1837        }
1838
1839        public String JavaDoc getName() {
1840            return "length";
1841        }
1842
1843        protected RestrictionType newInstance(String JavaDoc value) {
1844            return new Length(value);
1845        }
1846
1847        public void genRestriction(Writer out, String JavaDoc var, String JavaDoc type, String JavaDoc failVar, boolean passCheck) throws IOException {
1848            if (!passCheck) {
1849                out.write("if ("+JavaUtil.typeToString(type, var)+".length() != "+value+") {\n");
1850                out.write(failVar+" = true;\n");
1851                out.write("}\n");
1852            } else {
1853                out.write("if ("+JavaUtil.typeToString(type, var)+".length() == "+value+") {\n");
1854                out.write(failVar+" = true;\n");
1855                out.write("}\n");
1856            }
1857        }
1858    }
1859
1860    ////////////////////////////////////////////////////////////////
1861
public class WhiteSpace extends RestrictionType implements DataTypeRestriction {
1862        public WhiteSpace(String JavaDoc value) {
1863            super(value);
1864        }
1865
1866        public String JavaDoc getName() {
1867            return "whiteSpace";
1868        }
1869
1870        public boolean isPreserve() {
1871            return "preserve" == value;
1872        }
1873
1874        public boolean isReplace() {
1875            return "replace" == value;
1876        }
1877
1878        public boolean isCollapse() {
1879            return "collapse" == value;
1880        }
1881
1882        public void validate() {
1883            super.validate();
1884            if (value == null || value.equals(""))
1885                throw new IllegalStateException JavaDoc(Common.getMessage("MSG_InvalidWhiteSpaceValue", value));
1886            if (!(value == "preserve" || value == "replace" || value == "collapse"))
1887                throw new IllegalStateException JavaDoc(Common.getMessage("MSG_InvalidWhiteSpaceValue", value));
1888        }
1889
1890        protected RestrictionType newInstance(String JavaDoc value) {
1891            return new WhiteSpace(value);
1892        }
1893
1894        public void genRestriction(Writer out, String JavaDoc var, String JavaDoc type, String JavaDoc failVar, boolean passCheck) throws IOException {
1895            // Does not cause verify changes
1896
out.write("// has whitespace restriction\n");
1897        }
1898    }
1899
1900    ////////////////////////////////////////////////////////////////
1901
// the parent class for some others
1902
public abstract class ElementInformationItem extends ContainsSubElements {
1903        private String JavaDoc id;
1904        private String JavaDoc name;
1905
1906        public ElementInformationItem() {
1907        }
1908
1909        public ElementInformationItem(String JavaDoc id, String JavaDoc name) {
1910            this.id = id;
1911            this.name = name;
1912        }
1913
1914        public String JavaDoc getContentName() {
1915            return name;
1916        }
1917
1918        public String JavaDoc getId() {
1919            return id;
1920        }
1921
1922        public void setId(String JavaDoc id) {
1923            this.id = id;
1924        }
1925
1926        public String JavaDoc getElementName() {
1927            return name;
1928        }
1929
1930        public void setElementName(String JavaDoc name) {
1931            this.name = name;
1932        }
1933
1934        public boolean writeDTDName(StringBuffer JavaDoc out) {
1935            boolean hasNamedSubElements = writeDTDSubElementNames(out, true);
1936            return hasNamedSubElements;
1937        }
1938
1939        public String JavaDoc getAttributeString() {
1940            StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1941            if (name != null)
1942                sb.append(" name='"+name+"'");
1943            if (id != null)
1944                sb.append(" id='"+id+"'");
1945            return sb.toString();
1946        }
1947
1948        public void readSchema(org.w3c.dom.Element JavaDoc node) {
1949            String JavaDoc id = node.getAttribute("id"); // NOI18N
1950
String JavaDoc name = node.getAttribute("name"); // NOI18N
1951
ElementInformationItem el = newInstance();
1952            pushCurrent(el);
1953            if (id != null && !id.equals(""))
1954                el.setId(id);
1955            if (name != null && !name.equals(""))
1956                el.setElementName(name);
1957            read(node);
1958            popCurrent();
1959        }
1960
1961        protected abstract ElementInformationItem newInstance();
1962
1963        public boolean equals(Object JavaDoc o) {
1964            if (!(o instanceof ElementInformationItem))
1965                return false;
1966            ElementInformationItem el = (ElementInformationItem) o;
1967            if (id != el.id)
1968                return false;
1969            if (name != el.name)
1970                return false;
1971
1972            return super.equals(el);
1973        }
1974
1975        public int hashCode() {
1976            int result = 17;
1977            result = 37*result + ((id == null) ? 0 : id.hashCode());
1978            result = 37*result + ((name == null) ? 0 : name.hashCode());
1979            result = 37*result + super.hashCode();
1980            return result;
1981        }
1982    }
1983
1984    ////////////////////////////////////////////////////////////////
1985
public class Key extends ElementInformationItem {
1986        public Key() {
1987        }
1988
1989        public String JavaDoc getName() {
1990            return "key";
1991        }
1992
1993        public Map validSubElementTypeMap() {
1994            return keyValidSubElementTypeMap;
1995        }
1996
1997        protected ElementInformationItem newInstance() {
1998            return new Key();
1999        }
2000    }
2001    static private Map keyValidSubElementTypeMap = null;
2002    static {
2003        keyValidSubElementTypeMap = new HashMap();
2004        keyValidSubElementTypeMap.put(Annotation.class, null);
2005        keyValidSubElementTypeMap.put(Selector.class, null);
2006        keyValidSubElementTypeMap.put(Field.class, null);
2007    }
2008
2009    ////////////////////////////////////////////////////////////////
2010
public class Unique extends ElementInformationItem {
2011        public Unique() {
2012        }
2013
2014        public String JavaDoc getName() {
2015            return "unique";
2016        }
2017
2018        public Map validSubElementTypeMap() {
2019            return uniqueValidSubElementTypeMap;
2020        }
2021
2022        protected ElementInformationItem newInstance() {
2023            return new Unique();
2024        }
2025    }
2026    static private Map uniqueValidSubElementTypeMap = null;
2027    static {
2028        uniqueValidSubElementTypeMap = new HashMap();
2029        uniqueValidSubElementTypeMap.put(Annotation.class, null);
2030        uniqueValidSubElementTypeMap.put(Selector.class, null);
2031        uniqueValidSubElementTypeMap.put(Field.class, null);
2032    }
2033
2034    ////////////////////////////////////////////////////////////////
2035
public class KeyRef extends ElementInformationItem {
2036        private String JavaDoc refer;
2037
2038        public KeyRef() {
2039        }
2040
2041        public String JavaDoc getName() {
2042            return "keyref";
2043        }
2044
2045        public void setRefer(String JavaDoc refer) {
2046            this.refer = refer;
2047        }
2048
2049        public String JavaDoc getRefer() {
2050            return refer;
2051        }
2052
2053        public String JavaDoc getAttributeString() {
2054            StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.getAttributeString());
2055            if (refer != null)
2056                sb.append(" refer='"+refer+"'");
2057            return sb.toString();
2058        }
2059
2060        public void readSchema(org.w3c.dom.Element JavaDoc node) {
2061            String JavaDoc id = node.getAttribute("id"); // NOI18N
2062
String JavaDoc name = node.getAttribute("name"); // NOI18N
2063
String JavaDoc refer = node.getAttribute("refer"); // NOI18N
2064
KeyRef el = new KeyRef();
2065            pushCurrent(el);
2066            if (id != null && !id.equals(""))
2067                el.setId(id);
2068            if (name != null && !name.equals(""))
2069                el.setElementName(name);
2070            if (refer != null && !refer.equals(""))
2071                el.setRefer(refer);
2072            read(node);
2073            popCurrent();
2074        }
2075
2076        public Map validSubElementTypeMap() {
2077            return keyrefValidSubElementTypeMap;
2078        }
2079
2080        protected ElementInformationItem newInstance() {
2081            return new KeyRef();
2082        }
2083    }
2084    static private Map keyrefValidSubElementTypeMap = null;
2085    static {
2086        keyrefValidSubElementTypeMap = new HashMap();
2087        keyrefValidSubElementTypeMap.put(Annotation.class, null);
2088        keyrefValidSubElementTypeMap.put(Selector.class, null);
2089        keyrefValidSubElementTypeMap.put(Field.class, null);
2090    }
2091
2092    ////////////////////////////////////////////////////////////////
2093
public abstract class SelectorOrField extends ContainsSubElements {
2094        private String JavaDoc id;
2095        private String JavaDoc xpath;
2096
2097        public SelectorOrField() {
2098        }
2099
2100        public String JavaDoc getContentName() {
2101            return null;
2102        }
2103
2104        public String JavaDoc getId() {
2105            return id;
2106        }
2107
2108        public void setId(String JavaDoc id) {
2109            this.id = id;
2110        }
2111
2112        public String JavaDoc getXPath() {
2113            return xpath;
2114        }
2115
2116        public void setXPath(String JavaDoc xpath) {
2117            this.xpath = xpath;
2118        }
2119
2120        public String JavaDoc getAttributeString() {
2121            StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
2122            if (xpath != null)
2123                sb.append(" xpath='"+xpath+"'");
2124            if (id != null)
2125                sb.append(" id='"+id+"'");
2126            return sb.toString();
2127        }
2128
2129        public void readSchema(org.w3c.dom.Element JavaDoc node) {
2130            String JavaDoc id = node.getAttribute("id"); // NOI18N
2131
String JavaDoc xpath = node.getAttribute("xpath"); // NOI18N
2132
SelectorOrField el = newInstance();
2133            pushCurrent(el);
2134            if (id != null && !id.equals(""))
2135                el.setId(id);
2136            if (xpath != null && !xpath.equals(""))
2137                el.setXPath(xpath);
2138            read(node);
2139            popCurrent();
2140        }
2141
2142        protected abstract SelectorOrField newInstance();
2143
2144        public void writeDTD(StringBuffer JavaDoc out) {
2145            // ?
2146
}
2147
2148        public boolean writeDTDName(StringBuffer JavaDoc out) {
2149            // ?
2150
return false;
2151        }
2152
2153        public boolean equals(Object JavaDoc o) {
2154            if (!(o instanceof SelectorOrField))
2155                return false;
2156            SelectorOrField el = (SelectorOrField) o;
2157            if (id == null) {
2158                if (el.id != null)
2159                    return false;
2160            } else if (id != el.id)
2161                return false;
2162            if (xpath == null) {
2163                if (el.xpath != null)
2164                    return false;
2165            } else if (xpath != el.xpath)
2166                return false;
2167
2168            return super.equals(el);
2169        }
2170
2171        public int hashCode() {
2172            int result = 17;
2173            result = 37*result + ((id == null) ? 0 : id.hashCode());
2174            result = 37*result + ((xpath == null) ? 0 : xpath.hashCode());
2175            result = 37*result + super.hashCode();
2176            return result;
2177        }
2178    }
2179
2180    ////////////////////////////////////////////////////////////////
2181
public class Selector extends SelectorOrField {
2182        public Selector() {
2183        }
2184
2185        public String JavaDoc getName() {
2186            return "selector";
2187        }
2188
2189        public Map validSubElementTypeMap() {
2190            return selectorValidSubElementTypeMap;
2191        }
2192
2193        protected SelectorOrField newInstance() {
2194            return new Selector();
2195        }
2196    }
2197    static private Map selectorValidSubElementTypeMap = null;
2198    static {
2199        selectorValidSubElementTypeMap = new HashMap();
2200        selectorValidSubElementTypeMap.put(Annotation.class, null);
2201    }
2202
2203    public class Field extends SelectorOrField {
2204        public Field() {
2205        }
2206
2207        public String JavaDoc getName() {
2208            return "field";
2209        }
2210
2211        public Map validSubElementTypeMap() {
2212            return fieldValidSubElementTypeMap;
2213        }
2214
2215        protected SelectorOrField newInstance() {
2216            return new Field();
2217        }
2218    }
2219    static private Map fieldValidSubElementTypeMap = null;
2220    static {
2221        fieldValidSubElementTypeMap = new HashMap();
2222        fieldValidSubElementTypeMap.put(Annotation.class, null);
2223    }
2224
2225    ////////////////////////////////////////////////////////////////
2226
public class Include extends ElementExpr {
2227        private String JavaDoc schemaLocation;
2228
2229        public Include(String JavaDoc schemaLocation) {
2230            this.schemaLocation = schemaLocation;
2231        }
2232
2233        public String JavaDoc getContentName() {
2234            return null;
2235        }
2236
2237        public void readSchema(org.w3c.dom.Element JavaDoc node) {
2238            String JavaDoc schemaLocation = node.getAttribute("schemaLocation"); // NOI18N
2239
if (includedAlready.containsKey(schemaLocation))
2240                return;
2241            includedAlready.put(schemaLocation, null);
2242
2243            //System.out.println("Attempting to include "+schemaLocation);
2244
ParserSchemaState oldState = new ParserSchemaState();
2245            try {
2246                readSchemaFromLocation(schemaLocation);
2247            } catch (org.xml.sax.SAXException JavaDoc e) {
2248                throw new Schema2BeansRuntimeException(Common.getMessage("MSG_FailedToParse", schemaLocation), e);
2249            } catch (java.io.IOException JavaDoc e) {
2250                throw new Schema2BeansRuntimeException(Common.getMessage("MSG_FailedToParse", schemaLocation), e);
2251            } finally {
2252                oldState.reload();
2253            }
2254            //System.out.println("Finished reading include\n");
2255
/*
2256              if (oldNamespaceTable != null)
2257              namespaceTable = oldNamespaceTable;
2258            */

2259        }
2260
2261        public ElementExpr optimize() {
2262            return this;
2263        }
2264
2265        public void validate() {
2266        }
2267
2268        public String JavaDoc getName() {
2269            return "include";
2270        }
2271
2272        public void writeDTD(StringBuffer JavaDoc out) {
2273            // ?
2274
}
2275
2276        public boolean writeDTDName(StringBuffer JavaDoc out) {
2277            // ?
2278
return false;
2279        }
2280
2281        public void writeXMLSchema(XMLWriter out) throws IOException {
2282            out.startTag(getXSDNamespace(), getName(), false);
2283            if (schemaLocation != null) {
2284                out.write(" schemaLocation='");
2285                out.write(schemaLocation);
2286                out.write("'");
2287            }
2288            out.finishStartTag(false, true);
2289        }
2290
2291        public String JavaDoc toString() {
2292            if (schemaLocation == null)
2293                return getName();
2294            else
2295                return getName()+" schemaLocation="+schemaLocation;
2296        }
2297
2298        public boolean equals(Object JavaDoc o) {
2299            if (!(o instanceof Include))
2300                return false;
2301            Include el = (Include) o;
2302            if (schemaLocation == null) {
2303                if (el.schemaLocation != null)
2304                    return false;
2305            } else if (!schemaLocation.equals(el.schemaLocation))
2306                return false;
2307
2308            return super.equals(el);
2309        }
2310
2311        public int hashCode() {
2312            int result = 17;
2313            result = 37*result + ((schemaLocation == null) ? 0 : schemaLocation.hashCode());
2314            result = 37*result + super.hashCode();
2315            return result;
2316        }
2317    }
2318
2319    ////////////////////////////////////////////////////////////////
2320
public class Import extends ElementExpr {
2321        private String JavaDoc theNamespace;
2322        private String JavaDoc schemaLocation;
2323
2324        public Import() {
2325        }
2326
2327        public String JavaDoc getContentName() {
2328            return null;
2329        }
2330
2331        public String JavaDoc getNamespace() {
2332            return theNamespace;
2333        }
2334
2335        public void setNamespace(String JavaDoc namespace) {
2336            theNamespace = namespace;
2337        }
2338
2339        public String JavaDoc getSchemaLocation() {
2340            return schemaLocation;
2341        }
2342
2343        public void setSchemaLocation(String JavaDoc schemaLocation) {
2344            this.schemaLocation = schemaLocation;
2345        }
2346
2347        public void readSchema(org.w3c.dom.Element JavaDoc node) {
2348            String JavaDoc namespace = node.getAttribute("namespace"); // NOI18N
2349
String JavaDoc schemaLocation = node.getAttribute("schemaLocation"); // NOI18N
2350
//System.out.println("Attempting to import "+schemaLocation);
2351
ParserSchemaState oldState = new ParserSchemaState();
2352            if (namespace != null && !namespace.equals(""))
2353                targetNamespace = namespace;
2354            try {
2355                readSchemaFromLocation(schemaLocation);
2356            } catch (org.xml.sax.SAXException JavaDoc e) {
2357                throw new Schema2BeansRuntimeException(Common.getMessage("MSG_FailedToParse", schemaLocation), e);
2358            } catch (java.io.IOException JavaDoc e) {
2359                throw new Schema2BeansRuntimeException(Common.getMessage("MSG_FailedToParse", schemaLocation), e);
2360            } finally {
2361                oldState.reload();
2362            }
2363            //System.out.println("Finished reading import\n");
2364
}
2365
2366        public ElementExpr optimize() {
2367            return this;
2368        }
2369
2370        public void validate() {
2371        }
2372
2373        public String JavaDoc getName() {
2374            return "import";
2375        }
2376
2377        public void writeDTD(StringBuffer JavaDoc out) {
2378            // ?
2379
}
2380
2381        public boolean writeDTDName(StringBuffer JavaDoc out) {
2382            // ?
2383
return false;
2384        }
2385
2386        public void writeXMLSchema(XMLWriter out) throws IOException {
2387            out.startTag(getXSDNamespace(), getName(), false);
2388            if (theNamespace != null) {
2389                out.write(" theNamespace='");
2390                out.write(theNamespace);
2391                out.write("'");
2392            }
2393            if (schemaLocation != null) {
2394                out.write(" schemaLocation='");
2395                out.write(schemaLocation);
2396                out.write("'");
2397            }
2398            out.finishStartTag(false, true);
2399        }
2400
2401        public boolean equals(Object JavaDoc o) {
2402            if (!(o instanceof Import))
2403                return false;
2404            Import el = (Import) o;
2405            if (theNamespace == null) {
2406                if (el.theNamespace != null)
2407                    return false;
2408            } else if (!theNamespace.equals(el.theNamespace))
2409                return false;
2410            if (schemaLocation == null) {
2411                if (el.schemaLocation != null)
2412                    return false;
2413            } else if (!schemaLocation.equals(el.schemaLocation))
2414                return false;
2415
2416            return super.equals(el);
2417        }
2418
2419        public int hashCode() {
2420            int result = 17;
2421            result = 37*result + ((theNamespace == null) ? 0 : theNamespace.hashCode());
2422            result = 37*result + ((schemaLocation == null) ? 0 : schemaLocation.hashCode());
2423            result = 37*result + super.hashCode();
2424            return result;
2425        }
2426    }
2427
2428    ////////////////////////////////////////////////////////////////
2429
// See XML Schema sequence.
2430
public abstract class ModelGroup extends ContainsSubElements implements MinMaxOccurs {
2431        private String JavaDoc minOccurs;
2432        private String JavaDoc maxOccurs;
2433
2434        public ModelGroup() {
2435            super();
2436            minOccurs = "1";
2437            maxOccurs = "1";
2438        }
2439
2440        public String JavaDoc getContentName() {
2441            return null;
2442        }
2443
2444        public void setMinOccurs(String JavaDoc mino) {
2445            if (mino == null)
2446                mino = "1";
2447            minOccurs = mino.intern();
2448        }
2449
2450        public void setMaxOccurs(String JavaDoc maxo) {
2451            if (maxo == null)
2452                maxo = "1";
2453            maxOccurs = maxo.intern();
2454        }
2455
2456        public String JavaDoc getMinOccurs() {
2457            return minOccurs;
2458        }
2459
2460        public String JavaDoc getMaxOccurs() {
2461            return maxOccurs;
2462        }
2463
2464        /**
2465         * If we have no subelements, we ask to be deleted.
2466         * If we have only 1 element and attributes haven't been set, then
2467         * we replace ourselves with that element.
2468         */

2469        public ElementExpr optimize() {
2470            super.optimize();
2471            if (subElements.size() == 0)
2472                return null;
2473            /*
2474            if (subElements.size() == 1 && minOccurs == "1" && maxOccurs == "1")
2475                return (ElementExpr) subElements.iterator().next();
2476            */

2477            return this;
2478        }
2479
2480        public boolean writeDTDName(StringBuffer JavaDoc out) {
2481            boolean many = ("unbounded" == maxOccurs);
2482            boolean hasNamedSubElements = writeDTDSubElementNames(out, true);
2483            if (many)
2484                out.append("*");
2485            return hasNamedSubElements;
2486        }
2487
2488        public String JavaDoc getAttributeString() {
2489            StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
2490            if (minOccurs != "1")
2491                sb.append(" minOccurs='"+minOccurs+"'");
2492            if (maxOccurs != "1")
2493                sb.append(" maxOccurs='"+maxOccurs+"'");
2494            return sb.toString();
2495        }
2496
2497        public void readSchema(org.w3c.dom.Element JavaDoc node) {
2498            String JavaDoc minOccurs = node.getAttribute("minOccurs"); // NOI18N
2499
String JavaDoc maxOccurs = node.getAttribute("maxOccurs"); // NOI18N
2500
ModelGroup el = newInstance();
2501            pushCurrent(el);
2502            if (minOccurs != null && !minOccurs.equals(""))
2503                el.setMinOccurs(minOccurs);
2504            if (maxOccurs != null && !maxOccurs.equals(""))
2505                el.setMaxOccurs(maxOccurs);
2506            read(node);
2507            popCurrent();
2508        }
2509
2510        protected abstract ModelGroup newInstance();
2511
2512        public boolean equals(Object JavaDoc o) {
2513            if (!(o instanceof ModelGroup))
2514                return false;
2515            ModelGroup el = (ModelGroup) o;
2516            if (minOccurs != el.minOccurs)
2517                return false;
2518            if (maxOccurs != el.maxOccurs)
2519                return false;
2520
2521            return super.equals(el);
2522        }
2523
2524        public int hashCode() {
2525            int result = 17;
2526            result = 37*result + ((minOccurs == null) ? 0 : minOccurs.hashCode());
2527            result = 37*result + ((maxOccurs == null) ? 0 : maxOccurs.hashCode());
2528            result = 37*result + super.hashCode();
2529            return result;
2530        }
2531    }
2532
2533    ////////////////////////////////////////////////////////////////
2534
// See XML Schema sequence.
2535
public class Sequence extends ModelGroup {
2536        public Sequence() {
2537        }
2538
2539        public String JavaDoc getName() {
2540            return "sequence";
2541        }
2542
2543        public Map validSubElementTypeMap() {
2544            return sequenceValidSubElementTypeMap;
2545        }
2546
2547        protected ModelGroup newInstance() {
2548            return new Sequence();
2549        }
2550    }
2551    static private Map sequenceValidSubElementTypeMap = null;
2552    static {
2553        sequenceValidSubElementTypeMap = new HashMap();
2554        sequenceValidSubElementTypeMap.put(Annotation.class, null);
2555        sequenceValidSubElementTypeMap.put(Element.class, null);
2556        sequenceValidSubElementTypeMap.put(Any.class, null);
2557        sequenceValidSubElementTypeMap.put(Choice.class, null);
2558        sequenceValidSubElementTypeMap.put(Sequence.class, null);
2559        sequenceValidSubElementTypeMap.put(Group.class, null);
2560    }
2561
2562    ////////////////////////////////////////////////////////////////
2563
public class Choice extends ModelGroup {
2564        public Choice() {
2565        }
2566
2567        public String JavaDoc getName() {
2568            return "choice";
2569        }
2570
2571        public Map validSubElementTypeMap() {
2572            return choiceValidSubElementTypeMap;
2573        }
2574
2575        protected ModelGroup newInstance() {
2576            return new Choice();
2577        }
2578    }
2579    static private Map choiceValidSubElementTypeMap = null;
2580    static {
2581        choiceValidSubElementTypeMap = new HashMap();
2582        choiceValidSubElementTypeMap.put(Annotation.class, null);
2583        choiceValidSubElementTypeMap.put(Element.class, null);
2584        choiceValidSubElementTypeMap.put(Any.class, null);
2585        choiceValidSubElementTypeMap.put(Choice.class, null);
2586        choiceValidSubElementTypeMap.put(Sequence.class, null);
2587        choiceValidSubElementTypeMap.put(Group.class, null);
2588    }
2589
2590    ////////////////////////////////////////////////////////////////
2591
public class All extends ModelGroup {
2592        public All() {
2593        }
2594
2595        public String JavaDoc getName() {
2596            return "all";
2597        }
2598
2599        public Map validSubElementTypeMap() {
2600            return allValidSubElementTypeMap;
2601        }
2602
2603        protected ModelGroup newInstance() {
2604            return new All();
2605        }
2606    }
2607    static private Map allValidSubElementTypeMap = null;
2608    static {
2609        allValidSubElementTypeMap = new HashMap();
2610        allValidSubElementTypeMap.put(Annotation.class, null);
2611        allValidSubElementTypeMap.put(Element.class, null);
2612        allValidSubElementTypeMap.put(Any.class, null);
2613    }
2614
2615    ////////////////////////////////////////////////////////////////
2616
public class Group extends ModelGroup implements CanRef {
2617        private String JavaDoc name;
2618        private String JavaDoc ref;
2619
2620        public Group() {
2621        }
2622
2623        public Group(String JavaDoc n) {
2624            if (n != null && !n.equals("")) {
2625                name = normalizeTargetNamespace(n).intern();
2626                putSchemaTypeDef(name, this);
2627            }
2628        }
2629
2630        public ElementExpr optimize() {
2631            if (ref != null)
2632                return this;
2633            return super.optimize();
2634        }
2635
2636        public String JavaDoc getName() {
2637            return "group";
2638        }
2639
2640        public String JavaDoc getGroupName() {
2641            return name;
2642        }
2643
2644        public boolean hasRef() {
2645            return getRef() != null;
2646        }
2647
2648        /**
2649         * May return null.
2650         */

2651        public String JavaDoc getRef() {
2652            return ref;
2653        }
2654
2655        public Group getRefGroup() {
2656            if (ref == null)
2657                return null;
2658            Object JavaDoc o = getSchemaTypeDef(ref);
2659            if (o instanceof Group) {
2660                Group referredGroup = (Group) o;
2661                return referredGroup;
2662            } else {
2663                throw new IllegalStateException JavaDoc(Common.getMessage("MSG_FailedToFindRef",
2664                                                                  ref,
2665                                                                  this.toString()));
2666            }
2667        }
2668
2669        public ElementExpr getRefElementExpr() {
2670            return getRefGroup();
2671        }
2672
2673        public Map validSubElementTypeMap() {
2674            return groupValidSubElementTypeMap;
2675        }
2676
2677        public String JavaDoc getAttributeString() {
2678            StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
2679            if (name != null)
2680                sb.append(" name='"+name+"'");
2681            if (ref != null)
2682                sb.append(" ref='"+ref+"'");
2683            return sb.toString();
2684        }
2685
2686        public void readSchema(org.w3c.dom.Element JavaDoc node) {
2687            String JavaDoc myName = node.getAttribute("name"); // NOI18N
2688
String JavaDoc myRef = node.getAttribute("ref"); // NOI18N
2689
Group g = new Group(myName);
2690            if (myRef != null && !myRef.equals(""))
2691                g.ref = myRef.intern();
2692            pushCurrent(g);
2693            read(node);
2694            popCurrent();
2695        }
2696
2697        protected ModelGroup newInstance() {
2698            return new Group();
2699        }
2700
2701        public boolean equals(Object JavaDoc o) {
2702            if (!(o instanceof AttributeGroup))
2703                return false;
2704            AttributeGroup el = (AttributeGroup) o;
2705            if (name != el.name)
2706                return false;
2707            if (ref != el.ref)
2708                return false;
2709
2710            return super.equals(el);
2711        }
2712
2713        public int hashCode() {
2714            int result = 17;
2715            result = 37*result + ((name == null) ? 0 : name.hashCode());
2716            result = 37*result + ((ref == null) ? 0 : ref.hashCode());
2717            result = 37*result + super.hashCode();
2718            return result;
2719        }
2720    }
2721    static private Map groupValidSubElementTypeMap = null;
2722    static {
2723        groupValidSubElementTypeMap = new HashMap();
2724        groupValidSubElementTypeMap.put(All.class, null);
2725        groupValidSubElementTypeMap.put(Choice.class, null);
2726        groupValidSubElementTypeMap.put(Sequence.class, null);
2727        groupValidSubElementTypeMap.put(Annotation.class, null);
2728    }
2729
2730    ////////////////////////////////////////////////////////////////
2731
// See XML Schema annotation
2732
// (an XML Schema comment)
2733
public class Annotation extends ContainsSubElements {
2734        public Annotation() {
2735        }
2736
2737        public String JavaDoc getName() {
2738            return "annotation";
2739        }
2740
2741        public String JavaDoc getContentName() {
2742            return null;
2743        }
2744
2745        public ElementExpr optimize() {
2746            super.optimize();
2747            if (subElements.size() == 0)
2748                return null;
2749            return this;
2750        }
2751
2752        public boolean compressWhiteSpaceInner() {
2753            return true;
2754        }
2755
2756        public boolean writeDTDName(StringBuffer JavaDoc out) {
2757            return false;
2758        }
2759
2760        public void readSchema(org.w3c.dom.Element JavaDoc node) {
2761            Annotation el = new Annotation();
2762            pushCurrent(el);
2763            read(node);
2764            popCurrent();
2765        }
2766
2767        public Map validSubElementTypeMap() {
2768            return annotationValidSubElementTypeMap;
2769        }
2770    }
2771    static private Map annotationValidSubElementTypeMap = null;
2772    static {
2773        annotationValidSubElementTypeMap = new HashMap();
2774        annotationValidSubElementTypeMap.put(AppInfo.class, null);
2775        annotationValidSubElementTypeMap.put(Documentation.class, null);
2776    }
2777
2778
2779    ////////////////////////////////////////////////////////////////
2780
// See XML Schema appInfo
2781
public class AppInfo extends ContainsSubElements {
2782        public AppInfo() {
2783        }
2784
2785        public String JavaDoc getContentName() {
2786            return null;
2787        }
2788
2789        public boolean compressWhiteSpaceOuter() {
2790            return true;
2791        }
2792
2793        public boolean compressWhiteSpaceInner() {
2794            return true;
2795        }
2796
2797        public String JavaDoc getName() {
2798            return "appinfo";
2799        }
2800
2801        public ElementExpr optimize() {
2802            super.optimize();
2803            if (subElements.size() == 0)
2804                return null;
2805            return this;
2806        }
2807
2808        public boolean writeDTDName(StringBuffer JavaDoc out) {
2809            return false;
2810        }
2811
2812        public void readSchema(org.w3c.dom.Element JavaDoc node) {
2813            AppInfo el = new AppInfo();
2814            pushCurrent(el);
2815            read(node);
2816            popCurrent();
2817        }
2818
2819        public Map validSubElementTypeMap() {
2820            return appInfoValidSubElementTypeMap;
2821        }
2822    }
2823    static private Map appInfoValidSubElementTypeMap = null;
2824    static {
2825        appInfoValidSubElementTypeMap = new HashMap();
2826        appInfoValidSubElementTypeMap.put(AnyNode.class, null);
2827        appInfoValidSubElementTypeMap.put(TextNode.class, null);
2828    }
2829
2830
2831    ////////////////////////////////////////////////////////////////
2832
// See XML Schema documentation
2833
public class Documentation extends ContainsSubElements {
2834        public Documentation() {
2835        }
2836
2837        public String JavaDoc getContentName() {
2838            return null;
2839        }
2840
2841        public boolean compressWhiteSpaceOuter() {
2842            return true;
2843        }
2844
2845        public boolean compressWhiteSpaceInner() {
2846            return true;
2847        }
2848
2849        public String JavaDoc getName() {
2850            return "documentation";
2851        }
2852
2853        public boolean writeDTDName(StringBuffer JavaDoc out) {
2854            return false;
2855        }
2856
2857        public void readSchema(org.w3c.dom.Element JavaDoc node) {
2858            Documentation el = new Documentation();
2859            pushCurrent(el);
2860            read(node);
2861            popCurrent();
2862        }
2863
2864        public Map validSubElementTypeMap() {
2865            return documentationValidSubElementTypeMap;
2866        }
2867    }
2868    static private Map documentationValidSubElementTypeMap = null;
2869    static {
2870        documentationValidSubElementTypeMap = new HashMap();
2871        documentationValidSubElementTypeMap.put(AnyNode.class, null);
2872        documentationValidSubElementTypeMap.put(TextNode.class, null);
2873    }
2874
2875
2876    ////////////////////////////////////////////////////////////////
2877
// This represents any undefined node
2878
public class AnyNode extends ContainsSubElements {
2879        protected String JavaDoc name;
2880        protected String JavaDoc value;
2881
2882        public AnyNode(String JavaDoc n, String JavaDoc v) {
2883            name = n;
2884            value = v;
2885        }
2886
2887        public String JavaDoc getContentName() {
2888            return name;
2889        }
2890
2891        public String JavaDoc getName() {
2892            return name;
2893        }
2894
2895        public String JavaDoc getValue() {
2896            return value;
2897        }
2898
2899        public boolean compressWhiteSpaceInner() {
2900            return findSubElement("TextNode") != null;
2901        }
2902
2903        public ElementExpr optimize() {
2904            super.optimize();
2905            if (name == null && value == null && subElements.size() == 0)
2906                return null;
2907            return this;
2908        }
2909
2910        public boolean writeDTDName(StringBuffer JavaDoc out) {
2911            return false;
2912        }
2913
2914        public void writeXMLSchema(XMLWriter out) throws IOException {
2915            if (value == null)
2916                this.writeXMLSchema(out, true);
2917            else {
2918                out.startTag(getName());
2919                XMLUtil.printXML(out, value, false);
2920                this.writeXMLSchema(out, false);
2921                out.endTag();
2922            }
2923        }
2924
2925        public void readSchema(org.w3c.dom.Element JavaDoc node) {
2926            String JavaDoc myName = node.getLocalName();
2927            String JavaDoc myValue = node.getNodeValue();
2928            AnyNode el = new AnyNode(myName, myValue);
2929
2930            // Should read in attributes too.
2931
//System.out.println("Just read AnyNode: myName="+myName+" myValue="+myValue);
2932
pushCurrent(el);
2933            read(node);
2934            popCurrent();
2935        }
2936
2937        public String JavaDoc toString() {
2938            return "AnyNode("+name+")";
2939        }
2940
2941        public Map validSubElementTypeMap() {
2942            return anyNodeValidSubElementTypeMap;
2943        }
2944
2945        public boolean equals(Object JavaDoc o) {
2946            if (!(o instanceof AnyNode))
2947                return false;
2948            AnyNode el = (AnyNode) o;
2949            //System.out.println("value="+value+" el.value="+el.value);
2950
if (value == null) {
2951                if (el.value != null)
2952                    return false;
2953            } else if (!value.equals(el.value))
2954                return false;
2955            if (name == null) {
2956                if (el.name != null)
2957                    return false;
2958            } else if (!name.equals(el.name))
2959                return false;
2960            return true;
2961        }
2962
2963        public int hashCode() {
2964            int result = 17;
2965            result = 37*result + ((value == null) ? 0 : value.hashCode());
2966            result = 37*result + ((name == null) ? 0 : name.hashCode());
2967            result = 37*result + super.hashCode();
2968            return result;
2969        }
2970    }
2971    static private Map anyNodeValidSubElementTypeMap = null;
2972    static {
2973        anyNodeValidSubElementTypeMap = new HashMap();
2974        anyNodeValidSubElementTypeMap.put(AnyNode.class, null);
2975        anyNodeValidSubElementTypeMap.put(TextNode.class, null);
2976    }
2977
2978    ////////////////////////////////////////////////////////////////
2979
// This represents a text element and allows for subelements.
2980
public class TextNode extends ContainsSubElements {
2981        protected String JavaDoc text;
2982
2983        public TextNode(String JavaDoc text) {
2984            this.text = text;
2985        }
2986
2987        public String JavaDoc getText() {
2988            return text;
2989        }
2990
2991        public String JavaDoc getName() {
2992            return "TextNode";
2993        }
2994
2995        public String JavaDoc getContentName() {
2996            return null;
2997        }
2998
2999        public boolean compressWhiteSpaceInner() {
3000            return true;
3001        }
3002
3003        public ElementExpr optimize() {
3004            super.optimize();
3005            if (text == null || text.equals(""))
3006                return null;
3007            return this;
3008        }
3009
3010        public boolean writeDTDName(StringBuffer JavaDoc out) {
3011            return false;
3012        }
3013
3014        public void writeXMLSchema(XMLWriter out) throws IOException {
3015            XMLUtil.printXML(out, text, false);
3016            this.writeXMLSchema(out, false);
3017        }
3018
3019        public void readSchema(org.w3c.dom.Element JavaDoc node) {
3020            readSchema((Text) node);
3021        }
3022
3023        public void readSchema(Text node) {
3024            TextNode el = new TextNode(node.getData());
3025            //System.out.println("Just read TextNode: myName="+myName+" myValue="+myValue);
3026
pushCurrent(el);
3027            read(node);
3028            popCurrent();
3029        }
3030
3031        public String JavaDoc toString() {
3032            return "TextNode("+text+")";
3033        }
3034
3035        public Map validSubElementTypeMap() {
3036            return textNodeValidSubElementTypeMap;
3037        }
3038
3039        public boolean equals(Object JavaDoc o) {
3040            if (!(o instanceof TextNode))
3041                return false;
3042            TextNode el = (TextNode) o;
3043            if (text == null) {
3044                if (el.text != null)
3045                    return false;
3046            } else if (!text.equals(el.text))
3047                return false;
3048            return true;
3049        }
3050
3051        public int hashCode() {
3052            int result = 17;
3053            result = 37*result + ((text == null) ? 0 : text.hashCode());
3054            result = 37*result + super.hashCode();
3055            return result;
3056        }
3057    }
3058    static private Map textNodeValidSubElementTypeMap = null;
3059    static {
3060        textNodeValidSubElementTypeMap = new HashMap();
3061        textNodeValidSubElementTypeMap.put(AnyNode.class, null);
3062        textNodeValidSubElementTypeMap.put(TextNode.class, null);
3063    }
3064
3065    ////////////////////////////////////////////////////////////////
3066
// See XML Schema element
3067
// This implementation is missing attributes right now.
3068
public class Element extends ContainsSubElements implements MinMaxOccurs, CanRef {
3069        private String JavaDoc elementName;
3070        private String JavaDoc elementNamespace; // a URI
3071
private String JavaDoc defaultTargetNamespace;
3072        private boolean defaultFormQualified;
3073        private Boolean JavaDoc formQualified;
3074        // If type is set and there are subelements, then the type
3075
// is a "comment" about which java type should be used.
3076
private String JavaDoc type;
3077        private String JavaDoc xmlSchemaType;
3078        private String JavaDoc ref;
3079        private String JavaDoc refWithNamespace;
3080        private String JavaDoc minOccurs = "1";
3081        private String JavaDoc maxOccurs = "1";
3082        private boolean nillable = false;
3083        private String JavaDoc defaultValue;
3084        private ElementExpr parentElementExpr;
3085
3086        protected Element(String JavaDoc n, String JavaDoc t) {
3087            this(n);
3088            setType(t);
3089            putSchemaTypeDef(type, this);
3090        }
3091
3092        protected Element(String JavaDoc n) {
3093            setElementName(n);
3094            type = null;
3095        }
3096        
3097        private Element() {
3098        }
3099        
3100        public String JavaDoc getName() {
3101            return "element";
3102        }
3103
3104        public String JavaDoc getContentName() {
3105            return elementName;
3106        }
3107
3108        /**
3109         * @param n The qualified element name;
3110         * for instance, "address" or "tns:address"
3111         */

3112        private void setElementName(String JavaDoc n) {
3113            if (n != null && !n.equals("")) {
3114                String JavaDoc prefix = prefixOf(n);
3115                if (prefix == null) {
3116                    //
3117
// See XML Schema spec Part 1, section 3.3.2 and search
3118
// for "namespace" to figure out where the namespace
3119
// comes from.
3120
//
3121
if (parentElementExpr instanceof SchemaNode) {
3122                        elementNamespace = defaultTargetNamespace;
3123                    } else {
3124                        if (isFormQualified())
3125                            elementNamespace = defaultTargetNamespace;
3126                        else {
3127                            //elementNamespace = "";
3128
elementNamespace = null;
3129                        }
3130                    }
3131                    //System.out.println("n="+n+" elementNamespace="+elementNamespace);
3132
/*
3133                    if (!"dummy".equals(n) &&
3134                            ((elementNamespace == null && defaultTargetNamespace != null)
3135                            || (elementNamespace != null && !elementNamespace.equals(targetNamespace)))) {
3136                        System.out.println("Different namespace on "+n+" elementNamespace="+elementNamespace+" targetNamespace="+targetNamespace+" defaultTargetNamespace="+defaultTargetNamespace);
3137                    }
3138                     */

3139                    elementName = n;
3140                } else {
3141                    elementNamespace = getNamespaceURI(prefix);
3142                    elementName = removePrefix(n);
3143                }
3144            } else {
3145                elementName = null;
3146                elementNamespace = null;
3147            }
3148        }
3149
3150        public String JavaDoc getElementName() {
3151            return elementName;
3152        }
3153
3154        /**
3155         * Returns the URI namespace
3156         */

3157        public String JavaDoc getElementNamespace() {
3158            return elementNamespace;
3159        }
3160        
3161        public String JavaDoc getPrefix() {
3162            return getNamespace(elementNamespace);
3163        }
3164        
3165        public boolean isFormQualified() {
3166            if (formQualified != null)
3167                return formQualified.booleanValue();
3168            return defaultFormQualified;
3169        }
3170
3171        public String JavaDoc getType() {
3172            return type;
3173        }
3174
3175        public boolean hasRef() {
3176            return getRef() != null;
3177        }
3178
3179        /**
3180         * May return null.
3181         */

3182        public String JavaDoc getRef() {
3183            return ref;
3184        }
3185
3186        public Element getRefElement() {
3187            if (ref == null)
3188                return null;
3189            //Element referredElement = getDefinedElement(ref);
3190
Element referredElement = getDefinedElementResolvedNamespace(refWithNamespace);
3191            return referredElement;
3192        }
3193
3194        public ElementExpr getRefElementExpr() {
3195            return getRefElement();
3196        }
3197
3198        /**
3199         * Set the type of this element. The String passed in, @t, should
3200         * be a java type like 'java.lang.Integer' or
3201         * 'com.sun.forte4j.webdesigner.SOAPTest.Foo' or 'float'.
3202         * This type will later get converted into the XML Schema type
3203         * ('int' -> 'xsd:int', 'java.lang.String' -> 'xsd:string').
3204         */

3205        public void setType(String JavaDoc t) {
3206            if (t != null)
3207                t = t.intern();
3208            if ("void" == t)
3209                t = null;
3210            String JavaDoc oldType = type;
3211            type = t;
3212            if (schemaTypeDefs.containsKey(oldType)) // FIXME
3213
schemaTypeDefs.remove(oldType);
3214            if (t == null)
3215                return;
3216            putSchemaTypeDef(type, this);
3217            ref = null;
3218            if (debug)
3219                System.out.println("setType("+t+")");
3220            if (optionallyDefinedTypes.containsKey(t)) {
3221                //System.out.println("Found it!");
3222
requiredPredefinedTypes.put(t, "keep"); // Keep this one. NOI18N
3223
}
3224        }
3225
3226        /**
3227         * Bypass setType's converstion from java type to XML Schema type.
3228         * @t should be the XML Schema type. This should only be used
3229         * for types that are defined previously in this schema
3230         * or have special meaning to the eventual reader of this schema
3231         * (like, 'SOAP-INC:Array').
3232         */

3233        public void setXMLSchemaType(String JavaDoc t) {
3234            if (t == null) {
3235                xmlSchemaType = null;
3236            } else {
3237                xmlSchemaType = normalizeDocumentNamespace(t).intern();
3238                ref = null;
3239            }
3240        }
3241
3242        public String JavaDoc getJavaType() {
3243            //System.out.println("Element.getJavaType: type="+type+" ref="+ref+" xmlSchemaType="+xmlSchemaType);
3244
if (type != null)
3245                return type;
3246            if (ref != null) {
3247                Element referredElement = getRefElement();
3248                //System.out.println("Found "+referredElement);
3249
return referredElement.getJavaType();
3250            }
3251            if (xmlSchemaType == null)
3252                return null;
3253            String JavaDoc javaType = schemaTypeToJavaType(xmlSchemaType);
3254            if (nillable) {
3255                javaType = JavaUtil.toObjectType(javaType);
3256            }
3257            return javaType;
3258        }
3259
3260        public String JavaDoc getXMLSchemaType() {
3261            return xmlSchemaType;
3262        }
3263
3264        public void setMinOccurs(String JavaDoc mino) {
3265            if (mino == null)
3266                mino = "1";
3267            minOccurs = mino.intern();
3268        }
3269
3270        public void setMaxOccurs(String JavaDoc maxo) {
3271            if (maxo == null)
3272                maxo = "1";
3273            maxOccurs = maxo.intern();
3274        }
3275
3276        public String JavaDoc getMinOccurs() {
3277            return minOccurs;
3278        }
3279
3280        public String JavaDoc getMaxOccurs() {
3281            return maxOccurs;
3282        }
3283
3284        public boolean isNillable() {
3285            return nillable;
3286        }
3287
3288        public String JavaDoc getDefault() {
3289            return defaultValue;
3290        }
3291
3292        public void setDefault(String JavaDoc d) {
3293            defaultValue = d;
3294        }
3295
3296        public void writeDTD(StringBuffer JavaDoc out) {
3297            Element firstElement = (Element) elementTable.get(getElementName());
3298            if (firstElement == null)
3299                elementTable.put(getElementName(), this);
3300            else {
3301                // Gotta compare
3302
if (debug)
3303                    System.out.println("Found another element named "+getElementName());
3304                if (!equals(firstElement)) {
3305                    throw new RuntimeException JavaDoc(Common.getMessage("MSG_SameNameDifferentContents", getElementName()));
3306                }
3307                return;
3308            }
3309
3310            out.append("<!ELEMENT "+getElementName()+" ");
3311            // "EMPTY" correlates with hasNamedSubElements
3312
if (subElements.size() == 0) {
3313                if (type == null || type == "void")
3314                    out.append("()");
3315                else
3316                    out.append("#PCDATA");
3317            } else {
3318                if (!writeDTDSubElementNames(out))
3319                    out.append("()");
3320            }
3321            out.append(">\n");
3322
3323            // Now tell the subelements to print themselves out too
3324
super.writeDTD(out);
3325        }
3326
3327        public boolean hasNamedSubElements() {
3328            if (subElements.size() == 0) {
3329                return false;
3330            } else {
3331                if (!writeDTDSubElementNames(new StringBuffer JavaDoc()))
3332                    return false;
3333                else
3334                    return true;
3335            }
3336        }
3337
3338        public String JavaDoc getAttributeString() {
3339            StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
3340            if (elementName != null) {
3341                sb.append(" name='");
3342                if (elementNamespace != null && !elementNamespace.equals(targetNamespace)) {
3343                    sb.append(getNamespace(elementNamespace));
3344                    sb.append(':');
3345                }
3346                sb.append(elementName);
3347                sb.append("'");
3348            }
3349            if (ref != null) {
3350                sb.append(" ref='");
3351                sb.append(ref);
3352                sb.append("'");
3353            }
3354            if (xmlSchemaType != null) {
3355                sb.append(" type='");
3356                sb.append(xmlSchemaType);
3357                sb.append("'");
3358            } else if (type != null) {
3359                String JavaDoc theXmlSchemaType = javaType2XMLSchemaTypeComplex(getType());
3360                if (theXmlSchemaType != null) {
3361                    sb.append(" type='");
3362                    sb.append(theXmlSchemaType);
3363                    sb.append("'");
3364                } else {
3365                    //throw new IllegalStateException(Common.getMessage("MSG_FailedToFindXMLSchemaType", type));
3366
}
3367            }
3368            if (minOccurs != "1")
3369                sb.append(" minOccurs='"+minOccurs+"'");
3370            if (maxOccurs != "1")
3371                sb.append(" maxOccurs='"+maxOccurs+"'");
3372            if (nillable)
3373                sb.append(" nillable='true'");
3374            if (defaultValue != null)
3375                sb.append(" default='"+defaultValue+"'");
3376            if (formQualified != null)
3377                sb.append(" form='"+(formQualified.booleanValue() ? "qualified" : "unqualified")+"'");
3378            return sb.toString();
3379        }
3380
3381        public boolean isDefiningNewType() {
3382            if (ref == null)
3383                return true;
3384            return (subElements.size() >= 1 && type != null);
3385        }
3386
3387        public boolean writeDTDName(StringBuffer JavaDoc out) {
3388            out.append(getElementName());
3389            if ("unbounded" == maxOccurs)
3390                out.append("*");
3391            return true;
3392        }
3393
3394        public Map validSubElementTypeMap() {
3395            return elementValidSubElementTypeMap;
3396        }
3397
3398        public void readSchema(org.w3c.dom.Element JavaDoc node) {
3399            String JavaDoc elementName = node.getAttribute("name"); // NOI18N
3400
String JavaDoc elementType = node.getAttribute("type"); // NOI18N
3401
String JavaDoc elementRef = node.getAttribute("ref"); // NOI18N
3402
String JavaDoc minOccurs = node.getAttribute("minOccurs"); // NOI18N
3403
String JavaDoc maxOccurs = node.getAttribute("maxOccurs"); // NOI18N
3404
String JavaDoc myNillable = node.getAttribute("nillable"); // NOI18N
3405
String JavaDoc myDefault = node.getAttribute("default"); // NOI18N
3406
String JavaDoc myForm = node.getAttribute("form"); // NOI18N
3407
String JavaDoc defaultTargetNamespace;
3408            boolean defaultFormQualified;
3409            ElementExpr parent = peekCurrent();
3410            //System.out.println("elementName="+elementName+" parent="+parent);
3411
SchemaNode parentSchema;
3412            if (parent instanceof SchemaNode) {
3413                parentSchema = (SchemaNode) parent;
3414            } else {
3415                parentSchema = (SchemaNode) findAncestor(SchemaNode.class);
3416            }
3417            if (parentSchema != null) {
3418                //
3419
// Get the targetNamespace from the instance variable instead of
3420
// querying the schema node, since if multiple schema nodes are
3421
// merged, only the first keeps it's targetNamespace.
3422
//
3423
//defaultTargetNamespace = parentSchema.getTargetNamespace();
3424
defaultTargetNamespace = targetNamespace;
3425                //defaultFormQualified = parentSchema.isElementFormQualified();
3426
defaultFormQualified = elementFormQualifiedDefault;
3427            } else {
3428                defaultTargetNamespace = targetNamespace;
3429                defaultFormQualified = false;
3430            }
3431            Element el = new Element();
3432            el.parentElementExpr = parent;
3433            el.defaultTargetNamespace = defaultTargetNamespace;
3434            el.defaultFormQualified = defaultFormQualified;
3435            el.setElementName(elementName);
3436            if (myForm != null && !"".equals(myForm))
3437                el.formQualified = Boolean.valueOf("qualified".equals(myForm));
3438            if (elementRef != null && !elementRef.equals("")) {
3439                el.ref = elementRef.intern();
3440                el.refWithNamespace = resolveNamespace(el.ref);
3441            }
3442            if (elementType != null && !elementType.equals(""))
3443                el.setXMLSchemaType(elementType);
3444            if (minOccurs != null && !minOccurs.equals(""))
3445                el.setMinOccurs(minOccurs);
3446            if (maxOccurs != null && !maxOccurs.equals(""))
3447                el.setMaxOccurs(maxOccurs);
3448            if (myNillable != null && (myNillable.equals("true") || myNillable.equals("yes") || myNillable.equals("on")))
3449                el.nillable = true;
3450            if (myDefault != null && !"".equals(myDefault))
3451                el.setDefault(myDefault);
3452            pushCurrent(el);
3453            read(node);
3454            popCurrent();
3455        }
3456
3457        public boolean equals(Object JavaDoc o) {
3458            if (!(o instanceof Element))
3459                return false;
3460            Element el = (Element) o;
3461            //System.out.println("type="+type);
3462
if (type != el.type)
3463                return false;
3464            if (ref != el.ref)
3465                return false;
3466            if (xmlSchemaType != el.xmlSchemaType)
3467                return false;
3468            if (minOccurs != el.minOccurs)
3469                return false;
3470            if (maxOccurs != el.maxOccurs)
3471                return false;
3472            if (nillable != el.nillable)
3473                return false;
3474            if (formQualified == null) {
3475                if (el.formQualified != null)
3476                    return false;
3477            } else {
3478                if (el.formQualified == null
3479                        || formQualified.booleanValue() != el.formQualified.booleanValue())
3480                    return false;
3481            }
3482
3483            if (!elementName.equals(el.elementName))
3484                return false;
3485
3486            return super.equals(el);
3487        }
3488
3489        public int hashCode() {
3490            int result = 17;
3491            result = 37*result + ((type == null) ? 0 : type.hashCode());
3492            result = 37*result + ((ref == null) ? 0 : ref.hashCode());
3493            result = 37*result + ((xmlSchemaType == null) ? 0 : xmlSchemaType.hashCode());
3494            result = 37*result + ((minOccurs == null) ? 0 : minOccurs.hashCode());
3495            result = 37*result + ((maxOccurs == null) ? 0 : maxOccurs.hashCode());
3496            result = 37*result + (nillable ? 1 : 0);
3497            result = 37*result + ((elementName == null) ? 0 : elementName.hashCode());
3498            result = 37*result + super.hashCode();
3499            result = 37*result + ((formQualified == null) ? 0 : formQualified.hashCode());
3500            return result;
3501        }
3502    }
3503    static private Map elementValidSubElementTypeMap = null;
3504    static {
3505        elementValidSubElementTypeMap = new HashMap();
3506        elementValidSubElementTypeMap.put(Annotation.class, null);
3507        elementValidSubElementTypeMap.put(SimpleType.class, null);
3508        elementValidSubElementTypeMap.put(ComplexType.class, null);
3509        elementValidSubElementTypeMap.put(Unique.class, null);
3510        elementValidSubElementTypeMap.put(Key.class, null);
3511        elementValidSubElementTypeMap.put(KeyRef.class, null);
3512    }
3513
3514    public class Any extends ContainsSubElements implements MinMaxOccurs {
3515        private String JavaDoc minOccurs;
3516        private String JavaDoc maxOccurs;
3517        private String JavaDoc namespace;
3518        private String JavaDoc processContents;
3519
3520        public Any() {
3521            init();
3522        }
3523
3524        private void init() {
3525            minOccurs = "1";
3526            maxOccurs = "1";
3527        }
3528
3529        public String JavaDoc getName() {
3530            return "any";
3531        }
3532
3533        public String JavaDoc getContentName() {
3534            return null;
3535        }
3536
3537        public String JavaDoc getNamespace() {
3538            return namespace;
3539        }
3540
3541        public void setNamespace(String JavaDoc n) {
3542            namespace = (n == null) ? null : n.intern();
3543        }
3544
3545        public String JavaDoc getProcessContents() {
3546            return processContents;
3547        }
3548
3549        public void setProcessContents(String JavaDoc pc) {
3550            processContents = (pc == null) ? null : pc.intern();
3551        }
3552
3553        public void setMinOccurs(String JavaDoc mino) {
3554            if (mino == null)
3555                mino = "1";
3556            minOccurs = mino.intern();
3557        }
3558
3559        public void setMaxOccurs(String JavaDoc maxo) {
3560            if (maxo == null)
3561                maxo = "1";
3562            maxOccurs = maxo.intern();
3563        }
3564
3565        public String JavaDoc getMinOccurs() {
3566            return minOccurs;
3567        }
3568
3569        public String JavaDoc getMaxOccurs() {
3570            return maxOccurs;
3571        }
3572
3573        public String JavaDoc getAttributeString() {
3574            StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
3575            if (minOccurs != "1")
3576                sb.append(" minOccurs='"+minOccurs+"'");
3577            if (maxOccurs != "1")
3578                sb.append(" maxOccurs='"+maxOccurs+"'");
3579            if (namespace != null)
3580                sb.append(" namespace='"+namespace+"'");
3581            if (processContents != null)
3582                sb.append(" processContents='"+processContents+"'");
3583             return sb.toString();
3584       }
3585
3586        public Map validSubElementTypeMap() {
3587            return anyValidSubElementTypeMap;
3588        }
3589
3590        public boolean writeDTDName(StringBuffer JavaDoc out) {
3591            return false;
3592        }
3593
3594        public void readSchema(org.w3c.dom.Element JavaDoc node) {
3595            String JavaDoc namespace = node.getAttribute("namespace"); // NOI18N
3596
String JavaDoc processContents = node.getAttribute("processContents"); // NOI18N
3597
String JavaDoc minOccurs = node.getAttribute("minOccurs"); // NOI18N
3598
String JavaDoc maxOccurs = node.getAttribute("maxOccurs"); // NOI18N
3599
Any el = new Any();
3600            if (namespace != null && !namespace.equals(""))
3601                el.setNamespace(namespace);
3602            if (processContents != null && !processContents.equals(""))
3603                el.setProcessContents(processContents);
3604            if (minOccurs != null && !minOccurs.equals(""))
3605                el.setMinOccurs(minOccurs);
3606            if (maxOccurs != null && !maxOccurs.equals(""))
3607                el.setMaxOccurs(maxOccurs);
3608            pushCurrent(el);
3609            read(node);
3610            popCurrent();
3611        }
3612
3613        public boolean equals(Object JavaDoc o) {
3614            if (!(o instanceof Any))
3615                return false;
3616            Any el = (Any) o;
3617            if (minOccurs != el.minOccurs)
3618                return false;
3619            if (maxOccurs != el.maxOccurs)
3620                return false;
3621            if (namespace != el.namespace)
3622                return false;
3623            if (processContents != el.processContents)
3624                return false;
3625            return super.equals(el);
3626        }
3627
3628        public int hashCode() {
3629            int result = 17;
3630            result = 37*result + ((namespace == null) ? 0 : namespace.hashCode());
3631            result = 37*result + ((processContents == null) ? 0 : processContents.hashCode());
3632            result = 37*result + ((minOccurs == null) ? 0 : minOccurs.hashCode());
3633            result = 37*result + ((maxOccurs == null) ? 0 : maxOccurs.hashCode());
3634            return result;
3635        }
3636    }
3637    static private Map anyValidSubElementTypeMap = null;
3638    static {
3639        anyValidSubElementTypeMap = new HashMap();
3640        anyValidSubElementTypeMap.put(Annotation.class, null);
3641    }
3642
3643    public class AnyAttribute extends ContainsSubElements {
3644        private String JavaDoc namespace;
3645        private String JavaDoc processContents;
3646
3647        public AnyAttribute() {
3648        }
3649
3650        public String JavaDoc getName() {
3651            return "anyAttribute";
3652        }
3653
3654        public String JavaDoc getContentName() {
3655            return null;
3656        }
3657
3658        public String JavaDoc getNamespace() {
3659            return namespace;
3660        }
3661
3662        public void setNamespace(String JavaDoc n) {
3663            namespace = (n == null) ? null : n.intern();
3664        }
3665
3666        public String JavaDoc getProcessContents() {
3667            return processContents;
3668        }
3669
3670        public void setProcessContents(String JavaDoc pc) {
3671            processContents = (pc == null) ? null : pc.intern();
3672        }
3673
3674        public String JavaDoc getAttributeString() {
3675            StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
3676            if (namespace != null)
3677                sb.append(" namespace='"+namespace+"'");
3678            if (processContents != null)
3679                sb.append(" processContents='"+processContents+"'");
3680             return sb.toString();
3681       }
3682
3683        public Map validSubElementTypeMap() {
3684            return anyAttributeValidSubElementTypeMap;
3685        }
3686
3687        public boolean writeDTDName(StringBuffer JavaDoc out) {
3688            return false;
3689        }
3690
3691        public void readSchema(org.w3c.dom.Element JavaDoc node) {
3692            String JavaDoc namespace = node.getAttribute("namespace"); // NOI18N
3693
String JavaDoc processContents = node.getAttribute("processContents"); // NOI18N
3694
AnyAttribute el = new AnyAttribute();
3695            if (namespace != null && !namespace.equals(""))
3696                el.setNamespace(namespace);
3697            if (processContents != null && !processContents.equals(""))
3698                el.setProcessContents(processContents);
3699            pushCurrent(el);
3700            read(node);
3701            popCurrent();
3702        }
3703
3704        public boolean equals(Object JavaDoc o) {
3705            if (!(o instanceof AnyAttribute))
3706                return false;
3707            AnyAttribute el = (AnyAttribute) o;
3708            if (namespace != el.namespace)
3709                return false;
3710            if (processContents != el.processContents)
3711                return false;
3712            return super.equals(el);
3713        }
3714
3715        public int hashCode() {
3716            int result = 17;
3717            result = 37*result + ((namespace == null) ? 0 : namespace.hashCode());
3718            result = 37*result + ((processContents == null) ? 0 : processContents.hashCode());
3719            return result;
3720        }
3721    }
3722    static private Map anyAttributeValidSubElementTypeMap = null;
3723    static {
3724        anyAttributeValidSubElementTypeMap = new HashMap();
3725        anyAttributeValidSubElementTypeMap.put(Annotation.class, null);
3726    }
3727
3728    ////////////////////////////////////////////////////////////////
3729
// An Schema Attribute
3730
// An XML Schema attribute may contain simpleType as a subelement
3731
// and/or annotation.
3732
public class Attribute extends ContainsSubElements implements CanRef {
3733        private String JavaDoc namespace;
3734        private String JavaDoc name;
3735        private String JavaDoc type;
3736        private String JavaDoc defaultValue;
3737        private String JavaDoc id;
3738        private String JavaDoc ref;
3739        private String JavaDoc fixed;
3740        private String JavaDoc use;
3741        private String JavaDoc arrayType; // for rpc/encoded WSDL
3742

3743        public Attribute(String JavaDoc n) {
3744            this(n, targetNamespace);
3745        }
3746
3747        public Attribute(String JavaDoc n, String JavaDoc ns) {
3748            this(n, ns, null);
3749        }
3750
3751        public Attribute(String JavaDoc n, String JavaDoc ns, String JavaDoc type) {
3752            if (n == null || n.equals(""))
3753                name = null;
3754            else
3755                name = n.intern();
3756            namespace = ns;
3757            setType(type);
3758        }
3759
3760        public String JavaDoc getName() {
3761            return "attribute";
3762        }
3763
3764        public String JavaDoc getContentName() {
3765            return "@"+name;
3766        }
3767
3768        public String JavaDoc getAttributeName() {
3769            if (namespace == null || namespace.equals(targetNamespace)) {
3770                return name;
3771            }
3772            return getNamespace(namespace)+":"+name;
3773        }
3774
3775        public String JavaDoc getAttributeNamespace() {
3776            return namespace;
3777        }
3778
3779        public String JavaDoc getAttributeNameNoNS() {
3780            return name;
3781        }
3782
3783        private void setType(String JavaDoc ty) {
3784            if (ty == null) {
3785                type = null;
3786                return;
3787            }
3788            type = normalizeDocumentNamespace(ty).intern();
3789        }
3790
3791        public String JavaDoc getType() {
3792            return type;
3793        }
3794
3795        public String JavaDoc getJavaType() {
3796            if (type == null)
3797                return null;
3798            String JavaDoc javaType = schemaTypeToJavaType(type);
3799            if (false) {
3800                javaType = JavaUtil.toObjectType(javaType);
3801            }
3802            return javaType;
3803        }
3804
3805        public String JavaDoc getFixed() {
3806            return fixed;
3807        }
3808
3809        public boolean isRequired() {
3810            return use == "required";
3811        }
3812
3813        public boolean isOptional() {
3814            return use == "optional";
3815        }
3816
3817        public boolean isProhibited() {
3818            return use == "prohibited";
3819        }
3820
3821        public String JavaDoc getUse() {
3822            return use;
3823        }
3824
3825        public boolean isAttributeNamed(String JavaDoc n) {
3826            n = resolveNamespace(n);
3827            String JavaDoc fullName = resolveNamespace(namespace, name);
3828            return n.equals(fullName);
3829        }
3830
3831        public boolean hasRef() {
3832            return getRef() != null;
3833        }
3834
3835        /**
3836         * May return null.
3837         */

3838        public String JavaDoc getRef() {
3839            return ref;
3840        }
3841
3842        public Attribute getRefAttribute() {
3843            if (ref == null)
3844                return null;
3845            Object JavaDoc o = definedAttributes.get(ref);
3846            if (o instanceof Attribute) {
3847                Attribute referredAttribute = (Attribute) o;
3848                return referredAttribute;
3849            } else {
3850                throw new IllegalStateException JavaDoc(Common.getMessage("MSG_FailedToFindRef",
3851                                                                  ref,
3852                                                                  this.toString()));
3853            }
3854        }
3855
3856        public ElementExpr getRefElementExpr() {
3857            return getRefAttribute();
3858        }
3859
3860        public boolean isDefiningNewType() {
3861            if (ref == null)
3862                return true;
3863            return (subElements.size() >= 1 && type != null);
3864        }
3865
3866        public String JavaDoc getArrayType() {
3867            return arrayType;
3868        }
3869
3870        public String JavaDoc getDefaultValue() {
3871            return defaultValue;
3872        }
3873
3874        public void writeDTD(StringBuffer JavaDoc out) {
3875            writeDTD(out, "UNKNOWN");
3876        }
3877
3878        public void writeDTD(StringBuffer JavaDoc out, String JavaDoc elementName) {
3879            out.append("<!ATTLIST "+elementName+" "+name+" ");
3880            out.append(">\n");
3881        }
3882
3883        public boolean writeDTDName(StringBuffer JavaDoc out) {
3884            out.append(name);
3885            return false;
3886        }
3887
3888        public void validate() {
3889
3890            super.validate();
3891        }
3892
3893        public Map validSubElementTypeMap() {
3894            return attributeValidSubElementTypeMap;
3895        }
3896
3897        public String JavaDoc getAttributeString() {
3898            StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
3899            if (name != null)
3900                sb.append(" name='"+getAttributeName()+"'");
3901            if (type != null)
3902                sb.append(" type='"+type+"'");
3903            if (fixed != null) {
3904                sb.append(" fixed='");
3905                XMLUtil.printXML(sb, fixed, true);
3906                sb.append("'");
3907            }
3908            if (ref != null)
3909                sb.append(" ref='"+ref+"'");
3910            if (id != null)
3911                sb.append(" id='"+id+"'");
3912            if (defaultValue != null) {
3913                sb.append(" default='");
3914                XMLUtil.printXML(sb, defaultValue, true);
3915                sb.append("'");
3916            }
3917            if (use != null)
3918                sb.append(" use='"+use+"'");
3919            if (arrayType != null)
3920                sb.append(" wsdl:arrayType='"+arrayType+"'");
3921            return sb.toString();
3922        }
3923
3924        public void readSchema(org.w3c.dom.Element JavaDoc node) {
3925            String JavaDoc myName = node.getAttribute("name"); // NOI18N
3926
String JavaDoc myType = node.getAttribute("type"); // NOI18N
3927
String JavaDoc myFixed = node.getAttribute("fixed"); // NOI18N
3928
String JavaDoc myRef = node.getAttribute("ref"); // NOI18N
3929
String JavaDoc myId = node.getAttribute("id"); // NOI18N
3930
String JavaDoc myDefault = node.getAttribute("default"); // NOI18N
3931
String JavaDoc myUse = node.getAttribute("use"); // NOI18N
3932
String JavaDoc myArrayType = node.getAttributeNS("http://schemas.xmlsoap.org/wsdl/", "arrayType");
3933
3934            Attribute attr = new Attribute(myName);
3935            if (myType != null && !myType.equals(""))
3936                attr.setType(myType.intern());
3937            if (myFixed != null && !myFixed.equals(""))
3938                attr.fixed = myFixed.intern();
3939            if (myRef != null && !myRef.equals(""))
3940                attr.ref = myRef.intern();
3941            if (myId != null && !myId.equals(""))
3942                attr.id = myId.intern();
3943            if (myDefault != null && !myDefault.equals(""))
3944                attr.defaultValue = myDefault.intern();
3945            if (myUse != null && !myUse.equals(""))
3946                attr.use = myUse.intern();
3947            if (myArrayType != null && !myArrayType.equals(""))
3948                attr.arrayType = myArrayType.intern();
3949            pushCurrent(attr);
3950            read(node);
3951            popCurrent();
3952        }
3953
3954        public boolean equals(Object JavaDoc o) {
3955            if (!(o instanceof Attribute))
3956                return false;
3957            Attribute el = (Attribute) o;
3958            if (name != el.name)
3959                return false;
3960            if (type != el.type)
3961                return false;
3962            if (fixed != el.fixed)
3963                return false;
3964            if (ref != el.ref)
3965                return false;
3966            if (id != el.id)
3967                return false;
3968            if (use != el.use)
3969                return false;
3970            if (arrayType != el.arrayType)
3971                return false;
3972
3973            return super.equals(el);
3974        }
3975
3976        public int hashCode() {
3977            int result = 17;
3978            result = 37*result + ((name == null) ? 0 : name.hashCode());
3979            result = 37*result + ((type == null) ? 0 : type.hashCode());
3980            result = 37*result + ((fixed == null) ? 0 : fixed.hashCode());
3981            result = 37*result + ((ref == null) ? 0 : ref.hashCode());
3982            result = 37*result + ((id == null) ? 0 : id.hashCode());
3983            result = 37*result + ((use == null) ? 0 : use.hashCode());
3984            result = 37*result + ((arrayType == null) ? 0 : arrayType.hashCode());
3985            result = 37*result + super.hashCode();
3986            return result;
3987        }
3988    }
3989    static private Map attributeValidSubElementTypeMap = null;
3990    static {
3991        attributeValidSubElementTypeMap = new HashMap();
3992        attributeValidSubElementTypeMap.put(Annotation.class, null);
3993        attributeValidSubElementTypeMap.put(SimpleType.class, null);
3994    }
3995
3996
3997    ////////////////////////////////////////////////////////////////
3998
// An Schema AttributeGroup
3999
public class AttributeGroup extends ContainsSubElements implements CanRef {
4000        private String JavaDoc name;
4001        private String JavaDoc ref;
4002
4003        public AttributeGroup() {
4004        }
4005
4006        /**
4007         * Create it by name.
4008         */

4009        public AttributeGroup(String JavaDoc n) {
4010            if (n != null && !n.equals("")) {
4011                name = normalizeTargetNamespace(n).intern();
4012                putSchemaTypeDef(name, this);
4013            }
4014        }
4015
4016        public Map validSubElementTypeMap() {
4017            return attributeGroupValidSubElementTypeMap;
4018        }
4019
4020        public String JavaDoc getName() {
4021            return "attributeGroup";
4022        }
4023
4024        public String JavaDoc getContentName() {
4025            return name;
4026        }
4027
4028        public String JavaDoc getGroupName() {
4029            return name;
4030        }
4031
4032        public boolean hasRef() {
4033            return getRef() != null;
4034        }
4035
4036        public String JavaDoc getRef() {
4037            return ref;
4038        }
4039
4040        public AttributeGroup getRefAttributeGroup() {
4041            if (ref == null)
4042                return null;
4043            Object JavaDoc o = getSchemaTypeDef(ref);
4044            if (o instanceof AttributeGroup) {
4045                AttributeGroup referredGroup = (AttributeGroup) o;
4046                return referredGroup;
4047            } else {
4048                throw new IllegalStateException JavaDoc(Common.getMessage("MSG_FailedToFindRef",
4049                                                                  ref,
4050                                                                  this.toString()));
4051            }
4052        }
4053
4054        public ElementExpr getRefElementExpr() {
4055            return getRefAttributeGroup();
4056        }
4057
4058        public boolean writeDTDName(StringBuffer JavaDoc out) {
4059            return writeDTDSubElementNames(out, false);
4060        }
4061
4062        public String JavaDoc getAttributeString() {
4063            StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
4064            if (name != null)
4065                sb.append(" name='"+name+"'");
4066            if (ref != null)
4067                sb.append(" ref='"+ref+"'");
4068            return sb.toString();
4069        }
4070
4071        public void readSchema(org.w3c.dom.Element JavaDoc node) {
4072            String JavaDoc myName = node.getAttribute("name"); // NOI18N
4073
String JavaDoc myRef = node.getAttribute("ref"); // NOI18N
4074
AttributeGroup ag = new AttributeGroup(myName);
4075            if (myRef != null && !myRef.equals(""))
4076                ag.ref = myRef.intern();
4077            pushCurrent(ag);
4078            read(node);
4079            popCurrent();
4080        }
4081
4082        public boolean equals(Object JavaDoc o) {
4083            if (!(o instanceof AttributeGroup))
4084                return false;
4085            AttributeGroup el = (AttributeGroup) o;
4086            if (name != el.name)
4087                return false;
4088            if (ref != el.ref)
4089                return false;
4090
4091            return super.equals(el);
4092        }
4093
4094        public int hashCode() {
4095            int result = 17;
4096            result = 37*result + ((name == null) ? 0 : name.hashCode());
4097            result = 37*result + ((ref == null) ? 0 : ref.hashCode());
4098            result = 37*result + super.hashCode();
4099            return result;
4100        }
4101    }
4102    static private Map attributeGroupValidSubElementTypeMap = null;
4103    static {
4104        attributeGroupValidSubElementTypeMap = new HashMap();
4105        attributeGroupValidSubElementTypeMap.put(Annotation.class, null);
4106        attributeGroupValidSubElementTypeMap.put(Attribute.class, null);
4107        attributeGroupValidSubElementTypeMap.put(AttributeGroup.class, null);
4108        attributeGroupValidSubElementTypeMap.put(AnyAttribute.class, null);
4109    }
4110
4111    ////////////////////////////////////////////////////////////////
4112

4113    private class ParserSchemaState {
4114        private String JavaDoc targetNamespace;
4115        private String JavaDoc documentNamespace;
4116        private boolean elementFormQualifiedDefault;
4117        private boolean attributeFormQualifiedDefault;
4118        
4119        public ParserSchemaState() {
4120            this.targetNamespace = SchemaRep.this.targetNamespace;
4121            this.documentNamespace = SchemaRep.this.documentNamespace;
4122            this.elementFormQualifiedDefault = SchemaRep.this.elementFormQualifiedDefault;
4123            this.attributeFormQualifiedDefault = SchemaRep.this.attributeFormQualifiedDefault;
4124        }
4125        
4126        public void reload() {
4127            SchemaRep.this.targetNamespace = this.targetNamespace;
4128            SchemaRep.this.documentNamespace = this.documentNamespace;
4129            SchemaRep.this.elementFormQualifiedDefault = this.elementFormQualifiedDefault;
4130            SchemaRep.this.attributeFormQualifiedDefault = this.attributeFormQualifiedDefault;
4131        }
4132    }
4133    
4134    ////////////////////////////////////////////////////////////////
4135

4136    //protected String docType; // Not implemented
4137
protected Map namespaceTable; // Map<String, String> example: <xsd, http://www.w3.org/2001/XMLSchema>
4138
protected String JavaDoc targetNamespace;
4139    protected String JavaDoc documentNamespace;
4140    private boolean elementFormQualifiedDefault;
4141    private boolean attributeFormQualifiedDefault;
4142    protected ContainsSubElements rootElement;
4143
4144    // elementTable is used when creating the DTD to make sure that only
4145
// 1 element of a particular name is ever created.
4146
protected Map elementTable; // Map<String, Element>
4147

4148    // Several simple types are hard coded into this class and put into
4149
// optionallyDefinedTypes. They define how to map these simple types into
4150
// XML Schema types ('java.lang.Integer' -> 'xsd:int').
4151
protected Map optionallyDefinedTypes = null; // Map<String, ElementExpr>
4152

4153    // Top types defined in this schema. Mapping of java type name into
4154
// element expressions (just like optionallyDefinedTypes)
4155
protected Map definedTypes; // Map<String, ElementExpr>
4156
protected Map definedTypesFull; // Map<String, ElementExpr>
4157

4158    protected Map definedAttributes; // Map<String, Attribute>
4159

4160    // Standard types
4161
protected Map predefinedSchemaTypes; // Map<String, ElementExpr>
4162

4163    // requiredPredefinedTypes gets an entry whenever something in
4164
// optionallyDefinedTypes is used.
4165
protected Map requiredPredefinedTypes; // Map<String, null>
4166

4167    // The client adds elements to the current thing on the stack
4168
protected Stack currentElementStack; // Stack<ElementExpr>
4169

4170    // It's possible to have an Annotation added to the top level
4171
// and it's checked in writeXMLSchemaStandalone.
4172
protected Annotation topAnnotation;
4173
4174    // It's useful for some clients to know what the last popped was.
4175
protected ElementExpr lastPopped;
4176
4177    // A sample instance of every node that can be read in.
4178
protected Map sampleNodes;
4179
4180    // map from type name to ElementExpr
4181
private Map schemaTypeDefs; // Map<String, ElementExpr>
4182

4183    // Whether or not a URL has been included already or not.
4184
protected Map includedAlready = new HashMap(); // <String, null>
4185

4186    private String JavaDoc currentParsedURI;
4187
4188    private boolean useBigDataTypes = true;
4189
4190    public SchemaRep() {
4191        init();
4192    }
4193
4194    public SchemaRep(Document schemaDoc, String JavaDoc uri) {
4195        init();
4196        currentParsedURI = uri;
4197        readDocument(schemaDoc);
4198    }
4199
4200    public SchemaRep(Document schemaDoc, String JavaDoc uri, boolean useBigDataTypes) {
4201        this.useBigDataTypes = useBigDataTypes;
4202        init();
4203        currentParsedURI = uri;
4204        readDocument(schemaDoc);
4205    }
4206
4207    private void init() {
4208        targetNamespace = null;
4209        documentNamespace = null;
4210        namespaceTable = new HashMap();
4211        addToNamespace("xml", "http://www.w3.org/XML/1998/namespace");
4212        addToNamespace("xsd", XSD_NS);
4213
4214        schemaTypeDefs = new HashMap();
4215        predefinedSchemaTypes = new HashMap();
4216        insertPredefinedSchemaTypes(predefinedSchemaTypes);
4217
4218        currentElementStack = new Stack();
4219        topAnnotation = null;
4220        definedTypes = new HashMap();
4221        definedTypesFull = new HashMap();
4222        definedAttributes = new HashMap();
4223        //mapSimpleJavaTypesPredefined(definedTypes);
4224
mapSimpleAttributes(definedAttributes);
4225        requiredPredefinedTypes = new HashMap();
4226        if (optionallyDefinedTypes == null) {
4227            optionallyDefinedTypes = new HashMap();
4228            mapSimpleJavaTypesOptional(optionallyDefinedTypes);
4229        }
4230
4231        // sampleNodes are used while reading an XML Schema in
4232
sampleNodes = new HashMap();
4233        putSampleNode(new Element("dummy")); // NOI18N
4234
putSampleNode(new ComplexType());
4235        putSampleNode(new SimpleType(null)); // NOI18N
4236
putSampleNode(new UnionType(null, null)); // NOI18N
4237
putSampleNode(new Restriction());
4238        putSampleNode(new Sequence());
4239        putSampleNode(new Choice());
4240        putSampleNode(new All());
4241        putSampleNode(new Group());
4242        putSampleNode(new Annotation());
4243        putSampleNode(new AppInfo());
4244        putSampleNode(new Documentation());
4245        putSampleNode(new Attribute("dummy")); // NOI18N
4246
putSampleNode(new AttributeGroup());
4247        putSampleNode(new MaxExclusive("dummy")); // NOI18N
4248
putSampleNode(new MinExclusive("dummy")); // NOI18N
4249
putSampleNode(new Enumeration("dummy")); // NOI18N
4250
putSampleNode(new Pattern("dummy")); // NOI18N
4251
putSampleNode(new MinLength("0")); // NOI18N
4252
putSampleNode(new MaxLength("0")); // NOI18N
4253
putSampleNode(new TotalDigits("dummy")); // NOI18N
4254
putSampleNode(new MinInclusive("dummy")); // NOI18N
4255
putSampleNode(new MaxInclusive("dummy")); // NOI18N
4256
putSampleNode(new FractionDigits("dummy")); // NOI18N
4257
putSampleNode(new Length("dummy")); // NOI18N
4258
putSampleNode(new WhiteSpace("dummy")); // NOI18N
4259
putSampleNode(new Key());
4260        putSampleNode(new Unique());
4261        putSampleNode(new KeyRef());
4262        putSampleNode(new Selector());
4263        putSampleNode(new Field());
4264        putSampleNode(new Include("dummy")); // NOI18N
4265
putSampleNode(new Import());
4266        putSampleNode(new SimpleContent());
4267        putSampleNode(new ComplexContent());
4268        putSampleNode(new Extension());
4269        putSampleNode(new ListElement());
4270        putSampleNode(new Any());
4271        putSampleNode(new AnyAttribute());
4272    }
4273
4274
4275    /**
4276     * Example: ns=xsd, URI=http://www.w3.org/2001/XMLSchema
4277     */

4278    public void addToNamespace(String JavaDoc ns, String JavaDoc URI) {
4279        //System.out.println("Adding namespace "+ns+" as "+URI);
4280
namespaceTable.put(ns, URI);
4281    }
4282
4283    /**
4284     * Return the namespace URI for a given namespace prefix.
4285     * Will return null if the namespace URI is unheard of.
4286     * Example: ns="xsd", returns "http://www.w3.org/2001/XMLSchema"
4287     */

4288    public String JavaDoc getNamespaceURI(String JavaDoc ns) {
4289        return (String JavaDoc) namespaceTable.get(ns);
4290    }
4291
4292    /**
4293     * Return a namespace name for a given namespace URI. One will
4294     * be made up, if it doesn't already exist.
4295     * Example: URI="http://www.w3.org/2001/XMLSchema", returns "xsd"
4296     */

4297    public String JavaDoc getNamespace(String JavaDoc URI) {
4298        //assert !URI.equals("xml");
4299
String JavaDoc ns;
4300        Iterator it = namespaceTable.keySet().iterator();
4301        while (it.hasNext()) {
4302            ns = (String JavaDoc) it.next();
4303            if (URI.equals(namespaceTable.get(ns)))
4304                return ns;
4305        }
4306        ns = guessPrefix(URI);
4307        //System.out.println("guessing ns: ns="+ns+" gets="+namespaceTable.get(ns)+" URI="+URI);
4308
String JavaDoc baseNs = ns;
4309        for (int count = 2; namespaceTable.containsKey(ns); ++count)
4310            ns = baseNs+count;
4311        namespaceTable.put(ns, URI);
4312        return ns;
4313    }
4314
4315    /**
4316     * @return the Set of all namespace prefixes in use.
4317     */

4318    public Set getAllNamespaces() {
4319        return namespaceTable.keySet();
4320    }
4321
4322    public String JavaDoc getXSDNamespace() {
4323        String JavaDoc ns = getNamespace(XSD_NS);
4324        if (ns == null)
4325            return "xsd";
4326        return ns;
4327    }
4328
4329    public String JavaDoc getXMLNamespace() {
4330        String JavaDoc ns = getNamespace("http://www.w3.org/XML/1998/namespace");
4331        if (ns == null)
4332            return "xml";
4333        return ns;
4334    }
4335
4336    public Attribute getAttribute(String JavaDoc name) {
4337        if (name == null)
4338            return null;
4339        String JavaDoc ns = prefixOf(name);
4340        if (ns == null)
4341            name = resolveNamespace(documentNamespace, removePrefix(name));
4342        else
4343            name = resolveNamespace(ns, removePrefix(name));
4344        //System.out.println("getAttribute: looking up "+name);
4345
Attribute result = (Attribute) definedAttributes.get(name);
4346        if (result == null && ns == null) {
4347            // try the other namespace
4348
name = resolveNamespace(targetNamespace, name);
4349            //System.out.println("getAttribute2: looking up "+name);
4350
result = (Attribute) definedAttributes.get(name);
4351        }
4352        return result;
4353    }
4354
4355    protected void putSampleNode(ElementExpr ee) {
4356        sampleNodes.put(ee.getName(), ee);
4357    }
4358
4359    protected ElementExpr getSampleNode(String JavaDoc name) {
4360        return (ElementExpr) sampleNodes.get(name);
4361    }
4362
4363    public void setTargetNamespace(String JavaDoc ns) {
4364        targetNamespace = ns;
4365    }
4366
4367    public String JavaDoc getTargetNamespace() {
4368        return targetNamespace;
4369    }
4370
4371    public void setCurrentParsedURI(String JavaDoc uri) {
4372        currentParsedURI = uri;
4373    }
4374
4375    protected String JavaDoc getCurrentParsedURI() {
4376        return currentParsedURI;
4377    }
4378
4379    public void setRootElement(ContainsSubElements el) {
4380        if (debug)
4381            System.out.println("Changing rootElement of "+this+" to "+el);
4382        rootElement = el;
4383    }
4384
4385    public ContainsSubElements getRootElement() {
4386        return rootElement;
4387    }
4388
4389    public void addToTopAnnotation(ElementExpr subElement) {
4390        if (topAnnotation == null)
4391            topAnnotation = new Annotation();
4392        topAnnotation.addSubElement(subElement);
4393    }
4394
4395    public void addAppInfoToTopAnnotation(String JavaDoc name, String JavaDoc value) {
4396        AppInfo ai = new AppInfo();
4397        AnyNode ue = new AnyNode(name, value);
4398        ai.addSubElement(ue);
4399        addToTopAnnotation(ai);
4400    }
4401
4402    public void pushCurrent(ElementExpr el) {
4403        if (currentElementStack.empty()) {
4404            //System.out.println("Pushing '"+el+"'");
4405
setRootElement((ContainsSubElements)el);
4406        } else {
4407            //System.out.println("Pushing '"+el+"' into '"+peekCurrent()+"'");
4408
peekCurrentNeedSub().addSubElement(el);
4409        }
4410        currentElementStack.push(el);
4411    }
4412
4413    /**
4414     * Create the element, then push it onto the stack, making it the
4415     * current one.
4416     */

4417    public void pushElement(String JavaDoc elementName, String JavaDoc elementType) {
4418        pushCurrent(createElement(elementName, elementType));
4419    }
4420
4421    public void pushSchemaNode() {
4422        pushCurrent(new SchemaNode());
4423    }
4424
4425    public void pushComplexType() {
4426        pushCurrent(new ComplexType());
4427    }
4428
4429    public void pushSequence() {
4430        pushCurrent(new Sequence());
4431    }
4432
4433    public ElementExpr popCurrent() {
4434        lastPopped = (ElementExpr) currentElementStack.pop();
4435        return lastPopped;
4436    }
4437
4438    public ElementExpr peekCurrent() {
4439        return (ElementExpr) currentElementStack.peek();
4440    }
4441    
4442    /**
4443     * Find an ancestor in the current element stack of a certain type.
4444     * Will return null if not found.
4445     */

4446    private ElementExpr findAncestor(Class JavaDoc type) {
4447        for (int i = currentElementStack.size() - 1; i >= 0; --i) {
4448            ElementExpr ee = (ElementExpr) currentElementStack.get(i);
4449            if (type.isAssignableFrom(ee.getClass()))
4450                return ee;
4451        }
4452        return null;
4453    }
4454
4455    /**
4456     * Same thing as peekCurrent, but the caller needs it to be a
4457     * ContainsSubElements. If it isn't an exception is thrown.
4458     */

4459    public ContainsSubElements peekCurrentNeedSub() {
4460        if (!(currentElementStack.peek() instanceof ContainsSubElements))
4461            throw new ClassCastException JavaDoc("Expected ContainsSubElements, but got "+currentElementStack.peek().getClass()+" instead on object "+currentElementStack.peek());
4462        return (ContainsSubElements) currentElementStack.peek();
4463    }
4464
4465    public ElementExpr getLastPopped() {
4466        return lastPopped;
4467    }
4468
4469    /**
4470     * Create an Element an add it to the current one in the stack.
4471     */

4472    public void addElement(String JavaDoc name, String JavaDoc type) {
4473        Element el = createElement(name);
4474        el.setType(type);
4475        if (currentElementStack.empty())
4476            setRootElement(el);
4477        else
4478            peekCurrentNeedSub().addSubElement(el);
4479    }
4480
4481    /**
4482     * Create AppInfo and add it to the current thing in the stack.
4483     */

4484    public void addAppInfo(String JavaDoc name, String JavaDoc value) {
4485        ElementExpr e = peekCurrent();
4486        Annotation ann;
4487        if (e instanceof Annotation)
4488            ann = (Annotation) e;
4489        else
4490            ann = new Annotation();
4491        AppInfo ai = new AppInfo();
4492        AnyNode ue = new AnyNode(name, value);
4493        ai.addSubElement(ue);
4494        ann.addSubElement(ai);
4495        peekCurrentNeedSub().addSubElement(ann);
4496    }
4497
4498    /**
4499     * Only works if the current thing in the stack is an Element.
4500     * @t should be a java type.
4501     */

4502   public void setType(String JavaDoc t) {
4503        ElementExpr e = peekCurrent();
4504        if (e instanceof Element)
4505            ((Element)e).setType(t);
4506        else
4507            throw new IllegalStateException JavaDoc(Common.getMessage("MSG_TryingToCallOnWrongClass", "setType", e.getClass()));
4508    }
4509
4510    public List findAllSubElements(String JavaDoc name) {
4511        List lst = new LinkedList();
4512        rootElement.findAllSubElements(name, lst);
4513        return lst;
4514    }
4515
4516    /**
4517     * Only works if the current thing in the stack is an Element.
4518     * @t should be a java type.
4519     */

4520    /*
4521   public void setDefiningType(String t) {
4522        ElementExpr e = peekCurrent();
4523        if (e instanceof Element) {
4524            Element el = (Element) e;
4525            el.setDefiningType(t);
4526        } else
4527            throw new IllegalStateException(Common.getMessage("MSG_TryingToCallOnWrongClass", "setDefiningType", e.getClass()));
4528    }
4529    */

4530
4531    /**
4532     * Only works if the current thing in the stack is an Element.
4533     * @t should be a XML Schema type.
4534     */

4535   public void setXMLSchemaType(String JavaDoc t) {
4536        ElementExpr e = peekCurrent();
4537        if (e instanceof Element)
4538            ((Element)e).setXMLSchemaType(t);
4539        else
4540            throw new IllegalStateException JavaDoc(Common.getMessage("MSG_TryingToCallOnWrongClass", "setXMLSchemaType", e.getClass()));
4541    }
4542
4543    /**
4544     * Only works if the current thing in the stack is an Element or ModelGroup.
4545     */

4546    public void setMinOccurs(String JavaDoc t) {
4547        ElementExpr e = peekCurrent();
4548        if (e instanceof Element)
4549            ((Element)e).setMinOccurs(t);
4550        else if (e instanceof ModelGroup)
4551            ((ModelGroup)e).setMinOccurs(t);
4552        else
4553            throw new IllegalStateException JavaDoc(Common.getMessage("MSG_TryingToCallOnWrongClass", "setMinOccurs", e.getClass()));
4554    }
4555
4556    /**
4557     * Only works if the current thing in the stack is an Element or ModelGroup.
4558     */

4559    public void setMaxOccurs(String JavaDoc t) {
4560        ElementExpr e = peekCurrent();
4561        if (e instanceof Element)
4562            ((Element)e).setMaxOccurs(t);
4563        else if (e instanceof ModelGroup)
4564            ((ModelGroup)e).setMaxOccurs(t);
4565        else
4566            throw new IllegalStateException JavaDoc(Common.getMessage("MSG_TryingToCallOnWrongClass", "setMaxOccurs", e.getClass()));
4567    }
4568
4569    /**
4570     * Return an Element that represents an ELEMENT.
4571     */

4572    public Element createElement(String JavaDoc name) {
4573        Element el = new Element(name);
4574        return el;
4575    }
4576
4577    /**
4578     * Return an Element that represents an ELEMENT.
4579     */

4580    public Element createElement(String JavaDoc name, String JavaDoc type) {
4581        Element el = new Element(name);
4582        el.setType(type);
4583        return el;
4584    }
4585
4586    /*
4587    public Attribute createAttirbute(String name) {
4588        Attribute attr = (Attribute) attributeTable.get(name);
4589        if (attr == null) {
4590            attr = new Attribute(name);
4591            attributeTable.put(name, attr);
4592        }
4593        return attr;
4594    }
4595    */

4596
4597    public void addSubElement(String JavaDoc elementName, ElementExpr subElement) {
4598        Element el = createElement(elementName);
4599        el.addSubElement(subElement);
4600    }
4601
4602    public void addSubElement(String JavaDoc elementName, List subElements) {
4603        Element el = createElement(elementName);
4604        el.addSubElement(subElements);
4605    }
4606
4607    public void addSubElement(String JavaDoc elementName, String JavaDoc subElementName) {
4608        Element subElement = createElement(subElementName);
4609        addSubElement(elementName, subElement);
4610    }
4611
4612    /**
4613     * This addSubElement creates an Element and then adds it underneath
4614     * the current one.
4615     */

4616    public void addSubElementCurrent(String JavaDoc subElementName) {
4617        Element subElement = createElement(subElementName);
4618        peekCurrentNeedSub().addSubElement(subElement);
4619    }
4620
4621    /**
4622     * This is called before writing out a schema.
4623     */

4624    public ElementExpr optimize() {
4625        if (rootElement == null)
4626            return null;
4627        return rootElement.optimize();
4628    }
4629
4630    /**
4631     * Returns false if the schema is too simpile to need binding classes.
4632     */

4633    /*
4634    public boolean needsBindingClasses() {
4635        if (rootElement == null)
4636            return false;
4637        System.out.println("rootElement="+rootElement+" .class="+rootElement.getClass());
4638        if (rootElement instanceof Element) {
4639            Element root = (Element) rootElement;
4640            return root.hasNamedSubElements();
4641        }
4642        return true;
4643    }
4644    */

4645
4646    /**
4647     * If you want a DTD written to a Writer, this is the method
4648     * to call.
4649     */

4650    public void writeDTD(Writer out) throws java.io.IOException JavaDoc {
4651        if (rootElement == null)
4652            return;
4653        elementTable = new HashMap();
4654        optimize();
4655        rootElement.validate();
4656        StringBuffer JavaDoc outBuffer = new StringBuffer JavaDoc();
4657        rootElement.writeDTD(outBuffer);
4658        out.write(outBuffer.toString());
4659        elementTable = null;
4660    }
4661
4662    /**
4663     * nice method which returns writeDTD in an InputStream.
4664     */

4665    public InputStream dtdInputStream() throws java.io.IOException JavaDoc {
4666        StringWriter strWriter = new StringWriter();
4667        PrintWriter pw = new PrintWriter(strWriter);
4668        writeDTD(pw);
4669        pw.flush();
4670        ByteArrayInputStream bais = new ByteArrayInputStream(strWriter.toString().getBytes());
4671        return bais;
4672    }
4673
4674    /**
4675     * If you want an XML Schema written to a Writer, this is the method
4676     * to call.
4677     */

4678    public void writeXMLSchemaStandalone(Writer out) throws java.io.IOException JavaDoc {
4679        if (rootElement == null)
4680            return;
4681        XMLWriter xw = new XMLWriter();
4682        ContainsSubElements realRootElement = rootElement;
4683        if (!(realRootElement instanceof SchemaNode)) {
4684            SchemaNode sn = new SchemaNode();
4685            sn.addSubElement(realRootElement);
4686            realRootElement = sn;
4687            if (topAnnotation != null) {
4688                sn.addSubElement(topAnnotation);
4689            }
4690        }
4691
4692        if (true || optimize() != null) {
4693            realRootElement.validate();
4694            realRootElement.writeXMLSchema(xw);
4695        }
4696        xw.writeTo(out);
4697    }
4698
4699    /**
4700     * Convience method which returns writeXMLSchemaStandalone in an
4701     * InputStream.
4702     */

4703    public InputStream xmlSchemaInputStream() throws java.io.IOException JavaDoc {
4704        StringWriter strWriter = new StringWriter();
4705        PrintWriter pw = new PrintWriter(strWriter);
4706        writeXMLSchemaStandalone(pw);
4707        pw.flush();
4708        ByteArrayInputStream bais = new ByteArrayInputStream(strWriter.toString().getBytes());
4709        return bais;
4710    }
4711
4712    public void writeXMLSchema(XMLWriter out) throws IOException {
4713        if (rootElement == null)
4714            return;
4715        rootElement.writeXMLSchema(out);
4716    }
4717
4718    public void readSchemaFromLocation(String JavaDoc schemaLocation) throws IOException, SAXException {
4719        if (debug)
4720            System.out.println("Reading schema from "+schemaLocation);
4721        if (schemaLocation == null || "".equals(schemaLocation))
4722            return;
4723
4724        String JavaDoc oldParsedURI = currentParsedURI;
4725        try {
4726            javax.xml.parsers.DocumentBuilderFactory JavaDoc dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
4727            dbf.setNamespaceAware(true);
4728            dbf.setIgnoringComments(true);
4729            dbf.setIgnoringElementContentWhitespace(true);
4730            javax.xml.parsers.DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
4731            Document doc = null;
4732            IOException ioException = null;
4733            SAXException saxException = null;
4734            try {
4735                doc = db.parse(schemaLocation);
4736                currentParsedURI = schemaLocation;
4737            } catch (java.io.FileNotFoundException JavaDoc e) {
4738                ioException = e;
4739            } catch (org.xml.sax.SAXParseException JavaDoc e) {
4740                // Apache will throw this one for a file not found error,
4741
// it really should be java.io.FileNotFoundException
4742
saxException = e;
4743            }
4744            if (ioException != null || saxException != null) {
4745                //System.out.println("currentParsedURI="+currentParsedURI);
4746
if (currentParsedURI != null) {
4747                    // Try making a relative URI out of this.
4748
java.net.URI JavaDoc uri;
4749                    try {
4750                        uri = new java.net.URI JavaDoc(currentParsedURI);
4751                    } catch (java.net.URISyntaxException JavaDoc e) {
4752                        uri = new File(currentParsedURI).toURI();
4753                    }
4754                    if (debug)
4755                        System.out.println("uri="+uri);
4756                    java.net.URI JavaDoc schemaLocationURI = uri.resolve(schemaLocation);
4757                    currentParsedURI = schemaLocationURI.toString();
4758                    if (debug)
4759                        System.out.println("Since the first try failed, now trying to read currentParsedURI:"+ currentParsedURI);
4760                    doc = db.parse(currentParsedURI);
4761                    ioException = null;
4762                    saxException = null;
4763                }
4764            }
4765            if (ioException != null)
4766                throw ioException;
4767            if (saxException != null)
4768                throw saxException;
4769            org.w3c.dom.Element JavaDoc childNode = doc.getDocumentElement();
4770            if (childNode != null) {
4771                if (!childNode.getLocalName().equals("schema")) // NOI18N
4772
throw new IllegalStateException JavaDoc(Common.getMessage("MSG_ExpectedNode", "schema", childNode.getNodeName()));
4773                // Make sure to preserve the old stuff.
4774
readSchemaElement(childNode);
4775            }
4776        } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
4777            throw new Schema2BeansRuntimeException(Common.getMessage("MSG_FailedToParse", schemaLocation), e);
4778        } finally {
4779            currentParsedURI = oldParsedURI;
4780        }
4781    }
4782
4783    public void readDocument(Document doc) {
4784        includedAlready.clear();
4785        org.w3c.dom.Element JavaDoc childNode = doc.getDocumentElement();
4786        if (childNode != null) {
4787            if (!childNode.getLocalName().equals("schema")) // NOI18N
4788
throw new IllegalStateException JavaDoc(Common.getMessage("MSG_ExpectedNode", "schema", childNode.getNodeName()));
4789            pushSchemaNode();
4790            peekCurrentNeedSub().readSchema(childNode);
4791            readSchemaElement(childNode);
4792            popCurrent();
4793        }
4794    }
4795
4796    public void readSchemaElement(org.w3c.dom.Element JavaDoc el) {
4797        NamedNodeMap attrs = el.getAttributes();
4798        String JavaDoc theTargetNamespace = null;
4799        String JavaDoc theDocumentNamespaceURI = null;
4800        for (int i = 0, size = attrs.getLength(); i < size; ++i) {
4801            Node attr = attrs.item(i);
4802            //System.out.println("readSchemaElement: attr.prefix="+attr.getPrefix()+" localname="+attr.getLocalName()+" nodename="+attr.getNodeName()+" nodevalue="+attr.getNodeValue());
4803
if ("xmlns".equals(attr.getPrefix())) {
4804                addToNamespace(attr.getLocalName(), attr.getNodeValue());
4805            } else if ("targetNamespace".equals(attr.getNodeName())) {
4806                theTargetNamespace = attr.getNodeValue();
4807            } else if ("xmlns".equals(attr.getNodeName())) {
4808                theDocumentNamespaceURI = attr.getNodeValue();
4809            }
4810        }
4811        if (theDocumentNamespaceURI != null) {
4812            documentNamespace = theDocumentNamespaceURI;
4813            //System.out.println("readSchemaElement: just set documentNamespace to "+documentNamespace);
4814
}
4815        if (theTargetNamespace != null) {
4816            // This needs to be done after all the namespaces have been read
4817
targetNamespace = theTargetNamespace;
4818            //System.out.println("readSchemaElement: just set targetNamespace to "+targetNamespace);
4819
}
4820        ContainsSubElements re = getRootElement();
4821        if (re instanceof SchemaNode) {
4822            SchemaNode schemaNode = (SchemaNode) re;
4823            schemaNode.merge(el);
4824        }
4825        read(el);
4826    }
4827
4828    protected void read(Node node) {
4829        String JavaDoc nodeName;
4830        boolean ignoreUnknown = false;
4831        boolean keepText = false;
4832        if (peekCurrent() instanceof AnyNode || peekCurrent() instanceof Documentation || peekCurrent() instanceof AppInfo || peekCurrent() instanceof TextNode) {
4833            keepText = true;
4834            ignoreUnknown = true;
4835        }
4836        AnyNode anyNode = new AnyNode("dummy1", "dummy2"); // NOI18N
4837
TextNode textNode = new TextNode("dummy"); // NOI18N
4838
NodeList children = node.getChildNodes();
4839        for (int i = 0; i < children.getLength(); ++i) {
4840            Node childNode = children.item(i);
4841            if (childNode instanceof org.w3c.dom.Element JavaDoc) {
4842                org.w3c.dom.Element JavaDoc childElement = (org.w3c.dom.Element JavaDoc) childNode;
4843                //nodeName = childElement.getNodeName().intern();
4844
nodeName = childElement.getLocalName().intern();
4845                //System.out.println("Found a "+nodeName);
4846
ElementExpr ee = getSampleNode(nodeName);
4847                if (ee == null) {
4848                    if (!ignoreUnknown)
4849                        System.out.println("SchemaRep Warning: unknown node at "+getXPathExpr(childElement));
4850                    anyNode.readSchema(childElement);
4851                } else {
4852                    ee.readSchema(childElement);
4853                }
4854            } else if (keepText && childNode instanceof Text) {
4855                textNode.readSchema((Text)childNode);
4856            }
4857        }
4858    }
4859
4860    static public String JavaDoc getXPathExpr(Node node) {
4861        return getXPathExpr(node, true);
4862    }
4863
4864    static public String JavaDoc getXPathExpr(Node node, boolean addDescription) {
4865        if (node instanceof Document)
4866            return "/";
4867        Node parentNode = node.getParentNode();
4868        if (parentNode instanceof Document)
4869            return "/" + node.getNodeName();
4870        String JavaDoc curNodeName = node.getNodeName();
4871        if (addDescription && node instanceof org.w3c.dom.Element JavaDoc) {
4872            String JavaDoc nameAttr = ((org.w3c.dom.Element JavaDoc)node).getAttribute("name");
4873            if (nameAttr != null && !"".equals(nameAttr))
4874                curNodeName += " name='"+nameAttr+"'";
4875        }
4876        return getXPathExpr(node.getParentNode(), addDescription) + "/" + curNodeName;
4877    }
4878
4879    public String JavaDoc schemaTypeToJavaType(String JavaDoc xmlSchemaType) {
4880        ElementExpr schemaTypeDef = getSchemaTypeDef(xmlSchemaType);
4881        if (schemaTypeDef == null) {
4882            System.out.println(Common.getMessage("MSG_UnableToFindTypeDef", xmlSchemaType));
4883            return null;
4884        }
4885
4886        String JavaDoc javaType = null;
4887        //System.out.println("schemaTypeToJavaType: "+xmlSchemaType+" schemaTypeDef="+schemaTypeDef);
4888
if (schemaTypeDef instanceof HasJavaTypeName) {
4889            javaType = ((SchemaRep.HasJavaTypeName)schemaTypeDef).getJavaTypeName();
4890        } else {
4891            System.out.println("!!! What's the java type of "+schemaTypeDef+" for "+xmlSchemaType);
4892        }
4893        //System.out.println("schemaTypeToJavaType: "+xmlSchemaType+" -> "+javaType);
4894
return javaType;
4895    }
4896
4897    /**
4898     * Get an ElementExpr node for a named type.
4899     * null is an acceptable return value if not found.
4900     * If @param typeName is null, null is returned.
4901     * If @param typeName's namespace is not set, then we first
4902     * check the documentNamespace, then the targetNamespace.
4903     */

4904    public ElementExpr getSchemaTypeDef(String JavaDoc origTypeName) {
4905        String JavaDoc typeName = origTypeName;
4906        //System.out.println("getSchemaTypeDef: looking up "+typeName);
4907
if (typeName == null)
4908            return null;
4909        typeName = resolveNamespace(typeName);
4910        ElementExpr result = (ElementExpr) schemaTypeDefs.get(typeName);
4911        if (result != null)
4912            return result;
4913        String JavaDoc ns = prefixOf(origTypeName);
4914        if (ns == null)
4915            typeName = canonicalQName(documentNamespace,
4916                                      removePrefix(origTypeName));
4917        result = (ElementExpr) schemaTypeDefs.get(typeName);
4918        if (result != null)
4919            return result;
4920        /*
4921        if (result == null && ns == null) {
4922            // try the other namespace
4923            typeName = resolveNamespace(targetNamespace, typeName);
4924            System.out.println("getSchemaTypeDef2: looking up "+typeName);
4925            result = (ElementExpr) schemaTypeDefs.get(typeName);
4926        }
4927        */

4928        return (ElementExpr) definedTypes.get(origTypeName);
4929    }
4930
4931    /**
4932     * Same thing as getSchemaTypeDef, but for already resolved type names.
4933     */

4934    public ElementExpr getSchemaTypeDefResolvedNamespace(String JavaDoc typeName) {
4935        ElementExpr result = (ElementExpr) schemaTypeDefs.get(typeName);
4936        if (result == null) {
4937            result = (ElementExpr) definedTypes.get(typeName);
4938        }
4939        return result;
4940    }
4941
4942    /**
4943     * @return the Set of all defined element names in a format that
4944     * can be passed to getSchemaTypeDefResolvedNamespace.
4945     */

4946    public Set/*<String>*/ getSchemaTypeNames() {
4947        return schemaTypeDefs.keySet();
4948    }
4949
4950
4951    public Element getDefinedElement(String JavaDoc typeName) {
4952        String JavaDoc t = resolveNamespace(typeName);
4953        return getDefinedElementResolvedNamespace(t);
4954    }
4955
4956    /**
4957     * @param typeName is the resolved type name "{http://foo}login"
4958     * @return the Element which defines it, or null if not found.
4959     */

4960    public Element getDefinedElementResolvedNamespace(String JavaDoc typeName) {
4961        return (Element) definedTypesFull.get(typeName);
4962    }
4963
4964    /**
4965     * @return the Set of all defined element names in a format that
4966     * can be passed to getDefinedElementResolvedNamespace.
4967     */

4968    public Set/*<String>*/ getDefinedElementNames() {
4969        return definedTypesFull.keySet();
4970    }
4971
4972    public void putSchemaTypeDef(String JavaDoc typeName, ElementExpr ee) {
4973        if (typeName == null)
4974            return;
4975        String JavaDoc ns = prefixOf(typeName);
4976        if (ns == null)
4977            typeName = resolveNamespace(targetNamespace, removePrefix(typeName));
4978        else
4979            typeName = resolveNamespace(ns, removePrefix(typeName));
4980        //System.out.println("putSchemaTypeDef: putting in "+typeName);
4981
schemaTypeDefs.put(typeName, ee);
4982    }
4983
4984    /**
4985     * Will convert a typeName into {namespace}typeName format.
4986     * eg: 'xsd:string' -> '{http://www.w3.org/2001/XMLSchema}string'
4987     * eg: 'item' -> 'item'
4988     */

4989    public String JavaDoc resolveNamespace(String JavaDoc typeName) {
4990        return resolveNamespaceDefault(typeName, targetNamespace);
4991    }
4992
4993    protected String JavaDoc resolveNamespaceDefault(String JavaDoc typeName, String JavaDoc defaultNS) {
4994        if (typeName == null)
4995            return null;
4996        String JavaDoc prefix = prefixOf(typeName);
4997        //System.out.println("resolveNamespace1: ns="+ns+" typeName="+typeName);
4998
typeName = resolveNamespaceDefault(prefix, defaultNS, removePrefix(typeName));
4999        //System.out.println("resolveNamespace2: typeName="+typeName);
5000
return typeName;
5001    }
5002
5003    public String JavaDoc resolveNamespace(String JavaDoc ns, String JavaDoc type) {
5004        return resolveNamespaceDefault(ns, targetNamespace, type);
5005    }
5006
5007    protected String JavaDoc resolveNamespaceDefault(String JavaDoc prefix, String JavaDoc defaultNS, String JavaDoc type) {
5008        String JavaDoc uri;
5009        if (prefix == null) {
5010            if (defaultNS == null)
5011                return type;
5012            uri = defaultNS;
5013        } else {
5014            uri = getNamespaceURI(prefix);
5015            if (uri == null) {
5016                System.out.println("resolveNamespace: Namespace prefix '"+prefix+"' was not found (type="+type+").");
5017                new Exception JavaDoc().printStackTrace();
5018            }
5019        }
5020        return canonicalQName(uri, type);
5021    }
5022
5023    public static String JavaDoc canonicalQName(String JavaDoc namespaceURI, String JavaDoc localPart) {
5024        if (namespaceURI == null || namespaceURI.equals(""))
5025            return localPart;
5026        return "{" + namespaceURI + "}" + localPart;
5027    }
5028
5029    /**
5030     * Will add a namespace prefix to a type if the targetNamespace != null.
5031     * eg: targetNamespace='j2ee' type='ejbType' -> 'j2ee:ejbType'
5032     * eg: targetNamespace='j2ee' type='xsd:string' -> 'xsd:string'
5033     * eg: targetNamespace=null type='item' -> 'item'
5034     */

5035    protected String JavaDoc normalizeTargetNamespace(String JavaDoc type) {
5036        if (type == null)
5037            return null;
5038        if (targetNamespace == null)
5039            return type;
5040        if (type.indexOf(':') >= 0)
5041            return type;
5042        return normalizeNamespace(targetNamespace, type);
5043    }
5044    
5045    /**
5046     * return prefix ':' localPart
5047     * eg: namespaceURI="http://www.w3.org/XML/1998/namespace" localPart="id" -> "xml:id"
5048     * eg: namespaceURI="" localPart="foo" -> "foo"
5049     * eg: namespaceURI=null localPart="bar" -> "bar"
5050     */

5051    protected String JavaDoc normalizeNamespace(String JavaDoc namespaceURI, String JavaDoc localPart) {
5052        if (localPart == null)
5053            return null;
5054        if (namespaceURI == null || "".equals(namespaceURI))
5055            return localPart;
5056        String JavaDoc prefix = getNamespace(namespaceURI);
5057        return prefix + ":" + localPart;
5058    }
5059
5060    protected String JavaDoc normalizeDocumentNamespace(String JavaDoc type) {
5061        if (type == null)
5062            return null;
5063        if (documentNamespace == null)
5064            return type;
5065        if (type.indexOf(':') >= 0)
5066            return type;
5067        return normalizeNamespace(documentNamespace, type);
5068    }
5069
5070    /**
5071     * @param schemaTypeName The name of a leaf schema element, like 'integer'.
5072     * @param javaType The java class schemaTypeName gets mapped to, like 'int'.
5073     */

5074    public void setSchemaTypeMapping(String JavaDoc schemaTypeNamespace,
5075                                     String JavaDoc schemaTypeName, String JavaDoc javaType) {
5076        //System.out.println("setSchemaTypeMapping: schemaTypeNamespace="+schemaTypeNamespace+" schemaTypeName="+schemaTypeName+" javaType="+javaType);
5077
String JavaDoc ns;
5078        SimpleType st;
5079        if (schemaTypeNamespace == null) {
5080            st = new SimpleType(schemaTypeName, javaType);
5081        } else {
5082            ns = getNamespace(schemaTypeNamespace);
5083            st = new SimpleType(ns+":"+schemaTypeName, javaType);
5084        }
5085    }
5086
5087    /**
5088     * Setup the schema to java type mapping to make more sense for J2ME.
5089     */

5090    public void setSchemaTypesForME(boolean isFloatingPoint) {
5091        setSchemaTypeMapping(XSD_NS, "anyURI", "java.lang.String");
5092        setSchemaTypeMapping(XSD_NS, "decimal", isFloatingPoint ? "double" : "java.lang.String");
5093        setSchemaTypeMapping(XSD_NS, "integer", "long");
5094        if (!isFloatingPoint) {
5095            setSchemaTypeMapping(XSD_NS, "double", "java.lang.String");
5096            setSchemaTypeMapping(XSD_NS, "float", "java.lang.String");
5097        }
5098    }
5099
5100    private void insertPredefinedSchemaTypes(Map st) {
5101        //System.out.println("Hit insertPredefinedSchemaTypes");
5102
String JavaDoc namespace = getXSDNamespace()+":";
5103        String JavaDoc[] types = new String JavaDoc[] {"java.lang.String", "java.util.Calendar", "java.util.Calendar",
5104             "long", "int", "char", "short", "double", "float", "byte", "boolean",
5105             "java.lang.String", "long", "long", "long", "long", "long", "int", "short",
5106             "byte", "java.net.URI", "javax.xml.namespace.QName", "java.lang.String", "java.lang.String",
5107             "java.lang.String", "java.lang.String", "java.lang.String",
5108             "java.lang.String", "java.lang.String", "int", "java.lang.String", "java.lang.String",
5109             "java.lang.String", "java.lang.String"};
5110        String JavaDoc[] schemaType = new String JavaDoc[] {"string", "dateTime", "date",
5111             "long", "int", "char", "short", "double", "float", "byte", "boolean",
5112             "NMTOKEN", "positiveInteger", "nonNegativeInteger", "nonPositiveInteger", "negativeInteger", "unsignedLong", "unsignedInt", "unsignedShort",
5113             "unsignedByte", "anyURI", "QName", "NCName", "Name",
5114             "duration", "time", "ID",
5115             "token", "normalizedString", "gYear", "gYearMonth", "gMonthDay",
5116             "gDay", "gMonth"};
5117        for (int i = 0; i < schemaType.length; ++i)
5118            st.put(schemaType[i], new SimpleType(namespace+schemaType[i], types[i]));
5119        if (useBigDataTypes) {
5120            st.put("decimal", new SimpleType(namespace+"decimal", "java.math.BigDecimal"));
5121            st.put("integer", new SimpleType(namespace+"integer", "java.math.BigInteger"));
5122        } else {
5123            st.put("decimal", new SimpleType(namespace+"decimal", "double"));
5124            st.put("integer", new SimpleType(namespace+"integer", "long"));
5125        }
5126        String JavaDoc[] whiteSpaceReplace =
5127            new String JavaDoc[] {"normalizedString"};
5128        for (int i = 0; i < whiteSpaceReplace.length; ++i) {
5129            SimpleType t = (SimpleType) st.get(whiteSpaceReplace[i]);
5130            Restriction restrict = new Restriction(namespace+"string");
5131            t.addSubElement(restrict);
5132            WhiteSpace ws = new WhiteSpace("replace");
5133            restrict.addSubElement(ws);
5134        }
5135        String JavaDoc[] whiteSpaceCollapse =
5136            new String JavaDoc[] {"token", "NMTOKEN", "Name", "NCName", "ID"};
5137        for (int i = 0; i < whiteSpaceCollapse.length; ++i) {
5138            SimpleType t = (SimpleType) st.get(whiteSpaceCollapse[i]);
5139            Restriction restrict = new Restriction(namespace+"normalizedString");
5140            t.addSubElement(restrict);
5141            WhiteSpace ws = new WhiteSpace("collapse");
5142            restrict.addSubElement(ws);
5143        }
5144        addRestriction((SimpleType) st.get("nonPositiveInteger"), namespace+"integer", new MaxInclusive("0"));
5145        addRestriction((SimpleType) st.get("negativeInteger"), namespace+"nonPositiveInteger", new MaxExclusive("0"));
5146        addRestriction((SimpleType) st.get("nonNegativeInteger"), namespace+"integer", new MinInclusive("0"));
5147        addRestriction((SimpleType) st.get("unsignedLong"), namespace+"nonNegativeInteger", new MinInclusive("0"));
5148        addRestriction((SimpleType) st.get("unsignedInt"), namespace+"unsignedLong", new MinInclusive("0"));
5149        addRestriction((SimpleType) st.get("positiveInteger"), namespace+"nonNegativeInteger", new MinExclusive("0"));
5150
5151        st.put("hexBinary", new HexBinary());
5152        st.put("base64Binary", new Base64Binary());
5153    }
5154
5155    protected void addRestriction(ContainsSubElements cse,
5156                                  String JavaDoc restrictionBase, RestrictionType rt) {
5157        Restriction restrict = new Restriction(restrictionBase);
5158        cse.addSubElement(restrict);
5159        restrict.addSubElement(rt);
5160    }
5161
5162    /**
5163     * Map simple types to our schema.
5164     */

5165    public void mapSimpleJavaTypesOptional(Map optionalElementMap) {
5166        //System.out.println("Hit mapSimpleJavaTypesOptional");
5167
String JavaDoc namespace = getXSDNamespace()+":";
5168        // These guys are ones that we are willing to add to the XML Schema,
5169
// but are optional (not everyone uses everyone of them).
5170
String JavaDoc[] types = new String JavaDoc[] {"java.lang.Integer", "java.lang.Short", "java.lang.Long", "java.lang.Double", "java.lang.Float", "java.lang.Boolean", "java.lang.Character", "java.lang.StringBuffer", "java.lang.Byte", "java.math.BigInteger", "char[]", "char"};
5171        String JavaDoc[] restrictions = new String JavaDoc[] {"int", "short", "long", "double", "float", "boolean", "string", "string", "byte", "integer", "string", "string"};
5172        for (int i = 0; i < types.length; ++i)
5173            optionalElementMap.put(types[i], new SimpleType(javaType2XMLSchemaType(types[i]), new Restriction(namespace+restrictions[i])));
5174    }
5175
5176    private void mapSimpleAttributes(Map a) {
5177        a.put("xml:lang", new Attribute("lang", getNamespaceURI("xml"),
5178                                        "xsd:string"));
5179    }
5180
5181    /**
5182     * Map simple types to our schema.
5183     */

5184    public void mapSimpleJavaTypesPredefined(Map definedElementMap) {
5185        //System.out.println("Hit mapSimpleJavaTypesPredefined");
5186
String JavaDoc namespace = getXSDNamespace()+":";
5187        // These guys are already defined in XML Schema. The java
5188
// types have 1-to-1 mappings.
5189
String JavaDoc[] types = new String JavaDoc[] {"java.lang.String", "String", "java.math.BigDecimal", "java.util.Calendar", "long", "int", "char", "short", "double", "float", "byte", "boolean"};
5190        String JavaDoc[] schemaType = new String JavaDoc[] {"string", "string", "decimal", "dateTime", "long", "int", "char", "short", "double", "float", "byte", "boolean"};
5191        for (int i = 0; i < types.length; ++i) {
5192            ElementExpr def = getSchemaTypeDef(schemaType[i]);
5193            if (def == null)
5194                def = new SimpleType(namespace+schemaType[i], types[i]);
5195            definedElementMap.put(types[i], def);
5196        }
5197    }
5198
5199    /**
5200     * Define some helpful java to XML Schema data types that allow us
5201     * to go back and forth between the two (allows me to setup a 1-to-1
5202     * mapping with the client proxy generator).
5203     */

5204    public static void printExtraJavaTypes(Writer out, int level) throws java.io.IOException JavaDoc {
5205        // Create one of ourselves to get around the problem of calling
5206
// a nonstatic method (that does not need any context)
5207
// from a static method.
5208
SchemaRep schema = new SchemaRep();
5209        Map elementMap = new HashMap();
5210        schema.mapSimpleJavaTypesOptional(elementMap);
5211        Iterator it = elementMap.keySet().iterator();
5212        XMLWriter xw = new XMLWriter(false);
5213        while (it.hasNext()) {
5214            Object JavaDoc key = it.next();
5215            ElementExpr el = (ElementExpr) elementMap.get(key);
5216            el.writeXMLSchema(xw);
5217        }
5218        xw.writeTo(out);
5219        out.write("\n"); // NOI18N
5220
}
5221
5222    private static String JavaDoc javaType2XMLSchemaType(String JavaDoc typeName) {
5223        return javaType2XMLSchemaType(typeName, false);
5224    }
5225    /**
5226     * Convert a java type into an XML Schema type.
5227     * eg, java.lang.String -> xsd:string
5228     */

5229    private static String JavaDoc javaType2XMLSchemaType(String JavaDoc typeName,
5230                                                boolean unknownOkay) {
5231        // BEGIN_NOI18N
5232
if ("java.lang.String".equals(typeName) || "String".equals(typeName))
5233            return "xsd:string";
5234        if ("java.lang.Integer".equals(typeName) ||
5235            "java.lang.Short".equals(typeName) ||
5236            "java.lang.Long".equals(typeName) ||
5237            "java.lang.Double".equals(typeName) ||
5238            "java.lang.Boolean".equals(typeName) ||
5239            "java.lang.Character".equals(typeName) ||
5240            "java.lang.Float".equals(typeName) ||
5241            "java.lang.StringBuffer".equals(typeName) ||
5242            "java.lang.Byte".equals(typeName) ||
5243            "java.math.BigInteger".equals(typeName) ||
5244            "char".equals(typeName))
5245            return typeName;
5246        if ("char[]".equals(typeName))
5247            return "char_lb_rb";
5248        if (JavaUtil.isPrimitiveType(typeName))
5249            return "xsd:"+typeName;
5250        if ("java.math.BigDecimal".equals(typeName))
5251            return "xsd:decimal";
5252        if ("java.util.Calendar".equals(typeName))
5253            return "xsd:dateTime";
5254        if ("java.net.URI".equals(typeName))
5255            return "xsd:anyURI";
5256        if ("javax.xml.namespace.QName".equals(typeName))
5257            return "xsd:QName";
5258        if (!unknownOkay) {
5259            String JavaDoc err = "javaType2XMLSchemaType: No known XML Schema type for '"+typeName+"'.";
5260            System.out.println(err);
5261            return typeName;
5262        }
5263        return null;
5264        // END_NOI18N
5265
}
5266
5267    /**
5268     * Takes a Java type (like 'java.lang.String' or 'double') and
5269     * says whether or not javaType2XMLSchemaType will return a good,
5270     * predefined type for it.
5271     */

5272    private static boolean isXMLSchemaDefinedType(String JavaDoc typeName) {
5273        if (javaType2XMLSchemaType(typeName, true) == null)
5274            return false;
5275        return true;
5276    }
5277
5278    public String JavaDoc javaType2XMLSchemaTypeComplex(String JavaDoc type) {
5279        //System.out.println("javaType2XMLSchemaTypeComplex type="+type);
5280
Object JavaDoc el;
5281        el = definedTypes.get(type);
5282        if (el == null)
5283            el = optionallyDefinedTypes.get(type);
5284        if (el == null)
5285            el = predefinedSchemaTypes.get(type);
5286        if (el == null) {
5287            if (debug)
5288                System.out.println("No type found for "+type);
5289            return javaType2XMLSchemaType(type);
5290        }
5291        if (el instanceof SimpleType)
5292            return ((SimpleType)el).getTypeName();
5293        if (el instanceof String JavaDoc)
5294            return (String JavaDoc) el;
5295        if (el instanceof Element)
5296            return ((Element)el).getElementName();
5297        return el.getClass().toString();
5298    }
5299
5300    public boolean isDefinedType(String JavaDoc type) {
5301        if (optionallyDefinedTypes.containsKey(type))
5302            return true;
5303        if (definedTypes.containsKey(type))
5304            return true;
5305        return false;
5306    }
5307
5308    /*
5309     * Removes any namespace prefix.
5310     * eg: 'xsd:element' -> 'element'
5311     */

5312    public static String JavaDoc removePrefix(String JavaDoc typeName) {
5313        int pos = typeName.indexOf(':');
5314        if (pos < 0)
5315            return typeName;
5316        return typeName.substring(pos+1);
5317    }
5318
5319    /**
5320     * eg: 'xsd:element' -> 'xsd'
5321     */

5322    public static String JavaDoc prefixOf(String JavaDoc typeName) {
5323        int pos = typeName.indexOf(':');
5324        if (pos < 0)
5325            return null;
5326        return typeName.substring(0, pos);
5327    }
5328
5329    /**
5330     * Guess what a prefix of this namespaceURI might be.
5331     */

5332    public static String JavaDoc guessPrefix(String JavaDoc namespaceURI) {
5333        if ("http://www.w3.org/XML/1998/namespace".equals(namespaceURI))
5334            return "xml";
5335        String JavaDoc prefix;
5336        int pos = namespaceURI.lastIndexOf('/');
5337        if (pos >= 0)
5338            prefix = namespaceURI.substring(pos+1, namespaceURI.length());
5339        else
5340            prefix = namespaceURI;
5341        prefix = prefix.replace('#', '_').replace('\'', '_').replace('"', '_').
5342            replace(':', '_');
5343        // Not supposed to have a prefix that starts with xml
5344
if (prefix.startsWith("xml"))
5345            prefix = "x"+prefix;
5346        return prefix;
5347    }
5348
5349    /**
5350     * Try to figure out what prefix to use for the namespaceURI
5351     * using the namespace context mapping, and if that fails then it
5352     * calls guessPrefix.
5353     */

5354    public String JavaDoc guessPrefixFromURI(String JavaDoc uri) {
5355        String JavaDoc prefix = getNamespace(uri);
5356        if (prefix != null)
5357            return prefix;
5358        return guessPrefix(uri);
5359    }
5360}
5361
Popular Tags