/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.server.ims;

import java.io.*;
import java.util.Vector;
import java.util.Stack;
import org.xml.sax.*;

import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.FactoryConfigurationError;  
import javax.xml.parsers.DocumentBuilder;

/*
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.CharacterData;
import org.w3c.dom.NodeList;
import org.w3c.dom.DOMException;
*/

import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.*;


import org.bodington.assessment.*;
import org.bodington.server.BuildingServerException;


public class PackageOrganizationsHandler extends DefaultHandler
    {
    private ImsPackageItem organizations=null;
    private ImsPackageItem senior = null;
    private ImsPackageItem current = null;
    private Stack stack;
    StringBuffer title;
    private String version;
    private String orgs_tag, org_tag, rr_tag;
    
    private boolean done=false;
    private boolean in_title=false;
    private boolean in_organizations=false;
    
    //===========================================================
    // SAX DocumentHandler methods
    //===========================================================
    public PackageOrganizationsHandler()
        {
        title = new StringBuffer();
        }

    public ImsPackageItem getOrganizations()
        {
        return organizations;
        }

    public void startDocument ()
    throws SAXException
        {
        done=false;
        in_title=false;
        organizations = null;
        in_organizations=false;
        stack = new Stack();
        }

    public void endDocument ()
    throws SAXException
        {
        }

    public void startElement (  String uri, String localName, String name, Attributes attrs  )
    throws SAXException
        {
        if ( done )
            return;
            
        if ( name.equals( "manifest" ) )
            {
            version = attrs.getValue( "version" );
            if ( version == null )
            	version = "1.0";
            if ( version.equals( "1.1" ) )
            	{
            	orgs_tag = "organizations";
            	org_tag = "organization";
            	rr_tag = "resourceref";
            	}
            else if ( version.equals( "1.0" ) )
            	{
            	orgs_tag = "organizations";
            	org_tag = "tableofcontents";
            	rr_tag = "identifierref";
            	}
            else
            	throw new SAXException( "Only version 1.0 or 1.1 IMS package files are accepted." );
            }
            
        if ( name.equals( orgs_tag ) )
            {
            current = organizations = new ImsPackageItem( 
                                            ImsPackageItem.ORGANIZATIONS, null, null, null );
            if ( attrs.getValue( "default" ) != null )
            	current.setDefault( attrs.getValue( "default" ) );
            in_organizations=true;
            }
            
        if ( !in_organizations )
            return;
            
        if ( name.equals( org_tag ) )
            {
            senior = current;
            current = new ImsPackageItem( 
                            ImsPackageItem.ORGANIZATION, 
                            attrs.getValue( "identifier" ), 
                            null,
                            "untitled" );
            senior.appendItem( current );
            if ( attrs.getValue( "identifier" ).equals( organizations.getDefault() ))
                organizations.setDefaultOrganization( current );
            }
            
        if ( name.equals( "item" ) )
            {
            stack.push( senior );
            senior = current;
            current = new ImsPackageItem( 
                            ImsPackageItem.ITEM, 
                            attrs.getValue( "identifier" ), 
                            attrs.getValue( rr_tag ),
                            (version.equals("1.0"))?attrs.getValue( "title" ):"untitled" );
            senior.appendItem( current );
            }
            
        if ( name.equals( "title" ) )
            {
            in_title = true;
            title.setLength( 0 );
            }

        }

    public void endElement (  String uri, String localName, String name )
        throws SAXException
        {
        if ( !in_organizations )
            return;

        if ( name.equals( orgs_tag )  )
            {
            done =true;
            in_organizations=false;
            return;
            }
        
        if ( name.equals( org_tag )  )
            {
            current = senior;
            return;
            }
        
        if ( name.equals( "item" )  )
            {
            current = senior;
            senior = (ImsPackageItem)stack.pop();
            return;
            }

        if ( name.equals( "title" )  )
            {
            current.setTitle( title.toString() );
            return;
            }
        }


    public void characters (char buf [], int offset, int len)
        throws SAXException
        {
        if ( in_title )
            {
            title.append(buf, offset, len);
            }
        }

    }


