







































Module('ca.carleton.gcrc.atlas.wfs','$Revision: 2542 $',function(mod){

var ELEMENT_NODE=1;
var TEXT_NODE=3;






mod.WFS=Class(function(publ,priv){




publ.__init__=function(
url_
){

this.url=url_;


this.requests=new Array();


this.featureTypes=new Array();
}





publ.Connect=function(){
var xmlRequest=imprt('ca.carleton.gcrc.xml.request');


var effectiveURL=this.url+'?request=GetCapabilities';


var receiver=this;
try{
xmlRequest.SimpleGetXml(effectiveURL,function(doc_){
if(null!=doc_){
receiver._ConnectCapabilitiesReply(doc_);
}else{
alert("Error loading document: "+effectiveURL);
}
});
}catch(error){
alert('Error loading document: '+effectiveURL+' ('+error+')');
return;
}
}






publ._ConnectCapabilitiesReply=function(doc_)
{



if(false==this._CheckLocalName(doc_.documentElement,'WFS_Capabilities')){
alert('Wrong document type. Expected: \'WFS_Capabilities\' Received: '+doc_.documentElement.localName);
return;
}


var capabilityNodes=doc_.documentElement.getElementsByTagName('Capability');
var capabilityNodesIndex;
for(capabilityNodesIndex=0;capabilityNodesIndex<capabilityNodes.length;++capabilityNodesIndex){
var capabilityNode=capabilityNodes[capabilityNodesIndex];


var requestNodes=capabilityNode.getElementsByTagName('Request');
var requestNodesIndex;
for(requestNodesIndex=0;requestNodesIndex<requestNodes.length;++requestNodesIndex){
var requestNode=requestNodes[requestNodesIndex];


var children=requestNode.childNodes;
var childrenIndex;
for(childrenIndex=0;childrenIndex<children.length;++childrenIndex){
var node=children[childrenIndex];
var type=node.nodeType;


if(ELEMENT_NODE==type){
this.requests.push(new mod.WFSRequestType(node.nodeName));
}
}
}


var featureTypeListNode=this._GetChildByTagName(doc_.documentElement,'FeatureTypeList');
if(null!=featureTypeListNode){
var featureTypeNodes=featureTypeListNode.getElementsByTagName('FeatureType');
var featureTypeNodesIndex;
for(featureTypeNodesIndex=0;featureTypeNodesIndex<featureTypeNodes.length;++featureTypeNodesIndex){
var featureTypeNode=featureTypeNodes[featureTypeNodesIndex];

this._ProcessFeatureTypeNode(featureTypeNode);
}
}
}
}






publ._ProcessFeatureTypeNode=function(node_){

var nameNode=this._GetChildByTagName(node_,'Name');
if(null!=nameNode){
var name=nameNode.firstChild.nodeValue;


var featureType=new mod.WFSFeatureType(name);
this.featureTypes.push(featureType);


var srsNode=this._GetChildByTagName(node_,'SRS');
if(null!=srsNode){
featureType.srs=srsNode.firstChild.nodeValue;
}


var latLonNode=this._GetChildByTagName(node_,'LatLonBoundingBox');
if(null!=latLonNode){
var minxAttr=latLonNode.getAttributeNode('minx');
var minyAttr=latLonNode.getAttributeNode('miny');
var maxxAttr=latLonNode.getAttributeNode('maxx');
var maxyAttr=latLonNode.getAttributeNode('maxy');

if(null!=minxAttr)featureType.minx=minxAttr.nodeValue;
if(null!=minyAttr)featureType.miny=minyAttr.nodeValue;
if(null!=maxxAttr)featureType.maxx=maxxAttr.nodeValue;
if(null!=maxyAttr)featureType.maxy=maxyAttr.nodeValue;
}
}
}








publ._CheckLocalName=function(node_,localName_){
if(localName_==node_.nodeName){
return true;
}




var prefixedLocalName=''+node_.prefix+':'+localName_;
return(prefixedLocalName==node_.nodeName);
}









publ._CheckNodeName=function(node_,namespaceURI_,localName_){

if(namespaceURI_!=node_.namespaceURI){
return false;
}

return this._CheckLocalName(node_,localName_);
}









publ._GetChildrenByTagNameNS=function(node_,namespaceURI_,localName_){
var result=new Array();

var children=node_.childNodes;
var childrenIndex;
for(childrenIndex=0;childrenIndex<children.length;++childrenIndex){
var node=children[childrenIndex];
var type=node.nodeType;


if(ELEMENT_NODE==type){

if(this._CheckNodeName(node,name_)){
result.push(node);
}
}
}

return result;
}








publ._GetChildrenByTagName=function(node_,localName_){
var result=new Array();

var children=node_.childNodes;
var childrenIndex;
for(childrenIndex=0;childrenIndex<children.length;++childrenIndex){
var node=children.item(childrenIndex);
var type=node.nodeType;


if(ELEMENT_NODE==type){

if(this._CheckLocalName(node,localName_)){
result.push(node);
}
}
}

return result;
}








publ._GetChildByTagNameNS=function(node_,namespaceURI_,localName_){
var children=node_.childNodes;
var childrenIndex;
for(childrenIndex=0;childrenIndex<children.length;++childrenIndex){
var node=children.item(childrenIndex);
var type=node.nodeType;


if(ELEMENT_NODE==type){

if(this._CheckNodeName(node,localName_)){
return node;
}
}
}


return null;
}








publ._GetChildByTagName=function(node_,localName_){
var children=node_.childNodes;
var childrenIndex;
for(childrenIndex=0;childrenIndex<children.length;++childrenIndex){
var node=children[childrenIndex];
var type=node.nodeType;


if(ELEMENT_NODE==type){

if(this._CheckLocalName(node,localName_)){
return node;
}
}
}


return null;
}






publ._GetTextValue=function(node_){
var result='';

var children=node_.childNodes;
var childrenIndex;
for(childrenIndex=0;childrenIndex<children.length;++childrenIndex){
var childNode=children.item(childrenIndex);
var type=childNode.nodeType;


if(TEXT_NODE==type){
result+=childNode.nodeValue;
}else{
throw('Text value requested; non-text child was found. Node type: '+type);
}
}

return result;
}










publ.DescribeFeatureType=function(featureType_){

var typeName=featureType_;
if(typeof(featureType_)!='string'){
typeName=featureType_.Name();
}

var doc=Sarissa.getDomDocument();
doc.async=false;


var effectiveURL=this.url+'?request=DescribeFeatureType&version=1.0.0&typename='+typeName;


try{
doc.load(effectiveURL);
}catch(error){
alert('Error loading document: '+effectiveURL+' ('+error+')');
return;
}

if(doc.parseError!=0){
alert("Error loading document: "+effectiveURL);
return;
}


var ns=doc.documentElement.namespaceURI;


if(false==this._CheckLocalName(doc.documentElement,'schema')){
alert('Wrong document type. Expected: \'schema\' Received: '+doc.documentElement.nodeName);
return;
}


var description=new mod.WFSFeatureTypeDescription(typeName);


var complexTypeTags=this._GetChildrenByTagName(doc.documentElement,'complexType');
if(null==complexTypeTags||complexTypeTags.length<1){
alert('Error: complexType not found');
}else{

var complexContentTags=this._GetChildrenByTagName(complexTypeTags[0],'complexContent');
if(null==complexContentTags||complexContentTags.length<1){
alert('Error: complexContent not found');
}else{

var extensionTags=this._GetChildrenByTagName(complexContentTags[0],'extension');
if(null==extensionTags||extensionTags.length<1){
alert('Error: extension not found');
}else{

var sequenceTags=this._GetChildrenByTagName(extensionTags[0],'sequence');
if(null==sequenceTags||sequenceTags.length<1){
alert('Error: sequence not found');
}else{



var elementTags=this._GetChildrenByTagName(sequenceTags[0],'element');
var loop;
for(loop=0;loop<elementTags.length;++loop){
var node=elementTags[loop];
var name=node.getAttribute('name');
var type=node.getAttribute('type');

var attribute=new mod.WFSFeatureTypeAttributeDescription(name,type);
description.AddAttribute(attribute);
}
}
}
}
}

return description;
}





publ.CreateGetFeatureRequest=function(){
return new mod.WFSGetFeatureRequest(this);
}

});





mod.WFSRequestType=Class(function(publ,priv){




publ.__init__=function(
name_
){

this._name=name_;
}





publ.Name=function(){
return this._name;
}
});





mod.WFSFeatureType=Class(function(publ,priv){




publ.__init__=function(
name_
){

this._name=name_;


this.srs=null;


this.minx=null;


this.miny=null;


this.maxx=null;


this.maxy=null;
}





publ.Name=function(){
return this._name;
}





publ.toString=function(){
var desc='FeatureType{';

desc+='\nName:'+this._name+';';
desc+='\nSRS:'+this.srs+';';
desc+='\nBBox:'+this.minx+','+this.miny+','+this.maxx+','+this.maxy+';';
desc+='\n}';

return desc;
}
});







mod.WFSRequest=Class(function(publ,priv){




publ.__init__=function(
wfsObject_
){

this._wfsObject=wfsObject_;


this._responseHanlder=new Array();
}






publ.xmlRequest=function(){
alert('Function xmlRequest should be implemented by all subclasses of mod.WFSRequest');
}







publ.parseResponse=function(domResponse_){
alert('Function parseResponse should be implemented by all subclasses of mod.WFSRequest');
}








publ.InstallResponseHandler=function(object_,method_,data_){
var handlerDesc={};

handlerDesc.object=object_;
handlerDesc.method=method_;
handlerDesc.data=data_;

this._responseHanlder.push(handlerDesc);
}






publ.send=function(){
var xmlRequest=imprt('ca.carleton.gcrc.xml.request');


var wfsRequest=this.xmlRequest();



var receiver=this;
xmlRequest.SimplePostXml(
this._wfsObject.url,
wfsRequest,
[['Content-Type','text/xml; charset=UTF-8']],
function(doc_,error_,data_,url_,xmlhttp_){
if(null!=doc_){
receiver._WfsRequestReply(doc_,xmlhttp_);
}else{
alert('Unable to load '+receiver._wfsObject.url+' data: '+wfsRequest+' error: '+error_);
}
}
);

return null;
}






publ._WfsRequestReply=function(doc_,xmlhttp_){

var response;
try{
response=this.parseResponse(doc_);
}catch(e){
alert('Error obtaining data from '+this._wfsObject.url+': '+e+'\nData sent:'+this.xmlRequest()+'\nResponse: '+xmlhttp_.responseText);
}


var loop;
for(loop=0;loop<this._responseHanlder.length;++loop){
handlerDesc=this._responseHanlder[loop];
if(handlerDesc.object){

handlerDesc.method.call(handlerDesc.object,response,handlerDesc.data);
}else{

handlerDesc.method(response,handlerDesc.data);
}
}
}
});







mod.WFSGetFeatureRequest=Class(mod.WFSRequest,function(publ,priv,supr){




publ.__init__=function(
wfsObject_
){

supr.__init__.call(this,wfsObject_);


this.featureTypes=new Array();


this.filter=null;


this.properties=new Array();
}





publ.AddFeatureType=function(typeName_){
this.featureTypes.push(typeName_);
}





publ.AddProperty=function(property_){
this.properties.push(property_);
}






publ.UseFilter=function(filter_){
this.filter=filter_;
}





publ.xmlRequest=function(){

var xmlRequest='<?xml version="1.0" encoding="UTF-8"?>\n<GetFeature version="1.0.0" xmlns:gml="http://www.opengis.net/gml">';


var typeIndex;
for(typeIndex=0;typeIndex<this.featureTypes.length;++typeIndex){

xmlRequest+='<Query typeName="'+this.featureTypes[typeIndex]+'">';


var propIndex;
for(propIndex=0;propIndex<this.properties.length;++propIndex){

xmlRequest+='<PropertyName>'+this.properties[propIndex]+'</PropertyName>';
}


if(null!=this.filter){
xmlRequest+=this.filter.Print();
}


xmlRequest+='</Query>';
}


xmlRequest+='</GetFeature>\n';

return xmlRequest;










}







publ.parseResponse=function(domResponse_){

if(false==this._wfsObject._CheckLocalName(domResponse_.documentElement,'FeatureCollection')){
throw('Wrong document type. Expected: \'FeatureCollection\' Received: '+domResponse_.documentElement.localName);
return;
}


var features=new Array();


var featureMembers=this._wfsObject._GetChildrenByTagName(domResponse_.documentElement,'featureMember');
if(null==featureMembers){
throw('Error: gml:featureMember not found');
}else{


var memberIndex;
for(memberIndex=0;memberIndex<featureMembers.length;++memberIndex){

var children=featureMembers[memberIndex].childNodes;
var childrenIndex;
for(childrenIndex=0;childrenIndex<children.length;++childrenIndex){
var node=children.item(childrenIndex);
var type=node.nodeType;


if(ELEMENT_NODE==type){


var currentFeature=new mod.Feature();
currentFeature.typeName=node.nodeName;
currentFeature.identifier=node.getAttribute('fid');


features.push(currentFeature);


var properties=node.childNodes;
var propIndex;
for(propIndex=0;propIndex<properties.length;++propIndex){
var propertyNode=properties.item(propIndex);
var propertyNodeType=propertyNode.nodeType;


if(ELEMENT_NODE==type){

var propertyName=propertyNode.nodeName;


if(1==propertyNode.childNodes.length
&&TEXT_NODE==propertyNode.childNodes[0].nodeType){
currentFeature.AddProperty(propertyName,propertyNode.childNodes[0].nodeValue);
}else{

var polygonNode=this._wfsObject._GetChildByTagName(propertyNode,'MultiPolygon');
var lineStringNode=this._wfsObject._GetChildByTagName(propertyNode,'MultiLineString');
var pointNode=this._wfsObject._GetChildByTagName(propertyNode,'Point')

if(null!=polygonNode){
currentFeature.AddProperty(propertyName,this._ValueFromPolygonNode(polygonNode));
}else if(null!=lineStringNode){
currentFeature.AddProperty(propertyName,this._ValueFromLineStringNode(lineStringNode));
}else if(null!=pointNode){
currentFeature.AddProperty(propertyName,this._ValueFromPointNode(pointNode));
}else{

}
}
}
}


break;
}
}
}
}

return features;
}






publ._ValueFromPolygonNode=function(polygonNode_){
var result={};
result.string='';

var polygonMembers=this._wfsObject._GetChildrenByTagName(polygonNode_,'polygonMember');
if(null==polygonMembers)throw('Can not find gml:polygonMember in gml:MultiPolygon');
if(0==polygonMembers.length)throw('Can not find gml:polygonMember in gml:MultiPolygon');

var memberLoop;
for(memberLoop=0;memberLoop<polygonMembers.length;++memberLoop){
var polygon=this._wfsObject._GetChildByTagName(polygonMembers[memberLoop],'Polygon');
if(null==polygon)throw('Can not find gml:Polygon in gml:polygonMember');

var outerBoundaryIs=this._wfsObject._GetChildByTagName(polygon,'outerBoundaryIs');
if(null==outerBoundaryIs)throw('Can not find gml:outerBoundaryIs in gml:Polygon');

var linearRing=this._wfsObject._GetChildByTagName(outerBoundaryIs,'LinearRing');
if(null==linearRing)throw('Can not find gml:LinearRing in gml:outerBoundaryIs');

var coordinates=this._wfsObject._GetChildByTagName(linearRing,'coordinates');
if(null==coordinates)throw('Can not find gml:coordinates in gml:LinearRing');

if(0!=memberLoop){
result.string+=' ';
}
this._AppendCoordinates(result,this._wfsObject._GetTextValue(coordinates),true);
}

return result.string;
}






publ._ValueFromLineStringNode=function(lineStringNode_){
var result={};
result.string='';

var members=this._wfsObject._GetChildrenByTagName(lineStringNode_,'LineStringMember');
if(null==members)throw('Can not find gml:LineStringMember in gml:MultiLineString');
if(0==members.length)throw('Can not find gml:LineStringMember in gml:MultiLineString');

var memberLoop;
for(memberLoop=0;memberLoop<members.length;++memberLoop){
var lineString=this._wfsObject._GetChildByTagName(members[memberLoop],'LineString');
if(null==lineString)throw('Can not find gml:LineString in gml:LineStringMember');

var coordinates=this._wfsObject._GetChildByTagName(lineString,'coordinates');
if(null==coordinates)throw('Can not find gml:coordinates in gml:LineString');


if(0!=memberLoop){
result.string+=' ';
}
var coordText=this._wfsObject._GetTextValue(coordinates);
this._AppendCoordinates(result,coordText,false);
}

return result.string;
}

publ._AppendCoordinates=function(currentResult_,coordinates_,closeLine_){

var lastIndex=0;
var index=coordinates_.indexOf(' ');
while(-1!=index){
if(0==lastIndex){
currentResult_.string+='M';
}else{
currentResult_.string+=' L';
}

var coor=coordinates_.substr(lastIndex,index-lastIndex);
var commaIndex=coor.indexOf(',');
currentResult_.string+=' '+coor.substr(0,commaIndex)+' '+coor.substr(commaIndex+1);
lastIndex=index+1;
index=coordinates_.indexOf(' ',index+1);

if(-1==index&&lastIndex<(coordinates_.length-1)){

index=coordinates_.length;
}
}

if(closeLine_){
currentResult_.string+=' Z';
}
}






publ._ValueFromPointNode=function(pointNode_){
var result='';

var coordinates=this._wfsObject._GetChildrenByTagName(pointNode_,'coordinates');
if(null==coordinates)throw('Can not find gml:coordinates in gml:Point');

result=this._wfsObject._GetTextValue(coordinates);

return result;
}
});





mod.WFSFeatureTypeDescription=Class(function(publ,priv){




publ.__init__=function(
name_
){

this._name=name_;


this.attributes=new Array();
}





publ.Name=function(){
return this._name;
}





publ.AddAttribute=function(anAttribute_){
return this.attributes.push(anAttribute_);
}
});





mod.WFSFeatureTypeDescription=Class(function(publ,priv){





publ.__init__=function(
name_
,type_
){



this.name=name_;




this.type=type_;
}
});





mod.WFSFilter=Class(function(publ,priv){



publ.__init__=function(
){

this._op=null;


this._lhsTag=null;


this._lhsValue=null;


this._rhsTag=null;


this._rhsValue=null;
}







publ.Print=function(){
var result='<Filter>';
result+=this._PrintInner();
result+='</Filter>';

return result;
}






publ._PrintInner=function(){
var result=
'<'+this._op+'>'
+'<'+this._lhsTag+'>'
+this._lhsValue
+'</'+this._lhsTag+'>'
+'<'+this._rhsTag+'>'
+this._rhsValue
+'</'+this._rhsTag+'>'
+'</'+this._op+'>'
;

return result;
}







publ.PropertyEqualTo=function(propertyName_,literal_){
this._op='PropertyIsEqualTo';
this._lhsTag='PropertyName';
this._lhsValue=propertyName_;
this._rhsTag='Literal';
this._rhsValue=literal_;
}
});





mod.Feature=Class(function(publ,priv){



publ.__init__=function(
){

this.typeName=null;


this.identifier=null;


this._properties={};
}






publ.AddProperty=function(name_,value_){
this._properties[name_]=value_;
}






publ.PropertyNames=function(){
var names=new Array();
var name;
for(name in this._properties){
names.push(name);
}
return names;
}







publ.PropertyValueFromName=function(name_){
return this._properties[name_];
}

});
});
