~ubuntu-branches/ubuntu/raring/brailleutils/raring

« back to all changes in this revision

Viewing changes to src/org/daisy/printing/PrinterDevice.java

  • Committer: Package Import Robot
  • Author(s): Sebastian Humenda
  • Date: 2011-08-27 12:21:13 UTC
  • Revision ID: package-import@ubuntu.com-20110827122113-72udcdbhzrqtdlgx
Tags: upstream-1.2~b
Import upstream version 1.2~b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Braille Utils (C) 2010-2011 Daisy Consortium 
 
3
 * 
 
4
 * This library is free software; you can redistribute it and/or modify it under
 
5
 * the terms of the GNU Lesser General Public License as published by the Free
 
6
 * Software Foundation; either version 2.1 of the License, or (at your option)
 
7
 * any later version.
 
8
 * 
 
9
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
11
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 
12
 * details.
 
13
 * 
 
14
 * You should have received a copy of the GNU Lesser General Public License
 
15
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 
16
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
17
 */
 
18
package org.daisy.printing;
 
19
 
 
20
import java.io.File;
 
21
import java.io.FileInputStream;
 
22
import java.io.FileNotFoundException;
 
23
import java.io.IOException;
 
24
import java.io.InputStream;
 
25
import java.io.Reader;
 
26
 
 
27
import javax.print.Doc;
 
28
import javax.print.DocFlavor;
 
29
import javax.print.DocPrintJob;
 
30
import javax.print.PrintException;
 
31
import javax.print.PrintService;
 
32
import javax.print.PrintServiceLookup;
 
33
import javax.print.attribute.DocAttributeSet;
 
34
 
 
35
/**
 
36
 * Printer device class of type DocFlavor.INPUT_STREAM.AUTOSENSE
 
37
 * 
 
38
 * This class can be used when sending a file to a printer.
 
39
 * 
 
40
 * @author  Joel Håkansson
 
41
 * @version 3 jul 2008
 
42
 */
 
43
public class PrinterDevice implements Device {
 
44
        //private final static DocFlavor FLAVOR = DocFlavor.BYTE_ARRAY.AUTOSENSE;
 
45
        private final static DocFlavor FLAVOR = DocFlavor.INPUT_STREAM.AUTOSENSE;
 
46
        private PrintService service;
 
47
        
 
48
        /**
 
49
         * Create a device with the provided name.
 
50
         * @param deviceName the name of the device
 
51
         * @param fuzzyLookup If true, the returned device is any device whose name contains the 
 
52
         * supplied deviceName. If false, the returned device name equals the supplied deviceName. 
 
53
         * @throws IllegalArgumentException if no device is found.
 
54
         */
 
55
        public PrinterDevice(String deviceName, boolean fuzzyLookup) {
 
56
                PrintService[] printers = PrintServiceLookup.lookupPrintServices(FLAVOR, null);
 
57
                for (PrintService p : printers) {
 
58
                        if (p.getName().equals(deviceName)) {
 
59
                                service = p;
 
60
                                return;
 
61
                        }
 
62
                }
 
63
                if (fuzzyLookup) {
 
64
                        PrintService match = null;
 
65
                        double currentMatch = 0;
 
66
                        for (PrintService p : printers) {
 
67
                                if (p.getName().contains(deviceName)) {
 
68
                                        double thisMatch = deviceName.length() / (double)p.getName().length();
 
69
                                        if (thisMatch > currentMatch) {
 
70
                                                currentMatch = thisMatch;
 
71
                                                match = p;
 
72
                                        }
 
73
                                }
 
74
                        }
 
75
                        if (match != null) {
 
76
                                service = match;
 
77
                                return;
 
78
                        }                       
 
79
                }
 
80
                throw new IllegalArgumentException("Could not find embosser.");
 
81
        }
 
82
        
 
83
        /**
 
84
         * List available devices
 
85
         * @return returns a list of available devices that accepts DocFlavor.INPUT_STREAM.AUTOSENSE 
 
86
         */
 
87
        public static PrintService[] getDevices() {
 
88
                PrintService[] printers = PrintServiceLookup.lookupPrintServices(FLAVOR, null);
 
89
                return printers;
 
90
        }
 
91
 
 
92
        /**
 
93
         * Transmit a file to the device
 
94
         * @param file the file to transmit
 
95
         * @throws FileNotFoundException
 
96
         * @throws PrintException
 
97
         */
 
98
        public void transmit(File file) throws PrintException {
 
99
                try {
 
100
                        transmit(new FileInputStream(file));
 
101
                } catch (FileNotFoundException e) {
 
102
                        throw new PrintException(e);
 
103
                }
 
104
        }
 
105
        
 
106
        private void transmit(InputStream is) throws PrintException {
 
107
                InputStreamDoc doc = new InputStreamDoc(is);
 
108
                DocPrintJob dpj = service.createPrintJob();
 
109
                dpj.print(doc, null);
 
110
        }
 
111
 
 
112
        private class InputStreamDoc implements Doc {
 
113
                private InputStream stream;
 
114
                
 
115
                /**
 
116
                 * Default constructor
 
117
                 * @param file
 
118
                 * @throws FileNotFoundException
 
119
                 */
 
120
                public InputStreamDoc(InputStream stream) {
 
121
                        this.stream = stream;
 
122
                }
 
123
 
 
124
                public DocAttributeSet getAttributes() {
 
125
                        return null;
 
126
                }
 
127
 
 
128
                public DocFlavor getDocFlavor() {
 
129
                        return FLAVOR;
 
130
                }
 
131
 
 
132
                public Object getPrintData() throws IOException {
 
133
                        return getStreamForBytes();
 
134
                }
 
135
 
 
136
                public Reader getReaderForText() throws IOException {
 
137
                        return null;
 
138
                }
 
139
 
 
140
                public InputStream getStreamForBytes() throws IOException {
 
141
                        return stream;
 
142
                }
 
143
        }
 
144
}