package com.pisa.util;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Igor A. Pyankov
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Locale;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.PrintException;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.AttributeSetUtilities;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.HashPrintServiceAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.PrintServiceAttribute;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.Destination;
import javax.print.attribute.standard.DocumentName;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.RequestingUserName;
import org.apache.harmony.x.print.PrintClient;
import org.apache.harmony.x.print.ipp.IppAttribute;
import org.apache.harmony.x.print.ipp.IppAttributeGroup;
import org.apache.harmony.x.print.ipp.IppAttributeGroupSet;
import org.apache.harmony.x.print.ipp.IppDocument;
import org.apache.harmony.x.print.ipp.IppPrinter;
import org.apache.harmony.x.print.ipp.IppResponse;
import org.apache.harmony.x.print.ipp.util.Ipp2Java;
/*
* CUPSClient is a print client based on CUPS protocol.
* (see Common UNIX Printing System, http://www.cups.org/)
*
* The CUPS itself extends IPP protocol
* (see Internet Printing Protocol, http://www.pwg.org/ipp/index.html)
*
* So, this class supports as CUPS as IPP print servers
*
* The class uses special IPP package org.apache.harmony.x.print.ipp for
* ipp/cups specific operations.
*
* CUPSClient implements PrintClient interface, therefore
* see PrintClient.java for more information.
*
*
*/
/**
* @author skanion
*
*/
class CUPSClient implements PrintClient {
// for debug
/**
*
*/
private static int verbose = 0;
/**
*
*/
private IppPrinter printer;
/**
*
*/
private URI printeruri;
/**
*
*/
private PrintServiceAttributeSet attributeset;
/**
*
*/
private DocFlavor[] supportedFlavors = null;
/**
* @param name d
* @throws PrintException d
*/
CUPSClient(String name) throws PrintException {
try {
this .printeruri = new URI(name);
this .printer = new IppPrinter(printeruri);
this .attributeset = new HashPrintServiceAttributeSet();
} catch (Exception e) {
throw new PrintException(e);
}
}
/** (non-Javadoc)
* @see org.apache.harmony.x.print.PrintClient#getSupportedDocFlavors()
*/
/*
* SPECIAL - supportedFlavors is global for performance
* but it can be set local for dynamic
*
* @org.apache.harmony.x.print.PrintClient#getSupportedDocFlavors()
*/
public DocFlavor[] getSupportedDocFlavors() {
if (supportedFlavors == null) {
ArrayList df = new ArrayList();
try {
String[] mimetypes = new String[ALLDOCFLAVORS.length];
String[] validmimes;
for (int i = 0, ii = ALLDOCFLAVORS.length; i < ii; i++) {
mimetypes[i] = ALLDOCFLAVORS[i].getMimeType();
}
validmimes = printer
.requestGetSupportedMimeTypes(mimetypes);
for (int i = 0, ii = ALLDOCFLAVORS.length; i < ii; i++) {
if (validmimes[i] != null) {
if (validmimes[i].equals("application/ps")) {
/*
* SPECIAL processing application/ps
*/
df.add(ipp2java(ALLDOCFLAVORS[i]));
} else {
df.add(ALLDOCFLAVORS[i]);
}
}
}
} catch (Exception e) {
// IGNORE exception
e.printStackTrace();
}
supportedFlavors = (df.size() == 0 ? new DocFlavor[] { DocFlavor.INPUT_STREAM.AUTOSENSE }
: (DocFlavor[]) df.toArray(new DocFlavor[0]));
}
return supportedFlavors;
}
/**
* @see org.apache.harmony.x.print.PrintClient#getAttributes()
*/
public PrintServiceAttributeSet getAttributes() {
synchronized (this ) {
try {
IppResponse response;
IppAttributeGroup agroup;
IppAttribute attr;
Object[] attrx = new Object[0];
response = printer
.requestPrinterDescriptionAttributes();
agroup = response
.getGroup(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
if (agroup != null) {
attributeset.clear();
for (int i = 0, ii = agroup.size(); i < ii; i++) {
attr = (IppAttribute) agroup.get(i);
attrx = Ipp2Java.getJavaByIpp(attr);
for (int j = 0, jj = attrx.length; j < jj; j++) {
if (attrx[j] instanceof PrintServiceAttribute) {
attributeset.add((Attribute) attrx[j]);
}
}
}
}
} catch (Exception e) {
// IGNORE exception
e.printStackTrace();
}
}
return AttributeSetUtilities.unmodifiableView(attributeset);
}
/** (non-Javadoc)
* @see org.apache.harmony.x.print.PrintClient#getSupportedAttributeCategories()
*/
/*
* @return f
* @see org.apache.harmony.x.PrintClient#getSupportedAttributeCategories()
*/
public Class[] getSupportedAttributeCategories() {
ArrayList clazz = new ArrayList();
try {
IppResponse response = printer.requestPrinterAttributes();
IppAttributeGroup agroup = response
.getGroup(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
String aname;
Class claz;
IppAttribute attr;
if (agroup != null) {
for (int i = 0, ii = agroup.size(); i < ii; i++) {
attr = (IppAttribute) agroup.get(i);
aname = new String(attr.getName());
if (aname.indexOf("-supported") > 0) {
claz = Ipp2Java
.getClassByIppAttributeName(aname
.substring(0, aname
.indexOf("-supported")));
if (claz != null) {
clazz.add(claz);
}
}
}
}
// SPECIAL attributes processing
getSupportedAttributeCategoriesEx(clazz);
} catch (Exception e) {
// IGNORE exception
// e.printStackTrace();
}
return (clazz.size() == 0 ? new Class[0] : (Class[]) clazz
.toArray(new Class[0]));
}
/**
* @param clazz fd
*/
private void getSupportedAttributeCategoriesEx(ArrayList clazz) {
if (!clazz.contains(Destination.class)) {
clazz.add(Destination.class);
}
if (!clazz.contains(RequestingUserName.class)) {
clazz.add(RequestingUserName.class);
}
if (!clazz.contains(JobName.class)) {
clazz.add(JobName.class);
}
if (!clazz.contains(DocumentName.class)) {
clazz.add(DocumentName.class);
}
}
/** (non-Javadoc)
* @see org.apache.harmony.x.print.PrintClient#getDefaultAttributeValue(java.lang.Class)
*/
/*
* @see org.apache.harmony.x.print.PrintClient#getDefaultAttributeValue(java.lang.Class)
*/
public Object getDefaultAttributeValue(Class category) {
if (category == null) {
throw new NullPointerException("Argument is null");
}
if (!(Attribute.class.isAssignableFrom(category))) {
throw new IllegalArgumentException(
"Argument must implement interface Attribute");
}
Object defval[] = null;
// SPECIAL attributes processing
defval = getDefaultAttributeValueEx(category);
if (defval != null) {
if (defval.length == 0) {
return null;
}
return defval[0];
}
if (Media.class.isAssignableFrom(category)) {
category = Media.class;
}
try {
IppResponse response = printer.requestPrinterAttributes();
IppAttributeGroup agroup = response
.getGroup(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
IppAttribute attr;
String aname;
int andex;
if (agroup != null) {
aname = Ipp2Java.getIppAttributeNameByClass(category);
if (aname != null) {
if (aname.endsWith("-supported")) {
aname = aname.substring(0, aname
.indexOf("-supported"));
}
if (aname.endsWith("-default")) {
aname = aname.substring(0, aname
.indexOf("-default"));
}
andex = agroup.findAttribute(aname + "-default");
if (andex >= 0) {
attr = (IppAttribute) agroup.get(andex);
defval = Ipp2Java.getJavaByIpp(attr);
}
}
}
} catch (Exception e) {
// IGNORE exception
e.printStackTrace();
}
return (defval != null && defval.length > 0 ? defval[0] : null);
}
/**
* If attribute was processed - return Object[1]
* Else - return null
* @param category f
* @return f
*/
private Object[] getDefaultAttributeValueEx(Class category) {
if (Destination.class.isAssignableFrom(category)) {
return new Object[0];
} else if (RequestingUserName.class.isAssignableFrom(category)) {
return new Object[] { new RequestingUserName(
(String) AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
return System
.getProperty("user.name");
}
}), Locale.US) };
} else if (JobName.class.isAssignableFrom(category)) {
return new Object[] { new JobName("Java print job",
Locale.US) };
} else if (DocumentName.class.isAssignableFrom(category)) {
return new Object[] { new DocumentName(
"Java print document", Locale.US) };
}
return null;
}
/**
* @see org.apache.harmony.x.print.PrintClient#isAttributeValueSupported(javax.print.attribute.Attribute,
* javax.print.DocFlavor, javax.print.attribute.AttributeSet)
*/
public boolean isAttributeValueSupported(Attribute attribute,
DocFlavor flavor, AttributeSet attributes) {
// verify parameters
if (attribute == null) {
throw new NullPointerException("Argument is null");
}
if (flavor != null && !isDocFlavorSupported(flavor)) {
throw new IllegalArgumentException("DocFlavor '" + flavor
+ "' is not supported by the print service");
}
// SPECIAL attributes processing
boolean[] supportedEx = isAttributeValueSupportedEx(attribute,
flavor);
if (supportedEx != null) {
return supportedEx[0];
}
boolean supported = false;
try {
IppDocument document;
IppResponse response;
IppAttributeGroup agroup;
IppAttributeGroupSet agroupset;
Attribute[] attrs;
String mime = null;
String aname;
aname = Ipp2Java.getIppAttributeNameByClass(attribute
.getClass(), -1);
if (aname == null) {
return false;
}
if (flavor == null) {
mime = "application/octet-stream";
} else {
mime = java2ipp(flavor).getMimeType();
}
if (attributes == null || attributes.isEmpty()) {
document = new IppDocument("Qwerty", mime, "");
agroupset = new IppAttributeGroupSet();
agroupset.setAttribute(aname, Ipp2Java
.getIppByJava(attribute));
response = printer.requestValidateJob(aname, document,
agroupset);
agroup = response
.getGroup(IppAttributeGroup.TAG_UNSUPPORTED_ATTRIBUTES);
if (agroup == null) {
supported = true;
} else if (agroup != null
&& agroup.findAttribute(aname) < 0) {
supported = true;
}
} else {
document = new IppDocument("Qwerty", mime, "");
agroupset = new IppAttributeGroupSet();
agroupset.setAttribute(aname, Ipp2Java
.getIppByJava(attribute));
attrs = attributes.toArray();
for (int i = 0, ii = attrs.length; i < ii; i++) {
agroupset.setAttribute(Ipp2Java
.getIppAttributeNameByClass(attrs[i]
.getClass(), -1), Ipp2Java
.getIppByJava(attrs[i]));
}
response = printer.requestValidateJob(aname, document,
agroupset);
agroup = response
.getGroup(IppAttributeGroup.TAG_UNSUPPORTED_ATTRIBUTES);
if (agroup == null) {
supported = true;
} else if (agroup != null
&& agroup.findAttribute(aname) < 0) {
supported = true;
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return supported;
}
/**
* @param avalue f
* @param flavor f
* @return f
*/
/*
* If attribute was processed - return boolean[1]
* Else return null
*/
private boolean[] isAttributeValueSupportedEx(Attribute avalue,
DocFlavor flavor) {
if (Destination.class.isAssignableFrom(avalue.getCategory())) {
String ms = (flavor != null ? flavor.getMediaSubtype() : "");
Class cls = (flavor != null ? flavor.getClass() : null);
if (ms.equalsIgnoreCase("gif")
|| ms.equalsIgnoreCase("jpeg")
|| ms.equalsIgnoreCase("png")
|| ms.equalsIgnoreCase("postscript")
|| flavor == null
|| cls == DocFlavor.SERVICE_FORMATTED.class) {
if (!canPrintToFile()) {
return new boolean[] { false };
}
URI uri = ((Destination) avalue).getURI();
try {
File file = new File(uri);
if (file.isFile()) {
if (file.canWrite()) {
return new boolean[] { true };
}
return new boolean[] { false };
}
String path = file.getParent();
File parent = new File(path);
if (parent.isDirectory()) {
if (parent.canWrite()) {
return new boolean[] { true };
}
return new boolean[] { false };
}
} catch (Exception e) {
return new boolean[] { false };
}
}
}
return null;
}
/** (non-Javadoc)
* @see org.apache.harmony.x.print.PrintClient#getSupportedAttributeValues(java.lang.Class, javax.print.DocFlavor, javax.print.attribute.AttributeSet)
*/
/*
* @see org.apache.harmony.x.print.PrintClient#getSupportedAttributeValues(java.lang.Class,
* javax.print.DocFlavor, javax.print.attribute.AttributeSet)
*/
public Object getSupportedAttributeValues(Class category,
DocFlavor flavor, AttributeSet attributes) {
if (category == null) {
throw new NullPointerException("Argument is null");
}
if (!(Attribute.class.isAssignableFrom(category))) {
throw new IllegalArgumentException(
"Argument must implement interface Attribute");
}
if (flavor != null && !isDocFlavorSupported(flavor)) {
throw new IllegalArgumentException("DocFlavor '" + flavor
+ "' is not supported by the print service");
}
Object vals = null;
// SPECIAL attributes processing
vals = getSupportedAttributeValuesEx(category, flavor);
if (vals != null) {
if (((Object[]) vals).length == 0) {
return null;
}
return ((Object[]) vals)[0];
}
// General attributes
try {
String aname = Ipp2Java.getIppAttributeNameByClass(
category, 0)
+ "-supported";
doVerbose(2,
"CUPSClient.java: getSupportedAttributeValues(): ipp attribute: "
+ aname);
IppResponse response = printer.requestPrinterAttributes(
aname, (flavor == null ? null : java2ipp(flavor)
.getMimeType()));
doVerbose(2,
"CUPSClient.java: getSupportedAttributeValues(): response: "
+ response.toString());
IppAttributeGroup agroup = response
.getGroup(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
doVerbose(1,
"CUPSClient.java: getSupportedAttributeValues(): agroup: "
+ agroup.toString());
if (agroup != null) {
int aind = agroup.findAttribute(aname);
if (aind >= 0) {
IppAttribute attr = (IppAttribute) agroup.get(aind);
vals = Ipp2Java.getJavaByIpp(attr);
}
}
doVerbose(1,
"CUPSClient.java: getSupportedAttributeValues(): 1");
// Make right type/value for return
if (vals != null && vals.getClass().isArray()) {
Object[] ara = (Object[]) vals;
if (ara.length == 1 && ara[0].getClass() != category) {
vals = ara[0];
}
}
doVerbose(1,
"CUPSClient.java: getSupportedAttributeValues(): 2");
if (vals != null && vals.getClass().isArray()) {
int asize = ((Object[]) vals).length;
if (asize > 0) {
Class c = ((Object[]) vals)[0].getClass();
/* SPECIAL case for Media* attributes
*
* Special case for Media* attributes.
* vals[] contains all type of Media classes
* So, c must be Media type, not a[0] type
*/
if (Media.class.isAssignableFrom(c)) {
c = Media.class;
}
doVerbose(1,
"CUPSClient.java: getSupportedAttributeValues(): 3");
Object[] a = (Object[]) Array.newInstance(c, asize);
System.arraycopy(vals, 0, a, 0, a.length);
vals = a;
} else {
vals = null;
}
}
doVerbose(1,
"CUPSClient.java: getSupportedAttributeValues(): 4");
if (vals != null && vals.getClass().isArray()) {
for (int i = 0, ii = ((Attribute[]) vals).length; i < ii; i++) {
if (!isAttributeValueSupported(
((Attribute[]) vals)[i], flavor, attributes)) {
((Attribute[]) vals)[i] = null;
}
}
doVerbose(1,
"CUPSClient.java: getSupportedAttributeValues(): 5");
int newvalslength = 0;
for (int i = 0, ii = ((Attribute[]) vals).length; i < ii; i++) {
if (((Attribute[]) vals)[i] != null) {
newvalslength++;
}
}
doVerbose(1,
"CUPSClient.java: getSupportedAttributeValues(): 6");
if (newvalslength != ((Attribute[]) vals).length) {
Object[] newvals = new Object[newvalslength];
for (int j = 0, i = 0, ii = ((Attribute[]) vals).length; i < ii; i++) {
if (((Attribute[]) vals)[i] != null) {
newvals[j++] = ((Attribute[]) vals)[i];
}
}
vals = newvals;
}
} else if (vals != null) {
if (!isAttributeValueSupported((Attribute) vals,
flavor, attributes)) {
vals = null;
}
}
doVerbose(1,
"CUPSClient.java: getSupportedAttributeValues(): 7");
return vals;
} catch (Exception e) {
// IGNORE exception
e.printStackTrace();
}
doVerbose(1,
"CUPSClient.java: getSupportedAttributeValues(): 8");
return null;
}
/**
* @param category f
* @param flavor f
* @return f
*/
/*
* If category processed - return non-null value
*/
private Object[] getSupportedAttributeValuesEx(Class category,
DocFlavor flavor) {
if (Destination.class.isAssignableFrom(category)) {
String ms = flavor.getMediaSubtype();
if (ms.equalsIgnoreCase("gif")
|| ms.equalsIgnoreCase("jpeg")
|| ms.equalsIgnoreCase("png")
|| ms.equalsIgnoreCase("postscript")
|| flavor.getClass() == DocFlavor.SERVICE_FORMATTED.class) {
try {
return new Object[] { new Destination(new URI(
"file:///foo/bar")) };
} catch (URISyntaxException e) {
// return empty array - values are not supported
return new Object[0];
}
}
} else if (RequestingUserName.class.isAssignableFrom(category)) {
return new Object[] { new RequestingUserName("I.A.Muser",
Locale.US) };
} else if (JobName.class.isAssignableFrom(category)) {
return new Object[] { new JobName("Foo print job",
Locale.US) };
} else if (DocumentName.class.isAssignableFrom(category)) {
return new Object[] { new DocumentName("Foo document",
Locale.US) };
}
return null;
}
/** (non-Javadoc)
* @see org.apache.harmony.x.print.PrintClient#print(javax.print.Doc, javax.print.attribute.PrintRequestAttributeSet)
*/
/*
* @see org.apache.harmony.x.print.PrintClient#print(javax.print.Doc,
* javax.print.attribute.PrintRequestAttributeSet)
*/
public void print(Doc doc, PrintRequestAttributeSet attributes)
throws PrintException {
synchronized (this ) {
doVerbose(1, "Print " + doc.toString());
try {
DocFlavor df = doc.getDocFlavor();
if (!(df instanceof DocFlavor.INPUT_STREAM
|| df instanceof DocFlavor.BYTE_ARRAY
|| df instanceof DocFlavor.CHAR_ARRAY
|| df instanceof DocFlavor.STRING
|| df instanceof DocFlavor.READER || df instanceof DocFlavor.URL)) {
throw new PrintException("Doc flavor "
+ df.getRepresentationClassName()
+ " is not supported yet");
}
HashAttributeSet as = new HashAttributeSet();
DocAttributeSet das;
das = doc.getAttributes();
// construct attributes
if (das != null) {
as.addAll(das);
}
if (attributes != null) {
as.addAll(attributes);
}
as.addAll(attributeset);
// print
if (as.containsKey(Destination.class)) {
print2destination(doc, (Destination) as
.get(Destination.class));
} else {
printsimple(doc, as);
}
} catch (PrintException e) {
throw e;
} catch (Exception e) {
throw new PrintException(e);
}
}
}
/**
* @param doc d
* @param destination d
* @throws PrintException d
*/
/*
* printing to Destination
*/
private void print2destination(Doc doc, Destination destination)
throws PrintException {
try {
DataOutputStream bw = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(
new File(destination.getURI()))));
if (doc != null) {
if (doc.getDocFlavor() instanceof DocFlavor.INPUT_STREAM) {
InputStream stream = (InputStream) doc
.getPrintData();
byte[] buf = new byte[1024 * 8];
int count = 0;
while ((count = stream.read(buf, 0, buf.length)) != -1) {
bw.write(buf, 0, count);
}
stream.close();
} else if (doc.getDocFlavor() instanceof DocFlavor.URL) {
BufferedInputStream stream = new BufferedInputStream(
((URL) doc.getPrintData()).openStream());
byte[] buf = new byte[1024 * 8];
int count = 0;
while ((count = stream.read(buf, 0, buf.length)) != -1) {
if (count > 0) {
bw.write(buf, 0, count);
}
}
stream.close();
} else if (doc.getDocFlavor() instanceof DocFlavor.BYTE_ARRAY) {
InputStream stream = new ByteArrayInputStream(
(byte[]) doc.getPrintData());
byte[] buf = new byte[1024 * 8];
int count = 0;
while ((count = stream.read(buf, 0, buf.length)) != -1) {
bw.write(buf, 0, count);
}
stream.close();
} else if (doc.getDocFlavor() instanceof DocFlavor.SERVICE_FORMATTED) {
// TODO - print DocFlavor.SERVICE_FORMATTED
}
}
bw.flush();
bw.close();
} catch (Exception e) {
throw new PrintException(e);
}
}
/**
* @param doc d
* @param as d
* @throws PrintException d
*/
/*
* request IppPrinter printer to print document
*/
private void printsimple(Doc doc, HashAttributeSet as)
throws PrintException {
IppDocument document;
IppResponse response;
IppAttributeGroupSet agroupset;
Attribute[] attrs;
DocFlavor df = doc.getDocFlavor();
String docname = doc.toString();
try {
document = new IppDocument(docname, java2ipp(df)
.getMimeType(), doc.getPrintData());
agroupset = new IppAttributeGroupSet();
attrs = as.toArray();
for (int i = 0, ii = attrs.length; i < ii; i++) {
agroupset.setAttribute(Ipp2Java
.getIppAttributeNameByClass(
attrs[i].getClass(), -1), Ipp2Java
.getIppByJava(attrs[i]));
}
document.setAgroups(agroupset);
doVerbose(1, "Validating print job...");
response = printer.requestValidateJob(docname, document,
agroupset);
doVerbose(1, response.toString());
checkResponseIsZero(response, "IPP Validate Job: \n");
doVerbose(1, "Validate OK");
doVerbose(1, "Printing " + docname + "...");
response = printer.requestPrintJob(docname, document,
agroupset);
doVerbose(1, response.toString());
checkResponseIsZero(response, "IPP Print Job: \n");
doVerbose(1, "Printing OK");
} catch (PrintException e) {
throw e;
} catch (Exception e) {
if (getVerbose() > 1) {
e.printStackTrace();
}
throw new PrintException(e);
}
}
/**
* @param response d
* @param prefix d
* @throws PrintException d
*/
/*
* just check that IppResponse is OK
*/
private void checkResponseIsZero(IppResponse response, String prefix)
throws PrintException {
if (response.getStatusCode() != 0) {
String status = Integer.toHexString(response
.getStatusCode());
String id = Integer.toHexString(response.getRequestId());
throw new PrintException(prefix
+ "\n================ IPP response id: 0x" + id
+ " ====================="
+ "\nresponse status code: 0x" + status + "\n"
+ response.toString()
+ "\n================ end IPP response 0x" + id
+ " =====================");
}
}
/**
* @param pDocFlavor d
* @return d
*/
/*
* convert DocFlavor to DocFlavor ;-)
*
* some printers support application/ps instead of application/postscript
* So:
* if mimetype==application/postscript
* && printer does not support mimetype application/postscript
* && printer supports mimetype application/ps
* then
* we change mimetype of docflavor to application/ps
*/
private DocFlavor java2ipp(DocFlavor pDocFlavor) {
DocFlavor ippDocFlavor = pDocFlavor;
String mime = pDocFlavor.getMimeType();
/*
* SPECIAL processing application/ps
*/
if (mime.equals("application/postscript")) {
try {
IppDocument document = new IppDocument("Qwerty",
"application/postscript", "");
IppResponse response = printer.requestValidateJob(
"Qwerty", document, null);
if (response.getStatusCode() != 0) {
document = new IppDocument("Qwerty",
"application/ps", "");
response = printer.requestValidateJob("Qwerty",
document, null);
if (response.getStatusCode() == 0) {
if (pDocFlavor instanceof DocFlavor.INPUT_STREAM) {
ippDocFlavor = new DocFlavor.INPUT_STREAM(
"application/ps");
} else if (ippDocFlavor instanceof DocFlavor.BYTE_ARRAY) {
ippDocFlavor = new DocFlavor.BYTE_ARRAY(
"application/ps");
} else if (ippDocFlavor instanceof DocFlavor.URL) {
ippDocFlavor = new DocFlavor.URL(
"application/ps");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return ippDocFlavor;
}
/**
* @param ippDocFlavor d
* @return d
*/
/*
* opposite to java2ipp() method
*/
private DocFlavor ipp2java(DocFlavor ippDocFlavor) {
DocFlavor pDocFlavor = ippDocFlavor;
String mime = ippDocFlavor.getMimeType();
/*
* SPECIAL processing application/ps
*/
if (mime.equals("application/ps")) {
if (ippDocFlavor instanceof DocFlavor.INPUT_STREAM) {
pDocFlavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
} else if (ippDocFlavor instanceof DocFlavor.BYTE_ARRAY) {
pDocFlavor = DocFlavor.BYTE_ARRAY.POSTSCRIPT;
} else if (ippDocFlavor instanceof DocFlavor.URL) {
pDocFlavor = DocFlavor.URL.POSTSCRIPT;
}
}
return pDocFlavor;
}
/**
* @param flavor d
* @return d
*/
/*
* the method's name is saying all
*/
private boolean isDocFlavorSupported(DocFlavor flavor) {
if (flavor == null) {
throw new NullPointerException("DocFlavor flavor is null");
}
DocFlavor clientFlavors[] = getSupportedDocFlavors();
for (int i = 0; i < clientFlavors.length; i++) {
if (clientFlavors[i].equals(flavor)) {
return true;
}
}
return false;
}
/**
* @return d
*/
/*
* check permission to read/write to any file
*/
private boolean canPrintToFile() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkPermission(new FilePermission("<<ALL FILES>>",
"read,write"));
return true;
} catch (SecurityException e) {
return false;
}
}
return true;
}
/**
*
*/
/*
* just list of all doc flavors from specification
* it is used in getSupportedDocFlavors() method
*/
private static DocFlavor[] ALLDOCFLAVORS = {
DocFlavor.BYTE_ARRAY.TEXT_PLAIN_HOST,
DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_8,
DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_16,
DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_16BE,
DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_16LE,
DocFlavor.BYTE_ARRAY.TEXT_PLAIN_US_ASCII,
DocFlavor.BYTE_ARRAY.TEXT_HTML_HOST,
DocFlavor.BYTE_ARRAY.TEXT_HTML_UTF_8,
DocFlavor.BYTE_ARRAY.TEXT_HTML_UTF_16,
DocFlavor.BYTE_ARRAY.TEXT_HTML_UTF_16BE,
DocFlavor.BYTE_ARRAY.TEXT_HTML_UTF_16LE,
DocFlavor.BYTE_ARRAY.TEXT_HTML_US_ASCII,
DocFlavor.BYTE_ARRAY.PDF, DocFlavor.BYTE_ARRAY.POSTSCRIPT,
DocFlavor.BYTE_ARRAY.PCL, DocFlavor.BYTE_ARRAY.GIF,
DocFlavor.BYTE_ARRAY.JPEG, DocFlavor.BYTE_ARRAY.PNG,
DocFlavor.BYTE_ARRAY.AUTOSENSE,
DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST,
DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8,
DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_16,
DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_16BE,
DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_16LE,
DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII,
DocFlavor.INPUT_STREAM.TEXT_HTML_HOST,
DocFlavor.INPUT_STREAM.TEXT_HTML_UTF_8,
DocFlavor.INPUT_STREAM.TEXT_HTML_UTF_16,
DocFlavor.INPUT_STREAM.TEXT_HTML_UTF_16BE,
DocFlavor.INPUT_STREAM.TEXT_HTML_UTF_16LE,
DocFlavor.INPUT_STREAM.TEXT_HTML_US_ASCII,
DocFlavor.INPUT_STREAM.PDF,
DocFlavor.INPUT_STREAM.POSTSCRIPT,
DocFlavor.INPUT_STREAM.PCL, DocFlavor.INPUT_STREAM.GIF,
DocFlavor.INPUT_STREAM.JPEG, DocFlavor.INPUT_STREAM.PNG,
DocFlavor.INPUT_STREAM.AUTOSENSE,
DocFlavor.URL.TEXT_PLAIN_HOST,
DocFlavor.URL.TEXT_PLAIN_UTF_8,
DocFlavor.URL.TEXT_PLAIN_UTF_16,
DocFlavor.URL.TEXT_PLAIN_UTF_16BE,
DocFlavor.URL.TEXT_PLAIN_UTF_16LE,
DocFlavor.URL.TEXT_PLAIN_US_ASCII,
DocFlavor.URL.TEXT_HTML_HOST,
DocFlavor.URL.TEXT_HTML_UTF_8,
DocFlavor.URL.TEXT_HTML_UTF_16,
DocFlavor.URL.TEXT_HTML_UTF_16BE,
DocFlavor.URL.TEXT_HTML_UTF_16LE,
DocFlavor.URL.TEXT_HTML_US_ASCII, DocFlavor.URL.PDF,
DocFlavor.URL.POSTSCRIPT, DocFlavor.URL.PCL,
DocFlavor.URL.GIF, DocFlavor.URL.JPEG, DocFlavor.URL.PNG,
DocFlavor.URL.AUTOSENSE,
DocFlavor.CHAR_ARRAY.TEXT_PLAIN,
DocFlavor.CHAR_ARRAY.TEXT_HTML,
DocFlavor.STRING.TEXT_PLAIN, DocFlavor.STRING.TEXT_HTML,
DocFlavor.READER.TEXT_PLAIN, DocFlavor.READER.TEXT_HTML,
DocFlavor.SERVICE_FORMATTED.RENDERABLE_IMAGE,
DocFlavor.SERVICE_FORMATTED.PRINTABLE,
DocFlavor.SERVICE_FORMATTED.PAGEABLE,
/*
* Some printers accept "application/ps" instead of "application/postscript"
* So, we have special processing for those DocFlavor
* See comments with phrase:
* SPECIAL processing application/ps
*/
new DocFlavor.INPUT_STREAM("application/ps"),
new DocFlavor.URL("application/ps"),
new DocFlavor.BYTE_ARRAY("application/ps") };
/**
* @return d
*/
public static int getVerbose() {
return verbose;
}
/**
* @param newverbose d
*/
public static void setVerbose(int newverbose) {
verbose = newverbose;
IppPrinter.setVerbose(verbose);
}
/**
* @param v d
*/
public static void doVerbose(String v) {
System.out.println(v);
}
/**
* @param level d
* @param v d
*/
public static void doVerbose(int level, String v) {
if (verbose >= level) {
System.out.println(v);
}
}
}
分享到:
相关推荐
CupsClient for Windows 连接到 Cups 服务器,检索所有打印机(包括名称、型号、位置、评论、颜色、媒体、ppm)。 您可以根据所有信息过滤结果并安装您选择的打印机(带有 postscript 驱动程序)。
请在pom.xml文件中添加以下依赖项:< dependency> < groupId>org.cups4j</ groupId> < artifactId>cups4j</ artifactId> < version>0.7.6</ version></ dependency>用法基本用法CupsClient cupsClient = new ...
离线安装包,亲测可用
动态电压恢复器(DVR)模型【2.0】在Matlab Simulink环境下的精细化仿真:全面治理电能质量问题的时序解析,动态电压恢复器(DVR)模型【2.0】在Matlab Simulink下的电能质量问题治理详解:全面应对源侧电压暂降、暂升及负载影响,历时1.1秒,动态电压恢复器(DVR)模型【2.0】 Matlab simulink 可用于治理电能质量问题:仿真总时长1.1s,DVR始终接入,具体如下: 0.1-0.2s治理源侧电压暂降; 0.3-0.4s治理源侧电压暂升; 0.5-0.6s治理电机启动引起的电压暂降; 0.7-0.8s治理变压器励磁引起的电压暂降; 0.9-1.0s治理短路故障带来的不平衡问题。 ,DVR模型; Matlab simulink; 治理电能质量问题; 仿真时长; 电压暂降; 电压暂升; 电机启动; 变压器励磁; 短路故障。,Matlab Simulink中的DVR模型2.0:电能质量问题的动态治理策略
COMSOL仿真技术研究偶极光源特性与应用,COMSOL仿真技术下的偶极光源研究与应用,comsol仿真偶极光源 ,comsol仿真; 偶极光源; 偶极子辐射; 仿真建模,Comsol仿真:偶极光源模拟与优化研究
基于FPGA的四轴运动控制IP:逻辑控制代码编写与复杂运动规划功能实现,基于FPGA的四轴运动控制IP实现:逻辑VHDL编程,支持多种运动控制算法与mcx314相当性能,基于FPGA的四轴运动控制IP。 纯逻辑vhdl代码编写。 支持回零,直线圆弧插补,小直线速度前瞻,梯形加减速,S型加减速等。 性能等同于mcx314. ,基于FPGA的四轴运动控制IP; 纯逻辑Vhdl代码编写; 回零功能; 插补功能(直线、圆弧); 速度前瞻; 梯形加减速; S型加减速; 性能等同MCX314。,基于FPGA的Vhdl四轴运动控制IP:高性能、灵活配置的S型加减速插补器
机器人控制系统及设计程序,含仿真程序、控制系统及代码。
1、以上文章可用于参考,请勿直接抄袭,学习、当作参考文献可以,主张借鉴学习 2、资源本身不含 对应项目代码,如需完整项目源码,请私信博主获取
基于博途1200 PLC与HMI的十层二部电梯控制系统仿真工程:实现集群运行与优化配置的研究实践,基于博途PLC及HMI的十层二部电梯控制系统仿真与优化实践,基于博途1200PLC+HMI十层二部电梯控制系统仿真 程序: 1、任务:PLC.人机界面控制双部电梯集群运行 2、系统说明: 系统设有上呼、下呼、内呼、手动开关门、光幕、检修、故障、满载、等模拟模式控制, 系统共享厅外召唤信号,集选控制双部电梯运行。 十层二部电梯途仿真工程配套有博途PLC程序+IO点表 +PLC接线图+主电路图+控制流程图, 附赠:设计参考文档(与程序不是配套,仅供参考)。 博途V16+HMI 可直接模拟运行 程序简洁、精炼,注释详细 ,基于博途1200PLC; HMI双部电梯控制; 电梯控制模式; 控制系统仿真; 博途V16+HMI模拟运行。,基于博途1200的十层二部电梯控制系统仿真程序
COMSOL流体仿真下的流固耦合现象:圆管内流体驱动物块移动与扇叶转动探究,COMSOL流体仿真:流固耦合下的圆管内流体驱动动态模拟——流体驱动物块移动与扇叶转动研究,comsol流体仿真 ,流固耦合,圆管内流体驱动物块的移动和 流体驱动扇叶的转动 ,comsol流体仿真;流固耦合;圆管内流体驱动物块移动;流体驱动扇叶转动,Comsol流体仿真:圆管内流固耦合与流体驱动的物块移动及扇叶转动研究
本研究探讨了大型语言模型如何通过提示工程对科学论文中的句子进行分类。我们使用两种先进的基于网络的模型,OpenAI 的 GPT-4o 和 DeepSeek R1,将句子分类为预定义的关系类别。DeepSeek R1 已在其技术报告中测试过基准数据集。然而,其在科学文本分类中的性能尚未得到充分探索。为解决这一问题,我们引入了一种专门为该任务设计的新评估方法,并整理了一个来自多个领域的清洁科学论文数据集。该数据集提供了一个比较这两个模型的平台。通过使用此数据集,我们分析了它们在分类中的有效性和一致性。
海神之光上传的视频是由对应的完整代码运行得来的,完整代码皆可运行,亲测可用,适合小白; 1、从视频里可见完整代码的内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
三相逆变整流并网技术:正负序分离消除负序电流,保障光伏风力发电系统电流三相对称的研究,三相逆变整流并网技术的正负序分离与负序电流消除策略在电网电压不平衡跌落中的应用——适用于光伏与风力发电系统,三相逆变 整流并网,正负序分离,在电网电压不平衡跌落 平衡跌落时,消除负序电流,维持电网电流三相对称,可用于光伏和风力发电系统 有参考文献 ,三相逆变;整流并网;正负序分离;电网电压不平衡跌落;消除负序电流;光伏和风力发电系统;参考文献,三相逆变整流并网技术:电网电压不平衡下的负序电流消除策略
题目:基于51单片机的多参数水质监测与报警系统设计 主控:AT89C51 显示:LCD1602 DS18B20温度传感器 浊度传感器(PCF8591+滑动变阻器模拟) PH传感器(ADC0832+滑动变阻器) 声光报警 led*4 功能: 1.实时检测水质温度、浊度、PH 2.实时显示相关数据 3.可以通过按键修改阈值 4.各数值不在标准范围内启动声光报警 5.ph低于下限红色小灯点亮;ph高于上限绿色小灯电亮;温度低于阈值蓝色小灯电亮;浑浊度高于阈值橙色小灯电亮
海神之光上传的视频是由对应的完整代码运行得来的,完整代码皆可运行,亲测可用,适合小白; 1、从视频里可见完整代码的内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
内容概要:本文档《Java网络编程教程》详细介绍了Java在网络编程中的应用,覆盖了从基础知识到高级技术的各个层面。首先讲解了网络编程的概念和Java提供的相关库如java.net和java.nio,以及基于这两个库的Socket和非阻塞I/O的编程技巧,随后深入探讨了多线程并发处理、数据流的操作及网络数据格式(JSON与XML)。接着文档讨论了实践中常用的技术,如HTTP编程、聊天室应用的开发和异常处理。此外,还介绍了Java NIO的Buffer、Channel和Selector机制及其性能优化策略,分布式系统的RPC、RESTful API、消息队列和分布式缓存等内容。最后,探讨了网络安全方面的知识,包括SSL/TLS协议、数据加密技术和防火墙配置。 适合人群:适合初学者至中级水平的Java程序员,尤其是那些想深入理解和掌握Java在网络编程中的各类应用和技术的人群。 使用场景及目标:文档旨在使开发者深入了解并熟练掌握Java在网络编程领域的应用,从而能够独立开发高可靠性的网络应用程序和服务。无论是创建简单的客户端还是复杂的服务器端程序,或是参与大规模分布式的项目都适用。 其他说
海神之光上传的视频是由对应的完整代码运行得来的,完整代码皆可运行,亲测可用,适合小白; 1、从视频里可见完整代码的内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
基于模型预测控制的车辆换道轨迹跟踪:五次多项式换道轨迹设计与Matlab与Carsim联合仿真研究,基于模型预测控制的车辆换道轨迹跟踪研究:五次多项式换道轨迹与Matlab-Carsim联控应用,基于模型预测控制(mpc)的车辆道,车辆轨迹跟踪,道轨迹为五次多项式,matlab与carsim联防控制 ,基于模型预测控制(MPC)的车辆换道; 车辆轨迹跟踪; 换道轨迹五次多项式; MATLAB与CARSIM联防控制,基于MPC的车辆换道控制:五次多项式轨迹跟踪与Matlab-CarSim联合仿真
MATLAB Simulink单相并网逆变器主动移频法(AFD)孤岛检测仿真系统:全面集成高效响应与智能频移的电力安全保障技术,MATLAB Simulink单相并网逆变器主动移频法(AFD)孤岛检测仿真系统:全面集成高效响应与智能频移的电力安全保障技术,MATLAB simulink单相并网逆变器主动移频法(AFD)孤岛检测仿真系统,附相关说明。 全面仿真架构:本系统集成了单相电网、逆变器、滤波模块、PI控制器、PWM信号发生器、锁相环、AFD控制器S函数以及高精度测量模块,构建了一个完整且精确的仿真环境。 先进检测技术:采用前沿的主动移频法(AFD)进行孤岛检测,该技术通过智能调整逆变器输出电流的频率参考,有效识别孤岛状态,确保电力系统的安全稳定。 高效响应与精准检测:AFD算法设计巧妙,响应速度快,检测精度高,能在第一时间发现并响应孤岛事件,有效防止潜在的安全隐患。 智能频移机制:主动移频法(AFD)的核心在于,它通过在公共点电压频率上施加一个固定偏移量作为逆变器输出电流的参考频率。 电网正常供电时,电流频率受电网频率约束保持稳定;电网失电,逆变器参考电流频率中的偏移量将驱动本地
基于直驱永磁同步技术的风力发电机MATLAB仿真模型的研究与实现,直驱永磁同步风力发电机的MATLAB仿真模型构建与应用研究,直驱永磁同步风力发电机MATLAB仿真模型 ,直驱永磁; 同步风力; 发电机; MATLAB仿真模型; 核心关键词,MATLAB仿真模型:直驱永磁同步风力发电机