| 1 | package URI::chrome; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw(URI::_generic); | ||
| 7 | |||
| 8 | use Carp::Clan qw(croak); | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | URI::chrome - Mozilla chrome uri | ||
| 13 | |||
| 14 | =head1 VERSION | ||
| 15 | |||
| 16 | version 0.01 | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | our $VERSION = '0.01'; | ||
| 21 | |||
| 22 | our $CHROME_PACKAGE_REGEX = qr{([a-zA-Z0-9]+)}; | ||
| 23 | our $CHROME_PART_REGEX = qr{((content|skin|locale)(/[a-zA-Z0-9-._]+)*)}; | ||
| 24 | our $CHROME_FILE_REGEX = qr{([a-zA-Z0-9-._]+)}; | ||
| 25 | our $CHROME_REGEX = qr{^chrome://$CHROME_PACKAGE_REGEX/$CHROME_PART_REGEX/$CHROME_FILE_REGEX$}; | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | use URI; | ||
| 30 | use URI::chrome; | ||
| 31 | |||
| 32 | my $uri = URI->new("chrome://communicator/content/bookmarks/bookmarksManager.xul"); | ||
| 33 | local $\ = "\n"; | ||
| 34 | |||
| 35 | print $uri->package_name; # communicator | ||
| 36 | print $uri->part; # content/bookmarks | ||
| 37 | print $uri->file_name; # bookmarksManager.xul | ||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | =head2 The Chrome URL Specification | ||
| 42 | |||
| 43 | The basic syntax of a chrome URI is as follows, | ||
| 44 | |||
| 45 | chrome://<package_name>/<part>/<file_name> | ||
| 46 | |||
| 47 | The elements of chrome URI detail is as follows, | ||
| 48 | |||
| 49 | =over 4 | ||
| 50 | |||
| 51 | =item package_name | ||
| 52 | |||
| 53 | The "package_name" is the package name. | ||
| 54 | For example, "browser", "messenger" or "communicator". | ||
| 55 | |||
| 56 | =item part | ||
| 57 | |||
| 58 | The "part" is simillar to path of http URI. | ||
| 59 | It's string is beggining of "content", "skin" or "locale". | ||
| 60 | For example, "content", "content/bookmarks". | ||
| 61 | |||
| 62 | =item file_name | ||
| 63 | |||
| 64 | The "file_name" is the file name. | ||
| 65 | |||
| 66 | =back | ||
| 67 | |||
| 68 | More detail, please see http://developer.mozilla.org/en/docs/XUL_Tutorial:The_Chrome_URL | ||
| 69 | |||
| 70 | =head1 METHODS | ||
| 71 | |||
| 72 | =head2 package_name([$package_name]) | ||
| 73 | |||
| 74 | Getter/Setter of package_name | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub package_name { | ||
| 79 | my ($self, $package_name) = @_; | ||
| 80 | |||
| 81 | if ($package_name && $package_name =~ m|$CHROME_PACKAGE_REGEX|ox) { # setter | ||
| 82 | $self->_chrome_setter("package_name", $package_name); | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | return $self->_chrome_struct->{package_name}; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | =head2 part([$part]) | ||
| 90 | |||
| 91 | Getter/Setter of part | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub part { | ||
| 96 | my ($self, $part) = @_; | ||
| 97 | |||
| 98 | if ($part && $part =~ m|$CHROME_PART_REGEX|ox) { # setter | ||
| 99 | $self->_chrome_setter("part", $part); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | return $self->_chrome_struct->{part}; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | =head2 file_name([$file_name]) | ||
| 107 | |||
| 108 | Getter/Setter of file_name | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub file_name { | ||
| 113 | my ($self, $file_name) = @_; | ||
| 114 | |||
| 115 | if ($file_name && $file_name =~ m|$CHROME_FILE_REGEX|ox) { # setter | ||
| 116 | $self->_chrome_setter("file_name", $file_name); | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | return $self->_chrome_struct->{file_name}; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | #### protected method | ||
| 124 | |||
| 125 | sub _init { | ||
| 126 | my ($class, $uri_str, $scheme) = @_; | ||
| 127 | |||
| 128 | if ($uri_str =~ m|$CHROME_REGEX|ox) { | ||
| 129 | return $class->SUPER::_init($uri_str, $scheme); | ||
| 130 | } | ||
| 131 | else { | ||
| 132 | croak(q|Invalid part prefix, must be "content" or "skin" or "locale".|); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | sub _chrome_setter { | ||
| 137 | my ($self, $name, $value) = @_; | ||
| 138 | |||
| 139 | my $struct = $self->_chrome_struct; | ||
| 140 | $struct->{$name} = $value; | ||
| 141 | $$self = $self->_chrome_uri_string($struct); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub _chrome_uri_string { | ||
| 145 | my ($self, $struct) = @_; | ||
| 146 | |||
| 147 | return sprintf("chrome://%s/%s/%s", map { $struct->{$_} } qw(package_name part file_name)); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub _chrome_struct { | ||
| 151 | my $self = shift; | ||
| 152 | |||
| 153 | my ($package_name, $part, undef, undef, $file_name) | ||
| 154 | = ($$self =~ m|$CHROME_REGEX|ox); | ||
| 155 | |||
| 156 | return { | ||
| 157 | package_name => $package_name, | ||
| 158 | part => $part, | ||
| 159 | file_name => $file_name | ||
| 160 | }; | ||
| 161 | } | ||
| 162 | |||
| 163 | =head1 SEE ALSO | ||
| 164 | |||
| 165 | =over 4 | ||
| 166 | |||
| 167 | =item L<URI> | ||
| 168 | |||
| 169 | =item http://developer.mozilla.org/en/docs/XUL_Tutorial:The_Chrome_URL | ||
| 170 | |||
| 171 | =back | ||
| 172 | |||
| 173 | =head1 AUTHOR | ||
| 174 | |||
| 175 | Toru Yamaguchi, C<< <zigorou@cpan.org> >> | ||
| 176 | |||
| 177 | =head1 BUGS | ||
| 178 | |||
| 179 | Please report any bugs or feature requests to | ||
| 180 | C<bug-uri-chrome@rt.cpan.org>, or through the web interface at | ||
| 181 | L<http://rt.cpan.org>. I will be notified, and then you'll automatically be | ||
| 182 | notified of progress on your bug as I make changes. | ||
| 183 | |||
| 184 | =head1 COPYRIGHT & LICENSE | ||
| 185 | |||
| 186 | Copyright 2007 Toru Yamaguchi, All Rights Reserved. | ||
| 187 | |||
| 188 | This program is free software; you can redistribute it and/or modify it | ||
| 189 | under the same terms as Perl itself. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | 1; # End of URI::chrome |