| 1 | package App::perlrdf::FileSpec; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use autodie; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use utf8; | ||
| 8 | |||
| 9 | BEGIN { | ||
| 10 | $App::perlrdf::FileSpec::AUTHORITY = 'cpan:TOBYINK'; | ||
| 11 | $App::perlrdf::FileSpec::VERSION = '0.006'; | ||
| 12 | } | ||
| 13 | |||
| 14 | use Moose; | ||
| 15 | use Moose::Util::TypeConstraints; | ||
| 16 | use JSON; | ||
| 17 | use PerlX::Maybe; | ||
| 18 | use RDF::Trine; | ||
| 19 | use URI; | ||
| 20 | use URI::file; | ||
| 21 | use namespace::clean; | ||
| 22 | |||
| 23 | class_type PathClassFile => { class => 'Path::Class::File' }; | ||
| 24 | class_type AbsoluteUri => { class => 'URI' }; | ||
| 25 | |||
| 26 | coerce 'AbsoluteUri', | ||
| 27 | from Str => via { | ||
| 28 | if (/^std(in|out):$/i) { URI->new(lc $_) } | ||
| 29 | elsif (/^\w\w+:/i) { URI->new($_) } | ||
| 30 | else { URI::file->new_abs($_) } | ||
| 31 | }; | ||
| 32 | |||
| 33 | coerce 'AbsoluteUri', | ||
| 34 | from PathClassFile => via { | ||
| 35 | URI::file->new_abs("$_") | ||
| 36 | }; | ||
| 37 | |||
| 38 | has uri => ( | ||
| 39 | is => 'ro', | ||
| 40 | isa => 'AbsoluteUri', | ||
| 41 | required => 1, | ||
| 42 | coerce => 1, | ||
| 43 | ); | ||
| 44 | |||
| 45 | has base => ( | ||
| 46 | is => 'ro', | ||
| 47 | isa => 'AbsoluteUri', | ||
| 48 | lazy_build => 1, | ||
| 49 | coerce => 1, | ||
| 50 | ); | ||
| 51 | |||
| 52 | has 'format' => ( | ||
| 53 | is => 'ro', | ||
| 54 | isa => 'Str', | ||
| 55 | lazy_build => 1, | ||
| 56 | ); | ||
| 57 | |||
| 58 | sub DEFAULT_STREAM | ||
| 59 | { | ||
| 60 | warn "DEFAULT_STREAM is 'stdout:'\n"; | ||
| 61 | return "stdout:"; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _jsonish | ||
| 65 | { | ||
| 66 | my ($self, $str) = @_; | ||
| 67 | $str =~ s/(^\{)|(\}$)//g; # strip curlies | ||
| 68 | |||
| 69 | my $opts = {}; | ||
| 70 | while ($str =~ m{ \s* (\w+|"[^"]+"|'[^']+') \s* [:] (\w+|"[^"]+"|'[^']+') \s* ([;,]|$) }xg) | ||
| 71 | { | ||
| 72 | my $key = $1; | ||
| 73 | my $val = $2; | ||
| 74 | $val = $1 if $val =~ /^["'](.+).$/; | ||
| 75 | $opts->{$key} = $val; | ||
| 76 | } | ||
| 77 | |||
| 78 | return $opts; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub new_from_filespec | ||
| 82 | { | ||
| 83 | my ($class, $spec, $default_format, $default_base) = @_; | ||
| 84 | |||
| 85 | my ($optstr, $name) = ($spec =~ m<^ (\{ .*? \}) (.+) $>x) | ||
| 86 | ? ($1, $2) | ||
| 87 | : ('{}', $spec); | ||
| 88 | my $opts = $class->_jsonish($optstr); | ||
| 89 | |||
| 90 | $class->new( | ||
| 91 | 'uri' => ($name eq '-' ? $class->DEFAULT_STREAM : $name), | ||
| 92 | maybe('format' => ($opts->{format} // $default_format)), | ||
| 93 | maybe('base' => ($opts->{base} // $default_base)), | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _build_base | ||
| 98 | { | ||
| 99 | shift->uri; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _build_format | ||
| 103 | { | ||
| 104 | return $1 if shift->uri =~ /\.(\w+)$/; | ||
| 105 | return ''; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub TO_JSON | ||
| 109 | { | ||
| 110 | my ($self, $stringify) = @_; | ||
| 111 | my $r = +{ | ||
| 112 | base => $self->base->as_string, | ||
| 113 | format => $self->format, | ||
| 114 | uri => $self->uri->as_string, | ||
| 115 | }; | ||
| 116 | return $stringify | ||
| 117 | ? to_json($r => +{ pretty => 1, canonical => 1 }) | ||
| 118 | : $r; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub AUTHORITY | ||
| 122 | { | ||
| 123 | my $class = ref($_[0]) || $_[0]; | ||
| 124 | no strict qw(refs); | ||
| 125 | ${"$class\::AUTHORITY"}; | ||
| 126 | } | ||
| 127 | |||
| 128 | 1; |