/** The XResource class represents a single resource, represented by a URL.
* Note that this class may be extended to connect XResources to their representations
* if desired
* @version 0.10 - Last Modified 12/16/98
* Copyright 1998 Simon St.Laurent
* Information on this project is at: http://www.simonstl.com/projects/xlinkfilter/
* This code is licensed by Simon St.Laurent under the Mozilla Public License.
* See http://www.mozilla.org/MPL/ for details.
* No warranty provided - use at your own risk */

/* Modification history
* 12/16/98 - Created XResource to simplify Arc and Link. - Simon St.Laurent
*/

package com.simonstl.sax.xlink;

import java.net.URL;

public class XResource {
	protected String xrURL, 
			xrConnect,
			xrLoc,
			xrRole,
			xrTitle;
/**Zero-argument constructor; Application will need to use set methods to assign values
*/
public XResource () {

    }

/** Two-argument constructor for XResource destinations, which contain all info in HREF. No role or title
*/
public XResource(String value, String baseURL) {
	/*URL and Loc split up the full URI*/
		  URL tempURL;
		  URL baseURLtoURL;
			
               int testId=value.indexOf('#');
               if (testId != -1)
                  {
		  try {
			  baseURLtoURL=new URL(baseURL);
			  tempURL=new URL(baseURLtoURL,value.substring(0,testId));
			  xrURL=tempURL.toString();
                	  xrLoc=value.substring(testId+1);
                  } catch (Exception e) {
		    	e.printStackTrace();
		  }//end try-catch

                  }//end if
	else
	 {
		testId=value.indexOf("|");
                if (testId != -1)
                  {
		  xrConnect="|";
		  try {
			  baseURLtoURL=new URL(baseURL);
			  tempURL=new URL(baseURLtoURL,value.substring(0,testId));
			  xrURL=tempURL.toString();
                	  xrLoc=value.substring(testId+1);
                  } catch (Exception e) {
		    	e.printStackTrace();
		  }//end try-catch

                  }//end if
		else
	{
		testId=value.indexOf("?XML-XPTR=");
               if (testId != -1)
                  {
		  xrConnect="?XML-XPTR=";
		  xrURL=value;
		  xrLoc="%none";

                  }//end if
		//original else
		else
                  {
		  
		  try {
			baseURLtoURL=new URL(baseURL);
			tempURL=new URL(baseURLtoURL,value);
			xrURL=tempURL.toString();
			xrLoc="%none";
		    } catch (Exception e) {
			e.printStackTrace();
		    }//end try-catch

                  }//end original else
		}//end XML-XPTR else
		}//end | else

}//end constructor

/** Three-argument constructor for use with origin links, where no HREF resolution is necessary, no role or title.
*/    
public XResource(String baseURL, String connector, String location) {
    xrURL=baseURL;
    xrConnect=connector;
    xrLoc=location;
  }//end constructor
  	
/** Four-argument constructor for XResource destinations, which contain all info in HREF.
*/
public XResource(String value, String baseURL, String role, String title) {
    this(value, baseURL);
    xrRole=role;
    xrTitle=title;
}//end constructor

/** Five-argument constructor for use with origin links, where no HREF resolution is necessary.
*/    
public XResource(String baseURL, String connector, String location, String role, String title) {
    this(baseURL, connector, location);
    xrRole=role;
    xrTitle=title;
  }//end constructor
  
  	public String getURL() {return xrURL;}

	public String getConnect() {return xrConnect;}
	
	public String getLoc() {return xrLoc;}
	
	public String getFullURL() {return xrURL+xrConnect+xrLoc;}
	
	public String getRole() {return xrRole;}
	
	public String getTitle() {return xrTitle;}
	
	public void setURL(String newURL) {xrURL=newURL;}

	public void setConnect(String newConnect) {xrConnect=newConnect;}
	
	public void setLoc(String newLoc) {xrLoc=newLoc;}

	public void setRole(String newRole) {xrRole=newRole;}
	
	public void setTitle(String newTitle) {xrTitle=newTitle;}
	
	public boolean equals (XResource compareResource) {
	    
	    if ((xrURL.equals(compareResource.getURL())) &&
	        (xrConnect.equals(compareResource.getConnect())) &&
		(xrLoc.equals(compareResource.getLoc())) &&
		(xrRole.equals(compareResource.getRole())) &&
		(xrTitle.equals(compareResource.getTitle())) )
	    {
		return true;
	    }
	    else {
		return false;
	    }
	}
	
}//end class