/*
 */

package au.gov.ansto.bragg.nexus.tool;

import au.gov.ansto.bragg.forward.All_;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.StringTokenizer;

/**
 *
 * @author drck
 * @version "%I%, %G%"
 */

public class XmlTemplate_ 
        extends All_
         implements XmlTemplate
{
    
    /** Test only of propagation of constructor parameters.
     *
     * @param args Not used.
     */
    public static void main(String[] args) {
        All_.DEBUG = true;
        XmlTemplate sourceXml = new XmlTemplate_(
                "<stem>",
                "<file>"
                );
    }
    
    //private String $stem_ = "";
    //    return $stem_;
    /*
    public String $stem() { 
        return "<?stem>";
    }
     */
    
    //private String $file_ = "";
    //return $file_;
    
    public String get$path() { 
        return url_.getFile();
    }

    public String $path() { 
        return url_.getPath();
    }
    
    /*
    public String $longName(){
        return $stem()+$file();
    }
     */
    public String get$package() {
        return get$path().replaceAll("/",".");//TODO WARN ? copy first
        //return "<?package>";
    }

    private URL url_;
    public URL getURL() {return url_;}
    
    public XmlTemplate_(URL url) {
        if (url==null) error("null url");
        url_  = url;
    }
    
    /** Default host  */
    static final public String HOST = "localhost.localdomain";
    
    /** Specifying a port number of -1  indicates that the URL
     * should use the default port for the protocol.
     */
    static final public int PORT = -1;
        
    static final public String PROTOCOL_FILE = "file";
    static final public String PROTOCOL = PROTOCOL_FILE;
    
    /** Constructs with file under stem as URL using the default PORT */
    public XmlTemplate_(
            String $stem,
            String $file) {
        try {            
            
            url_ = new URL(
                    PROTOCOL_FILE,
                    null,
                    PORT,
                    "/"+$stem+"/"+$file
                    );
            debug("url",url_);
            debug("url.protocol",url_.getProtocol());
            debug("url.host",url_.getHost());
            debug("url.port",url_.getPort());
            debug("url.file",url_.getFile());
            debug("url.path",url_.getPath());
            
            debug("$package",get$package());
            debug("$file",get$path());
            
        } catch (MalformedURLException ex) {
            error(ex);
        }
    }

    /*
    public XmlSource_() {
        this("<stem>","<file>");
    }
     */
    
    public String toString() { 
        return getURL()+"";
    }    
}
