DAIA::Message - An optional information text or error message


DAIA documentation Contained in the DAIA distribution.

Index


Code Index:

NAME

Top

DAIA::Message - An optional information text or error message

DESCRIPTION

Top

Messages can occurr as property of DAIA::Response, DAIA::Document, DAIA::Item, and DAIA::Availability objects.

PROPERTIES

Top

content

The message as plain Unicode string. The default value is the empty string.

lang

A mandatory RFC 3066 language code. The default value is defined in $DAIA::Message::DEFAULT_LANG and set to 'en'.

errno

An integer value error code. By default a message has no error code.

The message function is a shortcut for the DAIA::Message constructor:

  $msg = DAIA::Message->new( ... );
  $msg = message( ... );

The constructor understands several abbreviated ways to define a message:

  $msg = message( $content [, lang => $lang ] [, errno => $errno ] )
  $msg = message( $lang => $content [, errno => $errno ] )
  $msg = message( $lang => $content [, $errno ] )

The value undef as error code is ignored on construction. You can undefine an error code by explicitely setting it to undef.

  $msg->errno( undef );

To set or get all messages of an object, you use the messages accessor. You can pass an array reference or an array:

  $messages = $document->message;  # returns an array reference

  $document->message( [ $msg1, $msg2 ] );
  $document->message( [ $msg ] );
  $document->message( $msg1, $msg2);
  $document->message( $msg );

To append a message you can use the add or the addMessage method:

  $document->add( $msg );         # $msg must be a DAIA::Message
  $document->addMessage( ... );   # ... is passed to message constructor

  $document += $msg;              # same as $document->add( $msg );

In addition to the message function there is the error function to quickly construct error messages. The first parameter is always treated as error number:

  error() # errno = 0
  error( $errno [, $lang => $content ] ) 
  error( $errno, $content [, lang => $lang ] )

FUNCTIONS

Top

is_language_tag ( $tag )

Returns whether $tag is a formally valid language tag. The regular expression follows XML Schema type xs:language instead of RFC 3066. For true RFC 3066 support have a look at I18N::LangTags.

AUTHOR

Top

Jakob Voss <jakob.voss@gbv.de>

LICENSE

Top

Copyright (C) 2009-2010 by Verbundzentrale Goettingen (VZG) and Jakob Voss

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.


DAIA documentation Contained in the DAIA distribution.
package DAIA::Message;

use strict;
use base 'DAIA::Object';
our $VERSION = '0.27';

our $DEFAULT_LANG = 'en';

our %PROPERTIES = (
    content => { 
        default => '', 
        filter => sub { "$_[0]" }  # stringify everything
    },
    lang => { 
        default => sub { $DEFAULT_LANG },
        filter => sub { 
            is_language_tag("$_[0]") ? lc("$_[0]") : undef;
        },
    },
    errno => {                                           
        filter => sub { 
            $_[0] =~ m/^-?\d+$/ ? $_[0] : undef  
        } 
    },
);

# called by the constructor
sub _buildargs {
    my $self = shift;
    if ( @_ % 2 ) {  # content as first parameter
        my ($content, %p) = @_;
        if ( @_ == 3 and not defined $PROPERTIES{$_[1]} ) {
            return ( lang => $_[0], content => $_[1], errno => $_[2] );
        } else {
            return ( content => $content, %p );
        }
    } elsif ( defined $_[0] and not defined $PROPERTIES{$_[0]} and is_language_tag($_[0]) ) {
        my ($lang, $content, %p) = @_;
        return ( lang => $lang, content => $content, %p );
    } else {
        return @_;
    }

    return @_;
}

sub is_language_tag {
    my($tag) = lc($_[0]);
    return $tag =~ /^[a-z]{1,8}(-[a-z0-9]{1,8})*$/;
}

1;