﻿//-----------------------------------------------------------------------------
// XLocationMap
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

XLocationMap.prototype = new XConfig;
XLocationMap.prototype.constructor = XLocationMap;

//=============================================================================
// Constructor

function XLocationMap(
   create
)
{
   create = (create == null) ? true : false;

   if (create)
      return new XLocationMap(false);

   //--------------------------------------------------------------------------
   // Private Interface

   //--------------------------------------------------------------------------
   // Privileged Interface

   this.valueOf = function()
   {

      return oXNode;
   }

   //--------------------------------------------------------------------------

   this.setObjectValue = function()
   {

      oXNode = XLocationMap.getXNodeFrom();

      return oXNode;
   }

   //--------------------------------------------------------------------------
   // Initialization

   var oXNode = this.setObjectValue();

}

XLocationMap.prototype.objectClass = "XLocationMap";

//=============================================================================
// Static Interface

XLocationMap._xConfig = null;

XLocationMap.getXNodeFrom = function()
{

   if (!XLocationMap._xConfig)
   {
      var locationMapURL = XApp.URL_LOCATION_MAP;
      var xConfig = XConfig(locationMapURL);
      XLocationMap._xConfig = xConfig.valueOf();
   }

   return XLocationMap._xConfig;
}

//=============================================================================
// Public Interface

XLocationMap.prototype.mapURN = function(
   urn,
   defaultFormat

)
{
   defaultFormat = (defaultFormat == null) ? null : defaultFormat;

   //--------------------------------------------------------------------------

   function mapArgs(
      urn,
      url
   )
   {
      urn = (urn == null) ? null : urn.toString();
      url = (url == null) ? null : url.toString();

      var argNum = 0;
      while (XMatch(urn, /\:([^\:]*)/))
      {
         argNum++;
         var arg = XMatch.matches[1];
         urn = XMatch.rightContext;
         url = url.replace(new RegExp("\{arg" + argNum + "\}"), arg);
      }

      // Get rid of unfulfilled args
      url = url.replace(/\{arg[0-9]*\}/g, "");

      return url;
   }

   //--------------------------------------------------------------------------

   // Remove the format
   var variant = null;
   var format = defaultFormat;
   if (XMatch(urn, /^(urn:[^:]*:[^:]*:[^:]*:[^:]*):([^:]*)/))
   {
      urn = XMatch.matches[1];
      format = XMatch.matches[2];
      if (XMatch(format, /\-/))
      {
         variant = XMatch.leftContext;
         format = XMatch.rightContext;
      }
   }
   var searchURN = urn;
   var path = "";
   while ((/\:/).test(searchURN))
   {
      var xPath = "/xcfg:LocationMap/xcfg:Location[starts-with('" + searchURN + "', @urn)]";
      var locationXNode = this.X$(xPath);
      if (locationXNode != null)
      {
         var urn = locationXNode.getAttribute("urn");
         if (XMatch(searchURN, urn))
         {
            var rem = XMatch.rightContext;
            if (!(/\:/).test(rem)) // We are attempting to match urn:calm:2003x1 up to /cache/ca/docs/2003x1
            {
               var url = locationXNode.getAttribute("url") + rem;
               url = (XString(path).isSomething()) ? url.replace(/\/?$/, "/") + path : url;
               url = (XString(variant).isSomething()) ? url + "." + variant : url;
               url = (XString(format).isSomething()) ? url + "." + format : url;
               url = (!(/^\//).test(url)) ? "/" + url : url;
               url = mapArgs(urn, url);
               if (XApp.getMode() == XApp.MODE_STANDALONE || XApp.getMode() == XApp.MODE_SERVER)
                  return XRef.mapPath(url.replace(/^http:\/\//, "/"));
               else
                  return url;
            }
         }
      }
      path = searchURN.replace(/^.*\:([^\:]*)$/, "$1") + ((path != '') ? "." + path : "") ;
      searchURN = searchURN.replace(/\:[^\:]*$/,"");
   }

   return null;
}

//-----------------------------------------------------------------------------

XLocationMap.prototype.mapURL = function(
   url
)
{

   var searchURL = url;
   while ((/\//).test(searchURL))
   {
      var xPath = "/xcfg:LocationMap/xcfg:Location[@url='" + searchURL + "']";
      var locationXNode = this.X$(xPath);
      if (locationXNode)
      {
         var urn = locationXNode.getAttribute("urn");
         return urn;
      }
      searchURL = searchURL.replace(/\/[^\/]*$/,"");
   }

   return null;
}

//-----------------------------------------------------------------------------

