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

import java.io.IOException;

import org.apache.log4j.Logger;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

class OUCSCollegeParser extends XppIterator
{

    private static final Logger log = Logger.getLogger(OUCSCollegeParser.class);
    
    protected Object loadNext()
    {
        OUCSCollege college = new OUCSCollege();

        try
        {
            while (xpp.getEventType() != XmlPullParser.END_DOCUMENT)
            {

                if (xpp.getEventType() == XmlPullParser.START_TAG)
                {
                    try
                    {
                        if (("RECORD").equalsIgnoreCase(xpp.getName()))
                            college = new OUCSCollege();
                        else if (checkTagAndProgress("CODE"))
                            college.setCode(xpp.getText());
                        else if (checkTagAndProgress("NICKNAME"))
                            college.setNickname(xpp.getText());
                        else if (checkTagAndProgress("FULLNAME"))
                            college.setName(xpp.getText());
                    } 
                    catch (IllegalStateException ise)
                    {
                        log.error(getXppPosition()+ "Data format error: "+ ise.getMessage());
                    }
                }
                else if (xpp.getEventType() == XmlPullParser.END_TAG)
                {
                    if ("RECORD".equals(xpp.getName()))
                    {
                        xpp.next();
                        return college;
                    }
                }

                xpp.next();
            }
        }
        catch (IOException ioe)
        {
        	log.error("Problem reading source: "+ ioe.getMessage());
        }
        catch (XmlPullParserException xppe)
        {
            log.error("Problem with XML format: "+xppe.getMessage());
        }
        return null;
    }

}
