File Coverage

blib/lib/VoiceXML/Client/Parser.pm
Criterion Covered Total %
statement 45 47 95.7
branch 8 18 44.4
condition 6 16 37.5
subroutine 6 6 100.0
pod 2 2 100.0
total 67 89 75.2


line stmt bran cond sub pod time code
1             package VoiceXML::Client::Parser;
2              
3 3     3   3172 use XML::Mini::Document;
  3         225617  
  3         120  
4 3     3   2161 use VoiceXML::Client::Item::Factory;
  3         11  
  3         92  
5 3     3   1894 use VoiceXML::Client::Document;
  3         12  
  3         86  
6              
7 3     3   23 use strict;
  3         6  
  3         1850  
8              
9              
10              
11             =head1 COPYRIGHT AND LICENSE
12              
13            
14             Copyright (C) 2007,2008 by Pat Deegan.
15             All rights reserved
16             http://voicexml.psychogenic.com
17              
18             This library is released under the terms of the GNU GPL version 3, making it available only for
19             free programs ("free" here being used in the sense of the GPL, see http://www.gnu.org for more details).
20             Anyone wishing to use this library within a proprietary or otherwise non-GPLed program MUST contact psychogenic.com to
21             acquire a distinct license for their application. This approach encourages the use of free software
22             while allowing for proprietary solutions that support further development.
23              
24              
25             This file is part of VoiceXML::Client.
26              
27            
28            
29             VoiceXML::Client is free software: you can redistribute it and/or modify
30             it under the terms of the GNU General Public License as published by
31             the Free Software Foundation, either version 3 of the License, or
32             (at your option) any later version.
33              
34             VoiceXML::Client is distributed in the hope that it will be useful,
35             but WITHOUT ANY WARRANTY; without even the implied warranty of
36             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37             GNU General Public License for more details.
38              
39             You should have received a copy of the GNU General Public License
40             along with VoiceXML::Client. If not, see .
41              
42              
43             =cut
44              
45             =head2 new [FILE]
46              
47             Creates a new instance
48              
49              
50             =cut
51              
52             sub new {
53 3     3 1 5737 my $class = shift;
54 3         8 my $file = shift;
55            
56 3         8 my $self = {};
57            
58 3   33     29 bless $self, ref $class || $class;
59            
60 3 50       14 $self->parse($file) if ($file);
61            
62            
63 3         14 return $self;
64             }
65              
66              
67             =head2 parse FILE
68              
69             Parses VXML file FILE and returns the VoiceXML::Client::Document object.
70              
71             =cut
72              
73             sub parse {
74 3     3 1 19 my $self = shift;
75 3   50     15 my $fileOrString = shift || die "VoiceXML::Client::Parser::parse MUST pass string or file";
76 3   50     31 my $runtime = shift || die "VoiceXML::Client::Parser::parse MUST pass runtime";
77            
78 3         36 $self->{'_XMLDoc'} = XML::Mini::Document->new();
79 3         192 $self->{'vxml'} = []; # make sure we're nice and empty
80            
81 3         8 my $numChildrenFound;
82 3 50 33     57 if ($fileOrString =~ m/\n/ || length($fileOrString) > 250)
83             {
84             # it is VXML content?
85             # TODO: Need a better test...
86 0         0 $numChildrenFound = $self->{'_XMLDoc'}->fromString($fileOrString);
87            
88 0 0       0 VoiceXML::Client::Util::error("VoiceXML::Client::Parser::parse() No xml children found in string ")
89             unless($numChildrenFound);
90             } else {
91            
92 3 50       12 VoiceXML::Client::Util::error("VoiceXML::Client::Parser::parse() Must pass a filename or contents")
93             unless ($fileOrString);
94            
95 3 50       76 VoiceXML::Client::Util::error("VoiceXML::Client::Parser::parse() Can't find file '$fileOrString'")
96             unless(-e $fileOrString);
97            
98 3 50       61 VoiceXML::Client::Util::error("VoiceXML::Client::Parser::parse() Can't read file '$fileOrString'")
99             unless(-r $fileOrString);
100            
101 3 50       27 $numChildrenFound = $self->{'_XMLDoc'}->fromFile($fileOrString) if ($fileOrString);
102            
103 3 50       139938 VoiceXML::Client::Util::error("VoiceXML::Client::Parser::parse() No xml children found in $fileOrString")
104             unless($numChildrenFound);
105             }
106            
107             #print $self->{'_XMLDoc'}->toString(); exit(0);
108            
109            
110 3         26 $self->{'_XMLRoot'} = $self->{'_XMLDoc'}->getRoot();
111            
112 3         57 $self->{'itemFactory'} = VoiceXML::Client::Item::Factory->new();
113            
114            
115 3   33     18 my $vxmlElement = $self->{'_XMLRoot'}->getElement('vxml')
116             || VoiceXML::Client::Util::error("VoiceXML::Client::Parser::parse() no vxml element found in document.");
117            
118 3         173 my $vxmlChildren = $vxmlElement->getAllChildren();
119 3         199 my $numChildren = scalar(@{$vxmlChildren});
  3         7  
120            
121 3 50       28 VoiceXML::Client::Util::error("VoiceXML::Client::Parser::parse() no form elements found in vxml element.")
122             unless ($numChildren);
123            
124            
125 3         8 my $docname = $fileOrString;
126 3         19 $docname =~ s|^.*/||;
127            
128 3         33 $self->{'VXMLDoc'} = VoiceXML::Client::Document->new($docname, $runtime);
129            
130 3         14 for(my $i=0; $i < $numChildren; $i++)
131             {
132 12   33     49 my $name = $vxmlChildren->[$i]->name() || VoiceXML::Client::Util::error("No name for child $i in VoiceXML::Client::Parser::parse()");
133 12         135 $self->{'vxml'}->[$i] = $self->{'itemFactory'}->newItem($name, $vxmlChildren->[$i], $self->{'VXMLDoc'});
134            
135             }
136            
137 3         17 $self->{'VXMLDoc'}->addItem($self->{'vxml'});
138            
139 3         17 return $self->{'VXMLDoc'};
140             }
141              
142             1;