// -*-Mode: Java;-*- 

/*
JourneySat, a library to georeference a travel journal using Google Maps.
Copyright (C) 2005  Paolo Montrasio, paolo AT paolomontrasio.com

This file is part of JourneySat (the Program).

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 2
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; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/**
 * @author Paolo Montrasio paolo AT paolomontrasio.com
 */

// This is a kind of package JourneySat declaration in Java
if (JourneySat == undefined) var JourneySat = {};

/**
 * @class
 * Provides methods to parse a XML file
 * @constructor
 */

JourneySat.XMLParser = function () {};

/**
 * Scans the child nodes of a parent and returns the one corresponding
 * to a tag.
 * @param {String} tag The tag name
 * @param {Element} parent The XML DOM node 
 * @returns The XML DOM node for the tag
 * @type {Element}
 */
JourneySat.XMLParser.prototype.getNode = function (tag, parent) {
  var name = parent.nodeName;
  if (parent.hasChildNodes()) {
    var node = parent.firstChild;
    while (1) {
      if (node.nodeName == tag) {
	return node;
      }
      if (node == parent.lastChild) {
	return null;
      }
      node = node.nextSibling;
    }
  } else {
    return null;
  }
};

/**
 * Scans the child nodes of a parent and returns the value of the
 * one corresponding to a tag.
 * @param {String} tag The tag name
 * @param {Element} parent The XML DOM node 
 * @returns The XML DOM node for the tag
 * @type {any}
 */  
JourneySat.XMLParser.prototype.getValueOfNode = function (tag, parent) {
  var n = this.getNode(tag, parent);
  if (n == null) return null;
  if (n.firstChild == null) {
    return null; // empty tag -> null value
  }
  return n.firstChild.nodeValue;
};


/**
 * @class JourneySat.XMLEditorParser
 * @extends JourneySat.XMLParser
 * Provides methods to parse the XML files returned by the server side
 * components of the JourneySat editor
 * @constructor
 */
JourneySat.XMLEditorParser = function () {};

JourneySat.XMLEditorParser.prototype = new JourneySat.XMLParser();
JourneySat.XMLEditorParser.prototype.constructor = JourneySat.XMLEditorParser;
JourneySat.XMLEditorParser.prototype.superclass = JourneySat.XMLParser;

/**
 * Returns the value of the <status> tag
 * @returns the value of the <status> tag
 * @type String
 */
JourneySat.XMLEditorParser.prototype.getStatus = function (node) {
  return this.getValueOfNode("status", node);
};


/**
 * @class JourneySat.XMLTravelParser
 * @extends JourneySat.XMLParser
 * Provides methods to parse the JourneySat XML files
 * @constructor
 */
JourneySat.XMLTravelParser = function () {};
JourneySat.XMLTravelParser.prototype = new JourneySat.XMLParser();

/**
 * Returns the value of the <name> tag
 * @returns the value of the <name> tag
 * @type String
 */
JourneySat.XMLTravelParser.prototype.getName = function (node) {
  return this.getValueOfNode("name", node);
};

/**
 * Returns the value of the <description> tag
 * @returns the value of the <description> tag
 * @type String
 */
JourneySat.XMLTravelParser.prototype.getDescription = function (node) {
  return this.getValueOfNode("description", node);
};

/**
 * Returns the value of the <cbody> tag
 * @returns the value of the <cbody> tag
 * @type String
 */
JourneySat.XMLTravelParser.prototype.getCbody = function (node) {
  return this.getValueOfNode("cbody", node);
};
      
/**
 * Returns the value of the <zoom> tag
 * @returns the value of the <zoom> tag
 * @type String
 */
JourneySat.XMLTravelParser.prototype.getZoom = function (node) {
  return this.getValueOfNode("zoom", node);
};
      
/**
 * Returns the value of the <href> tag
 * @returns the value of the <href> tag
 * @type String
 */
JourneySat.XMLTravelParser.prototype.getHref = function (node) {
  return this.getValueOfNode("href", node);
};

/**
 * Returns the value of the <type> tag
 * @returns the value of the <type> tag
 * @type String
 */
JourneySat.XMLTravelParser.prototype.getType = function (node) {
  return this.getValueOfNode("type", node);
};


