protected void genFetchXMLEventReader()

in ide/schema2beans/src/org/netbeans/modules/schema2beansdev/JavaBeanClass.java [2221:3026]


    protected void genFetchXMLEventReader() throws IOException {
        jw.select(BODY_SECTION);
        String commonBean = commonBeanType();
        String beanName = beanElement.node.getName();
        if (beanElement.isRoot) {
            String beanQNameName = declareQName(beanElement.getNamespace(), beanName, null);

            jw.beginMethod("fetchXMLEventReader", "", null, "javax.xml.stream.XMLEventReader");
            jw.writeEol("return new ", getRootClassName()+".XMLEventStateManager(this, ", beanQNameName, ")");
            jw.endMethod();
            
            jw.write("static class XMLEventStateManager implements javax.xml.stream.XMLEventReader");
            jw.begin();
            jw.writeEol("private java.util.Stack<"+commonBean+"> beans = new java.util.Stack<"+commonBean+">()");
            jw.writeEol("private java.util.Stack<javax.xml.namespace.QName> nodeNames = new java.util.Stack<javax.xml.namespace.QName>()");
            jw.writeEol("private java.util.Stack<Integer> eventNumbers = new java.util.Stack<Integer>()");
            jw.writeEol("private java.util.Stack<Integer> arrayIndices = new java.util.Stack<Integer>()");
            jw.writeEol("private java.util.Stack<Integer> elementNumbers = new java.util.Stack<Integer>()");
            jw.writeEol("private "+commonBean+" currentBean");
            jw.writeEol("private javax.xml.namespace.QName currentNodeName");
            jw.writeEol("private int currentEventNumber");
            jw.writeEol("private int currentArrayIndex");
            jw.writeEol("private int currentElementNumber");
            jw.writeEol("private javax.xml.stream.events.XMLEvent next");
            jw.cr();
            jw.write("public XMLEventStateManager("+commonBean+" bean, javax.xml.namespace.QName nodeName)");
            jw.begin();
            jw.writeEol("currentBean = bean");
            jw.writeEol("this.currentNodeName = nodeName");
            jw.writeEol("currentEventNumber = 0");
            jw.writeEol("currentArrayIndex = 0");
            jw.writeEol("currentElementNumber = 0");
            jw.end();
            jw.cr();
            jw.write("public int getEventNumber()");
            jw.begin();
            jw.writeEol("return currentEventNumber");
            jw.end();
            jw.cr();
            jw.write("public void nextEventNumber()");
            jw.begin();
            jw.writeEol("++currentEventNumber");
            jw.writeEol("currentArrayIndex = 0");
            jw.writeEol("currentElementNumber = 0");
            jw.end();
            jw.cr();
            jw.write("public int getArrayIndex()");
            jw.begin();
            jw.writeEol("return currentArrayIndex");
            jw.end();
            jw.cr();
            jw.write("public void nextArrayIndex()");
            jw.begin();
            jw.writeEol("++currentArrayIndex");
            jw.writeEol("currentElementNumber = 0");
            jw.end();
            jw.cr();
            jw.write("public int getElementNumber()");
            jw.begin();
            jw.writeEol("return currentElementNumber");
            jw.end();
            jw.cr();
            jw.write("public void nextElementNumber()");
            jw.begin();
            jw.writeEol("++currentElementNumber");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.namespace.QName getNodeName()");
            jw.begin();
            jw.writeEol("return currentNodeName");
            jw.end();
            jw.cr();
            jw.write("public void enterChildBean("+commonBean+" bean, javax.xml.namespace.QName nodeName)");
            jw.begin();
            jw.writeEol("beans.push(currentBean)");
            jw.writeEol("eventNumbers.push(currentEventNumber)");
            jw.writeEol("arrayIndices.push(currentArrayIndex)");
            jw.writeEol("elementNumbers.push(currentElementNumber)");
            jw.writeEol("nodeNames.push(currentNodeName)");
            jw.writeEol("currentBean = bean");
            jw.writeEol("currentEventNumber = 0");
            jw.writeEol("currentArrayIndex = 0");
            jw.writeEol("currentElementNumber = 0");
            jw.writeEol("currentNodeName = nodeName");
            jw.end();
            jw.cr();
            jw.write("public void doneWithBean()");
            jw.begin();
            jw.write("if (beans.isEmpty())");
            jw.begin();
            jw.writeEol("currentBean = null");
            jw.writeEol("currentEventNumber = -1");
            jw.end();
            jw.write("else");
            jw.begin();
            jw.writeEol("currentBean = beans.pop()");
            jw.writeEol("currentEventNumber = eventNumbers.pop()");
            jw.writeEol("currentArrayIndex = arrayIndices.pop()");
            jw.writeEol("currentElementNumber = elementNumbers.pop()");
            jw.writeEol("currentNodeName = nodeNames.pop()");
            jw.end();
            jw.end();
            jw.cr();
            jw.write("public Object getProperty(String name) throws IllegalArgumentException");
            jw.begin();
            jw.writeEol("throw new IllegalArgumentException(\"Got no properties\")");
            jw.end();
            jw.cr();
            jw.write("public void remove()");
            jw.begin();
            jw.writeEol("throw new UnsupportedOperationException()");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.stream.events.XMLEvent peek() throws javax.xml.stream.XMLStreamException");
            jw.begin();
            jw.beginIf("next != null");
            jw.writeEol("return next");
            jw.end();
            jw.writeEol("setNextEvent()");
            jw.writeEol("return next");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.stream.events.XMLEvent nextTag() throws javax.xml.stream.XMLStreamException");
            jw.begin();
            jw.writeEol("throw new IllegalArgumentException()");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.stream.events.XMLEvent nextEvent() throws javax.xml.stream.XMLStreamException");
            jw.begin();
            jw.writeEol("javax.xml.stream.events.XMLEvent result = null");
            jw.write("if (next != null)");
            jw.begin();
            jw.writeEol("result = next");
            jw.writeEol("next = null");
            jw.writeEol("return result");
            jw.end();
            jw.writeEol("setNextEvent()");
            jw.beginIf("next == null");
            jw.writeEol("throw new java.util.NoSuchElementException()");
            jw.end();
            jw.writeEol("result = next");
            jw.writeEol("next = null");
            jw.writeEol("return result");
            jw.end();
            jw.cr();
            jw.write("private void setNextEvent() throws javax.xml.stream.XMLStreamException");
            jw.begin();
            jw.write("while (next == null && currentEventNumber >= 0)");
            jw.begin();
            jw.writeEol("next = currentBean.getXMLEvent(this)");
            jw.end();
            jw.end();
            jw.cr();
            jw.write("public Object next()");
            jw.begin();
            jw.beginTry();
            jw.writeEol("return nextEvent()");
            jw.write("} catch (javax.xml.stream.XMLStreamException e)");
            jw.begin();
            jw.writeEol("throw new RuntimeException(e)");
            jw.end();
            jw.end();
            jw.cr();
            jw.write("public boolean hasNext()");
            jw.begin();
            jw.beginIf("next != null");
            jw.writeEol("return true");
            jw.end();
            jw.beginTry();
            jw.writeEol("setNextEvent()");
            jw.endCatch("javax.xml.stream.XMLStreamException e");
            jw.writeEol("throw new RuntimeException(e)");
            jw.end();
            jw.writeEol("return (next != null)");
            jw.end();
            jw.cr();
            jw.write("public String getElementText() throws javax.xml.stream.XMLStreamException");
            jw.begin();
            jw.writeEol("throw new IllegalArgumentException()");
            jw.end();
            jw.cr();
            jw.write("public void close() throws javax.xml.stream.XMLStreamException");
            jw.begin();
            jw.writeEol("currentEventNumber = -1");
            jw.writeEol("beans.clear()");
            jw.writeEol("eventNumbers.clear()");
            jw.writeEol("arrayIndices.clear()");
            jw.writeEol("nodeNames.clear()");
            jw.end();
            jw.end();

            jw.write("static abstract class BaseXMLEvent implements javax.xml.stream.events.XMLEvent ");
            jw.begin();
            jw.write("public void writeAsEncodedUnicode(java.io.Writer writer) throws javax.xml.stream.XMLStreamException ");
            jw.begin();
            jw.writeEol("throw new UnsupportedOperationException()");
            jw.end();
            jw.cr();
            jw.write("public boolean isStartElement() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public boolean isStartDocument() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.stream.events.Characters asCharacters() ");
            jw.begin();
            jw.writeEol("return (javax.xml.stream.events.Characters) this");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.stream.events.EndElement asEndElement() ");
            jw.begin();
            jw.writeEol("return (javax.xml.stream.events.EndElement) this");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.stream.events.StartElement asStartElement() ");
            jw.begin();
            jw.writeEol("return (javax.xml.stream.events.StartElement) this");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.stream.Location getLocation() ");
            jw.begin();
            jw.writeEol("throw new UnsupportedOperationException()");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.namespace.QName getSchemaType() ");
            jw.begin();
            jw.writeEol("return null");
            jw.end();
            jw.cr();
            jw.write("public boolean isAttribute() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public boolean isCharacters() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public boolean isEndDocument() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public boolean isEndElement() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public boolean isEntityReference() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public boolean isNamespace() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public boolean isProcessingInstruction() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.end();
            jw.cr();
            jw.write("static class ThisStartDocument extends BaseXMLEvent implements javax.xml.stream.events.StartDocument ");
            jw.begin();
            jw.write("public int getEventType() ");
            jw.begin();
            jw.writeEol("return javax.xml.stream.XMLStreamConstants.START_DOCUMENT");
            jw.end();
            jw.cr();
            jw.write("public boolean isStartDocument() ");
            jw.begin();
            jw.writeEol("return true");
            jw.end();
            jw.cr();
            jw.write("public boolean standaloneSet() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public boolean isStandalone() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public String getVersion() ");
            jw.begin();
            jw.writeEol("return \"1.0\"");
            jw.end();
            jw.cr();
            jw.write("public String getSystemId() ");
            jw.begin();
            jw.writeEol("return null");
            jw.end();
            jw.cr();
            jw.write("public String getCharacterEncodingScheme() ");
            jw.begin();
            jw.writeEol("return \"UTF-8\"");
            jw.end();
            jw.cr();
            jw.write("public boolean encodingSet() ");
            jw.begin();
            jw.writeEol("return true");
            jw.end();
            jw.end();
            jw.cr();
            jw.write("static class ThisAttribute extends BaseXMLEvent implements javax.xml.stream.events.Attribute ");
            jw.begin();
            jw.writeEol("private javax.xml.namespace.QName name");
            jw.writeEol("private String value");
            jw.writeEol("private String dtdType");
            jw.writeEol("private javax.xml.namespace.QName schemaType");
            jw.cr();
            jw.write("public ThisAttribute(javax.xml.namespace.QName name, String value, String dtdType, javax.xml.namespace.QName schemaType) ");
            jw.begin();
            jw.writeEol("this.name = name");
            jw.writeEol("this.value = value");
            jw.writeEol("this.dtdType = dtdType");
            jw.writeEol("this.schemaType = schemaType");
            jw.end();
            jw.cr();
            jw.write("public int getEventType() ");
            jw.begin();
            jw.writeEol("return javax.xml.stream.XMLStreamConstants.ATTRIBUTE");
            jw.end();
            jw.cr();
            jw.write("public boolean isSpecified() ");
            jw.begin();
            jw.writeEol("return true");
            jw.end();
            jw.cr();
            jw.write("public String getValue() ");
            jw.begin();
            jw.writeEol("return value");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.namespace.QName getName() ");
            jw.begin();
            jw.writeEol("return name");
            jw.end();
            jw.cr();
            jw.write("public String getDTDType() ");
            jw.begin();
            jw.writeEol("return dtdType");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.namespace.QName getSchemaType() ");
            jw.begin();
            jw.writeEol("return schemaType");
            jw.end();
            jw.end();
            jw.cr();
            jw.write("static class ThisNamespace extends ThisAttribute implements javax.xml.stream.events.Namespace ");
            jw.begin();
            jw.writeEol("private String prefix");
            jw.cr();
            jw.write("public ThisNamespace(String prefix, String uri) ");
            jw.begin();
            jw.writeEol("super(new javax.xml.namespace.QName(\"http://FIXME/xmlns\", prefix, \"xmlns\"), uri, null, null)");
            jw.writeEol("this.prefix = prefix");
            jw.end();
            jw.cr();
            jw.write("public boolean isDefaultNamespaceDeclaration() ");
            jw.begin();
            jw.writeEol("return \"\".equals(prefix)");
            jw.end();
            jw.cr();
            jw.write("public String getPrefix() ");
            jw.begin();
            jw.writeEol("return prefix");
            jw.end();
            jw.cr();
            jw.write("public String getNamespaceURI() ");
            jw.begin();
            jw.writeEol("return getValue()");
            jw.end();
            jw.end();
            jw.cr();
            jw.write("static class ThisStartElement extends BaseXMLEvent implements javax.xml.stream.events.StartElement ");
            jw.begin();
            jw.writeEol("private javax.xml.namespace.QName name");
            jw.writeEol("private java.util.Map<javax.xml.namespace.QName, ThisAttribute> attributes = new java.util.HashMap<javax.xml.namespace.QName, ThisAttribute>()");
            jw.writeEol("private java.util.Map<String, ThisNamespace> namespaces = new java.util.HashMap<String, ThisNamespace>()");
            jw.cr();
            jw.write("public ThisStartElement(javax.xml.namespace.QName name) ");
            jw.begin();
            jw.writeEol("this.name = name");
            jw.end();
            jw.cr();
            jw.write("public void addAttribute(javax.xml.namespace.QName attrName, String attrValue, String dtdType, javax.xml.namespace.QName schemaType) ");
            jw.begin();
            jw.writeEol("attributes.put(attrName, new ThisAttribute(attrName, attrValue, dtdType, schemaType))");
            jw.end();
            jw.cr();
            jw.write("public void addNamespace(String prefix, String uri) ");
            jw.begin();
            jw.writeEol("namespaces.put(prefix, new ThisNamespace(prefix, uri))");
            jw.end();
            jw.cr();
            jw.write("public int getEventType() ");
            jw.begin();
            jw.writeEol("return javax.xml.stream.XMLStreamConstants.START_ELEMENT");
            jw.end();
            jw.cr();
            jw.write("public boolean isStartElement() ");
            jw.begin();
            jw.writeEol("return true");
            jw.end();
            jw.cr();
            jw.write("public String getNamespaceURI(String prefix) ");
            jw.begin();
            jw.writeEol("return namespaces.get(prefix).getNamespaceURI()");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.stream.events.Attribute getAttributeByName(javax.xml.namespace.QName name) ");
            jw.begin();
            jw.writeEol("return attributes.get(name)");
            jw.end();
            jw.cr();
            jw.write("public java.util.Iterator getNamespaces() ");
            jw.begin();
            jw.writeEol("return namespaces.values().iterator()");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.namespace.NamespaceContext getNamespaceContext() ");
            jw.begin();
            jw.writeEol("throw new UnsupportedOperationException()");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.namespace.QName getName() ");
            jw.begin();
            jw.writeEol("return name");
            jw.end();
            jw.cr();
            jw.write("public java.util.Iterator getAttributes() ");
            jw.begin();
            jw.writeEol("return attributes.values().iterator()");
            jw.end();
            jw.write("public String toString() ");
            jw.begin();
            jw.writeEol("return \"StartElement: \"+name.toString()");
            jw.end();
            jw.end();
            jw.cr();
            jw.write("static class ThisEndElement extends BaseXMLEvent implements javax.xml.stream.events.EndElement ");
            jw.begin();
            jw.writeEol("private javax.xml.namespace.QName name");
            jw.cr();
            jw.write("public ThisEndElement(javax.xml.namespace.QName name) ");
            jw.begin();
            jw.writeEol("this.name = name");
            jw.end();
            jw.cr();
            jw.write("public int getEventType() ");
            jw.begin();
            jw.writeEol("return javax.xml.stream.XMLStreamConstants.END_ELEMENT");
            jw.end();
            jw.cr();
            jw.write("public boolean isEndElement() ");
            jw.begin();
            jw.writeEol("return true");
            jw.end();
            jw.cr();
            jw.write("public java.util.Iterator getNamespaces() ");
            jw.begin();
            jw.writeEol("throw new UnsupportedOperationException()");
            jw.end();
            jw.cr();
            jw.write("public javax.xml.namespace.QName getName() ");
            jw.begin();
            jw.writeEol("return name");
            jw.end();
            jw.write("public String toString() ");
            jw.begin();
            jw.writeEol("return \"EndElement: \"+name.toString()");
            jw.end();
            jw.end();
            jw.cr();
            jw.write("static class ThisCharacters extends BaseXMLEvent implements javax.xml.stream.events.Characters ");
            jw.begin();
            jw.writeEol("private String data");
            jw.cr();
            jw.write("public ThisCharacters(String data) ");
            jw.begin();
            jw.writeEol("this.data = data");
            jw.end();
            jw.cr();
            jw.write("public int getEventType() ");
            jw.begin();
            jw.writeEol("return javax.xml.stream.XMLStreamConstants.CHARACTERS");
            jw.end();
            jw.cr();
            jw.write("public boolean isCharacters() ");
            jw.begin();
            jw.writeEol("return true");
            jw.end();
            jw.cr();
            jw.write("public boolean isWhiteSpace() ");
            jw.begin();
            jw.write("for (int i = 0; i < data.length(); ++i) ");
            jw.begin();
            jw.writeEol("char c = data.charAt(i)");
            jw.writeEol("if (!Character.isWhitespace(c))");
            jw.writeEol("return false");
            jw.end();
            jw.writeEol("return true");
            jw.end();
            jw.cr();
            jw.write("public boolean isIgnorableWhiteSpace() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public boolean isCData() ");
            jw.begin();
            jw.writeEol("return false");
            jw.end();
            jw.cr();
            jw.write("public String getData() ");
            jw.begin();
            jw.writeEol("return data");
            jw.end();
            jw.write("public String toString() ");
            jw.begin();
            jw.writeEol("return \"Characters: \"+data");
            jw.end();
            jw.end();
            jw.cr();
            jw.write("static class ThisEndDocument extends BaseXMLEvent implements javax.xml.stream.events.EndDocument ");
            jw.begin();
            jw.write("public int getEventType() ");
            jw.begin();
            jw.writeEol("return javax.xml.stream.XMLStreamConstants.END_DOCUMENT");
            jw.end();
            jw.cr();
            jw.write("public boolean isEndDocument() ");
            jw.begin();
            jw.writeEol("return true");
            jw.end();
            jw.end();
            jw.cr();
        }
        jw.beginMethod("getXMLEvent", getRootClassName()+".XMLEventStateManager state", null, "javax.xml.stream.events.XMLEvent");
        jw.write("switch (state.getEventNumber()) ");
        jw.begin();
        int caseNum = 0;
        if (beanElement.isRoot) {
            jw.writecr("case "+caseNum++, ":");
            jw.indentRight();
            jw.writeEol("state.nextEventNumber()");
            jw.writeEol("return new ThisStartDocument()");
            jw.indentLeft();
        }
        
        jw.writecr("case "+caseNum++, ":");
        jw.begin();
        jw.writeEol("state.nextEventNumber()");
        jw.writeEol(getRootClassName()+".ThisStartElement startElement = new ", 
                getRootClassName(),".ThisStartElement(state.getNodeName())");
        int size = attrList.size();
        for (int i = 0; i < size; i++) {
            Property a = (Property)attrList.get(i);
            if (!a.isAttribute())
                continue;
            if (!a.isDirectChild())
                continue;
            String qNameName = declareQName(a.getNamespace(), a.dtdName, null);
            String typeQNameName;
            //System.out.println("graphNode null for "+a.getType());
            SchemaRep schemaRep = new SchemaRep();
            String xmlSchemaType = schemaRep.javaType2XMLSchemaTypeComplex(a.getType());
            //System.out.println("xmlSchemaType="+xmlSchemaType);
            typeQNameName = declareQName(schemaRep.getNamespaceURI(schemaRep.prefixOf(xmlSchemaType)),
                    schemaRep.removePrefix(xmlSchemaType), null);

            String varName = "_"+a.name;
            if (!a.isScalar()) {
                jw.beginIf(varName+" != null");
            } else if (config.isOptionalScalars() && a.isScalar()) {
                jw.beginIf(a.getScalarIsSet());
            }
            jw.write("startElement.addAttribute(", qNameName, ", ");
            jw.write(JavaUtil.typeToString(a.getType(), varName));
            jw.writeEol(", \"CDATA\", ", typeQNameName, ")");
            if (!a.isScalar() || (config.isOptionalScalars() && a.isScalar()))
                jw.end();
        }

        if (beanElement.isRoot) {
            jw.beginIf("schemaLocation != null");
            String slQNameName = declareQName("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", "xsi");
            String typeQNameName = declareQName("http://www.w3.org/2001/XMLSchema", "string", "xsd");
            jw.write("startElement.addAttribute(", slQNameName, ", ");
            jw.writeEol("schemaLocation, \"CDATA\", ", typeQNameName, ")");
            jw.end();
        }
        jw.writeEol("return startElement");
        jw.end();

        int numberOfNonAttributeProperties = countNumberOfNonAttributeProperties();
        comment("Number of non attribute properties: "+numberOfNonAttributeProperties);
        int childStartCaseNum = caseNum;
        if (caseNum < childStartCaseNum + numberOfNonAttributeProperties) {
            while (caseNum < childStartCaseNum + numberOfNonAttributeProperties) {
                jw.writecr("case "+caseNum++, ":");
            }
            jw.indentRight();
            // numberOfNonAttributeProperties
            jw.writeEol("javax.xml.stream.events.XMLEvent event = getXMLEventChildren(state, state.getEventNumber() - "+(childStartCaseNum)+")");
            jw.writeEol("return event");
            jw.indentLeft();
       }
        
        jw.writecr("case "+caseNum++, ":");
        jw.indentRight();
        jw.writeEol("state.nextEventNumber()");
        jw.writeEol("return new ", getRootClassName(), ".ThisEndElement(state.getNodeName())");
        jw.indentLeft();

        if (beanElement.isRoot) {
            jw.writecr("case "+caseNum++, ":");
            jw.indentRight();
            jw.writeEol("state.nextEventNumber()");
            jw.writeEol("return new ThisEndDocument()");
            jw.indentLeft();
        }
        jw.writecr("default:");
        jw.indentRight();
        jw.writeEol("state.doneWithBean()");
        jw.writeEol("break");
        jw.indentLeft();
        jw.end();
        jw.writeEol("return null");
        jw.endMethod();
        
        jw.beginMethod("getXMLEventChildren", getRootClassName()+".XMLEventStateManager state, int eventNumber", null, "javax.xml.stream.events.XMLEvent", jw.PROTECTED);
        caseNum = 0;
        jw.write("switch (eventNumber) ");
        jw.begin();
        
        if (config.isRespectExtension() && beanElement.getExtension() != null) {
            jw.writecr("default:");
            jw.indentRight();
            jw.writeEol("javax.xml.stream.events.XMLEvent event = super.getXMLEventChildren(state, eventNumber)");
            jw.beginIf("true");  // workaround "unreachable statement" error from javac which can happen if default is the only thing in the switch
            jw.writeEol("return event");
            jw.end();
            jw.indentLeft();
            caseNum = countNumberOfNonAttributePropertiesRecurse(beanElement.getExtension());
        }
        for (int i = 0; i < size; i++) {
            Property a = (Property)attrList.get(i);
            if (a.isAttribute())
                continue;
            boolean indexed = a.isIndexed();
            String varName = "_"+a.name;
            
            String qNameName = declareQName(a.getNamespace(), a.dtdName, null);
            jw.writecr("case "+caseNum++, ":");
            jw.begin();
            if (indexed) {
                varName = "element";
                jw.writeEol(a.getTypeFullClassName(packageName), " element = null");
                if (!a.isBean) {
                    jw.beginIf("state.getElementNumber() == 0");
                }
                jw.beginWhile("state.getArrayIndex() < size"+a.name+"()");
                jw.writeEol("element = ", a.getReadMethod(true), "(state.getArrayIndex())");
                jw.beginIf("element != null");
                jw.writeEol("break");
                jw.end();
                jw.writeEol("state.nextArrayIndex()");
                jw.end();
                if (!a.isBean) {
                    jw.endElseBegin();
                    jw.writeEol("element = ", a.getReadMethod(true), "(state.getArrayIndex())");
                    jw.writeEol("assert element != null");
                    jw.end();
                }
            } else {
            }
            if (a.isBean) {
                jw.beginIf(varName+" != null");
                if (indexed)
                    jw.writeEol("state.nextArrayIndex()");
                else
                    jw.writeEol("state.nextEventNumber()");
                jw.write("state.enterChildBean(", varName, ", ");
                jw.writeEol(qNameName, ")");
                jw.writeEol("return ", varName, ".getXMLEvent(state)");
                jw.endElseBegin();
                jw.writeEol("state.nextEventNumber()");
                jw.end();
                jw.writeEol("break");
                jw.end();
            } else {
                if (!a.isScalar()) {
                    jw.beginIf(varName+" != null");
                } else if (config.isOptionalScalars() && a.isScalar()) {
                    jw.beginIf(a.getScalarIsSet());
                }
                jw.writeEol("state.nextElementNumber()");
                jw.write("switch (state.getElementNumber()) ");
                jw.begin();
                jw.writecr("case 1:");
                jw.indentRight();
                jw.write(getRootClassName(), ".ThisStartElement startElement = new ", getRootClassName());
                jw.writeEol(".ThisStartElement(", qNameName, ")");
                for (int attrNum = 0; attrNum < a.attributes.length; ++attrNum) {
                    AttrProp attr = a.attributes[attrNum];
                    //jw.comment("attr "+attr);
                    Property prop = findProperty(attr);

                    String qNameNameAttr = declareQName(prop.getNamespace(), prop.dtdName, null);
                    String typeQNameNameAttr;
                    //System.out.println("graphNode null for "+a.getType());
                    SchemaRep schemaRep = new SchemaRep();
                    String xmlSchemaType = schemaRep.javaType2XMLSchemaTypeComplex(a.getType());
                    //System.out.println("xmlSchemaType="+xmlSchemaType);
                    typeQNameNameAttr = declareQName(schemaRep.getNamespaceURI(schemaRep.prefixOf(xmlSchemaType)),
                            schemaRep.removePrefix(xmlSchemaType), null);

                    String varNameAttr = "_"+prop.name;
                    if (!a.isScalar()) {
                        jw.beginIf(varNameAttr+" != null");
                    } else if (config.isOptionalScalars() && a.isScalar()) {
                        jw.beginIf(a.getScalarIsSet());
                    }
                    jw.write("startElement.addAttribute("+qNameNameAttr+", ");
                    jw.write(JavaUtil.typeToString(a.getType(), varNameAttr));
                    jw.writeEol(", \"CDATA\", ", typeQNameNameAttr, ")");
                    if (!a.isScalar() || (config.isOptionalScalars() && a.isScalar()))
                        jw.end();
                }
                jw.writeEol("return startElement");
                jw.indentLeft();
                jw.writecr("case 2:");
                jw.indentRight();
                jw.write("return new ", getRootClassName(), ".ThisCharacters(");
                if (isTypeQName(a.getType())) {
                    // see line 3118 or so
                    jw.write(a.getType());
                } else if ("byte[]".equals(a.getType())) {
                    // Is this hexBinary or base64Binary?
                    SchemaRep.EncodingStyle style = (SchemaRep.EncodingStyle) a.searchExtraData(SchemaRep.EncodingStyle.class);
                    if (style instanceof SchemaRep.HexBinary) {
                    } else if (style instanceof SchemaRep.Base64Binary) {
                        jw.write(getRootClassName(), ".encodeBase64BinaryString(");
                        jw.writeEol(varName, "))");
                    } else {
                        config.messageOut.println("Unknown encoding style for "+a.getType()+" for property "+a.name);
                    }
                } else {
                    jw.write(JavaUtil.typeToString(a.getType(), varName));
                    jw.writeEol(")");
                }
                jw.indentLeft();
                jw.writecr("case 3:");
                jw.indentRight();
                jw.write("return new ", getRootClassName(), ".ThisEndElement(", qNameName);
                jw.writeEol(")");
                jw.indentLeft();
                jw.writecr("default:");
                jw.indentRight();
                if (indexed) {
                    jw.writeEol("state.nextArrayIndex()");
                    jw.writeEol("return getXMLEvent(state)");
                } else {
                    jw.writeEol("break");
                }
                jw.indentLeft();
                jw.end();
                if (!a.isScalar() || (config.isOptionalScalars() && a.isScalar())) {
                    if (indexed) {
                        jw.endElse();
                        jw.cr();
                        jw.indentOneLevel();
                    } else {
                        jw.end();
                    }
                }
                jw.writeEol("state.nextEventNumber()");
                jw.writeEol("break");
                jw.end();
            }
            /*
            if (indexed) {
                jw.writeEol("state.nextEventNumber()");
                jw.writeEol("return getXMLEvent(state)");
                jw.end();
            }
             */
        }

        jw.end();
        jw.writeEol("return null");
        jw.endMethod();

    }