File Coverage

blib/lib/XML/Printer/ESCPOS.pm
Criterion Covered Total %
statement 9 11 81.8
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 13 15 86.6


line stmt bran cond sub pod time code
1             package XML::Printer::ESCPOS;
2              
3 1     1   13693 use 5.010;
  1         4  
4 1     1   5 use strict;
  1         2  
  1         15  
5 1     1   4 use warnings;
  1         5  
  1         26  
6 1     1   250 use XML::Parser;
  0            
  0            
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. 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            
40             bold text
41             underlined text
42            
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 Cnew(...)->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 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 !)') 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 object is created.
129             It catches all method calls and the C 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            
135            
136             fontFamily = "DejaVu Sans"
137             fontStyle = "Bold"
138             fontSize = "76"
139             lineHeight = "115"
140             paperWidth = "832">This module
141            
142            
143             fontFamily = "DejaVu Sans"
144             fontStyle = "Bold"
145             fontSize = "36"
146             lineHeight = "55"
147             paperWidth = "832"
148             >is great for writing templates
149             https://github.com/sonntagd/XML-Printer-ESCPOS
150            
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<< >>
189              
190             =head1 BUGS
191              
192             Please report any bugs or feature requests by opening an issue on Github:
193             L
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
205              
206             =cut
207              
208             1; # End of XML::Printer::ESCPOS