<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    layout="vertical"
    xmlns:mx="http://www.adobe.com/2006/mxml"
    backgroundColor="0xEFEFEF"
     viewSourceURL="srcview/index.html">
    
    <mx:Script>
        <![CDATA[
            import vo.CarVO;
            import vo.GarageVO;
            import mx.utils.ObjectUtil;
        
            import mx.rpc.xml.*;
            
            import rpc.xml.PatchedXMLDecoder;
            
            public function decodeXML():void
            {
                // create a couple of qualified class names
                
                var garageType:QName = new QName("http://www.andymcintosh.com", "garageType");
                
                var carType:QName = new QName("http://www.andymcintosh.com", "carType");
                
                
                // map the class names to action script classes
                
                SchemaTypeRegistry.getInstance().registerClass(garageType, GarageVO);
                
                SchemaTypeRegistry.getInstance().registerClass(carType, CarVO);
                
                
                // create a new Schema from the xsd xml
                
                var schema:Schema = new Schema(new XML(schemaInput.text));
                
                
                // create an instance of the decoder and assign the schema to it
                
                var decoder:XMLDecoder;
                
                if(patchToggle.selected)
                {
                    decoder = new PatchedXMLDecoder()
                }
                else
                {
                    decoder = new XMLDecoder();
                }
                
                decoder.schemaManager.addSchema(schema);
                
                
                // decode the xml, specifying that the root node should decode to the garageType
                
                var decodedObject:Object = decoder.decode(new XML(dataInput.text), garageType, garageType);
                
                
                // write out the decoded object to the output box
                
                output.text = ObjectUtil.toString(decodedObject);
            }
            
        ]]>
    </mx:Script>
    
    <mx:XML id="xsd" xmlns="http://www.andymcintosh.com">
        <xs:schema 
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns="http://www.andymcintosh.com"
            targetNamespace="http://www.andymcintosh.com"
            elementFormDefault="qualified"
            >
        
            <xs:element name="garage" type="garageType" />
            
            <xs:complexType name="carType">
                <xs:attribute name="make" type="xs:string"/>
                <xs:attribute name="model" type="xs:string"/>
            </xs:complexType>
                
            <xs:complexType name="garageType">
                <xs:sequence>
                    <xs:element name="car" type="carType"/>
                </xs:sequence>
            </xs:complexType>    
            
        </xs:schema>
    </mx:XML>
    
    <mx:XML id="data" xmlns="http://www.andymcintosh.com">
        <garage xmlns="http://www.andymcintosh.com">
            <car make="Subaru" model="Impreza">
            </car>
        </garage>
    </mx:XML>
    
    <mx:Label text="Schema" width="100%" textAlign="left"/>
    
    <mx:TextArea id="schemaInput" width="100%" height="200" text="{xsd.toString()}" />
    
    <mx:Label text="Data" width="100%" textAlign="left"/>
    
    <mx:TextArea id="dataInput" width="100%" height="120" text="{data.toString()}" />
    
    <mx:HBox>
        <mx:CheckBox id="patchToggle" label="Patch?" />
        <mx:Button label="Parse" click="decodeXML()" />
    </mx:HBox>
    
    
    
    <mx:HBox width="100%">
        
        <mx:VBox width="100%">
            <mx:Label text="Expected Results" />
            <mx:TextArea width="100%" height="120" >
                <mx:text>
                    <![CDATA[(vo::GarageVO)#0
    car = (vo::CarVO)#1
        make = "Subaru"
        model = "Impreza"]]>
                </mx:text>
            </mx:TextArea>
        </mx:VBox>
        
        <mx:VBox width="100%">
            <mx:Label text="Actual Results" />
            <mx:TextArea id="output" width="100%" height="120" />
        </mx:VBox>
        
    </mx:HBox>
    
    
    
    
    
</mx:Application>