/[cvs]/nfo/perl/libs/Data/Storage/Handler/XML.pm
ViewVC logotype

Annotation of /nfo/perl/libs/Data/Storage/Handler/XML.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Tue May 13 08:08:17 2003 UTC (21 years, 1 month ago) by joko
Branch: MAIN
Changes since 1.2: +10 -2 lines
comments, todos

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed