annotate Lib/IMPL/Config.pm @ 46:75148ccd732d

Upgrading resources
author Sergey
date Tue, 02 Feb 2010 17:09:49 +0300
parents 03e58a454b20
children a9b70d836b28
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 package IMPL::Config;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use warnings;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5 use base qw(IMPL::Object IMPL::Object::Serializable IMPL::Object::Autofill);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7 __PACKAGE__->PassThroughArgs;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 use IMPL::Class::Member;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 use IMPL::Class::PropertyInfo;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 use IMPL::Exception;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13 use IMPL::Serialization;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 use IMPL::Serialization::XmlFormatter;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 sub LoadXMLFile {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17 my ($self,$file) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19 my $class = ref $self || $self;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 my $serializer = new IMPL::Serializer(
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 Formatter => new IMPL::Serialization::XmlFormatter(
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23 IdentOutput => 1,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 SkipWhitespace => 1
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25 )
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 open my $hFile,'<',$file or die new IMPL::Exception("Failed to open file",$file,$!);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30 my $obj;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 eval {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32 $obj = $serializer->Deserialize($hFile);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33 };
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 if ($@) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36 my $e=$@;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 die new IMPL::Exception("Can't load the configuration file",$file,$e);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39 return $obj;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42 sub SaveXMLFile {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43 my ($this,$file) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 my $serializer = new IMPL::Serializer(
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 Formatter => new IMPL::Serialization::XmlFormatter(
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 IdentOutput => 1,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48 SkipWhitespace => 1
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49 )
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52 open my $hFile,'>',$file or die new IMPL::Exception("Failed to open file",$file,$!);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 $serializer->Serialize($hFile, $this);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57 sub xml {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 my $serializer = new IMPL::Serializer(
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 Formatter => new IMPL::Serialization::XmlFormatter(
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61 IdentOutput => 1,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 SkipWhitespace => 1
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63 )
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 my $str = '';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66 open my $hFile,'>',\$str or die new IMPL::Exception("Failed to open stream",$!);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 $serializer->Serialize($hFile, $this);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 undef $hFile;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 return $str;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 sub save {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 my ($this,$ctx) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78 foreach my $info ($this->get_meta('IMPL::Class::PropertyInfo')) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 next if $info->Access != IMPL::Class::Member::MOD_PUBLIC; # save only public properties
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81 my $name = $info->Name;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 $ctx->AddVar($name => $this->$name()) if $this->$name();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 __END__
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 =pod
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91 =h1 SYNOPSIS
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 package App::Config
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94 use base qw(IMPL::Config)
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 use IMPL::Class::Property;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97 use IMPL::Config::Class;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100 public property SimpleString => prop_all;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 public property MyClass => prop_all;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106 $this->superCTOR(@_);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
108 $this->MyClass(new IMPL::Config::Class(Type => MyClass)) unless $this->MyClass;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
109 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
110
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
111 =head1 DESCRIPTION
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
112
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
113 Ïîçâîëÿåò ñîõðàíèòü/çàãðóçèòü êîíôèãóðàöèþ. Òàêæå âñå êëàññû êîíôèãóðàöèè
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
114 äîëæíû íàñëåäîâàòüñÿ îò äàííîãî êëàññà, è âñå Public ñâîéñòâà áóäóò
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
115 àâòîìàòè÷åñêè ñîõðàíÿòüñÿ è âîññòàíàâëèâàòüñÿ.
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
116
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
117 =head1 MEMBERS
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
118
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
119 =item static LoadXMLFile($fileName)
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
120 Ñîçäàåò èç XML ôàéëà ýêçåìïëÿð ïðèëîæåíèÿ
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
121
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
122 =item SaveXMLFile($fileName)
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
123 Ñîõðàíÿåò ïðèëîæåíèå â ôàéë
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
124
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
125 =item xml
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
126 Ñîõðàíÿåò êîíôèãóðàöèþ ïðèëîæåíèÿ â XML ñòðîêó
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
127
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
128 =cut