Visio.pm

filter: Statementstmt 23.9% 39 / 163 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 11 / 33 Podpod 68.1% 15 / 22 totaltotal 27.3% 65 / 238 CC 11 SCAR 14.5
CC
11
Cov
21%
create_master
12.0
generic_create_node
12.0
new
6.0
CRAP 4.3
« Visio/Shape.pm ↑ Summary Visio/Page.pm »
1 package Visio;
2  
3  
4 #PLEASE SEE MSPATENTLICENSE
5 # "This product may incorporate intellectual property owned by
6 # Microsoft Corporation. The terms and conditions upon which Microsoft
7 # is licensing such intellectual property may be found at
8 # http://msdn.microsoft.com/library/en-us/odcXMLRef/html/odcXMLRefLegalNotice.asp"
9  
10 # Copyright 2005 Aamer Akhter. All rights reserved.
11  
12 # Redistribution and use in source and binary forms, with or without
13 # modification, are permitted provided that the following conditions are
14 # met:
15  
16 # 1. Redistributions of source code must retain the above copyright
17 # notice, this list of conditions and the following disclaimer.
18  
19 # 2. Redistributions in binary form must reproduce the above copyright
20 # notice, this list of conditions and the following disclaimer in the
21 # documentation and/or other materials provided with the distribution.
22  
23 # THIS SOFTWARE IS PROVIDED BY THE LICENSOR(S) ``AS IS'' AND ANY EXPRESS
24 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 # DISCLAIMED. IN NO EVENT SHALL THE LICENSOR(S) OR OTHER CONTRIBUTORS BE
27 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33 # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  
35 # The views and conclusions contained in the software and documentation
36 # are those of the authors and should not be interpreted as representing
37 # official policies, either expressed or implied, of the Licensor(s).
38  
39 # The software may be subject to additional license restrictions
40 # provided by the Licensor(s).
41  
42  
43  
44  
45 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use Log::Log4perl qw(get_logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 1 ▶use Visio::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use Visio::Master;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 $VERSION = sprintf "%d.%03d", q$Revision: 1.10 $ =~ /: (\d+)\.(\d+)/;
59  
60 1 Log::Log4perl->init_once(\ qq{
61 log4perl.logger = DEBUG, Screen
62 log4perl.appender.Screen = Log::Log4perl::Appender::ScreenColoredLevels
63 log4perl.appender.Screen.layout = PatternLayout
64 log4perl.appender.Screen.layout.ConversionPattern=[%d] [%c] [%F{1}:%L] [%p] %m%n
65 });
66 1 my $log = get_logger('visio');
67 1 my $vNS = 'visio';
68 1 my $vNSURI = 'http://schemas.microsoft.com/visio/2003/core';
69  
70 1 our $xmlParser = XML::LibXML->new();
71  
72 # Preloaded methods go here.
73  
74 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
75 0 my $class = shift;
76 0 my $opts = shift;
77 0 my $fileName = $$opts{fromFile};
78 0 my $self = {};
79 0 $self->{xmlparser} = $Visio::xmlParser;
80  
81 0 ▶ if (defined $fileName) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fileName) { } 0 0 0
82 0 $self->{xmldoc} = $self->{xmlparser}->parse_file($fileName);
83 0 $log->debug("visio object created from $fileName");
84 0 $self->{xmlroot} = $self->{xmldoc}->documentElement();
85 0 bless($self,$class);
86 } else {
87 0 $self->{xmldoc} = $self->{xmlparser}->parse_string(<<'EOT');
88 <VisioDocument xmlns='http://schemas.microsoft.com/visio/2003/core'>
89 </VisioDocument>
90 EOT
91 0 $self->{xmlroot} = $self->{xmldoc}->documentElement();
92 0 bless($self,$class);
93 0 $log->debug("visio xml object created");
94 }
95 0 $self->{vNSURI} = $self->{xmlroot}->namespaceURI;
96 #add the visio ns as a prefix
97 $self->{xmlroot}->setNamespace
98 (
99 0 ($self->{vNSURI}),
100 $vNS,
101 0
102 );
103 0 return $self;
104 }
105  
106 0 ▶sub addpage {
Subroutine addpage: not called CC=2 SCAR=17.9
Pod documented
107 0 my $self = shift;
108 0 my $pagesN;
109  
110 #if pages nodes does not exist, create it;
111 $pagesN = generic_create_node($self->{xmlroot},
112 0 'Pages'
113 );
114  
115 0 ▶ if (defined $self->{maxpageid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'maxpageid'}) { } 0 0 0
116 0 $self->{maxpageid} = $self->{maxpageid} + 1;
117 } else {
118 0 $self->{maxpageid} = 0;
119 }
120 0 my $page = new Visio::Page($pagesN,$self->{maxpageid});
121 0 return $page;
122 }
123  
124 0 ▶sub toFile {
Subroutine toFile: not called CC=1 SCAR=6.9
Pod documented
125 0 my $self = shift;
126 0 my $filename = shift;
127 0 $self->{xmldoc}->toFile($filename,1);
128 }
129  
130 0 ▶sub toString {
Subroutine toString: not called CC=1 SCAR=6.9
Pod documented
131 0 my $self = shift;
132 0 $self->{xmldoc}->toString(2);
133 }
134  
135 0 ▶sub toXmlDoc {
Subroutine toXmlDoc: not called CC=1 SCAR=6.9
Pod documented
136 0 my $self = shift;
137 0 return $self->{xmldoc};
138 }
139  
140 0 ▶sub set_docprop_node {
Subroutine set_docprop_node: not called CC=1 SCAR=6.9
Pod undocumented
141 0 my $self = shift;
142 0 my $nodeType = shift;
143 0 my $text = shift;
144 0 my $dpt = $self->create_docprop_node($nodeType);
145 0 my $textNode = $self->{xmldoc}->createTextNode($text);
146 0 $dpt->removeChildNodes();
147 0 $log->debug("doc $nodeType set");
148 0 $dpt->appendChild($textNode);
149 }
150  
151 0 ▶sub set_docprop_timenode {
Subroutine set_docprop_timenode: not called CC=2 SCAR=17.9
Pod undocumented
152 0 my $self = shift;
153 0 my $nodeType = shift;
154 0 my $text = shift;
155 0 ▶ $text = visio_timeformat() if (!defined $text);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
156 0 $self->set_docprop_node($nodeType,$text);
157 }
158  
159 0 ▶sub visio_timeformat {
Subroutine visio_timeformat: not called CC=2 SCAR=17.9
Pod documented
160 0 my $time = shift;
161 0 ▶ if (!defined $time) {$time = time()};
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 0 0 0
162 0 my @gmTime = gmtime($time);
163 0 $gmTime[4]++; # perl months are from 0..11
164 0 $gmTime[5] += 1900; # perl years
165 0 return sprintf("%4d-%02d-%02dT%02d:%02d:%02d",
166 $gmTime[5],
167 $gmTime[3],
168 $gmTime[4],
169 $gmTime[2],
170 $gmTime[1],
171 $gmTime[0]
172 );
173 }
174  
175 0 ▶sub set_timeSaved {
Subroutine set_timeSaved: not called CC=1 SCAR=6.9
Pod documented
176 0 my $self = shift;
177 0 my $text = shift;
178 0 $self->set_docprop_timenode('TimeSaved',$text);
179 }
180  
181 0 ▶sub set_timeCreated {
Subroutine set_timeCreated: not called CC=1 SCAR=6.9
Pod documented
182 0 my $self = shift;
183 0 my $text = shift;
184 0 $self->set_docprop_timenode('TimeCreated',$text);
185 }
186  
187  
188 0 ▶sub set_title {
Subroutine set_title: not called CC=1 SCAR=6.9
Pod documented
189 0 my $self = shift;
190 0 my $text = shift;
191 0 $self->set_docprop_node('Title',$text);
192 }
193  
194 0 ▶sub set_subject {
Subroutine set_subject: not called CC=1 SCAR=6.9
Pod documented
195 0 my $self = shift;
196 0 my $text = shift;
197 0 $self->set_docprop_node('Subject',$text);
198 }
199  
200 0 ▶sub set_manager {
Subroutine set_manager: not called CC=1 SCAR=6.9
Pod documented
201 0 my $self = shift;
202 0 my $text = shift;
203 0 $self->set_docprop_node('Manager',$text);
204 }
205  
206 0 ▶sub set_company {
Subroutine set_company: not called CC=1 SCAR=6.9
Pod documented
207 0 my $self = shift;
208 0 my $text = shift;
209 0 $self->set_docprop_node('Company',$text);
210 }
211  
212 0 ▶sub set_desc {
Subroutine set_desc: not called CC=1 SCAR=6.9
Pod documented
213 0 my $self = shift;
214 0 my $text = shift;
215 0 $self->set_docprop_node('Desc',$text);
216 }
217  
218 0 ▶sub set_creator {
Subroutine set_creator: not called CC=1 SCAR=6.9
Pod documented
219 0 my $self = shift;
220 0 my $text = shift;
221 0 $self->set_docprop_node('Creator',$text);
222 }
223  
224 0 ▶sub find_master_dom {
Subroutine find_master_dom: not called CC=2 SCAR=17.9
Pod documented
225 0 my $self = shift;
226 0 my $opts = shift;
227 0 my $nname = $opts->{'name'};
228 0 my $xp = "/$vNS:VisioDocument/$vNS:Masters/$vNS:Master";
229 0 my $xpSel = "";
230 0 ▶ if (defined $nname) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nname) 0 0 0
231 0 $xpSel .= "\@Name = '$nname'";
232 }
233 0 $xp .= "[$xpSel]";
234 0 $log->debug("searching with $xp");
235 0 return $self->{xmlroot}->findnodes($xp);
236 }
237  
238  
239 0 ▶sub create_master {
Subroutine create_master: not called CC=3 SCAR=24.8
Pod undocumented
240 0 my $self = shift;
241 0 my $opts = shift;
242 0 my $fromDom = $$opts{'fromDom'};
243 0 my $master;
244 my $mastersNode = generic_create_node($self->{xmlroot},
245 0 'Masters'
246 );
247 0 ▶ if (defined $self->{maxMasterid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'maxMasterid'}) { } 0 0 0
248 0 $self->{maxMasterid} = $self->{maxMasterid} + 1;
249 } else {
250 0 $self->{maxMasterid} = 0;
251 }
252 0 ▶ if (defined $fromDom) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fromDom) 0 0 0
253 $master = new Visio::Master($mastersNode,
254 $self->{maxMasterid},
255 0 {fromDom=>$fromDom}
256 );
257 }
258 0 return $master;
259 }
260  
261  
262  
263  
264 #================ PRIIVATE FUNCTIONS
265  
266  
267 0 ▶sub create_docprop_node {
Subroutine create_docprop_node: not called CC=1 SCAR=6.9
Pod undocumented
268 0 my $self = shift;
269 0 my $node = shift;
270 0 my $dp = $self->create_docprop();
271 0 return generic_create_node($dp, $node);
272 }
273  
274 0 ▶sub create_docprop {
Subroutine create_docprop: not called CC=2 SCAR=17.9
Pod undocumented
275 0 my $self = shift;
276 #see of DocumentProperties already exists, if not crate it
277 0 my $docpropNL = $self->{xmlroot}->findnodes("DocumentProperties");
278 0 ▶ return $docpropNL->pop if ($docpropNL->size() > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $docpropNL->size > 0 0 0 0
279 0 $log->debug('creating DocumentProperties node');
280 0 my $docpropN = $self->{xmldoc}->createElement("DocumentProperties");
281 $self->{xmlroot}->insertBefore(
282 $docpropN,
283 $self->{xmlroot}->firstChild
284 0 );
285 }
286  
287 0 ▶sub generic_create_node {
Subroutine generic_create_node: not called CC=3 SCAR=24.8
Pod undocumented
288 0 my $parent = shift;
289 0 my $node = shift;
290 0 my $nodeNL = $parent->findnodes("$node");
291 0 ▶ return $nodeNL->pop if ($nodeNL->size() > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $nodeNL->size > 0 0 0 0
292 0 $log->debug("creating $parent/$node node");
293 0 my $uri = $parent->namespaceURI;
294 0 ▶ if (!defined $uri) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uri) 0 0 0
295 0 $uri = $vNSURI;
296 }
297 0 my $nodeN = $parent->ownerDocument->createElement("$node");
298 0 $parent->appendChild($nodeN);
299 0 return $nodeN;
300 }
301  
302 0 ▶sub generic_settext {
Subroutine generic_settext: not called CC=1 SCAR=6.9
Pod undocumented
303 0 my $node = shift;
304 0 my $text = shift;
305 0 my $textNode = $node->ownerDocument->createTextNode($text);
306 0 $node->removeChildNodes();
307 0 $log->debug("doc $node text set");
308 0 $node->appendChild($textNode);
309 }
310  
311  
312 1 1;
313 __END__
« Visio/Shape.pm ↑ Summary Visio/Page.pm »