diff Lib/IMPL/Config.pm @ 0:03e58a454b20

Создан репозитарий
author Sergey
date Tue, 14 Jul 2009 12:54:37 +0400
parents
children a9b70d836b28
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lib/IMPL/Config.pm	Tue Jul 14 12:54:37 2009 +0400
@@ -0,0 +1,128 @@
+package IMPL::Config;
+use strict;
+use warnings;
+
+use base qw(IMPL::Object IMPL::Object::Serializable IMPL::Object::Autofill);
+
+__PACKAGE__->PassThroughArgs;
+
+use IMPL::Class::Member;
+use IMPL::Class::PropertyInfo;
+use IMPL::Exception;
+
+use IMPL::Serialization;
+use IMPL::Serialization::XmlFormatter;
+
+sub LoadXMLFile {
+    my ($self,$file) = @_;
+    
+    my $class = ref $self || $self;
+    
+    my $serializer = new IMPL::Serializer(
+        Formatter => new IMPL::Serialization::XmlFormatter(
+            IdentOutput => 1,
+            SkipWhitespace => 1
+        )
+    );
+    
+    open my $hFile,'<',$file or die new IMPL::Exception("Failed to open file",$file,$!);
+    
+    my $obj;
+    eval {
+        $obj = $serializer->Deserialize($hFile);
+    };
+    
+    if ($@) {
+        my $e=$@;
+        die new IMPL::Exception("Can't load the configuration file",$file,$e);
+    }
+    return $obj;
+}
+
+sub SaveXMLFile {
+    my ($this,$file) = @_;
+    
+    my $serializer = new IMPL::Serializer(
+        Formatter => new IMPL::Serialization::XmlFormatter(
+            IdentOutput => 1,
+            SkipWhitespace => 1
+        )
+    );
+    
+    open my $hFile,'>',$file or die new IMPL::Exception("Failed to open file",$file,$!);
+    
+    $serializer->Serialize($hFile, $this);
+}
+
+sub xml {
+    my $this = shift;
+    my $serializer = new IMPL::Serializer(
+        Formatter => new IMPL::Serialization::XmlFormatter(
+            IdentOutput => 1,
+            SkipWhitespace => 1
+        )
+    );
+    my $str = '';
+    open my $hFile,'>',\$str or die new IMPL::Exception("Failed to open stream",$!);
+    
+    $serializer->Serialize($hFile, $this);
+    
+    undef $hFile;
+    
+    return $str;
+}
+
+sub save {
+    my ($this,$ctx) = @_;
+
+    foreach my $info ($this->get_meta('IMPL::Class::PropertyInfo')) {
+        next if $info->Access != IMPL::Class::Member::MOD_PUBLIC; # save only public properties
+
+        my $name = $info->Name;
+        $ctx->AddVar($name => $this->$name()) if $this->$name();
+    }
+}
+
+1;
+__END__
+
+=pod
+
+=h1 SYNOPSIS
+
+package App::Config
+use base qw(IMPL::Config)
+
+use IMPL::Class::Property;
+use IMPL::Config::Class;
+
+BEGIN {
+    public property SimpleString => prop_all;
+    public property MyClass => prop_all;
+}
+
+sub CTOR {
+    my $this = shift;
+    $this->superCTOR(@_);
+
+    $this->MyClass(new IMPL::Config::Class(Type => MyClass)) unless $this->MyClass;
+}
+
+=head1 DESCRIPTION
+
+Ïîçâîëÿåò ñîõðàíèòü/çàãðóçèòü êîíôèãóðàöèþ. Òàêæå âñå êëàññû êîíôèãóðàöèè
+äîëæíû íàñëåäîâàòüñÿ îò äàííîãî êëàññà, è âñå Public ñâîéñòâà áóäóò
+àâòîìàòè÷åñêè ñîõðàíÿòüñÿ è âîññòàíàâëèâàòüñÿ.
+
+=head1 MEMBERS
+
+=item static LoadXMLFile($fileName)
+Ñîçäàåò èç XML ôàéëà ýêçåìïëÿð ïðèëîæåíèÿ
+
+=item SaveXMLFile($fileName)
+Ñîõðàíÿåò ïðèëîæåíèå â ôàéë
+
+=item xml
+Ñîõðàíÿåò êîíôèãóðàöèþ ïðèëîæåíèÿ â XML ñòðîêó
+
+=cut
\ No newline at end of file