#!/usr/bin/perl -w # # mob 2010-03-24 # The usual free license verbiage here. Make it better and send it back. # # This code is cleaner than the others I have of this style, since some vars are # passed into the stages. # # # perl program to mimic the display from metacat. I have discovered that this is a # really clunky operation. It re-reads the EML doc for every "display module". # I guess it needs to, since it is generating a new web page each time. (You # could get on with that project to pull the entire EML only once,and hide # sections with tabs. # # ## NOTE on versions of XML-LibXML and XSLT which are available on Penguin: # XML-LibXML # Currently penguin is running http://search.cpan.org/~pajas/XML-LibXML-1.69/ # which uses this syntax for parsing: # my $source = $parser->parse_file( "${xml_dataset_query_url}${docid}" ); # But the current version as of march 2010 is 1.7 which has a different syntax: # my $source = XML::LibXML->load_xml(location => 'test.xml'); # my $style_doc = XML::LibXML->load_xml(location=>'test_person.xsl', no_cdata=>1); # XML-LibXSLT: # per http://search.cpan.org/~msergeant/XML-LibXSLT-1.58/LibXSLT.pm # print $stylesheet->output_string($results); # The 1.7 version is # print $stylesheet->output_as_bytes($results); #--------------------------------------------------------------------------80c-> # load perl modules use strict; # turn on strict syntax checking. use CGI; use XML::LibXSLT; use XML::LibXML; use URI; # for parsing URL syntax. not curently used use LWP::Simple; # for grabbing url. not currently used, but might need its "get" method for URIs use File::Basename; # app must know its own name so stylesheets can create new URLs back #--------------------------------------------------------------------------80c-> # Set up debug status my $debug = 1; #--------------------------------------------------------------------------80c-> # Set up pgm configuration - use template vars here. # create a name for yourself! my $referrer = 'showDataset.cgi'; my $xslt_root_path = '/var/www/sbc.lternet.edu/lib/xslt'; # configure which metacat to use. Use a timeout value to check the health of the # primary Metacat. # The Metacats we have available # Use the long format, so you can add the sessionId param when you get logins running.. my $xml_dataset_query_url = 'http://metacat.lternet.edu/knb/metacat?action=read&qformat=xml&docid='; my $alternate_query_url = 'http://knb.ecoinformatics.org/knb/metacat?action=read&qformat=xml&docid='; my $xml_source_url = ""; # check the primary Metacat URL, and use alternate if there is a problem my $ua = LWP::UserAgent->new; $ua->timeout(5); my $response = $ua->get("$xml_dataset_query_url"); if ($response->is_success) { $xml_source_url = "$xml_dataset_query_url"; } else { $xml_source_url = "$alternate_query_url"; } debug("XML source is: $xml_source_url"); #--------------------------------------------------------------------------80c-> # instantiate common objects and variables # create an object for the input params my $query = new CGI; my $docid = $query->param('docid'); # these for reading XML and transforming my $parser = XML::LibXML->new(); my $xslt = XML::LibXSLT->new(); # set path for EML xslt stylesheets: my $xslt_path = "${xslt_root_path}/eml"; my $source=""; ## parse 2 XML files: the eml dataset and the xslt, then create a styelsheet object $source = $parser->parse_file( "${xml_source_url}${docid}" ); my $style_doc = $parser->parse_file("${xslt_path}/eml-2.0.0.xsl"); my $stylesheet = $xslt->parse_stylesheet($style_doc); #--------------------------------------------------------------------------80c-> # Define the main program logic that calls subroutines to do the work #--------------------------------------------------------------------------80c-> # The processing steps we are handling. A dataset summary view is the default. # "displaymodule" is analogous to "stage" in other cgis, but consistent with # existing wording in eml-*.xsl my $displaymodule = $query->param('displaymodule') || 'dataset'; # define the possible stages my %displaymodules = ( 'dataset' => \&dataset, 'entity' => \&entity, 'attribute' => \&attribute, 'attributedomain' => \&attributeDomain, 'attributecoverage' => \&attributeCoverage, 'attributedetail' => \&attributeDetail, # not sure of use 'attributemethod' => \&attributeMethod, # not yet tested 'inlinedata' => \&inlineData, # not yet tested 'additionalmetadata'=> \&additionalMetadata, 'responsibleparties'=> \&responsibleParties, 'coverage' => \&coverage, # local coveral 'coverageall' => \&coverageall, 'methodsall' => \&methodsall, ); # call the appropriate routine based on the stage. # all stages use the vars for docid, source_xml and xsl_stylesheet # 2010-05-04: referrer, too! if ( $displaymodules{$displaymodule} ) { $displaymodules{ $displaymodule }->("$docid", "$source", "$stylesheet", "$referrer"); } else { &handleResponseMessage(); } #--------------------------------------------------------------------------80c-> # Define the subroutines to do the work #--------------------------------------------------------------------------80c-> ##### ### Default stage, show a dataset # sub dataset { # no other query params needed # create HTML output from EML and stylesheet and send it back my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } ##### ### stage to show all the people as a separate webpage # sub responsibleParties { # get the other params from CGI my $displaymodule = $query->param('displaymodule'); # create HTML output from EML and stylesheet and send it back my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", displaymodule => "$displaymodule", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } ##### ### stage to show all the coverage elements as a separate webpage # sub coverage { # get the other params from CGI my $displaymodule = $query->param('displaymodule'); # create HTML output from EML and stylesheet and send it back my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", displaymodule => "$displaymodule", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } ##### ### stage to show all the coverage elements as a separate webpage # sub coverageall { # get the other params from CGI my $displaymodule = $query->param('displaymodule'); # create HTML output from EML and stylesheet and send it back my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", displaymodule => "$displaymodule", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } ##### ### stage to show all the methods elements as a separate webpage # sub methodsall { # get the other params from CGI my $displaymodule = $query->param('displaymodule'); # create HTML output from EML and stylesheet and send it back my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", displaymodule => "$displaymodule", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } ##### ### # # stage to show the data entity (dataTable tree) sub entity { # get the other params from CGI my $displaymodule = $query->param('displaymodule'); my $entitytype = $query->param('entitytype'); my $entityindex = $query->param('entityindex'); my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", displaymodule => "$displaymodule", entitytype => "$entitytype", entityindex => "$entityindex", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } ##### ### # # stage showing attribute details sub attribute { # get the other params from CGI my $displaymodule = $query->param('displaymodule'); my $entitytype = $query->param('entitytype'); my $entityindex = $query->param('entityindex'); my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", displaymodule => "$displaymodule", entitytype => "$entitytype", entityindex => "$entityindex", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } # note that attributeDomain","attributeCoverage" "attributeMethod", all use the same params, # only the displaymodule changes [HAVE NOT YET TESTED attributeMethod ] ##### ### # sub attributeDomain { # get the other params from CGI my $displaymodule = $query->param('displaymodule'); my $entitytype = $query->param('entitytype'); my $entityindex = $query->param('entityindex'); my $attributeindex = $query->param('attributeindex'); my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", displaymodule => "$displaymodule", entitytype => "$entitytype", entityindex => "$entityindex", attributeindex => "$attributeindex", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } ##### ### # sub attributeCoverage { # get the other params from CGI my $displaymodule = $query->param('displaymodule'); my $entitytype = $query->param('entitytype'); my $entityindex = $query->param('entityindex'); my $attributeindex = $query->param('attributeindex'); my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", displaymodule => "$displaymodule", entitytype => "$entitytype", entityindex => "$entityindex", attributeindex => "$attributeindex", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } ##### ### # sub attributeMethod { # get the other params from CGI my $displaymodule = $query->param('displaymodule'); my $entitytype = $query->param('entitytype'); my $entityindex = $query->param('entityindex'); my $attributeindex = $query->param('attributeindex'); my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", displaymodule => "$displaymodule", entitytype => "$entitytype", entityindex => "$entityindex", attributeindex => "$attributeindex", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } ##### ### # sub inlineData { # get the other params from CGI my $displaymodule = $query->param('displaymodule'); my $entitytype = $query->param('entitytype'); my $entityindex = $query->param('entityindex'); my $distributionlevel = $query->param('distributionlevel'); my $physicalindex = $query->param('physicalindex'); my $distributionindex = $query->param('distributionindex'); my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", displaymodule => "$displaymodule", entitytype => "$entitytype", entityindex => "$entityindex", distributionlevel => "$distributionlevel", physicalindex => "$physicalindex", distributionindex => "$distributionindex", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } ##### ### # sub additionalMetadata { # get the other params from CGI my $displaymodule = $query->param('displaymodule'); my $additionalmetadataindex = $query->param('additionalmetadataindex'); my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string( docid => "$docid", displaymodule => "$displaymodule", additionalmetadataindex => "$additionalmetadataindex", referrer => "$referrer", )); print "Content-type: text/html\n\n"; print $stylesheet->output_string($results); } ##### ### HandleResponseMssage # sub handleResponseMessage { # user picked a stage that does not exist yet. send an error msg. # user picked an EML docid that does not exist # (handled by returning the metacat error xslt) # metacat is not returning data } ##### ### Debug message # sub debug { my $msg = shift; if ($debug) { print STDERR "showDataset.cgi: $msg\n"; } }