📄 ippprintservice.java
字号:
/* IppPrintService.java -- Copyright (C) 2006 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.javax.print.ipp;import gnu.classpath.SystemProperties;import gnu.classpath.debug.Component;import gnu.classpath.debug.SystemLogger;import gnu.javax.print.ipp.attribute.DefaultValueAttribute;import gnu.javax.print.ipp.attribute.RequestedAttributes;import gnu.javax.print.ipp.attribute.defaults.CopiesDefault;import gnu.javax.print.ipp.attribute.defaults.FinishingsDefault;import gnu.javax.print.ipp.attribute.defaults.JobHoldUntilDefault;import gnu.javax.print.ipp.attribute.defaults.JobPriorityDefault;import gnu.javax.print.ipp.attribute.defaults.JobSheetsDefault;import gnu.javax.print.ipp.attribute.defaults.MediaDefault;import gnu.javax.print.ipp.attribute.defaults.MultipleDocumentHandlingDefault;import gnu.javax.print.ipp.attribute.defaults.NumberUpDefault;import gnu.javax.print.ipp.attribute.defaults.OrientationRequestedDefault;import gnu.javax.print.ipp.attribute.defaults.PrintQualityDefault;import gnu.javax.print.ipp.attribute.defaults.PrinterResolutionDefault;import gnu.javax.print.ipp.attribute.defaults.SidesDefault;import gnu.javax.print.ipp.attribute.printer.DocumentFormat;import gnu.javax.print.ipp.attribute.supported.CompressionSupported;import gnu.javax.print.ipp.attribute.supported.DocumentFormatSupported;import gnu.javax.print.ipp.attribute.supported.FinishingsSupported;import gnu.javax.print.ipp.attribute.supported.JobHoldUntilSupported;import gnu.javax.print.ipp.attribute.supported.JobSheetsSupported;import gnu.javax.print.ipp.attribute.supported.MediaSupported;import gnu.javax.print.ipp.attribute.supported.MultipleDocumentHandlingSupported;import gnu.javax.print.ipp.attribute.supported.OperationsSupported;import gnu.javax.print.ipp.attribute.supported.OrientationRequestedSupported;import gnu.javax.print.ipp.attribute.supported.PageRangesSupported;import gnu.javax.print.ipp.attribute.supported.PrintQualitySupported;import gnu.javax.print.ipp.attribute.supported.PrinterResolutionSupported;import gnu.javax.print.ipp.attribute.supported.PrinterUriSupported;import gnu.javax.print.ipp.attribute.supported.SidesSupported;import java.io.IOException;import java.lang.reflect.Field;import java.net.URI;import java.util.ArrayList;import java.util.Arrays;import java.util.Date;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.logging.Logger;import javax.print.DocFlavor;import javax.print.DocPrintJob;import javax.print.PrintService;import javax.print.ServiceUIFactory;import javax.print.attribute.Attribute;import javax.print.attribute.AttributeSet;import javax.print.attribute.AttributeSetUtilities;import javax.print.attribute.HashAttributeSet;import javax.print.attribute.HashPrintServiceAttributeSet;import javax.print.attribute.IntegerSyntax;import javax.print.attribute.PrintServiceAttribute;import javax.print.attribute.PrintServiceAttributeSet;import javax.print.attribute.standard.Compression;import javax.print.attribute.standard.Copies;import javax.print.attribute.standard.CopiesSupported;import javax.print.attribute.standard.Fidelity;import javax.print.attribute.standard.Finishings;import javax.print.attribute.standard.JobHoldUntil;import javax.print.attribute.standard.JobImpressions;import javax.print.attribute.standard.JobImpressionsSupported;import javax.print.attribute.standard.JobKOctets;import javax.print.attribute.standard.JobKOctetsSupported;import javax.print.attribute.standard.JobMediaSheets;import javax.print.attribute.standard.JobMediaSheetsSupported;import javax.print.attribute.standard.JobName;import javax.print.attribute.standard.JobPriority;import javax.print.attribute.standard.JobPrioritySupported;import javax.print.attribute.standard.JobSheets;import javax.print.attribute.standard.Media;import javax.print.attribute.standard.MultipleDocumentHandling;import javax.print.attribute.standard.NumberUp;import javax.print.attribute.standard.NumberUpSupported;import javax.print.attribute.standard.OrientationRequested;import javax.print.attribute.standard.PageRanges;import javax.print.attribute.standard.PrintQuality;import javax.print.attribute.standard.PrinterName;import javax.print.attribute.standard.PrinterResolution;import javax.print.attribute.standard.PrinterURI;import javax.print.attribute.standard.RequestingUserName;import javax.print.attribute.standard.Sides;import javax.print.event.PrintServiceAttributeListener;/** * Implementation of the PrintService interface * for IPP based printers. * * @author Wolfgang Baer (WBaer@gmx.de) */public class IppPrintService implements PrintService{ /** * A Map with sets of attributes. * key: A attribute category * value: A set with values * * IPP may return sets of attributes e.g. for supported * compression methods so we need to map to sets here. */ private Map printerAttr; /** The set of listeners.*/ private HashSet printServiceAttributeListener; /** The username. */ private transient String user; /** The password of the user. */ private transient String passwd; /** The name of this print service. */ private String name; /** The list of supported document flavors. */ private List flavors; /** The standard printer URI. */ private PrinterURI printerUri; /** The list of all supported printer URIs. */ private ArrayList printerUris; /** * Logger for tracing - enable by passing * -Dgnu.classpath.debug.components=ipp to the vm. */ static final Logger logger = SystemLogger.SYSTEM; /** * requesting-user-name defaults to the executing user. */ public static final RequestingUserName REQUESTING_USER_NAME; /** * job-name defaults to "Java Printing". */ public static final JobName JOB_NAME; static { JOB_NAME = new JobName("Java Printing", null); REQUESTING_USER_NAME = new RequestingUserName( SystemProperties.getProperty("user.name", ""), null); } // TODO Implement service listener notification and change detection. /** * Creates a <code>IppPrintService</code> object. * * @param uri the URI of the IPP printer. * @param username the user of this print service. * @param password the password of the user. * * @throws IppException if an error during connection occurs. */ public IppPrintService(URI uri, String username, String password) throws IppException { printerUri = new PrinterURI(uri); user = username; passwd = password; printServiceAttributeListener = new HashSet(); printerAttr = getPrinterAttributes(); processResponse(); } /** * Fetches all printer attributes from the IPP printer. * * @return The Map with the printer attributes. * @throws IppException if an error occurs. */ private Map getPrinterAttributes() throws IppException { IppResponse response = null; try { IppRequest request = new IppRequest(printerUri.getURI(), user, passwd); int operation = OperationsSupported.GET_PRINTER_ATTRIBUTES.getValue(); request.setOperationID((short) operation); request.setOperationAttributeDefaults(); request.addOperationAttribute(printerUri); response = request.send(); } catch (IOException e) { throw new IppException("IOException in IPP request/response.", e); } return (Map) response.getPrinterAttributes().get(0); } /** * Extracts the set of attribute values for a given * attribute category from the printer attributes map. * * @param attributeClass the category * @return The set of attributes of the category. */ private Set getPrinterAttributeSet(Class attributeClass) { return (Set) printerAttr.get(attributeClass); } /** * Extracts the default attribute value for the given * default attribute category from the printer attributes map. * * @param attributeClass the category * @return The default attribute. * * @throws ClassCastException if attributClass is not an * instance of <code>DefaultValueAttribute</code>. */ private Attribute getPrinterDefaultAttribute(Class attributeClass) { Set set = (Set) printerAttr.get(attributeClass); return ((DefaultValueAttribute) set.toArray()[0]).getAssociatedAttribute(); } /** * Processes the response, sorts and splits the attributes. */ private void processResponse() { // printer name PrinterName[] tmp = (PrinterName[]) getPrinterAttributeSet( PrinterName.class).toArray(new PrinterName[1]); name = tmp[0].getValue(); // supported flavors // TODO Check if charsets-supported are charsets that are actually supported // for text doc flavors as cups doesn't send charset parameters // utf-8 is supported at least - so we go with this only for now flavors = new ArrayList(); Set flavorAttributes = getPrinterAttributeSet(DocumentFormatSupported.class); if (flavorAttributes != null) { for (Iterator it = flavorAttributes.iterator(); it.hasNext();) { String mimeType = ((DocumentFormatSupported) it.next()).getValue(); if (mimeType.equals("text/plain")) { flavors.add(DocFlavor.CHAR_ARRAY.TEXT_PLAIN); flavors.add(DocFlavor.READER.TEXT_PLAIN); flavors.add(DocFlavor.STRING.TEXT_PLAIN); // add utf-8 mimeType = mimeType + "; charset=utf-8"; } else if (mimeType.equals("text/html")) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -