Search in sources :

Example 1 with PrinterName

use of javax.print.attribute.standard.PrinterName in project jdk8u_jdk by JetBrains.

the class UnixPrintServiceLookup method getPrintServices.

private PrintService[] getPrintServices(PrintServiceAttributeSet serviceSet) {
    if (serviceSet == null || serviceSet.isEmpty()) {
        return getPrintServices();
    }
    /* Typically expect that if a service attribute is specified that
         * its a printer name and there ought to be only one match.
         * Directly retrieve that service and confirm
         * that it meets the other requirements.
         * If printer name isn't mentioned then go a slow path checking
         * all printers if they meet the reqiremements.
         */
    PrintService[] services;
    PrinterName name = (PrinterName) serviceSet.get(PrinterName.class);
    PrintService defService;
    if (name != null && (defService = getDefaultPrintService()) != null) {
        /* To avoid execing a unix command  see if the client is asking
             * for the default printer by name, since we already have that
             * initialised.
             */
        PrinterName defName = (PrinterName) defService.getAttribute(PrinterName.class);
        if (defName != null && name.equals(defName)) {
            if (matchesAttributes(defService, serviceSet)) {
                services = new PrintService[1];
                services[0] = defService;
                return services;
            } else {
                return new PrintService[0];
            }
        } else {
            /* Its not the default service */
            PrintService service = getServiceByName(name);
            if (service != null && matchesAttributes(service, serviceSet)) {
                services = new PrintService[1];
                services[0] = service;
                return services;
            } else {
                return new PrintService[0];
            }
        }
    } else {
        /* specified service attributes don't include a name.*/
        Vector matchedServices = new Vector();
        services = getPrintServices();
        for (int i = 0; i < services.length; i++) {
            if (matchesAttributes(services[i], serviceSet)) {
                matchedServices.add(services[i]);
            }
        }
        services = new PrintService[matchedServices.size()];
        for (int i = 0; i < services.length; i++) {
            services[i] = (PrintService) matchedServices.elementAt(i);
        }
        return services;
    }
}
Also used : PrinterName(javax.print.attribute.standard.PrinterName) Vector(java.util.Vector) MultiDocPrintService(javax.print.MultiDocPrintService) PrintService(javax.print.PrintService)

Example 2 with PrinterName

use of javax.print.attribute.standard.PrinterName in project jdk8u_jdk by JetBrains.

the class Win32PrintServiceLookup method getPrintServices.

public PrintService[] getPrintServices(DocFlavor flavor, AttributeSet attributes) {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkPrintJobAccess();
    }
    PrintRequestAttributeSet requestSet = null;
    PrintServiceAttributeSet serviceSet = null;
    if (attributes != null && !attributes.isEmpty()) {
        requestSet = new HashPrintRequestAttributeSet();
        serviceSet = new HashPrintServiceAttributeSet();
        Attribute[] attrs = attributes.toArray();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i] instanceof PrintRequestAttribute) {
                requestSet.add(attrs[i]);
            } else if (attrs[i] instanceof PrintServiceAttribute) {
                serviceSet.add(attrs[i]);
            }
        }
    }
    /*
         * Special case: If client is asking for a particular printer
         * (by name) then we can save time by getting just that service
         * to check against the rest of the specified attributes.
         */
    PrintService[] services = null;
    if (serviceSet != null && serviceSet.get(PrinterName.class) != null) {
        PrinterName name = (PrinterName) serviceSet.get(PrinterName.class);
        PrintService service = getPrintServiceByName(name.getValue());
        if (service == null || !matchingService(service, serviceSet)) {
            services = new PrintService[0];
        } else {
            services = new PrintService[1];
            services[0] = service;
        }
    } else {
        services = getPrintServices();
    }
    if (services.length == 0) {
        return services;
    } else {
        ArrayList matchingServices = new ArrayList();
        for (int i = 0; i < services.length; i++) {
            try {
                if (services[i].getUnsupportedAttributes(flavor, requestSet) == null) {
                    matchingServices.add(services[i]);
                }
            } catch (IllegalArgumentException e) {
            }
        }
        services = new PrintService[matchingServices.size()];
        return (PrintService[]) matchingServices.toArray(services);
    }
}
Also used : PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) Attribute(javax.print.attribute.Attribute) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) ArrayList(java.util.ArrayList) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintService(javax.print.PrintService) MultiDocPrintService(javax.print.MultiDocPrintService) PrinterName(javax.print.attribute.standard.PrinterName) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 3 with PrinterName

use of javax.print.attribute.standard.PrinterName in project jdk8u_jdk by JetBrains.

the class UnixPrintServiceLookup method getServiceByName.

/* On a network with many (hundreds) of network printers, it
     * can save several seconds if you know all you want is a particular
     * printer, to ask for that printer rather than retrieving all printers.
     */
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;
}
Also used : PrinterName(javax.print.attribute.standard.PrinterName) URL(java.net.URL) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) MultiDocPrintService(javax.print.MultiDocPrintService) PrintService(javax.print.PrintService)

Example 4 with PrinterName

use of javax.print.attribute.standard.PrinterName in project jdk8u_jdk by JetBrains.

the class GetPrintServices method lookupByName.

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;
}
Also used : PrinterName(javax.print.attribute.standard.PrinterName) AttributeSet(javax.print.attribute.AttributeSet) HashAttributeSet(javax.print.attribute.HashAttributeSet) HashAttributeSet(javax.print.attribute.HashAttributeSet) PrintService(javax.print.PrintService)

Aggregations

PrintService (javax.print.PrintService)4 PrinterName (javax.print.attribute.standard.PrinterName)4 MultiDocPrintService (javax.print.MultiDocPrintService)3 IOException (java.io.IOException)1 URL (java.net.URL)1 PrivilegedActionException (java.security.PrivilegedActionException)1 ArrayList (java.util.ArrayList)1 Vector (java.util.Vector)1 Attribute (javax.print.attribute.Attribute)1 AttributeSet (javax.print.attribute.AttributeSet)1 HashAttributeSet (javax.print.attribute.HashAttributeSet)1 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)1 HashPrintServiceAttributeSet (javax.print.attribute.HashPrintServiceAttributeSet)1 PrintRequestAttribute (javax.print.attribute.PrintRequestAttribute)1 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)1 PrintServiceAttribute (javax.print.attribute.PrintServiceAttribute)1 PrintServiceAttributeSet (javax.print.attribute.PrintServiceAttributeSet)1