[Bps-public-commit] r14064 - in Data-Plist: . lib/Data/Plist
alexmv at bestpractical.com
alexmv at bestpractical.com
Mon Jul 14 12:25:31 EDT 2008
Author: alexmv
Date: Mon Jul 14 12:25:30 2008
New Revision: 14064
Modified:
Data-Plist/ (props changed)
Data-Plist/ical-import.pl
Data-Plist/lib/Data/Plist/Writer.pm
Data-Plist/lib/Data/Plist/XMLWriter.pm
Data-Plist/lib/Foundation/LibraryToDo.pm
Data-Plist/lib/Foundation/NSDate.pm
Data-Plist/lib/Foundation/NSMutableArray.pm
Data-Plist/lib/Foundation/NSMutableData.pm
Data-Plist/lib/Foundation/NSObject.pm
Data-Plist/lib/Foundation/NSURL.pm
Data-Plist/lib/Foundation/ToDoAlarm.pm
Log:
r34123 at kohr-ah: chmrr | 2008-07-14 12:25:13 -0400
* Serialization now works, AFAICT
* Declare XML plists to be UTF8
Modified: Data-Plist/ical-import.pl
==============================================================================
--- Data-Plist/ical-import.pl (original)
+++ Data-Plist/ical-import.pl Mon Jul 14 12:25:30 2008
@@ -18,6 +18,5 @@
}
my $p = Data::Plist::BinaryReader->open_string($content);
- print YAML::Dump($p->raw_object);
-# print Data::Plist::XMLWriter->open_string($p->raw_object);
+ print Data::Plist::XMLWriter->open_string($p->object("Foundation"));
}
Modified: Data-Plist/lib/Data/Plist/Writer.pm
==============================================================================
--- Data-Plist/lib/Data/Plist/Writer.pm (original)
+++ Data-Plist/lib/Data/Plist/Writer.pm Mon Jul 14 12:25:30 2008
@@ -64,9 +64,11 @@
my $self = shift;
my $object = shift;
+ $object = $object->serialize if ref($object) ne "ARRAY" and $object->can("serialize");
+
local $self->{objects} = [];
local $self->{objcache} = {};
- my $top = $self->fold_uids( [ dict => { root => $object } ] );
+ my $top = $self->fold_uids( [ dict => { root => [ UID => $object ] } ] );
return [
dict => {
Modified: Data-Plist/lib/Data/Plist/XMLWriter.pm
==============================================================================
--- Data-Plist/lib/Data/Plist/XMLWriter.pm (original)
+++ Data-Plist/lib/Data/Plist/XMLWriter.pm Mon Jul 14 12:25:30 2008
@@ -14,7 +14,7 @@
my ( $fh, $object ) = @_;
local $self->{x}
= XML::Writer->new( OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 8 );
- $self->{x}->xmlDecl();
+ $self->{x}->xmlDecl( "UTF-8" );
$self->{x}->doctype(
"plist",
"-//Apple//DTD PLIST 1.0//EN",
Modified: Data-Plist/lib/Foundation/LibraryToDo.pm
==============================================================================
--- Data-Plist/lib/Foundation/LibraryToDo.pm (original)
+++ Data-Plist/lib/Foundation/LibraryToDo.pm Mon Jul 14 12:25:30 2008
@@ -6,22 +6,22 @@
use base qw/Foundation::ToDo Class::Accessor/;
my %mapping = (
- alarms => [ "ToDo Alarms" => undef ],
- cal_id => [ "ToDo Calendar ID" => "string" ],
- calendar => [ "ToDo Calendar Title" => "string" ],
- complete => [ "ToDo Completed" => "bool" ],
- completed_at => [ "ToDo Date Completed" => undef ],
- created => [ "ToDo Date Created" => undef ],
- due => [ "ToDo Due Date" => undef ],
- notes => [ "ToDo Notes" => "string" ],
- priority => [ "ToDo Priority" => "int" ],
- title => [ "ToDo Title" => "string" ],
- url => [ "ToDo URL" => undef ],
- id => [ "ToDo iCal ID" => "string" ],
- keys_digest => [ "ToDo Keys Digest" => undef ],
+ alarms => "ToDo Alarms",
+ cal_id => "ToDo Calendar ID",
+ calendar => "ToDo Calendar Title",
+ complete => "ToDo Completed",
+ completed_at => "ToDo Date Completed",
+ created => "ToDo Date Created",
+ due => "ToDo Due Date",
+ notes => "ToDo Notes",
+ priority => "ToDo Priority",
+ title => "ToDo Title",
+ url => "ToDo URL",
+ id => "ToDo iCal ID",
+ keys_digest => "ToDo Keys Digest",
);
-my %lookup = (map {($mapping{$_}[0] => $_)} keys %mapping);
+my %lookup = (reverse %mapping);
sub init {
@@ -34,16 +34,32 @@
$self->priority(undef) unless delete $self->{"ToDo Priority Enabled"};
}
+sub serialize_equiv {
+ my $self = shift;
+ my $hash = {};
+ $hash->{$mapping{$_}} = $self->{$_} for keys %{$self};
+ return $hash;
+}
+
sub serialize {
my $self = shift;
- my $ret = {};
+ my $ret = $self->SUPER::serialize;
+ $ret->[1]{"ToDo Completed"} = $self->{complete} ? [ true => 1 ] : [ false => 0 ];
+ if ($self->{due}) {
+ $ret->[1]{"ToDo Due Date Enabled"} = [ true => 1 ];
+ } else {
+ delete $ret->[1]{"ToDo Due Date"};
+ $ret->[1]{"ToDo Due Date Enabled"} = [ false => 0 ];
+ }
- for my $k (keys %mapping) {
+ if (defined $self->{priority}) {
+ $ret->[1]{"ToDo Priority Enabled"} = [ true => 1 ];
+ } else {
+ $ret->[1]{"ToDo Priority"} = [ integer => 1 ];
+ $ret->[1]{"ToDo Priority Enabled"} = [ false => 0 ];
}
- return ["dict", $ret];
+ return $ret;
}
1;
-
-
Modified: Data-Plist/lib/Foundation/NSDate.pm
==============================================================================
--- Data-Plist/lib/Foundation/NSDate.pm (original)
+++ Data-Plist/lib/Foundation/NSDate.pm Mon Jul 14 12:25:30 2008
@@ -12,9 +12,12 @@
return $dt;
}
-sub serialize {
+sub serialize_equiv {
my $self = shift;
- return { "NS.time" => $self->epoch - 978307200 };
+ my $secs = ($self->epoch - 978307200);
+ $secs += $self->nanosecond / 1e9;
+ $secs .= ".0" unless $secs =~ /\D/; # This forces it to be stored as "real"
+ return { "NS.time" => $secs };
}
1;
Modified: Data-Plist/lib/Foundation/NSMutableArray.pm
==============================================================================
--- Data-Plist/lib/Foundation/NSMutableArray.pm (original)
+++ Data-Plist/lib/Foundation/NSMutableArray.pm Mon Jul 14 12:25:30 2008
@@ -5,6 +5,16 @@
use base qw/Foundation::NSArray/;
+sub serialize {
+ my $self = shift;
+ my $ret = $self->SUPER::serialize;
+ $ret->[1]{'NS.objects'}
+ = [
+ array => [ map { [ UID => $_ ] } @{ $ret->[1]{'NS.objects'}[1][1] } ]
+ ];
+ return $ret;
+}
+
1;
Modified: Data-Plist/lib/Foundation/NSMutableData.pm
==============================================================================
--- Data-Plist/lib/Foundation/NSMutableData.pm (original)
+++ Data-Plist/lib/Foundation/NSMutableData.pm Mon Jul 14 12:25:30 2008
@@ -10,7 +10,11 @@
return $self->{"NS.data"};
}
-sub serialize {
+sub serialize_equiv {
+ my $self = shift;
+ return $self->SUPER::serialize_equiv unless ref $self->data;
+ # XXX TODO: This should be BinaryWriter, but it hasn't been written yet
+ return { "NS.data" => Data::Plist::XMLWriter->open_string($self->data) };
}
1;
Modified: Data-Plist/lib/Foundation/NSObject.pm
==============================================================================
--- Data-Plist/lib/Foundation/NSObject.pm (original)
+++ Data-Plist/lib/Foundation/NSObject.pm Mon Jul 14 12:25:30 2008
@@ -2,6 +2,9 @@
use strict;
use warnings;
+use Class::ISA;
+use UNIVERSAL::isa;
+use Scalar::Util qw//;
sub init {
my $self = shift;
@@ -13,4 +16,80 @@
return $self;
}
+sub serialize_class {
+ my $self = shift;
+ $self = ref $self if ref $self;
+
+ my $short = $self;
+ $short =~ s/^Foundation:://;
+ return [
+ UID => [
+ dict => {
+ '$classes' => [
+ array => [
+ map { s/^Foundation:://; [ string => $_ ] }
+ grep { $_->isa("Foundation::NSObject") }
+ Class::ISA::self_and_super_path($self)
+ ]
+ ],
+ '$classname' => [ string => $short ],
+ }
+ ]
+ ];
+}
+
+sub serialize_equiv {
+ my $self = shift;
+ return { %{ $self } };
+}
+
+sub serialize_value {
+ my $self = shift;
+ my ($value) = @_;
+ if (not defined $value) {
+ return [ string => '$null' ];
+ } elsif ( ref $value ) {
+ if ( ref $value eq "ARRAY" ) {
+ return [
+ array => [ map { $self->serialize_value($_) } @{$value} ] ];
+ } elsif ( ref $value and ref $value eq "HASH" ) {
+ my %hash = %{$value};
+ $hash{$_} = $self->serialize_value( $hash{$_} ) for keys %hash;
+ return [ dict => \%hash ];
+ } elsif ($value->isa("Foundation::NSObject")) {
+ return $value->serialize;
+ } else {
+ die "Can't serialize unknown ref @{[ref $value]}\n";
+ }
+ } elsif ( $value !~ /\D/ ) {
+ return [ integer => $value ];
+ } elsif ( Scalar::Util::looks_like_number($value) ) {
+ return [ real => $value ];
+ } elsif ( $value =~ /\0/ or $value =~ /<\?xml/) {
+ # XXX TODO: The /<\?xml/ is a hack to get it labelled DATA
+ # until we use BinaryWriter to write nested plists
+ return [ data => $value ];
+ } else {
+ return [ string => $value ];
+ }
+}
+
+
+
+sub serialize {
+ my $self = shift;
+ my %dict;
+ $dict{'$class'} = $self->serialize_class;
+ my $equiv = $self->serialize_equiv;
+ for my $key (keys %{$equiv}) {
+ my $value = $self->serialize_value($equiv->{$key});
+ if ($value->[0] =~ /^(data|integer|real|true|false)$/) {
+ $dict{$key} = $value;
+ } else {
+ $dict{$key} = [ UID => $value ];
+ }
+ }
+ return [ dict => \%dict ];
+}
+
1;
Modified: Data-Plist/lib/Foundation/NSURL.pm
==============================================================================
--- Data-Plist/lib/Foundation/NSURL.pm (original)
+++ Data-Plist/lib/Foundation/NSURL.pm Mon Jul 14 12:25:30 2008
@@ -12,9 +12,9 @@
return $uri;
}
-sub serialize {
+sub serialize_equiv {
my $self = shift;
- return { "NS.relative" => $self->as_string };
+ return { "NS.relative" => $self->as_string, "NS.base" => undef };
}
1;
Modified: Data-Plist/lib/Foundation/ToDoAlarm.pm
==============================================================================
--- Data-Plist/lib/Foundation/ToDoAlarm.pm (original)
+++ Data-Plist/lib/Foundation/ToDoAlarm.pm Mon Jul 14 12:25:30 2008
@@ -5,4 +5,11 @@
use base qw/Foundation::NSObject/;
+sub serialize {
+ my $self = shift;
+ my $ret = $self->SUPER::serialize;
+ $ret->{"ToDo Alarm Enabled"} = $ret->{"ToDo Alarm Enabled"}[1] ? [ true => 1 ] : [ false => 0 ];
+ return $ret;
+}
+
1;
More information about the Bps-public-commit
mailing list