/**
 * mm8DXL.js
 * Autor: Matthias Eipeldauer, newmagic datensysteme gmbh
 * Datum: 19.09.2008
 *
 * Diese Datei beinhaltet die Klassen für die Umwandlung der Domino XML (DXL) Daten in JavaScript lesbare Form (JSON).
 * Dafür werden eigens automatisierte Anonyme Klassen erstellt und zur Laufzeit eingebunden.
 *
 * Die Elemente, die Übersetzt werden, landen dann in einem Output-Array, welches alle Instanzen dieser Klasse beinhaltet.
 **/
 
include("mm8Atom.js");
include("mm8Container.js");

/*class*/function mm8DXL(id)/*extends mm8Atom*/
{
  // VERERBUNG
  this.superclass = new mm8Atom(id);
  for (x in this.superclass) (x!="superclass")?this[x]=this.superclass[x]:"";
  this.type.push("mm8DXL");
  // VERERBUNG ENDE
  
  this.ToString = function()
  {
    return superclass.ToString().replace("}", "[newmagic DXL to nmJSON Converter]");
  };
  
  // ----
  // Diese Funktion parst einen ViewEntry - JSON Inhalt in ein von newmagic lesbares Format.
  // Allerdings muss hier spezifiziert werden, welcher Art der Inhalt ist.
  this.Parse = function(oContent)
  {
    if (mm8IsMSIE)
    {
      xRootNode = oContent.lastChild;
    }
    else 
    {
      xRootNode = oContent.childNodes[0];
    }
    xReturn = new mm8Container("tmp" + new Date().getTime());
    
    sDescendantName = "";
    sDescendantValue = "";
    for (iCount = 0; iCount < xRootNode.childNodes.length; iCount++)
    {
      xChild = xRootNode.childNodes[iCount];
      if (xChild.nodeName != "#text")
      {
        if (!this.CheckForDescendants(xChild))        
        {
          xTemp = this.ParseObject(xChild);
          if (sDescendantName != "")
          {
              xTemp.data[sDescendantName] = sDescendantValue;
          }
          xReturn.AppendChild(xTemp);
        }
        else
        {
          // !! Hier geht's um einen Descendanten! (also eine Kategorie!)
          for(iSubCount=0;iSubCount<xChild.childNodes.length;iSubCount++)
          {
            xSubChild = xChild.childNodes[iSubCount];
            if (xSubChild.nodeName != "#text")
            {
              sDescendantName = this.GetXmlAttribute(xSubChild, "name");
              if (mm8IsMSIE)
                sDescendantValue = xChild.text.replace(/\n/g, "").replace("'", "\'").replace('"', "&quot;");  
              else
                sDescendantValue = xChild.textContent.replace(/\n/g, "").replace("'", "\'").replace('"', "&quot;");
            }
          }
        }
      }
    }
    return xReturn;
  };
  
  // ---
  // Diese Funktion prüft nach, ob es sich hierbei um eine Kategorisierte Ansicht handelt oder nicht.
  this.CheckForDescendants = function(xNode)
  {
    try
    {
      this.GetXmlAttribute(xNode, "unid");
      return false;
    }
    catch(ex)
    {
      return true;
    }
    return true;
  };
  
  // ----
  // Diese Funktion parst einen XMLNode, in dem alle ChildNodes durchgegangen werden und die notwendigen Informationen 
  // herausgelesen werden.
  this.ParseObject = function(xNode)
  {
    xID = this.GetXmlAttribute(xNode, "unid");
    xIComObj = new mm8Container(xID);
    xIComObj.position = this.GetXmlAttribute(xNode, "position");
    xIComObj.data = new Object();
    xIComObj.data["unid"] = xID;
    if (this.bDescending)
    {
      
    }
    for(iChildCount=0;iChildCount<xNode.childNodes.length;iChildCount++)
    {
      xChild = xNode.childNodes[iChildCount];
      if (xChild.nodeName != "#text") 
      {
        if (mm8IsMSIE)
        {
          sText = xChild.text;
        }
        else
        {
          sText = xChild.textContent;
        }
        sText = sText.replace(/\n/g, "").replace(/'/g, "&lsquo;").replace(/\"/g, "&quot;");  
        if (sText.indexOf("'") > 0) 
        {
          debugger;
        }
        if (sText.indexOf('"') > 0) 
        {
           debugger;
        }
        xIComObj.data[this.GetXmlAttribute(xChild, "name")] = sText;  
      }
    }
    return xIComObj;
  };
  
  this.GetXmlAttribute = function(xTempNode, xAttributeName)
  {
    if (mm8IsMSIE)
    {
      return xTempNode.attributes.getNamedItem(xAttributeName).nodeValue;
    }
    else
    {
      return xTempNode.attributes[xAttributeName].nodeValue;
    }
  };
  
} 