#!/usr/bin/perl -Tw use strict; # to process query string paramters and print the proper HTTP header. use CGI ":cgi"; use XML::LibXML; my $q = new CGI; # For test for test #my $q = new CGI('request=GetObservation&offering=http://marinemetadata.org/cf#sea_water_salinity&SensorId=urn:gomoos:org:platform#A0116'); ####################################### # Determine the REQUEST type and make it upper case. ####################################### my $request = ''; $request = uc( $q->param('request') ) if ( $q->param('request') ); $request = uc( $q->param('REQUEST') ) if ( $q->param('REQUEST') ); error("Missing REQUEST") if ! $request; ####################################### # set the proper template and local data files based on $request ####################################### my ($template_file, $data_file, $data_field_count) = ('', '', 0); my @input_data = (); my @input_field_names = (); my @input_field_data = (); if( $request eq 'GETCAPABILITIES' ){ $template_file = 'GetCapabilities.tpl'; $data_file = 'GetCapabilities.txt'; # $data_field_count = 23; $data_field_count = 15; # drf5n } if( $request eq 'GETOBSERVATION' ){ # GetObservation passes these my ($SensorId, $offering, $time, $urn) = ('','','', ''); $template_file = 'GetObservation.tpl'; $data_field_count = 2; $SensorId = uc( $q->param('SensorId') ) if $q->param('SensorId'); error("SensorId required. Try SensorId=%23A011") if (!$SensorId); ($urn, $SensorId) = split(/#/, $SensorId); # The Observation data text file is named: GetObservation_.txt # At GoMOOS the buoy id is the SensorId. $data_file = "GetObservation_$SensorId.txt"; $offering = uc( $q->param('offering') ) if $q->param('offering'); error("offering parameter required, try offering=URN:#23SEA_WATER_SALINITY") if (!$offering); # We don't care about the uri part. ($urn, $offering) = split(/#/, $offering); error("Unknown offering parameter: $offering") if ($offering ne 'SEA_WATER_SALINITY'); # We are not using time yet. We assume observation files contain the latest salinity readings. $time = $q->param('time') if $q->param('time'); } ######## Assert a good request error("No template file -- request=$request not understood\n") if ( ! ($template_file =~ /.+/) ); ####################################### # open the template ###################### # this allow us to open the template file and read it as one big string. my $parser = XML::LibXML->new(); my $doc = $parser->parse_file( $template_file ) or error("Could not open: $template_file $!"); #my $sos_xml = $doc->serialize; # drf get the template back from the DOM ####################################### # open the local data file. ####################################### open(DATA, "< $data_file") or error("Could not open: $data_file"); @input_data = ; close(DATA); chomp($input_data[0]); chomp($input_data[1]); # line 1 contains the field names @input_field_names = split(/\|/, $input_data[0]); # line 2 contains the local data @input_field_data = split(/\|/, $input_data[1]); if( @input_field_names != @input_field_data){ error("Input field name count does not match input data count."); } if( @input_field_data != $data_field_count ) { error("$request data field count must be $data_field_count not $#input_field_data +1"); } ####################################### # substitute our values in the template ####################################### for (my $j = 0; $j <= $#input_field_names; $j++) { my $fld = $input_field_names[$j]; my $val = $input_field_data[$j]; # print "#$fld:$val:looping replacement\n"; # $sos_xml =~ s/\$$fld/$val/; # don't do the text subs my @nodelist = ($doc->findnodes("//ows:$fld/child::text()")); # print "\nTrying //$fld", @nodelist; if (@nodelist) { # print "--:",$nodelist[0]->data,"\n"; $nodelist[0]->setData($val); }; } #print "\n--------\n"; if($request eq 'GETCAPABILITES') { # skip the first two lines, each record afterward is an OMResult tuple, separated by ' '; for (my $j = 2; $j <= $#input_data; $j++){ } } my $sos_xml = $doc->serialize; # drf get the template back from the DOM ####################################### # GETOBSERVATION OMResult ####################################### if($request eq 'GETOBSERVATION') { my $tuple_separator = ' '; my $om_results = ''; # skip the first two lines, each record afterward is an OMResult tuple, separated by ' '; for (my $j = 2; $j <= $#input_data; $j++){ # skip comments next if( $input_data[$j] =~ /^#/); # Remove newlines and make sure no blanks. blank is our tuple separator. chomp($input_data[$j]); $input_data[$j] =~ s/$tuple_separator//g; $om_results .= $input_data[$j] . $tuple_separator; } $sos_xml =~ s/\$OMResult/$om_results/; } ####################################### # print the template ####################################### print header( -type => 'text/xml', ); print $sos_xml; exit; ####################################### sub error { my $msg = shift; print header( -type => 'text/html', ); print "

$msg

\n"; warn $msg; exit; }