//
// CopyRight (C) 2002, IPA, eLC, NTT Resonant Inc. All rights reserved.
//
//	Resource
//
//	変更履歴
//		2002.12.25  新規作成
//

package jp.co.ntt.lms.lo.scorm.kernel;

import jp.co.ntt.lms.lo.scorm.kernel.exception.IllegalManifestException;
import jp.co.ntt.lms.lo.scorm.kernel.exception.SystemException;
import jp.co.ntt.lms.lo.scorm.util.CachedNode;

/**
 * Resource class<BR>
 * リソース情報を表すクラス<BR>
 */
public class Resource {

	/** resource node */
	private CachedNode resourceNode;

	/** identifierRef */
	private String identifierRef;

	/**
	 *	コンストラクタ
	 *
	 * @param	identifierRef 	取出したいリソースノードのID
	 * @param	resourcesNode	resourcesノード
	 */
	public Resource(String identifierRef, CachedNode resourcesNode) throws SystemException {

		try {
			if (resourcesNode == null) {
				throw new IllegalManifestException("Manifest is Illegal. [(manifest)/resources=null]");
			}

			this.identifierRef = identifierRef;

			resourceNode = resourcesNode.selectSingleNodeWithNodeTest("resource", "@identifier", identifierRef);
			if (resourceNode == null) {
				throw new IllegalManifestException("Manifest is Illegal. [(resources/)resource[@identifier='"+identifierRef+"']=null]");
			}
		}
		catch (Exception e) {
			throw new SystemException("Resource failed.", e);
		}
	}

	/**
	 *	Href の取得
	 *
	 *	@return	リソースのhref属性値
	 */
	public String getHref() throws SystemException {

		if (resourceNode == null) {
			throw new IllegalManifestException("Manifest is Illegal. [(resources/)resource[@identifier='"+identifierRef+"']=null]");
		}
		try {
			//@20040603 ADD NTTR
			StringBuffer retUrl = new StringBuffer("");
			CachedNode bNode = resourceNode.selectSingleChildNode("@xml:base");
			if(bNode != null){
				retUrl.append(bNode.getNodeValue());
			}
			
			//ノードからhref属性値を取り出す
			CachedNode n = resourceNode.selectSingleChildNode("@href");
			if (n == null) {
				return null;
			}
			retUrl.append(n.getNodeValue());
			return retUrl.toString();
		}
		catch (Exception e) {
			throw new SystemException("getHref failed.", e);
		}
	}

	/**
	 *	Href の取得
     *
	 *	@return	リソースのhref属性値
	 */
	public String getBaseURL() throws SystemException {
		if (resourceNode == null) {
			throw new IllegalManifestException("Manifest is Illegal. [(resources/)resource[@identifier='"+identifierRef+"']=null]");
		}
		try {
			//@20040603 ADD NTTR
			StringBuffer retUrl = new StringBuffer("");
			CachedNode bNode = resourceNode.selectSingleChildNode("@base");
			if(bNode != null){
				retUrl.append(bNode.getNodeValue());
			}
			return retUrl.toString();
		}
		catch (Exception e) {
			throw new SystemException("getBaseUrl failed.", e);
		}
			
	}
	
	/**
	 *	ScormType の取得
	 *
	 *	@return	リソースのscormtype属性値
	 */
	public String getScormType() throws SystemException {

		if (resourceNode == null) {
			throw new IllegalManifestException("Manifest is Illegal. [(resources/)resource[@identifier='"+identifierRef+"']=null]");
		}
		try {
			// ノードからscormtype属性値を取り出す
			CachedNode scormType = resourceNode.selectSingleChildNode("@scormType");
			if (scormType != null) {
				return scormType.getNodeValue();
			}else{
				scormType = resourceNode.selectSingleChildNode("@scormtype");
				if (scormType != null) {
					return scormType.getNodeValue();
				}
			}
			return null;
		}
		catch (Exception e) {
			throw new SystemException("getScormType failed.", e);
		}
	}

	/**
	 * 終期化
	 */
	public void terminate() {
		resourceNode = null;
	}
}
