package EPrints::Plugin::Export::ETD_MS; use EPrints::Plugin::Export; @ISA = ( "EPrints::Plugin::Export" ); use strict; sub new { my( $class, %opts ) = @_; my $self = $class->SUPER::new( %opts ); $self->{metadataPrefix} = "oai_etdms"; $self->{name} = "ETD_MS"; $self->{accept} = ['dataobj/eprint', 'list/eprint']; $self->{visible} = "all"; $self->{suffix} = ".xml"; $self->{mimetype} = "text/xml; charset=utf-8"; $self->{xmlns} = "http://www.ndltd.org/standards/metadata/etdms/1.0/"; $self->{schemaLocation} = "http://www.ndltd.org/standards/metadata/etdms/1.0/etdms.xsd"; $self->{disable} = 1; return $self; } sub output_list { my( $plugin, %opt ) = @_; my $dc = $plugin->{session}->make_element( "collection", ); foreach my $dataobj ($opt{list}->get_records) { $dc->appendChild( $plugin->xml_dataobj($dataobj) ); } #filehandle provided by the command line export tool if (defined $opt{fh}) { print {$opt{fh}} EPrints::XML::to_string($dc); return undef; } else { return EPrints::XML::to_string($dc); } } sub output_dataobj { my( $plugin, $dataobj ) = @_; my $xml = $plugin->xml_dataobj( $dataobj ); return EPrints::XML::to_string( $xml ); } sub xml_dataobj { my( $plugin, $dataobj ) = @_; my $data = $plugin->convert_dataobj( $dataobj ); my $dc = $plugin->{session}->make_element( "etd_ms:thesis", "xmlns:etd_ms" => "http://www.ndltd.org/standards/metadata/etdms/1.0/", "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation" => "http://www.ndltd.org/standards/metadata/etdms/1.0/ http://www.ndltd.org/standards/metadata/etdms/1.0/etdms.xsd" ); # turn the list of pairs into XML blocks (indented by 8) and add them to the DC element. foreach( @{$data} ) { $dc->appendChild( $_ ); # produces value } return $dc; } sub convert_dataobj { my( $plugin, $eprint ) = @_; my $session = $plugin->{session}; my @dcdata = (); if ( $eprint->exists_and_set( "type" ) && $eprint->get_value( "type" ) eq "thesis" ) { my $fields = $session->get_conf('etd_ms','fields'); foreach my $field_conf (@{$fields}) { my $tags = $plugin->generate_tag($eprint, $field_conf); foreach my $tag (@{$tags}) { push @dcdata, ($tag) if $tag; #note that some tags may be undef -- don't include these (a bit of a hack, but simplifies the code immensely) } } } return \@dcdata } #returns an arrayref to handle multiple tags sub generate_tag { my ($plugin, $eprint, $field_conf) = @_; #Constant value if ($field_conf->{type} eq 'constant') { return $plugin->generate_constant_tag($eprint, $field_conf); } elsif ($field_conf->{type} eq 'simple_text') { return $plugin->generate_simple_text_tag($eprint, $field_conf); } elsif ($field_conf->{type} eq 'name') { return $plugin->generate_name_tag($eprint, $field_conf); } elsif ($field_conf->{type} eq 'compound') { return $plugin->generate_compound_tag($eprint, $field_conf); } elsif ($field_conf->{type} eq 'function') { return $plugin->generate_function_tag($eprint, $field_conf); } return []; } sub generate_constant_tag { my ($plugin, $eprint, $field_conf) = @_; my $tag = $plugin->_simple_tag($field_conf, $field_conf->{value}); return [ $tag ]; } sub generate_function_tag { my ($plugin, $eprint, $field_conf) = @_; return $plugin->repository->call($field_conf->{function}, $plugin, $eprint, $field_conf); #must return an arrayref to an array of tags } sub generate_simple_text_tag { my ($plugin, $eprint, $field_conf) = @_; my $tags = []; my $fieldname = $field_conf->{eprint_fieldname}; if ($eprint->exists_and_set($fieldname)) { my $value = $eprint->value($fieldname); my $values = [$value] unless ref $value eq 'ARRAY'; #normalise to an arrayref foreach my $value (@{$values}) { push @{$tags}, $plugin->_simple_tag($field_conf, $value); last unless $field_conf->{multiple}; #only do one if it isn't multiple } } else { push @{$tags}, $plugin->_simple_tag($field_conf); } return $tags; } sub generate_name_tag { my ($plugin, $eprint, $field_conf) = @_; my $tags = []; my $fieldname = $field_conf->{eprint_fieldname}; if ($eprint->exists_and_set($fieldname)) { my $names = $eprint->value($fieldname); $names = [ $names ] unless ref $names eq 'ARRAY'; #normalise to an arrayref foreach my $name (@{$names}) { push @{$tags}, $plugin->_simple_tag($field_conf, EPrints::Utils::make_name_string($name)); last if !$field_conf->{multiple}; #only do the first one unless multiple is set -- this might be a good idea to do for all functions } } else { push @{$tags}, $plugin->_simple_tag($field_conf); #in case we include null values } return $tags } sub generate_compound_tag { my ($plugin, $eprint, $field_conf) = @_; my $tags = []; my $value_is_set_flag = 0; my $parent = $plugin->{session}->make_element( $field_conf->{tagname}); foreach my $subfield (@{$field_conf->{parts}}) { $tags = $plugin->generate_tag($eprint, $subfield); if (scalar @{$tags}) { my $tag = $tags->[0]; #just take the first one -- we don't support multiple compound if ($tag) { $value_is_set_flag++; $parent->appendChild($tag); } } } if ($value_is_set_flag || $field_conf->{include_null_value}) { push @{$tags}, $parent; } return $tags; } sub _simple_tag { my ($plugin, $field_conf, $value) = @_; if (!defined $value && $field_conf->{include_null_value}) { $value = ""; } my %opts = undef; if ($field_conf->{opts}) { %opts = %{$field_conf->{opts}}; } if (defined $value) { return $plugin->{session}->render_data_element(8, $field_conf->{tagname}, $value, %opts) } return undef; } 1;