/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Verbosio Base URI map.
 *
 * The Initial Developer of the Original Code is
 * Alexander J. Vincent <ajvincent@gmail.com>.
 * Portions created by the Initial Developer are Copyright (C) 2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

// Get JS modules.
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://app/modules/ArrayConverter.jsm");
Components.utils.import("resource://app/modules/ecma-debug.jsm");

var C_i = Components.interfaces;

const xeIBaseURIMap               = C_i.xeIBaseURIMap;
const nsIProgrammingLanguage      = C_i.nsIProgrammingLanguage;
const nsISecurityCheckedComponent = C_i.nsISecurityCheckedComponent;
const nsIClassInfo                = C_i.nsIClassInfo;
const nsISupports                 = C_i.nsISupports;

C_i = null;

function XMLBaseURIMap() {
  this._typesMap = {};
  this.documentWrappers = {};
  this.wrappedJSObject = this;
}
XMLBaseURIMap.prototype = {
  // xeIBaseURIMap
  addType: function addType(aType, aURI) {
    if (aType in this._typesMap) {
      throw Components.results.NS_ERROR_FAILURE;
    }

    if ((aURI.charAt(aURI.length - 1) != "/") &&
        (aURI.charAt(aURI.length - 1) != "\\")) {
      throw new Error("URI must end in a slash!");
    }

    this._typesMap[aType] = aURI;
  },

  // xeIBaseURIMap
  getURITypes: function getURITypes(aCount) {
    var retval = [];
    for (var prop in this._typesMap) {
      retval[retval.length] = prop;
    }
    aCount.value = retval.length;
    return retval;
  },

  // xeIBaseURIMap
  getURI: function getURI(aType) {
    return (aType in this._typesMap) ? this._typesMap[aType] : null;
  },

  // xeIBaseURIMap
  getPrimaryLocalURI: function getPrimaryLocalURI(aPath) {
    for (var currentType in this._typesMap) {
      var uri = this.getURI(currentType);
      if (!uri) {
        throw Components.results.NS_ERROR_FAILURE;
      }
      if (aPath.indexOf(uri) == 0) {
        var pathToDocument = aPath.substr(uri.length);
        var localPath = this._typesMap["local"];
        if (!localPath) {
          throw Components.results.NS_ERROR_FAILURE;
        }
        if (localPath.indexOf("/") == -1) {
          pathToDocument = pathToDocument.replace(/\//g, "\\");
        }
        return localPath + pathToDocument;
      }
    }

    throw Components.results.NS_ERROR_FAILURE;
  },

  _interfaces: [xeIBaseURIMap,
                nsIClassInfo],

  // nsIClassInfo
  getInterfaces: ArrayConverter.xpcomArrayMethod("_interfaces"),

  // nsIClassInfo
  getHelperForLanguage: function getHelperForLanguage(aLanguage) {
    return null;
  },

  // nsIClassInfo
  classDescription: "xeIBaseURIMap",

  // nsIClassInfo
  classID: Components.ID("{02b4bc8b-6a66-4457-856c-5d93f12f3a81}"),

  // nsIClassInfo
  contractID: "@verbosio.mozdev.org/base-uri-map;1",

  // nsIClassInfo
  implementationLanguage: nsIProgrammingLanguage.JAVASCRIPT,

  // nsIClassInfo
  flags: 0, /* nsIClassInfo.SINGLETON for services */

  // nsISupports
  QueryInterface: function QueryInterface(aIID)
  {
    for (var j = 0; j < this._interfaces.length; j++) {
      if (aIID.equals(this._interfaces[j])) {
        return this;
      }
    }

    if (aIID.equals(nsISupports))
      return this;

    // Known interfaces we don't support
    if (aIID.equals(nsISecurityCheckedComponent)) {
      throw Components.results.NS_ERROR_NO_INTERFACE;
      return null;
    }

    // Discover what interfaces we need
    var i = Components.interfaces;
    var found = false;
    for (var prop in i) {
      if (aIID.equals(i[prop])) {
        found = true;
        dump("QUERYINTERFACE baseURIMap: " + prop + "\n");
        break;
      }
    }
    if (!found) {
      dump("QUERYINTERFACE baseURIMap: Unknown\n");
    }

    throw Components.results.NS_ERROR_NO_INTERFACE;
    return null;
  }
}

var NSGetModule = XPCOMUtils.generateNSGetModule([
  XMLBaseURIMap
], null, null);
