Search in sources :

Example 1 with DocAttributeSet

use of javax.print.attribute.DocAttributeSet in project jdk8u_jdk by JetBrains.

the class Win32PrintJob method initializeAttributeSets.

/* There's some inefficiency here as the job set is created even though
     * it may never be requested.
     */
private synchronized void initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet) {
    reqAttrSet = new HashPrintRequestAttributeSet();
    jobAttrSet = new HashPrintJobAttributeSet();
    Attribute[] attrs;
    if (reqSet != null) {
        reqAttrSet.addAll(reqSet);
        attrs = reqSet.toArray();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i] instanceof PrintJobAttribute) {
                jobAttrSet.add(attrs[i]);
            }
        }
    }
    DocAttributeSet docSet = doc.getAttributes();
    if (docSet != null) {
        attrs = docSet.toArray();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i] instanceof PrintRequestAttribute) {
                reqAttrSet.add(attrs[i]);
            }
            if (attrs[i] instanceof PrintJobAttribute) {
                jobAttrSet.add(attrs[i]);
            }
        }
    }
    /* add the user name to the job */
    String userName = "";
    try {
        userName = System.getProperty("user.name");
    } catch (SecurityException se) {
    }
    if (userName == null || userName.equals("")) {
        RequestingUserName ruName = (RequestingUserName) reqSet.get(RequestingUserName.class);
        if (ruName != null) {
            jobAttrSet.add(new JobOriginatingUserName(ruName.getValue(), ruName.getLocale()));
        } else {
            jobAttrSet.add(new JobOriginatingUserName("", null));
        }
    } else {
        jobAttrSet.add(new JobOriginatingUserName(userName, null));
    }
    /* if no job name supplied use doc name (if supplied), if none and
         * its a URL use that, else finally anything .. */
    if (jobAttrSet.get(JobName.class) == null) {
        JobName jobName;
        if (docSet != null && docSet.get(DocumentName.class) != null) {
            DocumentName docName = (DocumentName) docSet.get(DocumentName.class);
            jobName = new JobName(docName.getValue(), docName.getLocale());
            jobAttrSet.add(jobName);
        } else {
            String str = "JPS Job:" + doc;
            try {
                Object printData = doc.getPrintData();
                if (printData instanceof URL) {
                    str = ((URL) (doc.getPrintData())).toString();
                }
            } catch (IOException e) {
            }
            jobName = new JobName(str, null);
            jobAttrSet.add(jobName);
        }
    }
    jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
}
Also used : PrintJobAttribute(javax.print.attribute.PrintJobAttribute) PrintJobAttribute(javax.print.attribute.PrintJobAttribute) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) Attribute(javax.print.attribute.Attribute) HashPrintJobAttributeSet(javax.print.attribute.HashPrintJobAttributeSet) JobName(javax.print.attribute.standard.JobName) IOException(java.io.IOException) java.awt.print(java.awt.print) URL(java.net.URL) DocAttributeSet(javax.print.attribute.DocAttributeSet) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) RequestingUserName(javax.print.attribute.standard.RequestingUserName) JobOriginatingUserName(javax.print.attribute.standard.JobOriginatingUserName) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) DocumentName(javax.print.attribute.standard.DocumentName)

Example 2 with DocAttributeSet

use of javax.print.attribute.DocAttributeSet in project jdk8u_jdk by JetBrains.

the class PSStreamPrintJob method initializeAttributeSets.

/* There's some inefficiency here as the job set is created even though
     * it may never be requested.
     */
private synchronized void initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet) {
    reqAttrSet = new HashPrintRequestAttributeSet();
    jobAttrSet = new HashPrintJobAttributeSet();
    Attribute[] attrs;
    if (reqSet != null) {
        reqAttrSet.addAll(reqSet);
        attrs = reqSet.toArray();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i] instanceof PrintJobAttribute) {
                jobAttrSet.add(attrs[i]);
            }
        }
    }
    DocAttributeSet docSet = doc.getAttributes();
    if (docSet != null) {
        attrs = docSet.toArray();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i] instanceof PrintRequestAttribute) {
                reqAttrSet.add(attrs[i]);
            }
            if (attrs[i] instanceof PrintJobAttribute) {
                jobAttrSet.add(attrs[i]);
            }
        }
    }
    /* add the user name to the job */
    String userName = "";
    try {
        userName = System.getProperty("user.name");
    } catch (SecurityException se) {
    }
    if (userName == null || userName.equals("")) {
        RequestingUserName ruName = (RequestingUserName) reqSet.get(RequestingUserName.class);
        if (ruName != null) {
            jobAttrSet.add(new JobOriginatingUserName(ruName.getValue(), ruName.getLocale()));
        } else {
            jobAttrSet.add(new JobOriginatingUserName("", null));
        }
    } else {
        jobAttrSet.add(new JobOriginatingUserName(userName, null));
    }
    /* if no job name supplied use doc name (if supplied), if none and
         * its a URL use that, else finally anything .. */
    if (jobAttrSet.get(JobName.class) == null) {
        JobName jobName;
        if (docSet != null && docSet.get(DocumentName.class) != null) {
            DocumentName docName = (DocumentName) docSet.get(DocumentName.class);
            jobName = new JobName(docName.getValue(), docName.getLocale());
            jobAttrSet.add(jobName);
        } else {
            String str = "JPS Job:" + doc;
            try {
                Object printData = doc.getPrintData();
                if (printData instanceof URL) {
                    str = ((URL) (doc.getPrintData())).toString();
                }
            } catch (IOException e) {
            }
            jobName = new JobName(str, null);
            jobAttrSet.add(jobName);
        }
    }
    jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
}
Also used : PrintJobAttribute(javax.print.attribute.PrintJobAttribute) PrintJobAttribute(javax.print.attribute.PrintJobAttribute) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) Attribute(javax.print.attribute.Attribute) HashPrintJobAttributeSet(javax.print.attribute.HashPrintJobAttributeSet) JobName(javax.print.attribute.standard.JobName) IOException(java.io.IOException) java.awt.print(java.awt.print) URL(java.net.URL) DocAttributeSet(javax.print.attribute.DocAttributeSet) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) RequestingUserName(javax.print.attribute.standard.RequestingUserName) JobOriginatingUserName(javax.print.attribute.standard.JobOriginatingUserName) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) DocumentName(javax.print.attribute.standard.DocumentName)

Example 3 with DocAttributeSet

use of javax.print.attribute.DocAttributeSet in project jdk8u_jdk by JetBrains.

the class UnixPrintJob method initializeAttributeSets.

/* There's some inefficiency here as the job set is created even though
     * it may never be requested.
     */
private synchronized void initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet) {
    reqAttrSet = new HashPrintRequestAttributeSet();
    jobAttrSet = new HashPrintJobAttributeSet();
    Attribute[] attrs;
    if (reqSet != null) {
        reqAttrSet.addAll(reqSet);
        attrs = reqSet.toArray();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i] instanceof PrintJobAttribute) {
                jobAttrSet.add(attrs[i]);
            }
        }
    }
    DocAttributeSet docSet = doc.getAttributes();
    if (docSet != null) {
        attrs = docSet.toArray();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i] instanceof PrintRequestAttribute) {
                reqAttrSet.add(attrs[i]);
            }
            if (attrs[i] instanceof PrintJobAttribute) {
                jobAttrSet.add(attrs[i]);
            }
        }
    }
    /* add the user name to the job */
    String userName = "";
    try {
        userName = System.getProperty("user.name");
    } catch (SecurityException se) {
    }
    if (userName == null || userName.equals("")) {
        RequestingUserName ruName = (RequestingUserName) reqSet.get(RequestingUserName.class);
        if (ruName != null) {
            jobAttrSet.add(new JobOriginatingUserName(ruName.getValue(), ruName.getLocale()));
        } else {
            jobAttrSet.add(new JobOriginatingUserName("", null));
        }
    } else {
        jobAttrSet.add(new JobOriginatingUserName(userName, null));
    }
    /* if no job name supplied use doc name (if supplied), if none and
         * its a URL use that, else finally anything .. */
    if (jobAttrSet.get(JobName.class) == null) {
        JobName jobName;
        if (docSet != null && docSet.get(DocumentName.class) != null) {
            DocumentName docName = (DocumentName) docSet.get(DocumentName.class);
            jobName = new JobName(docName.getValue(), docName.getLocale());
            jobAttrSet.add(jobName);
        } else {
            String str = "JPS Job:" + doc;
            try {
                Object printData = doc.getPrintData();
                if (printData instanceof URL) {
                    str = ((URL) (doc.getPrintData())).toString();
                }
            } catch (IOException e) {
            }
            jobName = new JobName(str, null);
            jobAttrSet.add(jobName);
        }
    }
    jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
}
Also used : PrintJobAttribute(javax.print.attribute.PrintJobAttribute) PrintJobAttribute(javax.print.attribute.PrintJobAttribute) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) Attribute(javax.print.attribute.Attribute) HashPrintJobAttributeSet(javax.print.attribute.HashPrintJobAttributeSet) JobName(javax.print.attribute.standard.JobName) IOException(java.io.IOException) java.awt.print(java.awt.print) URL(java.net.URL) DocAttributeSet(javax.print.attribute.DocAttributeSet) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) RequestingUserName(javax.print.attribute.standard.RequestingUserName) JobOriginatingUserName(javax.print.attribute.standard.JobOriginatingUserName) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) DocumentName(javax.print.attribute.standard.DocumentName)

Example 4 with DocAttributeSet

use of javax.print.attribute.DocAttributeSet in project ofbiz-framework by apache.

the class OutputServices method sendPrintFromScreen.

public static Map<String, Object> sendPrintFromScreen(DispatchContext dctx, Map<String, ? extends Object> serviceContext) {
    Locale locale = (Locale) serviceContext.get("locale");
    VisualTheme visualTheme = (VisualTheme) serviceContext.get("visualTheme");
    String screenLocation = (String) serviceContext.remove("screenLocation");
    Map<String, Object> screenContext = UtilGenerics.checkMap(serviceContext.remove("screenContext"));
    String contentType = (String) serviceContext.remove("contentType");
    String printerContentType = (String) serviceContext.remove("printerContentType");
    if (UtilValidate.isEmpty(screenContext)) {
        screenContext = new HashMap<>();
    }
    screenContext.put("locale", locale);
    if (UtilValidate.isEmpty(contentType)) {
        contentType = "application/postscript";
    }
    if (UtilValidate.isEmpty(printerContentType)) {
        printerContentType = contentType;
    }
    try {
        MapStack<String> screenContextTmp = MapStack.create();
        screenContextTmp.put("locale", locale);
        Writer writer = new StringWriter();
        // substitute the freemarker variables...
        ScreenStringRenderer foScreenStringRenderer = new MacroScreenRenderer(visualTheme.getModelTheme().getType("screenfop"), visualTheme.getModelTheme().getScreenRendererLocation("screenfop"));
        ScreenRenderer screensAtt = new ScreenRenderer(writer, screenContextTmp, foScreenStringRenderer);
        screensAtt.populateContextForService(dctx, screenContext);
        screenContextTmp.putAll(screenContext);
        screensAtt.getContext().put("formStringRenderer", foFormRenderer);
        screensAtt.render(screenLocation);
        // create the input stream for the generation
        StreamSource src = new StreamSource(new StringReader(writer.toString()));
        // create the output stream for the generation
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Fop fop = ApacheFopWorker.createFopInstance(baos, MimeConstants.MIME_PDF);
        ApacheFopWorker.transform(src, null, fop);
        baos.flush();
        baos.close();
        // Print is sent
        DocFlavor psInFormat = new DocFlavor.INPUT_STREAM(printerContentType);
        InputStream bais = new ByteArrayInputStream(baos.toByteArray());
        DocAttributeSet docAttributeSet = new HashDocAttributeSet();
        List<Object> docAttributes = UtilGenerics.checkList(serviceContext.remove("docAttributes"));
        if (UtilValidate.isNotEmpty(docAttributes)) {
            for (Object da : docAttributes) {
                Debug.logInfo("Adding DocAttribute: " + da, module);
                docAttributeSet.add((DocAttribute) da);
            }
        }
        Doc myDoc = new SimpleDoc(bais, psInFormat, docAttributeSet);
        PrintService printer = null;
        // lookup the print service for the supplied printer name
        String printerName = (String) serviceContext.remove("printerName");
        if (UtilValidate.isNotEmpty(printerName)) {
            PrintServiceAttributeSet printServiceAttributes = new HashPrintServiceAttributeSet();
            printServiceAttributes.add(new PrinterName(printerName, locale));
            PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, printServiceAttributes);
            if (printServices.length > 0) {
                printer = printServices[0];
                Debug.logInfo("Using printer: " + printer.getName(), module);
                if (!printer.isDocFlavorSupported(psInFormat)) {
                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentPrinterNotSupportDocFlavorFormat", UtilMisc.toMap("psInFormat", psInFormat, "printerName", printer.getName()), locale));
                }
            }
            if (printer == null) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentPrinterNotFound", UtilMisc.toMap("printerName", printerName), locale));
            }
        } else {
            // if no printer name was supplied, try to get the default printer
            printer = PrintServiceLookup.lookupDefaultPrintService();
            if (printer != null) {
                Debug.logInfo("No printer name supplied, using default printer: " + printer.getName(), module);
            }
        }
        if (printer == null) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentPrinterNotAvailable", locale));
        }
        PrintRequestAttributeSet praset = new HashPrintRequestAttributeSet();
        List<Object> printRequestAttributes = UtilGenerics.checkList(serviceContext.remove("printRequestAttributes"));
        if (UtilValidate.isNotEmpty(printRequestAttributes)) {
            for (Object pra : printRequestAttributes) {
                Debug.logInfo("Adding PrintRequestAttribute: " + pra, module);
                praset.add((PrintRequestAttribute) pra);
            }
        }
        DocPrintJob job = printer.createPrintJob();
        job.print(myDoc, praset);
    } catch (PrintException | IOException | TemplateException | GeneralException | SAXException | ParserConfigurationException e) {
        Debug.logError(e, "Error rendering [" + contentType + "]: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentRenderingError", UtilMisc.toMap("contentType", contentType, "errorString", e.toString()), locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) ScreenRenderer(org.apache.ofbiz.widget.renderer.ScreenRenderer) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) HashDocAttributeSet(javax.print.attribute.HashDocAttributeSet) DocAttributeSet(javax.print.attribute.DocAttributeSet) PrintService(javax.print.PrintService) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) SAXException(org.xml.sax.SAXException) PrintException(javax.print.PrintException) StringWriter(java.io.StringWriter) SimpleDoc(javax.print.SimpleDoc) StringReader(java.io.StringReader) Doc(javax.print.Doc) SimpleDoc(javax.print.SimpleDoc) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GeneralException(org.apache.ofbiz.base.util.GeneralException) HashDocAttributeSet(javax.print.attribute.HashDocAttributeSet) TemplateException(freemarker.template.TemplateException) Fop(org.apache.fop.apps.Fop) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DocPrintJob(javax.print.DocPrintJob) IOException(java.io.IOException) ScreenStringRenderer(org.apache.ofbiz.widget.renderer.ScreenStringRenderer) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) PrinterName(javax.print.attribute.standard.PrinterName) ByteArrayInputStream(java.io.ByteArrayInputStream) VisualTheme(org.apache.ofbiz.widget.renderer.VisualTheme) DocFlavor(javax.print.DocFlavor) Writer(java.io.Writer) StringWriter(java.io.StringWriter) PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Aggregations

IOException (java.io.IOException)4 DocAttributeSet (javax.print.attribute.DocAttributeSet)4 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)4 java.awt.print (java.awt.print)3 URL (java.net.URL)3 Attribute (javax.print.attribute.Attribute)3 HashPrintJobAttributeSet (javax.print.attribute.HashPrintJobAttributeSet)3 PrintJobAttribute (javax.print.attribute.PrintJobAttribute)3 PrintRequestAttribute (javax.print.attribute.PrintRequestAttribute)3 DocumentName (javax.print.attribute.standard.DocumentName)3 JobName (javax.print.attribute.standard.JobName)3 JobOriginatingUserName (javax.print.attribute.standard.JobOriginatingUserName)3 RequestingUserName (javax.print.attribute.standard.RequestingUserName)3 TemplateException (freemarker.template.TemplateException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1