ESCPOS.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 SCAR 0.1
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« ESCPOS/Debug.pm ↑ Summary
1 package XML::Printer::ESCPOS;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use XML::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use XML::Printer::ESCPOS::Tags;
8 use XML::Printer::ESCPOS::Debug;
9  
10 =head1 NAME
11  
12 XML::Printer::ESCPOS - An XML parser for generating ESCPOS output.
13  
14 =head1 DESCRIPTION
15  
16 This module provides a markup language that describes what your ESCPOS printer should do.
17 It works on top of the great and easy to use L<Printer::ESCPOS>. Now you can save your printer
18 output in an XML file and you can write templates to be processed by Template Toolkit or the
19 template engine of your choice.
20  
21 =cut
22  
23 our $VERSION = '0.05';
24  
25 =head1 SYNOPSIS
26  
27 use Printer::ESCPOS;
28 use XML::Printer::ESCPOS;
29  
30 # connect to your printer, see Printer::ESCPOS for more examples
31 my $device = Printer::ESCPOS->new(
32 driverType => 'Network',
33 deviceIp => '192.168.0.10',
34 devicePort => 9100,
35 );
36  
37 my $parser = XML::Printer::ESCPOS->new(printer => $device->printer);
38 $parser->parse(q#
39 <escpos>
40 <bold>bold text</bold>
41 <underline>underlined text</underline>
42 </escpos>
43 #) or die "Error parsing ESCPOS XML file: ".$parser->errormessage;
44  
45 $device->printer->cutPaper();
46 $device->printer->print();
47  
48  
49 =head1 METHODS
50  
51 =head2 new(printer => $printer)
52  
53 Constructs a new XML::Printer::ESCPOS object. You must provide a printer object you
54 get by C<Printer::ESCPOS->new(...)->printer>.
55  
56 If you do not set a printer, the module works in debug mode (see below).
57  
58 =cut
59  
60 sub new {
61 my ( $class, %options ) = @_;
62 $options{printer} ||= XML::Printer::ESCPOS::Debug->new();
63 return bless {%options}, $class;
64 }
65  
66 =head2 parse($xml)
67  
68 Parses the XML data given by C<$xml>. C<$xml> should contain the file content.
69 Returns 1 on success, undef otherwise. If parsing was unsuccessful, you can find the
70 errormessage by calling the C<errormessage> method.
71  
72 =cut
73  
74 sub parse {
75 my ( $self, $xml ) = @_;
76 my $printer = $self->{printer};
77  
78 my $xmlparser = XML::Parser->new( Style => 'Tree', );
79 my $tree = $xmlparser->parse($xml);
80  
81 return _set_error_message('no document found') if !@{$tree};
82 return _set_error_message('more than one base tag found') if @{$tree} > 2;
83 return _set_error_message('document is not an ESCPOS doc (start document with <escpos>!)') if $tree->[0] ne 'escpos';
84  
85 my $tags = XML::Printer::ESCPOS::Tags->new(
86 printer => $self->{printer},
87 caller => $self,
88 );
89  
90 # reset error message on new parser run
91 delete $self->{errormessage};
92  
93 my $parsed = $tags->parse( $tree->[1] );
94 if ( ref $self->{printer} eq 'XML::Printer::ESCPOS::Debug' ) {
95 return $self->{printer}->as_perl_code();
96 }
97 return $parsed;
98 }
99  
100 =head2 errormessage
101  
102 Returns the last error message.
103  
104 =cut
105  
106 sub errormessage {
107 my $self = shift;
108 return $self->{errormessage};
109 }
110  
111 =head1 INTERNAL METHODS
112  
113 =head2 _set_error_message( $message )
114  
115 Internal method to set the error message in the object before the parser returns.
116  
117 =cut
118  
119 sub _set_error_message {
120 my ( $self, $message ) = @_;
121 $self->{errormessage} = $message;
122 return;
123 }
124  
125 =head1 DEBUG MODE
126  
127 Sometimes it helps to see what the module would do instead of doing it. That's why I added the debug mode:
128 If you do not set a printer object when calling the constructor, an L<XML::Printer::ESCPOS::Debug> object is created.
129 It catches all method calls and the C<parse> method returns the perl code that would have been executed if
130 you had set the printer object.
131  
132 This XML code
133  
134 <escpos>
135 <utf8ImagedText
136 fontFamily = "DejaVu Sans"
137 fontStyle = "Bold"
138 fontSize = "76"
139 lineHeight = "115"
140 paperWidth = "832">This module</utf8ImagedText>
141 <lf />
142 <utf8ImagedText
143 fontFamily = "DejaVu Sans"
144 fontStyle = "Bold"
145 fontSize = "36"
146 lineHeight = "55"
147 paperWidth = "832"
148 >is great for writing templates</utf8ImagedText>
149 <qr>https://github.com/sonntagd/XML-Printer-ESCPOS</qr>
150 </escpos>
151  
152 will create the following perl code:
153  
154 $device->printer->utf8ImagedText(
155 'This module',
156 'fontFamily',
157 'DejaVu Sans',
158 'fontSize',
159 '76',
160 'fontStyle',
161 'Bold',
162 'lineHeight',
163 '115',
164 'paperWidth',
165 '832'
166 );
167 $device->printer->lf();
168 $device->printer->utf8ImagedText(
169 'is great for writing templates',
170 'fontFamily',
171 'DejaVu Sans',
172 'fontSize',
173 '36',
174 'fontStyle',
175 'Bold',
176 'lineHeight',
177 '55',
178 'paperWidth',
179 '832'
180 );
181 $device->printer->qr(
182 'https://github.com/sonntagd/XML-Printer-ESCPOS'
183 );
184  
185  
186 =head1 AUTHOR
187  
188 Dominic Sonntag, C<< <dominic at s5g.de> >>
189  
190 =head1 BUGS
191  
192 Please report any bugs or feature requests by opening an issue on Github:
193 L<https://github.com/sonntagd/XML-Printer-ESCPOS/issues>
194  
195  
196 =head1 LICENSE AND COPYRIGHT
197  
198 Copyright 2017 Dominic Sonntag.
199  
200 This program is free software; you can redistribute it and/or modify it
201 under the terms of the the Artistic License (2.0). You may obtain a
202 copy of the full license at:
203  
204 L<http://www.perlfoundation.org/artistic_license_2_0>
205  
206 =cut
207  
208 1; # End of XML::Printer::ESCPOS
« ESCPOS/Debug.pm ↑ Summary