// ===================================================================
// PairSetDomReader
// $Id: PairSetDomReader.js 3967 2009-11-16 20:47:29Z helmut $

var Bible20;
if (!Bible20) {
  Bible20 = {};
}
else if (typeof Bible20 != "object") {
  throw new Error("Bible20 already exists and is not an object");
}

if (!Bible20.Pair) {
  Bible20.Pair = {};
}
else if (typeof Bible20.Pair != "object") {
  throw new Error("Bible20.Pair already exists and is not an object");
}


Bible20.Pair.PairSetDomReader = function()
{
}

Bible20.Pair.PairSetDomReader.prototype.readPairProps = function(PairElem, aPair)
{
  // for overwrite
}

Bible20.Pair.PairSetDomReader.prototype.readPair = function(PairElem, aPairSet, aPool)
{
  var firstParolID  = PairElem.getAttribute("first");
  var secondParolID = PairElem.getAttribute("second");

  // trap missing fields
  if (firstParolID && secondParolID) {
    var FirstParol  = aPool.findAppendParol(firstParolID );
    var SecondParol = aPool.findAppendParol(secondParolID);
    var aPair = aPairSet.addFirstSecond(FirstParol, SecondParol);
    this.readPairProps(PairElem, aPair);
  }
}

// read(dom, aPairSet, aPool)
// For ids that cannot be resolved, appends new Parol with id set.
Bible20.Pair.PairSetDomReader.prototype.read = function(dom, aPairSet, aPool)
{
  try {
    var PairElems = dom.getElementsByTagName("pair");
    for (var i = 0; i < PairElems.length; ++i) {
      var PairElem = PairElems[i];
      this.readPair(PairElem, aPairSet, aPool);
    }
  }
  catch (e) {
    alert("PairSetDomReader.read: " + e);
  }
}

// readWithFunc
// For ids that cannot be resolved, calls func(aPool, parolID)
// func return behaviour:
// - Parol: to be inserted
// - non-true: pair will be skipped
// - throw exception: stop reading

Bible20.Pair.PairSetDomReader.prototype.readStrict = function(dom, aPairSet, aPool, func)
{
  try {
    var PairElems = dom.getElementsByTagName("pair");
    for (var i = 0; i < PairElems.length; ++i) {
      var PairElem = PairElems[i];
      var firstParolID  = PairElem.getAttribute("first");
      var secondParolID = PairElem.getAttribute("second");

      // trap missing fields
      if (firstParolID && secondParolID) {
        var FirstParol  = aPool.findParol(firstParolID ) || func(aPool, firstParolID );
        var SecondParol = aPool.findParol(secondParolID) || func(aPool, secondParolID);
        if (FirstParol && SecondParol) {
          aPairSet.addFirstSecond(FirstParol, SecondParol);
        }
      }
    }
  }
  catch (e) {
    alert("PairSetDomReader.read: " + e);
  }
}
