/* vc_view.js: view- and controller-related functions for VC Filter

   Copyright (C) 2009 Black Mesa Technologies LLC

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program, in the Licenses subdirectory, in file
   GNU_GPL.
   
   If the license was not provided, or is missing, see 
   <http://www.gnu.org/licenses/>.

*/

/* Revisions:
   2009-05-02 : CMSMcQ : made first version (empty) of this file
*/

var vcxsl;
var vcParams;
var verbosity = 0;

function loadvc() {
  // Load the vc stylesheet into the global variable vcxsl.
  var xhr = new XMLHttpRequest();
  xhr.open("GET","xsd.vc.xsl",false);
  xhr.send("");
  vcxsl = xhr.responseXML;

  vcParams = new Object();
  vcParams['vc.Version'] = 1.1;
  vcParams['vc.AdditionalTypes'] = '';
  vcParams['vc.AdditionalFaceets'] = '';

}

function filtervc() {
  // Feed parameters to vcxsl, 
  // parse the input as a DOM object, 
  // filter the input using vcxsl, 
  // and display the results.

  var xsd0 = DomFromXmlstring( xsl_id('xsdIn').value );
  vcSetParam('vc.Version','versionNumber');
  vcSetParam('vc.AdditionalTypes','automaticTypes');
  vcSetParam('vc.AdditionalFacets','automaticFacets');

  // var xsd1 = DomFromTransformXmlXslt(xsd0,vcxsl);
  var xsd1 = DomFromTransformXmlXsltParams(xsd0,vcxsl,vcParams);
  var serializer = new XMLSerializer();
  xsl_id('results').innerHTML = serializer.serializeToString(xsd1).replace(/&/g,"&amp;").replace(/</g,"&lt;");
  // optionally do XML(serializer.serializeToString(xsd1)).toXMLString();
  // alleged to pretty print with indent step of 2
}

function vcSetParam(nmParam,nmInput) {
  var v = xsl_id(nmInput).value;
  switch (nmParam) {
  case 'vc.Version':
    // check version number as decimal, strip whitespace, ...
    v = dec_clean_dec(v,1.1);
    break;
  case 'vc.AdditionalTypes':
    v = len_clean_len(v);

    break;
  case 'vc.AdditionalFacets':
    // check additional facets as expanded names ...
    v = len_clean_len(v);
    break;
  }
  vcParams[nmParam] = v;
}
  
function bam(s,desc,fallback) {
  alert("The string\n\n   "
	+ s
	+ "\n\nis not recognized as "
	+ desc
	+ ".\n\n"
	+ fallback);
}

function len_clean_len(v) {
  // take what purports to be a white-space-delimited sequence of
  // expanded names, verify it, dropping illegal entries
  vItems = v.split(/\s+/);
  v = "";
  for (i in vItems) {
    expname = vItems[i];

    if (expname === "") {
      // an artifact of our method of splitting ...
      ;
    } else {
      // expname not null
      enParts = expname.match(/^\{([^\}]*)\}(.+)$/);
      if (enParts === null) {
	bam(expname,"an expanded name","Ignoring it.");     
      } else {
	// expname has the pattern {...}...
	
	var nsname = enParts[1];
	var localname = enParts[2];
	
	urimatch = mp_NT_Regex['URI'].exec(nsname);
	lnmatch = mp_NT_Regex['NCName'].exec(localname);
	if ((urimatch === null) && (nsname !== "")) {
	  bam(nsname,
	      "a URI, and thus as a possible namespace name",
	      "Ignoring this entry in the list.");
	  /*
	    alert("The string\n\n    "
	    + nsname
	    + "\n\nis not recognized as a URI, and thus not as a possible namespace name.  "
	    + "Ignoring \""
	    + expname
	    + "\"."
	    );
	  */
	}
	if (lnmatch === null) {
	  bam(localname,
	      "an NCName, and thus as the local-name part of an expanded name",
	      "Ignoring this entry in the list");
	}
	if (urimatch === null || lnmatch === null) {
	  // error message already issued above
	  ;
	} else {
	  // nsname good, local name good
	  v += " " + expname;
	} // if expname is good
      } // if expname has the pattern {...}...
    } // if expname not null
  } // for expname in vItems
  return v;
} // function len_clean_len

function dec_clean_dec(s,decDefault) {
  v = s.replace(/^\s+/,'').replace(/\s+$/,'');
  v = v.replace(/^\+/,''); // strip leading plus sign
  if (v.match(/^-?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))$/)) {
    ;
  } else {
    bam(s,"a decimal number","Using 1.1 instead.");
    /*
      alert("The string \n\n    " 
      + xsl_id(nmInput).value 
      + "\n\nis not recognized as a decimal number.  Using 1.1 instead.");
    */
    v = decDefault;
  }
  return v;
}
