/* ======================================================================
   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.resources;

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.xml.sax.ext.LexicalHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.*;


import org.bodington.server.BuildingServerException;


public class MenuHandler extends DefaultHandler
    {
    
    private Menu menu = null;
    private MenuItem senior, current;
    
    private Stack stack;
    StringBuffer title;

    private boolean in_title=false;
    
    //===========================================================
    // SAX DocumentHandler methods
    //===========================================================
    public MenuHandler()
        {
        title = new StringBuffer();
        
        }

    public Menu getMenu()
        {
        return menu;
        }

    public void startDocument ()
   		throws SAXException
        {
        in_title=false;
        stack = new Stack();

        senior = null;
        current = new MenuItem( "Custom Resource Menu", null, "container", 0 );
        menu = new Menu( current );
        }

    public void endDocument ()
    throws SAXException
        {
        }

    public void startElement (  String uri, String localName, String name, Attributes attrs  )
    throws SAXException
        {
        if ( name.equals( "menu" ) )
            {
            String auto = attrs.getValue( "automatic" );
            if ( auto!=null && auto.length()>0 )
            	{
            	if ( auto.equalsIgnoreCase( "true" ) || auto.equalsIgnoreCase( "yes" ) )
            		{
            		menu.setAutomatic( true );
            		current.setTitle( "Automatic Resource Menu" );
            		}
            	}
            }

        if ( name.equals( "item" ) )
            {
            if ( senior != null )
            	stack.push( senior );
            senior = current;
            long size = 0;
            try
            {
                size = Long.parseLong(attrs.getValue("size"));
            }
            catch (NumberFormatException nfe)
            {}
            
            current = new MenuItem( null, attrs.getValue( "href" ), attrs.getValue( "type" ), size  );
            menu.insertNodeInto( current, senior, menu.getChildCount( senior ) );
            }
            
        if ( name.equals( "title" ) )
            {
            in_title = true;
            title.setLength( 0 );
            }

        }

    public void endElement (  String uri, String localName, String name )
        throws SAXException
        {
        if ( name.equals( "item" )  )
            {
            current = senior;
            if ( stack.empty() )
            	senior = null;
            else
            	senior = (MenuItem)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);
            }
        }

    }


