public void print(Doc doc, PrintRequestAttributeSet attributes)
throws PrintException {
Object data;
DocFlavor docflavor;
String docflavorClassName;
Image image = null;
int x = 0;
int y = 0;
int width;
int height;
int iWidth;
int iHeight;
int newWidth;
int newHeight;
float scaleX;
float scaleY;
synchronized (this) {
if (action) {
throw new PrintException("printing is in action");
}
action = true;
}
try { // for finally block. To make action false.
docflavor = doc.getDocFlavor();
try {
data = doc.getPrintData();
} catch (IOException ioexception) {
throw new PrintException("no data for print: "
+ ioexception.toString());
}
if (docflavor == null) {
throw new PrintException("flavor is null");
}
if (!begetPrintService.isDocFlavorSupported(docflavor)) {
throw new PrintException("invalid flavor :"
+ docflavor.toString());
}
docflavorClassName = docflavor.getRepresentationClassName();
if (docflavor.equals(DocFlavor.INPUT_STREAM.GIF) ||
docflavor.equals(DocFlavor.BYTE_ARRAY.GIF) ||
docflavor.equals(DocFlavor.INPUT_STREAM.JPEG) ||
docflavor.equals(DocFlavor.BYTE_ARRAY.JPEG) ||
docflavor.equals(DocFlavor.INPUT_STREAM.PNG) ||
docflavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
try {
image = readImage(doc.getStreamForBytes());
} catch (IOException ioe) {
throw new PrintException(ioe);
}
} else if (docflavor.equals(DocFlavor.URL.GIF) ||
docflavor.equals(DocFlavor.URL.JPEG) ||
docflavor.equals(DocFlavor.URL.PNG)) {
URL url = (URL) data;
try {
image = readImage(url.openStream());
} catch (IOException ioe) {
throw new PrintException(ioe);
}
} else if (docflavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)){
Printable printable = (Printable)data;
print(printable, null);
} else if (docflavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE)) {
Pageable pageable = (Pageable)data;
print(null, pageable);
} else {
throw new PrintException("Wrong DocFlavor class: "
+ docflavorClassName);