public <T extends PrintServiceAttribute> T getAttribute(Class<T> category) { if (category == null) { throw new NullPointerException("category"); } if (!(PrintServiceAttribute.class.isAssignableFrom(category))) { throw new IllegalArgumentException("Not a PrintServiceAttribute"); } if (category == PrinterName.class) { return (T)getPrinterName(); } else if (category == PrinterState.class) { return (T)getPrinterState(); } else if (category == PrinterStateReasons.class) { return (T)getPrinterStateReasons(); } else if (category == QueuedJobCount.class) { return (T)getQueuedJobCount(); } else if (category == PrinterIsAcceptingJobs.class) { return (T)getPrinterIsAcceptingJobs(); } else { return null; } }
@Override public <T extends PrintServiceAttribute>T getAttribute(Class<T> category){ if(category == null){ throw new NullPointerException("category"); } if(!(PrintServiceAttribute.class.isAssignableFrom(category))){ throw new IllegalArgumentException("The categhory '" + category + "' is not a valid PrintServiceAttribute"); } if(category == ColorSupported.class){ // works better than settings.get_SupportsColor(); if(settings.get_DefaultPageSettings().get_Color()){ return (T)ColorSupported.SUPPORTED; }else{ return (T)ColorSupported.NOT_SUPPORTED; } }else if(category == PrinterName.class){ return (T)getPrinterName(); } else { // QueuedJobCount and PrinterIsAcceptingJobs return (T)peer.getPrinterStatus(printer, category); } }
public PrintServiceStub(String name) { _name = name; _flavors = new HashSet<DocFlavor>(); _flavors.add(DocFlavor.SERVICE_FORMATTED.PAGEABLE); _flavors.add(DocFlavor.SERVICE_FORMATTED.PRINTABLE); _attributes = new HashMap<>(); _attributes.put(PrinterName.class, new PrinterName(name, null)); _attributes.put(PrinterState.class, PrinterState.IDLE); _attributes.put(PrinterInfo.class, new PrinterInfo("Custom location", null)); _attributes.put(PrinterIsAcceptingJobs.class, PrinterIsAcceptingJobs.ACCEPTING_JOBS); _attributes.put(PrinterMakeAndModel.class, new PrinterMakeAndModel( "Custom printer", null)); _attributes.put(Media.class, new Media[] { MediaSizeName.ISO_A4 }); }
@SuppressWarnings("unchecked") public <T extends PrintServiceAttribute> T getAttribute(Class<T> category) { if (category == null) { throw new NullPointerException("category"); } if (!(PrintServiceAttribute.class.isAssignableFrom(category))) { throw new IllegalArgumentException("Not a PrintServiceAttribute"); } if (category == PrinterName.class) { return (T)getPrinterName(); } else if (category == PrinterState.class) { return (T)getPrinterState(); } else if (category == PrinterStateReasons.class) { return (T)getPrinterStateReasons(); } else if (category == QueuedJobCount.class) { return (T)getQueuedJobCount(); } else if (category == PrinterIsAcceptingJobs.class) { return (T)getPrinterIsAcceptingJobs(); } else { return null; } }
public static void main(String[] args) throws Exception { for (PrintService service : PrintServiceLookup.lookupPrintServices(null, null)) { String serviceName = service.getName(); PrinterName name = service.getAttribute(PrinterName.class); String printerName = name.getValue(); PrintService serviceByName = lookupByName(printerName); System.out.println("service " + service); System.out.println("serviceByName " + serviceByName); if (!service.equals(serviceByName)) { throw new RuntimeException("NOK " + serviceName + " expected: " + service.getClass().getName() + " got: " + serviceByName.getClass().getName()); } } System.out.println("Test PASSED"); }
@Override public void create(String filename) { // find the printing service //http://www.coderanch.com/t/385989/java/java/send-raw-data-printer AttributeSet attributeSet = new HashAttributeSet(); attributeSet.add(new PrinterName(filename, null)); PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributeSet); if(services.length > 0){ if(printFile != null){ printFile.deleteOnExit(); printFile = null; } try { printFile = File.createTempFile("printer_"+name,".redirect"); } catch (IOException e) { e.printStackTrace(); } printService = services[0]; } }
/** * Function to print code Zpl to local zebra(usb) * * @param zpl * code Zpl to print * @param ip * ip adress * @param port * port * @throws ZebraPrintException * if zpl could not be printed */ public static void printZpl(String zpl, String printerName) throws ZebraPrintException { try { PrintService psZebra = null; String sPrinterName = null; PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); for (int i = 0; i < services.length; i++) { PrintServiceAttribute attr = services[i].getAttribute(PrinterName.class); sPrinterName = ((PrinterName) attr).getValue(); if (sPrinterName.toLowerCase().indexOf(printerName) >= 0) { psZebra = services[i]; break; } } if (psZebra == null) { throw new ZebraPrintNotFoundException("Zebra printer not found : " + printerName); } DocPrintJob job = psZebra.createPrintJob(); byte[] by = zpl.getBytes(); DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; Doc doc = new SimpleDoc(by, flavor, null); job.print(doc, null); } catch (PrintException e) { throw new ZebraPrintException("Cannot print label on this printer : " + printerName, e); } }
/** * Invoked by the RasterPrintJob super class * this method is called after that last page * has been imaged. */ protected void endDoc() throws PrinterException { if (mPSStream != null) { mPSStream.println(EOF_COMMENT); mPSStream.flush(); if (mDestType != RasterPrinterJob.STREAM) { mPSStream.close(); } } if (mDestType == RasterPrinterJob.PRINTER) { PrintService pServ = getPrintService(); if (pServ != null) { mDestination = pServ.getName(); if (isMac) { PrintServiceAttributeSet psaSet = pServ.getAttributes(); if (psaSet != null) { mDestination = psaSet.get(PrinterName.class).toString() ; } } } PrinterSpooler spooler = new PrinterSpooler(); java.security.AccessController.doPrivileged(spooler); if (spooler.pex != null) { throw spooler.pex; } } }
public <T extends PrintServiceAttribute> T getAttribute(Class<T> category) { if (category == null) { throw new NullPointerException("category"); } if (!(PrintServiceAttribute.class.isAssignableFrom(category))) { throw new IllegalArgumentException("Not a PrintServiceAttribute"); } if (category == ColorSupported.class) { int caps = getPrinterCapabilities(); if ((caps & DEVCAP_COLOR) != 0) { return (T)ColorSupported.SUPPORTED; } else { return (T)ColorSupported.NOT_SUPPORTED; } } else if (category == PrinterName.class) { return (T)getPrinterName(); } else if (category == PrinterState.class) { return (T)getPrinterState(); } else if (category == PrinterStateReasons.class) { return (T)getPrinterStateReasons(); } else if (category == QueuedJobCount.class) { return (T)getQueuedJobCount(); } else if (category == PrinterIsAcceptingJobs.class) { return (T)getPrinterIsAcceptingJobs(); } else { return null; } }
private PrintService getServiceByName(PrinterName nameAttr) { String name = nameAttr.getValue(); if (name == null || name.equals("") || !checkPrinterName(name)) { return null; } /* check if all printers are already available */ if (printServices != null) { for (PrintService printService : printServices) { PrinterName printerName = (PrinterName)printService.getAttribute(PrinterName.class); if (printerName.getValue().equals(name)) { return printService; } } } /* take CUPS into account first */ if (CUPSPrinter.isCupsRunning()) { try { return new IPPPrintService(name, new URL("http://"+ CUPSPrinter.getServer()+":"+ CUPSPrinter.getPort()+"/"+ name)); } catch (Exception e) { IPPPrintService.debug_println(debugPrefix+ " getServiceByName Exception "+ e); } } /* fallback if nothing not having a printer at this point */ PrintService printer = null; if (isMac() || isSysV()) { printer = getNamedPrinterNameSysV(name); } else if (isAIX()) { printer = getNamedPrinterNameAIX(name); } else { printer = getNamedPrinterNameBSD(name); } return printer; }
private static PrintService lookupByName(String name) { AttributeSet attributes = new HashAttributeSet(); attributes.add(new PrinterName(name, null)); for (PrintService service : PrintServiceLookup.lookupPrintServices(null, attributes)) { return service; } return null; }
/** * Invoked by the RasterPrintJob super class * this method is called after that last page * has been imaged. */ protected void endDoc() throws PrinterException { if (mPSStream != null) { mPSStream.println(EOF_COMMENT); mPSStream.flush(); if (mPSStream.checkError()) { abortDoc(); throw new PrinterException("Error while writing to file"); } if (mDestType != RasterPrinterJob.STREAM) { mPSStream.close(); } } if (mDestType == RasterPrinterJob.PRINTER) { PrintService pServ = getPrintService(); if (pServ != null) { mDestination = pServ.getName(); if (isMac) { PrintServiceAttributeSet psaSet = pServ.getAttributes(); if (psaSet != null) { mDestination = psaSet.get(PrinterName.class).toString() ; } } } PrinterSpooler spooler = new PrinterSpooler(); java.security.AccessController.doPrivileged(spooler); if (spooler.pex != null) { throw spooler.pex; } } }
private PrintService getServiceByName(PrinterName nameAttr) { String name = nameAttr.getValue(); if (name == null || name.equals("") || !checkPrinterName(name)) { return null; } /* check if all printers are already available */ if (printServices != null) { for (PrintService printService : printServices) { PrinterName printerName = printService.getAttribute(PrinterName.class); if (printerName.getValue().equals(name)) { return printService; } } } /* take CUPS into account first */ if (CUPSPrinter.isCupsRunning()) { try { return new IPPPrintService(name, new URL("http://"+ CUPSPrinter.getServer()+":"+ CUPSPrinter.getPort()+"/"+ name)); } catch (Exception e) { IPPPrintService.debug_println(debugPrefix+ " getServiceByName Exception "+ e); } } /* fallback if nothing not having a printer at this point */ PrintService printer = null; if (isMac() || isSysV()) { printer = getNamedPrinterNameSysV(name); } else if (isAIX()) { printer = getNamedPrinterNameAIX(name); } else { printer = getNamedPrinterNameBSD(name); } return printer; }
@SuppressWarnings("unchecked") public <T extends PrintServiceAttribute> T getAttribute(Class<T> category) { if (category == null) { throw new NullPointerException("category"); } if (!(PrintServiceAttribute.class.isAssignableFrom(category))) { throw new IllegalArgumentException("Not a PrintServiceAttribute"); } if (category == ColorSupported.class) { int caps = getPrinterCapabilities(); if ((caps & DEVCAP_COLOR) != 0) { return (T)ColorSupported.SUPPORTED; } else { return (T)ColorSupported.NOT_SUPPORTED; } } else if (category == PrinterName.class) { return (T)getPrinterName(); } else if (category == PrinterState.class) { return (T)getPrinterState(); } else if (category == PrinterStateReasons.class) { return (T)getPrinterStateReasons(); } else if (category == QueuedJobCount.class) { return (T)getQueuedJobCount(); } else if (category == PrinterIsAcceptingJobs.class) { return (T)getPrinterIsAcceptingJobs(); } else { return null; } }
public DummyPrintService(String name) { _name = name; _supportedFlavors = new HashSet<DocFlavor>(); _supportedFlavors.add(DocFlavor.SERVICE_FORMATTED.PAGEABLE); _supportedFlavors.add(DocFlavor.SERVICE_FORMATTED.PRINTABLE); _printServiceAttributeSet = new HashPrintServiceAttributeSet(); _printServiceAttributeSet.add(new PrinterName(name, null)); _printServiceAttributeSet.add(PrinterState.IDLE); }