0
|
1 package IMPL::Config;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
|
5 use base qw(IMPL::Object IMPL::Object::Serializable IMPL::Object::Autofill);
|
|
6
|
|
7 __PACKAGE__->PassThroughArgs;
|
|
8
|
|
9 use IMPL::Class::Member;
|
|
10 use IMPL::Class::PropertyInfo;
|
|
11 use IMPL::Exception;
|
|
12
|
|
13 use IMPL::Serialization;
|
|
14 use IMPL::Serialization::XmlFormatter;
|
|
15
|
|
16 sub LoadXMLFile {
|
|
17 my ($self,$file) = @_;
|
|
18
|
|
19 my $class = ref $self || $self;
|
|
20
|
|
21 my $serializer = new IMPL::Serializer(
|
|
22 Formatter => new IMPL::Serialization::XmlFormatter(
|
|
23 IdentOutput => 1,
|
|
24 SkipWhitespace => 1
|
|
25 )
|
|
26 );
|
|
27
|
|
28 open my $hFile,'<',$file or die new IMPL::Exception("Failed to open file",$file,$!);
|
|
29
|
|
30 my $obj;
|
|
31 eval {
|
|
32 $obj = $serializer->Deserialize($hFile);
|
|
33 };
|
|
34
|
|
35 if ($@) {
|
|
36 my $e=$@;
|
|
37 die new IMPL::Exception("Can't load the configuration file",$file,$e);
|
|
38 }
|
|
39 return $obj;
|
|
40 }
|
|
41
|
|
42 sub SaveXMLFile {
|
|
43 my ($this,$file) = @_;
|
|
44
|
|
45 my $serializer = new IMPL::Serializer(
|
|
46 Formatter => new IMPL::Serialization::XmlFormatter(
|
|
47 IdentOutput => 1,
|
|
48 SkipWhitespace => 1
|
|
49 )
|
|
50 );
|
|
51
|
|
52 open my $hFile,'>',$file or die new IMPL::Exception("Failed to open file",$file,$!);
|
|
53
|
|
54 $serializer->Serialize($hFile, $this);
|
|
55 }
|
|
56
|
|
57 sub xml {
|
|
58 my $this = shift;
|
|
59 my $serializer = new IMPL::Serializer(
|
|
60 Formatter => new IMPL::Serialization::XmlFormatter(
|
|
61 IdentOutput => 1,
|
|
62 SkipWhitespace => 1
|
|
63 )
|
|
64 );
|
|
65 my $str = '';
|
|
66 open my $hFile,'>',\$str or die new IMPL::Exception("Failed to open stream",$!);
|
|
67
|
|
68 $serializer->Serialize($hFile, $this);
|
|
69
|
|
70 undef $hFile;
|
|
71
|
|
72 return $str;
|
|
73 }
|
|
74
|
|
75 sub save {
|
|
76 my ($this,$ctx) = @_;
|
|
77
|
|
78 foreach my $info ($this->get_meta('IMPL::Class::PropertyInfo')) {
|
|
79 next if $info->Access != IMPL::Class::Member::MOD_PUBLIC; # save only public properties
|
|
80
|
|
81 my $name = $info->Name;
|
|
82 $ctx->AddVar($name => $this->$name()) if $this->$name();
|
|
83 }
|
|
84 }
|
|
85
|
|
86 1;
|
|
87 __END__
|
|
88
|
|
89 =pod
|
|
90
|
|
91 =h1 SYNOPSIS
|
|
92
|
|
93 package App::Config
|
|
94 use base qw(IMPL::Config)
|
|
95
|
|
96 use IMPL::Class::Property;
|
|
97 use IMPL::Config::Class;
|
|
98
|
|
99 BEGIN {
|
|
100 public property SimpleString => prop_all;
|
|
101 public property MyClass => prop_all;
|
|
102 }
|
|
103
|
|
104 sub CTOR {
|
|
105 my $this = shift;
|
|
106 $this->superCTOR(@_);
|
|
107
|
|
108 $this->MyClass(new IMPL::Config::Class(Type => MyClass)) unless $this->MyClass;
|
|
109 }
|
|
110
|
|
111 =head1 DESCRIPTION
|
|
112
|
|
113 / .
|
|
114 , Public
|
|
115 .
|
|
116
|
|
117 =head1 MEMBERS
|
|
118
|
|
119 =item static LoadXMLFile($fileName)
|
|
120 XML
|
|
121
|
|
122 =item SaveXMLFile($fileName)
|
|
123
|
|
124
|
|
125 =item xml
|
|
126 XML
|
|
127
|
|
128 =cut |