summaryrefslogtreecommitdiff
path: root/bPod/php/cybozuLab/rssParser/FetchingRss.as
blob: bca92c1b95a38714ccb4fa877cdd626c890a3c38 (plain)
1
/*
	Action script 2.0 RSS parser package
    Copyright (C) 2006 Cybozu Labs, Inc.
   
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
   
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
   
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

/*
	class: com.cybozuLab.rssParser.FetchingRss
	version: 0.1
	modified: 31/Jan/2006

	Fetching RSS2.0 feed and set Action Script 2.0 XML tree.
*/

import com.cybozuLab.rssParser.*;

class com.cybozuLab.rssParser.FetchingRss
{
	private static var DEBUG:Boolean = false;

	private var targetURL:String; //target rss feed URL
	private var httpStatusCode:Number;
	private var httpStatusType:String;
	private var errorMessage:String;
	private var rssXml:XML;
	private var rssParser:ParsingRss;
	private var calledParserFlag:Boolean;

	/*
		function: FetchingRss
		constructor
	*/
	public function FetchingRss( url:String )
	{
		var thisObj:FetchingRss = this;
		calledParserFlag = false;

		if( url != undefined )
		{
			targetURL = url;
		}

		rssXml = new XML();
		rssXml.ignoreWhite = true;

		
		rssXml.onHTTPStatus = function( httpStatus:Number )
		{
			thisObj.FetchingRssOnHTTPStatus( httpStatus );
		}

		rssXml.onLoad = function( successFlag:Boolean )
		{
			thisObj.FetchingRssOnLoad( successFlag, thisObj.rssXml.status );
			
			if( successFlag == true )
			{
				thisObj.calledParserFlag = true;
				thisObj.rssParser = new ParsingRss( thisObj.rssXml );
			}
			thisObj.onLoad( successFlag, thisObj.errorMessage );
		}
	}

	public function load( url:String ):Boolean
	{
		if( url != undefined )
		{
			targetURL = url;
		}

		if( targetURL == undefined )
		{
			errorMessage = "Setting Error : Called without source URL.";
			return false;
		}

		calledParserFlag = false;
		rssXml.load( targetURL );

		return true;
	}

	public function onLoad( success:Boolean, errorMessage:String )
	{
	}

	public function getTargetUrl():String
	{
		return targetURL;
	}
	
	public function getRssXml():XML
	{
		return rssXml;
	}

	public function getRssObject():Object
	{
		if( calledParserFlag )
		{
			return rssParser.getRssObject();
		}

		return new Object();
	}

	public function traceRssObject():Void
	{
		if( calledParserFlag )
		{
			Utils.traceObj( rssParser.getRssObject() );
		}
	}

	public function getErrorMessage():String
	{
		return errorMessage;
	}

	/*
		function: FetchingRssOnHTTPStatus
		Classify an HTTP status code.
	*/
	private function FetchingRssOnHTTPStatus( httpStatus:Number )
	{
		httpStatusCode = httpStatus;
		
		if( httpStatus < 100 )
		{
			httpStatusType = "flashError";
		}
		else if( httpStatus < 200 )
		{
			httpStatusType = "information";
		}
		else if( httpStatus < 300 )
		{
			httpStatusType = "successful";
		}
		else if( httpStatus < 400 )
		{
			httpStatusType = "redirection";
		}
		else if( httpStatus < 500 )
		{
			httpStatusType = "client error";
		}
		else if (httpStatus < 600 )
		{
			httpStatusType = "server error";
		}
		else
		{
			httpStatusType = "unknown";
		}

		if( DEBUG )
		{
			trace( "httpStatus : " + httpStatusCode );
			trace( "httpStatusType : " + httpStatusType );
		}
	}
	
	private function FetchingRssOnLoad( successFlag:Boolean, statusNum:Number )
	{
		var eMessage:String;

		if( successFlag )
		{
			switch ( statusNum ) {
			case 0 :
				eMessage = "Parse was completed successfully.";
				break;
			case -2 :
				eMessage = "A CDATA section was not properly terminated.";
				break;
			case -3 :
				eMessage = "The XML declaration was not properly terminated.";
				break;
			case -4 :
				eMessage = "The DOCTYPE declaration was not properly terminated.";
				break;
			case -5 :
				eMessage = "A comment was not properly terminated.";
				break;
			case -6 :
				eMessage = "An XML element was malformed.";
				break;
			case -7 :
				eMessage = "Out of memory.";
				break;
			case -8 :
				eMessage = "An attribute value was not properly terminated.";
				break;
			case -9 :
				eMessage = "A start-tag was not matched with an end-tag.";
				break;
			case -10 :
				eMessage = "An end-tag was encountered without a matching start-tag.";
				break;
			default :
				eMessage = "An unknown error has occurred.";
				break;
			}
			if(  statusNum != 0 )
			{
				errorMessage =  "Parsing Error : XML was loaded successfully, but was unable to be parsed. (Error : "+eMessage+")";
			}
			else
			{
				errorMessage = eMessage;
			}
		}
		else
		{
			errorMessage = "Loading Error : Failed to load RSS feed. ( httpStatus : "+httpStatusCode+" - "+httpStatusType+")";
		}
	}

}