| 1 | package AxKit::App::TABOO::Data::MediaType; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use Encode; | ||
| 6 | |||
| 7 | use Data::Dumper; | ||
| 8 | use AxKit::App::TABOO::Data; | ||
| 9 | use vars qw/@ISA/; | ||
| 10 | @ISA = qw(AxKit::App::TABOO::Data); | ||
| 11 | |||
| 12 | use DBI; | ||
| 13 | use Exception::Class::DBI; | ||
| 14 | use MIME::Types; | ||
| 15 | |||
| 16 | |||
| 17 | our $VERSION = '0.2'; | ||
| 18 | |||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | AxKit::App::TABOO::Data::MediaType - MediaType Data objects for TABOO | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | use AxKit::App::TABOO::Data::MediaType; | ||
| 27 | $type = AxKit::App::TABOO::Data::MediaType->new(@dbconnectargs); | ||
| 28 | $type->load(limit => {mimetype => 'text/html'}); | ||
| 29 | |||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | This contains a simple class for MIME Types as defined by IANA. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | AxKit::App::TABOO::Data::MediaType->elementorder("mimetype, name, uri"); | ||
| 38 | AxKit::App::TABOO::Data::MediaType->dbfrom("mediatypes"); | ||
| 39 | |||
| 40 | =head1 METHODS | ||
| 41 | |||
| 42 | This class implements only one method and reimplements two, in | ||
| 43 | addition to the constructor, the rest is inherited from | ||
| 44 | L<AxKit::App::TABOO::Data>. | ||
| 45 | |||
| 46 | =over | ||
| 47 | |||
| 48 | =item C<new(@dbconnectargs)> | ||
| 49 | |||
| 50 | The constructor. Nothing special. | ||
| 51 | |||
| 52 | |||
| 53 | =item C<load(what =E<gt> fields, limit =E<gt> {mimetype =E<gt> value, [...]})> | ||
| 54 | |||
| 55 | Nothing very different from other load methods. You would usually load | ||
| 56 | an object by specifying the C<mimetype> as in the above example. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub load { | ||
| 61 | my ($self, %args) = @_; | ||
| 62 | my $data = $self->_load(%args); | ||
| 63 | if ($data) { | ||
| 64 | ${$self}{'ONFILE'} = 1; | ||
| 65 | } else { | ||
| 66 | return undef; | ||
| 67 | } | ||
| 68 | $self->populate($data); | ||
| 69 | return $self; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | =item C<populate($args)> | ||
| 74 | |||
| 75 | The populate method is reimplemented, to deal well with L<MIME::Type> | ||
| 76 | objects. Its interface is unchanged, however. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub populate { | ||
| 81 | my $self = shift; | ||
| 82 | my $args = shift; | ||
| 83 | ${$self}{'uri'} = ${$args}{'uri'}; | ||
| 84 | ${$self}{'name'} = Encode::decode_utf8(${$args}{'name'}); | ||
| 85 | my $mimetypes = MIME::Types->new(only_complete => 1); | ||
| 86 | my MIME::Type $type = $mimetypes->type(${$args}{'mimetype'}); | ||
| 87 | ${$self}{'mimetype'} = ($type) ? $type : ${$args}{'mimetype'}; | ||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | =item C<mimetype> | ||
| 93 | |||
| 94 | Will return a L<MIME::Type> object with the loaded MIME Type. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub mimetype { | ||
| 99 | my $self = shift; | ||
| 100 | if (ref(${$self}{'mimetype'}) eq 'MIME::Type') { | ||
| 101 | return ${$self}{'mimetype'}; | ||
| 102 | } | ||
| 103 | my $mimetypes = MIME::Types->new(only_complete => 1); | ||
| 104 | my MIME::Type $type = $mimetypes->type(${$self}{'mimetype'}); | ||
| 105 | return $type; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | =back | ||
| 110 | |||
| 111 | =head1 STORED DATA | ||
| 112 | |||
| 113 | The data is stored in named fields, and for certain uses, it is good | ||
| 114 | to know them. If you want to subclass this class, you might want to | ||
| 115 | use the same names, see the documentation of | ||
| 116 | L<AxKit::APP::TABOO::Data> for more about this. These are the names of | ||
| 117 | the stored data of this class: | ||
| 118 | |||
| 119 | =over | ||
| 120 | |||
| 121 | =item * mimetype | ||
| 122 | |||
| 123 | The IANA-registered MIME types. | ||
| 124 | |||
| 125 | =item * name | ||
| 126 | |||
| 127 | An expanded name intended for human consumption. | ||
| 128 | |||
| 129 | |||
| 130 | =item * uri | ||
| 131 | |||
| 132 | Creating new media types seems like a long and burdensome process. I | ||
| 133 | was hoping that some day, also media types will be identified by | ||
| 134 | URIs. I don't know if there is any progress on that, but I reserve | ||
| 135 | this field in the hope. Also, in the Semantic Web you'd like to | ||
| 136 | identify things and their relationships with URIs, so we might as well | ||
| 137 | have a field. | ||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | =back | ||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | =head1 XML representation | ||
| 146 | |||
| 147 | The C<write_xml()> method, implemented in the parent class, can be | ||
| 148 | used to create an XML representation of the data in the object. The | ||
| 149 | above names will be used as element names. The C<xmlelement()>, | ||
| 150 | C<xmlns()> and C<xmlprefix()> methods can be used to set the name of | ||
| 151 | the root element, the namespace URI and namespace prefix | ||
| 152 | respectively. Usually, it doesn't make sense to change the default | ||
| 153 | namespace or prefix, that are | ||
| 154 | |||
| 155 | =over | ||
| 156 | |||
| 157 | =item * C<http://www.kjetil.kjernsmo.net/software/TABOO/NS/MediaType/Output> | ||
| 158 | |||
| 159 | =item * C<type> | ||
| 160 | |||
| 161 | =back | ||
| 162 | |||
| 163 | However, the root element may change depending on what kind of mediatype we have. The default is C<mediatype>. | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | sub new { | ||
| 168 | my $that = shift; | ||
| 169 | my $class = ref($that) || $that; | ||
| 170 | my $self = { | ||
| 171 | mimetype => undef, | ||
| 172 | name => undef, | ||
| 173 | uri => undef, | ||
| 174 | DBCONNECTARGS => \@_, | ||
| 175 | XMLELEMENT => 'mediatype', | ||
| 176 | XMLPREFIX => 'type', | ||
| 177 | XMLNS => 'http://www.kjetil.kjernsmo.net/software/TABOO/NS/MediaType/Output', | ||
| 178 | ONFILE => undef, | ||
| 179 | }; | ||
| 180 | bless($self, $class); | ||
| 181 | return $self; | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | =head1 FORMALITIES | ||
| 186 | |||
| 187 | See L<AxKit::App::TABOO>. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | 1; | ||
| 192 | |||
| 193 |