public void print(Doc doc, PrintRequestAttributeSet attributes)
throws PrintException {
synchronized (this) {
if (printing) {
throw new PrintException("already printing");
} else {
printing = true;
}
}
this.doc = doc;
/* check if the parameters are valid before doing much processing */
DocFlavor flavor = doc.getDocFlavor();
Object data;
try {
data = doc.getPrintData();
} catch (IOException e) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException("can't get print data: " + e.toString());
}
if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintJobFlavorException("invalid flavor", flavor);
}
initializeAttributeSets(doc, attributes);
getAttributeValues(flavor);
String repClassName = flavor.getRepresentationClassName();
if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) ||
flavor.equals(DocFlavor.INPUT_STREAM.JPEG) ||
flavor.equals(DocFlavor.INPUT_STREAM.PNG) ||
flavor.equals(DocFlavor.BYTE_ARRAY.GIF) ||
flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) ||
flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
try {
instream = doc.getStreamForBytes();
printableJob(new ImagePrinter(instream), reqAttrSet);
return;
} catch (ClassCastException cce) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(cce);
} catch (IOException ioe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(ioe);
}
} else if (flavor.equals(DocFlavor.URL.GIF) ||
flavor.equals(DocFlavor.URL.JPEG) ||
flavor.equals(DocFlavor.URL.PNG)) {
try {
printableJob(new ImagePrinter((URL)data), reqAttrSet);
return;
} catch (ClassCastException cce) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(cce);
}
} else if (repClassName.equals("java.awt.print.Pageable")) {
try {
pageableJob((Pageable)doc.getPrintData(), reqAttrSet);
return;
} catch (ClassCastException cce) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(cce);
} catch (IOException ioe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(ioe);
}
} else if (repClassName.equals("java.awt.print.Printable")) {
try {
printableJob((Printable)doc.getPrintData(), reqAttrSet);
return;
} catch (ClassCastException cce) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(cce);
} catch (IOException ioe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(ioe);
}
} else {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException("unrecognized class: "+repClassName);
}
}