/* Filename: terms.js
 * This file is part of the gRaDF Semantic Web browsing tool.
 * Copyright (C) 2006  Marcus Cobden
 * 
 * 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
 * (or see the file gpl.txt)
 * 
 * 	Description:
 * This file contains classes to store the information from rdf documents.
 * 
 *	Classes:
 * RDFStatement
 * RDFNodeFactory
 * RDFNode
 * RDFSymbol
 * RDFLiteral
 * RDFBlankNode
 * RDFCollection
 * RDF Constants
 * 
 * 	Version information:
 * $HeadURL$
 * $Date$
 * $Author$
 * $Rev$
 */

/**
 * RDF Statement
 * 
 * Has additional document property so we can tell where triples came from
 */
function RDFStatement(id, subject, predicate, object, src)
{
	this.id = id;
    this.subject = subject;
    this.predicate = predicate;
    this.object = object;
	this.src = src;

    return this;
}

RDFStatement.prototype.type = 'RDFStatement';

/**
 * RDF Blank Node Factory
 *
 */
function RDFNodeFactory()
{
	this.symbols = [];
	this.nextBNodeID = 0;
	this.nextStmtID = 0;
	
	return this;
}

RDFNodeFactory.prototype.getStatement = function(subj, pred, obj, src){
	// This is here so we can give statements an ID some day
	return new RDFStatement(this.nextStmtID++, subj, pred, obj, src);
};

RDFNodeFactory.prototype.getSymbol = function(uri){
	if(this.symbols[uri]){
		return this.symbols[uri];
	} else {
		this.symbols[uri] = new RDFSymbol(uri);
		return this.symbols[uri];
	}
};

RDFNodeFactory.prototype.getBnode = function(){
	return new RDFBlankNode('bNode-' + this.nextBNodeID++);
};

RDFNodeFactory.prototype.getCollection = function(){
	return new RDFCollection(this.nextBNodeID++);
};

RDFNodeFactory.prototype.getLiteral = function(value, lang, datatype){
	return new RDFLiteral(value, lang, datatype);
};

RDFNodeFactory.prototype.type = 'RDFNodeFactory';

/**
 * RDF Node
 * Abstract thing
 */
function RDFNode() {
	return this;
}

/**
 * RDF Symbol Node
 *
 */
function RDFSymbol(uri)
{
	this.uri = uri;
	this.value = this.uri;
	return this;
}

RDFSymbol.prototype = new RDFNode();
RDFSymbol.prototype.constructor = RDFSymbol;
RDFSymbol.prototype.type = 'RDFSymbol';

/**
 * RDF Literal Node
 *
 */
function RDFLiteral(value, lang, datatype)
{
	this.value = value;
    this.lang=lang;
    this.datatype=datatype;

	return this;
}

RDFLiteral.prototype = new RDFNode();
RDFLiteral.prototype.constructor = RDFLiteral;
RDFLiteral.prototype.type = 'RDFLiteral';

/**
 * RDF Blank Node 
 *
 */
function RDFBlankNode(id)
{
	this.id = id;
	this.value = this.id;
	return this;
}

RDFBlankNode.prototype = new RDFNode();
RDFBlankNode.prototype.constructor = RDFBlankNode;
RDFBlankNode.prototype.type = 'RDFBlankNode';

/**
 * RDF Collection
 *
 */
function RDFCollection(id)
{
    this.id = id;
    this.elements = [];
    this.closed = false;
	this.value = this.id;
	return this;
};

RDFCollection.prototype = new RDFNode();
RDFCollection.prototype.constructor = RDFCollection;
RDFCollection.prototype.type = 'RDFCollection';

RDFCollection.prototype.append = function (el) {
    this.elements.push(el);
};

RDFCollection.prototype.close = function () {
    this.closed = true;
};

/** RDF CONSTANTS **/

window.RDFConst = [];
RDFConst['ns'] = 
{
	rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
	rdfs: 'http://www.w3.org/2000/01/rdf-schema#'
};
RDFConst['abbr'] =
{
	/** RDF **/
	'rdf:type':				RDFConst.ns.rdf + 'type',
	'rdf:Property':			RDFConst.ns.rdf + 'Property',
	/** RDFS **/
	'rdfs:label':			RDFConst.ns.rdfs + 'label',
	'rdfs:Class':			RDFConst.ns.rdfs + 'Class',
	'rdfs:subClassOf':		RDFConst.ns.rdfs + 'subClassOf',
	'rdfs:subPropertyOf':	RDFConst.ns.rdfs + 'subPropertyOf'
};

