blib/lib/AxKit/XSP/Util.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1 # $Id: Util.pm,v 1.15 2002/05/27 15:52:01 matt Exp $
2  
3 package AxKit::XSP::Util;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use Apache::AxKit::Language::XSP;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use HTTP::GHTTP;
7 use Apache::File;
8 use XML::LibXML 1.31;
9 use Time::Piece; # overrides localtime
10  
11 use vars qw/@ISA $NS $VERSION/;
12  
13 @ISA = ('Apache::AxKit::Language::XSP');
14 $NS = 'http://apache.org/xsp/util/v1';
15  
16 $VERSION = "1.6";
17  
18 ## Taglib subs
19  
20 # insert from a local file
21 sub include_file {
22 my ($document, $parent, $filename) = @_;
23 my $doc = XML::LibXML->new()->parse_file($filename);
24 if ($doc) {
25 my $root = $doc->getDocumentElement();
26 $root = $document->importNode($root);
27 $parent->appendChild($root);
28 }
29 else {
30 die "XML::LibXML returned undef";
31 }
32 }
33  
34  
35 # insert from a (possibly) remote file
36 # the cool (or maybe *not* so cool) thing is that
37 # if the uri is located on an AxKit-enabled server,
38 # we get it "pre-transformed" by any stylesheets
39 # declared in the doc. could be useful for widget building. . .
40 sub include_uri {
41 my ($document, $parent, $uri) = @_;
42
43 my $raw_xml;
44 if ($uri =~ /^axkit:/) {
45 $raw_xml = AxKit::get_axkit_uri($uri);
46 }
47 else {
48 my $ua = HTTP::GHTTP->new($uri);
49 $ua->process_request;
50 $raw_xml = $ua->get_body;
51 }
52 my $doc = XML::LibXML->new()->parse_string($raw_xml);
53 if ($doc) {
54 my $root = $doc->getDocumentElement();
55 $root = $document->importNode($root);
56 $parent->appendChild($root);
57 }
58 else {
59 die "XML::LibXML returned undef";
60 }
61 }
62  
63 # insert from a SCALAR
64 sub include_expr {
65 my ($document, $parent, $frag) = @_;
66 if ($frag || $frag == 0) {
67 my $doc = XML::LibXML->new()->parse_string( $frag );
68 if ($doc) {
69 my $root = $doc->getDocumentElement();
70 $root = $document->importNode($root);
71 $parent->appendChild($root);
72 }
73 else {
74 die "XML::LibXML returned undef";
75 }
76 }
77 }
78  
79 # insert from a local file as plain text
80 sub get_file_contents {
81 my ($filename) = @_;
82 my $fh = Apache::File->new($filename) ||
83 throw Apache::AxKit::Exception::IO( -text => "error opening $filename");
84 flock($fh, 1);
85 local $/;
86 my $content = <$fh>;
87 $fh->close;
88 return $content;
89 }
90  
91 # return the time in strftime formats.
92 sub get_date {
93 my ($format) = @_;
94 my $t = localtime;
95 my $ret = $t->strftime($format);
96 return $ret;
97 }
98  
99 ## Parser subs
100
101 sub parse_char {
102 my ($e, $text) = @_;
103 $text =~ s/^\s*//;
104 $text =~ s/\s*$//;
105  
106 return '' unless $text;
107  
108 $text =~ s/\|/\\\|/g;
109 if ($e->current_element() =~ /^(format|name|href)$/) {
110 return ". q|$text|"
111 }
112 return ''; # nothing else in util: should have text (?)
113 }
114  
115 sub parse_start {
116 my ($e, $tag, %attribs) = @_;
117 # warn "Checking: $tag\n";
118  
119  
120 if ($tag eq 'include-file') {
121  
122 my $code = "{# start include-file\nmy (\$_file_name);";
123 if ($attribs{name}) {
124 $code .= '$_file_name = q|' . $attribs{name} . '|;';
125 }
126 return $code;
127 }
128 elsif ($tag eq 'include-uri') {
129 my $code = "{# start include-uri\nmy (\$_uri);";
130 if ($attribs{href}) {
131 $code .= '$_uri = q|' . $attribs{href} . '|;';
132 }
133 return $code;
134 }
135 elsif ($tag eq 'get-file-contents') {
136 $e->start_expr($tag);
137 my $code = 'my ($_file_name);';
138 if ($attribs{name}) {
139 $code .= '$_file_name = q|' . $attribs{name} . '|;';
140 }
141 return $code;
142 }
143 elsif ($tag eq 'time') {
144 $e->start_expr($tag);
145 my $code = 'my ($_format);';
146 if ($attribs{format}) {
147 $code .= '$_format = q|' . $attribs{format} . '|;';
148 }
149 return $code;
150 }
151 elsif ($tag eq 'format') {
152 return '$format = ""';
153 }
154 elsif ($tag eq 'include-expr') {
155 return '{ my $_expression = ""';
156 }
157 elsif ($tag eq 'name') {
158 return '$_file_name = ""';
159 }
160 elsif ($tag eq 'href') {
161 return '$_uri = ""';
162 }
163 else {
164 die "Unknown util tag: $tag";
165 }
166 }
167  
168 sub parse_end {
169 my ($e, $tag) = @_;
170  
171 if ($tag eq 'include-file') {
172 return ";\nAxKit::XSP::Util::include_file(\n" .
173 '$document, $parent, $_file_name' .
174 ");}\n";
175 }
176 elsif ($tag eq 'include-uri') {
177 return ";\nAxKit::XSP::Util::include_uri(\n" .
178 '$document, $parent, $_uri' .
179 ");}\n";
180 }
181 elsif ($tag eq 'include-expr') {
182 return ";\nAxKit::XSP::Util::include_expr(\n" .
183 '$document, $parent, $_expression' .
184 ");}\n";
185 }
186 elsif ($tag eq 'get-file-contents') {
187 $e->append_to_script('AxKit::XSP::Util::get_file_contents($_file_name);');
188 $e->end_expr();
189 return '';
190 }
191 elsif ($tag eq 'time') {
192 $e->append_to_script('AxKit::XSP::Util::get_date($_format)');
193 $e->end_expr();
194 return '';
195 }
196 elsif ($tag eq 'format') {
197 }
198 elsif ($tag eq 'name') {
199 }
200 elsif ($tag eq 'href') {
201 }
202 return ";";
203 }
204
205 1;
206
207 __END__
↑ Summary