use of javax.print.attribute.PrintServiceAttributeSet in project jdk8u_jdk by JetBrains.
the class PSPrinterJob method endDoc.
/**
* 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;
}
}
}
use of javax.print.attribute.PrintServiceAttributeSet in project jdk8u_jdk by JetBrains.
the class UnixPrintService method getUpdatedAttributes.
public PrintServiceAttributeSet getUpdatedAttributes() {
PrintServiceAttributeSet currSet = getDynamicAttributes();
if (lastSet == null) {
lastSet = currSet;
return AttributeSetUtilities.unmodifiableView(currSet);
} else {
PrintServiceAttributeSet updates = new HashPrintServiceAttributeSet();
Attribute[] attrs = currSet.toArray();
Attribute attr;
for (int i = 0; i < attrs.length; i++) {
attr = attrs[i];
if (!lastSet.containsValue(attr)) {
updates.add(attr);
}
}
lastSet = currSet;
return AttributeSetUtilities.unmodifiableView(updates);
}
}
use of javax.print.attribute.PrintServiceAttributeSet in project jdk8u_jdk by JetBrains.
the class UnixPrintService method getSysVServiceAttributes.
private PrintServiceAttributeSet getSysVServiceAttributes() {
PrintServiceAttributeSet attrs = new HashPrintServiceAttributeSet();
attrs.add(getQueuedJobCountSysV());
attrs.add(getPrinterIsAcceptingJobsSysV());
return attrs;
}
use of javax.print.attribute.PrintServiceAttributeSet in project jdk8u_jdk by JetBrains.
the class UnixPrintServiceLookup method getPrintServices.
/*
* If service attributes are specified then there must be additional
* filtering.
*/
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]);
}
}
}
PrintService[] services = getPrintServices(serviceSet);
if (services.length == 0) {
return services;
}
if (CUPSPrinter.isCupsRunning()) {
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);
} else {
// We only need to compare 1 PrintService because all
// UnixPrintServices are the same anyway. We will not use
// default PrintService because it might be null.
PrintService service = services[0];
if ((flavor == null || service.isDocFlavorSupported(flavor)) && service.getUnsupportedAttributes(flavor, requestSet) == null) {
return services;
} else {
return new PrintService[0];
}
}
}
use of javax.print.attribute.PrintServiceAttributeSet 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();
}
Aggregations