/* ***** 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 XUL Language Pack Service.
 *
 * The Initial Developer of the Original Code is
 * Alexander J. Vincent <ajvincent@gmail.com>.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * 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 ***** */

/* Begin static code. */

// Utility function.
function ERROR_FAILURE() {
  throw Components.results.NS_ERROR_FAILURE;
}

// Utility function.
function NOT_IMPLEMENTED() {
  throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
}

// Utility function.
function ERROR_ABORT() {
  throw Components.results.NS_ERROR_ABORT;
}

// Utility function
function NOT_INITIALIZED() {
  throw Components.results.NS_ERROR_NOT_INITIALIZED;
}

// Utility function
function ALREADY_INITIALIZED() {
  throw Components.results.NS_ERROR_ALREADY_INITIALIZED;
}

const C_i = Components.interfaces;

const mozIJSSubScriptLoader       = C_i.mozIJSSubScriptLoader;
const nsIScriptError              = C_i.nsIScriptError;
const nsIProgrammingLanguage      = C_i.nsIProgrammingLanguage;
const nsISecurityCheckedComponent = C_i.nsISecurityCheckedComponent;
const nsIClassInfo                = C_i.nsIClassInfo;
const nsIComponentRegistrar       = C_i.nsIComponentRegistrar;
const nsIFactory                  = C_i.nsIFactory;
const nsIModule                   = C_i.nsIModule;
const nsISupports                 = C_i.nsISupports;

var Module = {
  factoriesByClassId: {},

  constructorsArray: [],

  addConstructor: function addConstructor(aConstructor) {
    var cid = aConstructor.prototype.classID.toString();
    this.factoriesByClassId[cid] = {
      createInstance: function createInstance(outer, iid) {
        if (outer != null)
          throw Components.results.NS_ERROR_NO_AGGREGATION;
        return (new aConstructor()).QueryInterface(iid);
      }
    };

    this.constructorsArray[this.constructorsArray.length] = aConstructor;
  },

  registerSelf: function registerSelf(compMgr, fileSpec, location, type) {
    compReg = compMgr.QueryInterface(nsIComponentRegistrar);

    for (var i = 0; i < this.constructorsArray.length; i++) {
      var proto = this.constructorsArray[i].prototype;
      compReg.registerFactoryLocation(proto.classID,
                                      proto.classDescription,
                                      proto.contractID,
                                      fileSpec,
                                      location,
                                      type);
    }
  },

  getClassObject: function getClassObject(compMgr, aCID, aIID) {
    if (!aIID.equals(nsIFactory)) {
      throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
    }

    for (var prop in this.factoriesByClassId) {
      if (aCID.equals(Components.ID(prop))) {
        loadSubscripts();
        return this.factoriesByClassId[prop];
      }
    }
    throw Components.results.NS_ERROR_NO_INTERFACE;
  },

  canUnload: function canUnload() {
    return true;
  },

  QueryInterface: function QueryInterface(aIID) {
    if (aIID.equals(nsIModule) ||
       (aIID.equals(nsISupports)))
      return this;

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

function NSGetModule(compMgr, fileSpec) {
  return Module;
}

var subscriptsLoaded = false;
function loadSubscripts() {
  if (subscriptsLoaded) {
    return;
  }
  var jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
                           .getService(mozIJSSubScriptLoader);
  jsLoader.loadSubScript("resource://app/res/ecma-debug.js");
  subscriptsLoaded = true;
}

const xeIXMLLanguagePackService = C_i.xeIXMLLanguagePackService;
const xeILanguagePackService    = C_i.xeILanguagePackService;

const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

function LanguagePack()
{
}
LanguagePack.prototype =
{
  // xeIXMLLanguagePackService
  namespaceURI: XUL_NS,

  // xeIXMLLanguagePackService
  prefix: "xul",

  // xeIXMLLanguagePackService
  getDTDURIArray: function getDTDURIArray(aCount) {
    var aArray = [];
    aCount.value = aArray.length;
    return aArray;
  },

  // xeIXMLLanguagePackService
  getXMLSchemaArray: function getXMLSchemaArray(aCount) {
    var aArray = [];
    aCount.value = aArray.length;
    return aArray;
  },

  // xeIXMLLanguagePackService
  attributeWrapperName: "box",

  // xeIXMLLanguagePackService
  handleWhitespace: function handleWhitespace(aLocalName) {
    // XUL doesn't care about ignorable white space.
    return xeIXMLLanguagePackService.WHITESPACE_IGNORABLE |
           xeIXMLLanguagePackService.WHITESPACE_GLOBAL;
  },

  // xeIXMLLanguagePackService
  isURINode: function isURINode(aNode) {
    NOT_IMPLEMENTED();
  },

  // xeIXMLLanguagePackService
  checkValidity: function checkValidity(aNode, aWrapper, aRecursive) {
    NOT_IMPLEMENTED();
  },

  // xeILanguagePackService
  contentType: "application/vnd.mozilla.xul+xml",

  // xeILanguagePackService
  get specificationURI() {
    NOT_IMPLEMENTED();
  },

  // xeILanguagePackService
  description: "eXtensible User-interface Language",

  // xeILanguagePackService
  id: "XUL",

  // xeILanguagePackService
  version: "1.0",

  // xeILanguagePackService
  getNewTransaction: function getNewTransaction(aName) {
    NOT_IMPLEMENTED();
  },

  // xeILanguagePackService
  getFirstSyntaxError: function getFirstSyntaxError(aWrapper) {
    NOT_IMPLEMENTED();
  },

  // nsIClassInfo
  getInterfaces: function getInterfaces(aCount) {
    var interfaces = [xeIXMLLanguagePackService,
                      xeILanguagePackService,
                      nsIClassInfo];
    aCount.value = interfaces.length;
    return interfaces;
  },

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

  // nsIClassInfo
  classDescription: "Language Pack Service: XUL",

  // nsIClassInfo
  classID: Components.ID("14d626bd-6965-49c6-9cd5-3e30a2d59433"),

  // nsIClassInfo
  contractID: "@verbosio.mozdev.org/language-pack-service;1?namespaceURI=" +
              XUL_NS,

  // nsIClassInfo
  implementationLanguage: nsIProgrammingLanguage.JAVASCRIPT,

  // nsIClassInfo
  flags: nsIClassInfo.SINGLETON, // service

  // nsISupports
  QueryInterface: function QueryInterface(aIID) {
    if (aIID.equals(xeIXMLLanguagePackService) ||
        aIID.equals(xeILanguagePackService) ||
        aIID.equals(nsIClassInfo) ||
        aIID.equals(nsISupports))
      return this;

    // 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: XUL LanguagePack " + prop + "\n");
        break;
      }
    }
    if (!found) {
      dump("QUERYINTERFACE: XUL LanguagePack Unknown\n");
    }

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

Module.addConstructor(LanguagePack);
