Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / print / CupsPrintServiceLookup.java
blob1aa83218a8d8d5414d75d973483a68d184bbddc2
1 /* CupsPrintServiceLookup.java -- Implementation based on CUPS
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.print;
41 import gnu.javax.print.ipp.IppException;
43 import java.util.ArrayList;
45 import javax.print.DocFlavor;
46 import javax.print.MultiDocPrintService;
47 import javax.print.PrintService;
48 import javax.print.PrintServiceLookup;
49 import javax.print.attribute.Attribute;
50 import javax.print.attribute.AttributeSet;
52 /**
53 * The platform default implementation based on CUPS.
55 * @author Wolfgang Baer (WBaer@gmx.de)
57 public class CupsPrintServiceLookup extends PrintServiceLookup
59 private CupsServer server;
61 /**
62 * Default constructor checking security access.
64 public CupsPrintServiceLookup()
66 // security
67 SecurityManager sm = System.getSecurityManager();
68 if (sm != null)
69 sm.checkPrintJobAccess();
71 // use the localhost cups server
72 server = new CupsServer(null, null);
75 /**
76 * This is the printer marked as default in CUPS.
78 * @return The default lookup service or
79 * <code>null</code> if there is no default.
81 public PrintService getDefaultPrintService()
83 try
85 return server.getDefaultPrinter();
87 catch (IppException e)
89 // if discovery fails treat as if there is none
90 return null;
94 /**
95 * All printers and printer classes of the CUPS server are checked.
96 * If flavors or attributes are null the constraint is not used.
98 * @param flavors the document flavors which have to be supported.
99 * @param attributes the attributes which have to be supported.
101 * @return The multidoc print services of the implementing lookup service
102 * for the given parameters, or an array of length 0 if none is available.
104 public MultiDocPrintService[] getMultiDocPrintServices(DocFlavor[] flavors,
105 AttributeSet attributes)
107 ArrayList result = new ArrayList();
108 PrintService[] services = getPrintServices();
110 for (int i=0; i < services.length; i++)
112 if (checkMultiDocPrintService(flavors, attributes, services[i]))
113 result.add(services[i]);
116 return (MultiDocPrintService[]) result.toArray(
117 new MultiDocPrintService[result.size()]);
121 * These are all printers and printer classes of the CUPS server.
123 * @return All known print services regardless of supported features,
124 * or an array of length 0 if none is available.
126 public PrintService[] getPrintServices()
128 ArrayList result = new ArrayList();
132 result.addAll(server.getAllPrinters());
133 result.addAll(server.getAllClasses());
135 catch (IppException e)
137 // ignore as this method cannot throw exceptions
138 // if print service discovery fails - bad luck
140 return (PrintService[]) result.toArray(new PrintService[result.size()]);
145 * All printers and printer classes of the CUPS server are checked.
146 * If flavor or attributes are null the constraint is not used.
148 * @param flavor the document flavor which has to be supported.
149 * @param attributes the attributes which have to be supported.
151 * @return The print services of the implementing lookup service
152 * for the given parameters, or an array of length 0 if none is available.
154 public PrintService[] getPrintServices(DocFlavor flavor,
155 AttributeSet attributes)
157 ArrayList result = new ArrayList();
158 PrintService[] services = getPrintServices();
160 for (int i=0; i < services.length; i++)
162 if (checkPrintService(flavor, attributes, services[i]))
163 result.add(services[i]);
166 return (PrintService[]) result.toArray(new PrintService[result.size()]);
170 * Checks the given print service - own method so it can be used also
171 * to check application registered print services from PrintServiceLookup.
173 * @param flavor the document flavor which has to be supported.
174 * @param attributes the attributes which have to be supported.
175 * @param service the service to check
177 * @return <code>true</code> if all constraints match, <code>false</code>
178 * otherwise.
180 public boolean checkPrintService(DocFlavor flavor, AttributeSet attributes,
181 PrintService service)
183 boolean allAttributesSupported = true;
184 if (flavor == null || service.isDocFlavorSupported(flavor))
186 if (attributes == null || attributes.size() == 0)
187 return allAttributesSupported;
189 Attribute[] atts = attributes.toArray();
190 for (int i = 0; i < atts.length; i++)
192 if (! service.isAttributeCategorySupported(atts[i].getCategory()))
194 allAttributesSupported = false;
195 break;
198 return allAttributesSupported;
201 return false;
205 * Checks the given print service - own method so it can be used also
206 * to check application registered print services from PrintServiceLookup.
208 * @param flavors the document flavors which have to be supported.
209 * @param attributes the attributes which have to be supported.
210 * @param service the service to check
212 * @return <code>true</code> if all constraints match, <code>false</code>
213 * otherwise.
215 public boolean checkMultiDocPrintService(DocFlavor[] flavors,
216 AttributeSet attributes, PrintService service)
218 if (service instanceof MultiDocPrintService)
220 boolean allFlavorsSupported = true;
221 boolean allAttributesSupported = true;
223 if (flavors == null || flavors.length != 0)
224 allFlavorsSupported = true;
225 else
227 for (int k = 0; k < flavors.length; k++)
229 if (! service.isDocFlavorSupported(flavors[k]))
231 allFlavorsSupported = false;
232 break;
237 if (attributes == null || attributes.size() == 0)
238 allAttributesSupported = true;
239 else
241 Attribute[] atts = attributes.toArray();
242 for (int j = 0; j < atts.length; j++)
244 if (! service.isAttributeCategorySupported(
245 atts[j].getCategory()))
247 allAttributesSupported = false;
248 break;
253 if (allAttributesSupported && allFlavorsSupported)
254 return true;
257 return false;