1 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
2 |
joko |
1.5 |
## $Id: XML.pm,v 1.4 2003/06/06 03:58:13 joko Exp $ |
3 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
4 |
joko |
1.2 |
## $Log: XML.pm,v $ |
5 |
joko |
1.5 |
## Revision 1.4 2003/06/06 03:58:13 joko |
6 |
|
|
## minor updates to logging |
7 |
|
|
## |
8 |
joko |
1.4 |
## Revision 1.3 2003/05/13 08:08:17 joko |
9 |
|
|
## comments, todos |
10 |
|
|
## |
11 |
joko |
1.3 |
## Revision 1.2 2003/02/21 07:58:48 joko |
12 |
|
|
## enhanced error detection |
13 |
|
|
## |
14 |
joko |
1.2 |
## Revision 1.1 2003/02/20 20:20:54 joko |
15 |
|
|
## + initial commit |
16 |
|
|
## |
17 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
package Data::Storage::Handler::XML; |
21 |
|
|
|
22 |
|
|
use strict; |
23 |
|
|
use warnings; |
24 |
|
|
|
25 |
|
|
use base qw( |
26 |
|
|
DesignPattern::Object |
27 |
|
|
DesignPattern::Logger |
28 |
|
|
); |
29 |
|
|
|
30 |
|
|
|
31 |
joko |
1.5 |
# TODO: Load them on demand! |
32 |
joko |
1.1 |
use Data::Dumper; |
33 |
|
|
#use Data::Storage::Handler::File::Basic qw( s2f a2f f2s ); |
34 |
|
|
use XML::Simple; |
35 |
|
|
use XML::Parser; |
36 |
|
|
use XML::Parser::EasyTree; |
37 |
|
|
use XML::XPath; |
38 |
joko |
1.3 |
# TODO: use XML::Writer and/or XML::ValidWriter |
39 |
joko |
1.1 |
|
40 |
joko |
1.4 |
|
41 |
|
|
# get logger instance |
42 |
|
|
my $logger = Log::Dispatch::Config->instance; |
43 |
|
|
|
44 |
joko |
1.1 |
sub sendQuery { |
45 |
|
|
my $self = shift; |
46 |
|
|
my $xpq = shift; |
47 |
|
|
my $options = shift; |
48 |
|
|
|
49 |
joko |
1.2 |
$self->log( "xpq='$xpq'", 'debug' ); |
50 |
joko |
1.1 |
|
51 |
|
|
# trace |
52 |
|
|
#print Dumper($self); |
53 |
|
|
#exit; |
54 |
|
|
|
55 |
|
|
my $file = $self->{filename}; |
56 |
joko |
1.2 |
|
57 |
|
|
if (!$file) { |
58 |
joko |
1.4 |
$logger->error("No filename given."); |
59 |
joko |
1.2 |
return; |
60 |
|
|
} |
61 |
joko |
1.1 |
|
62 |
|
|
if (! -e $file) { |
63 |
joko |
1.4 |
$logger->error("File not found: '$file'."); |
64 |
joko |
1.1 |
return; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
#print "file: $file", "\n"; |
68 |
|
|
#print "xpq: $xpq", "\n"; |
69 |
joko |
1.4 |
|
70 |
joko |
1.5 |
$logger->debug( __PACKAGE__ . "->sendQuery - File: $file, xpq: $xpq"); |
71 |
joko |
1.4 |
|
72 |
joko |
1.1 |
# filter nodes by xpath-query |
73 |
|
|
my $xp = XML::XPath->new( filename => $file ); |
74 |
|
|
my $nodeset = $xp->find($xpq); |
75 |
|
|
|
76 |
|
|
# build result xml |
77 |
|
|
my $buffer; |
78 |
|
|
foreach my $node ($nodeset->get_nodelist) { |
79 |
|
|
$buffer .= XML::XPath::XMLParser::as_string($node) . "\n\n"; |
80 |
|
|
} |
81 |
|
|
#$buffer .= ''; |
82 |
|
|
#$buffer = "<result>$buffer</result>"; |
83 |
|
|
|
84 |
|
|
# trace |
85 |
|
|
#print $buffer, "\n"; |
86 |
|
|
#exit; |
87 |
|
|
|
88 |
|
|
$self->{buffer} = $buffer; |
89 |
|
|
|
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
sub getResult { |
93 |
|
|
my $self = shift; |
94 |
|
|
return $self->{buffer}; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
sub circumflex { |
98 |
|
|
my $self = shift; |
99 |
|
|
my $tag = shift; |
100 |
|
|
# mungle result payload |
101 |
|
|
$self->{buffer} = "<$tag>" . $self->{buffer} . "</$tag>" if $self->{buffer}; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
sub isEmpty { |
105 |
|
|
my $self = shift; |
106 |
|
|
return not defined $self->{buffer}; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
sub toEasyTree { |
110 |
|
|
my $self = shift; |
111 |
|
|
|
112 |
|
|
$XML::Parser::EasyTree::Noempty = 1; |
113 |
joko |
1.3 |
# what about ... |
114 |
|
|
#$XML::Parser::EasyTree::Latin = 1; |
115 |
|
|
# ... instead of an otherwise required |
116 |
|
|
# "expand" with proper utf8/latin-conversion |
117 |
|
|
# parameters set? |
118 |
joko |
1.1 |
|
119 |
|
|
# convert xml data to native perl data structure |
120 |
|
|
#my $parser = XML::Parser->new( Style => 'EasyTree', Handlers => { Char => sub { my $char = shift; print "char: ", Dumper($char), "\n"; } } ); |
121 |
|
|
#my $parser = XML::Parser->new( Style => 'EasyTree', Handlers => { Char => sub {} } ); |
122 |
|
|
my $parser = XML::Parser->new( Style => 'EasyTree' ); |
123 |
|
|
my $data = $parser->parse($self->{buffer}); |
124 |
|
|
|
125 |
|
|
# trace |
126 |
|
|
#print Dumper($data); |
127 |
|
|
#exit; |
128 |
|
|
|
129 |
|
|
# FIXME!? |
130 |
|
|
my $hash = $data->[0]; |
131 |
|
|
|
132 |
|
|
# trace |
133 |
|
|
#print Dumper($hash); |
134 |
|
|
#exit; |
135 |
|
|
|
136 |
|
|
#$self->{payload} = $hash; |
137 |
|
|
return $hash; |
138 |
|
|
|
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
sub toSimpleTree { |
142 |
|
|
my $self = shift; |
143 |
|
|
#print $self->{buffer}; |
144 |
|
|
#exit; |
145 |
|
|
#$self->{payload} = XMLin($self->{buffer}); |
146 |
|
|
return XMLin($self->{buffer}); |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
|
150 |
|
|
1; |
151 |
|
|
__END__ |