1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* 24 * @test 25 * @bug 4899773 26 * @summary Test for DocFlavor.URL.PDF support. No exception should be thrown. 27 * @run main URLPDFPrinting 28 */ 29 30 import java.awt.*; 31 import javax.print.*; 32 import javax.print.attribute.standard.*; 33 import javax.print.attribute.*; 34 import java.io.*; 35 import java.util.Locale; 36 import java.net.URL; 37 38 public class URLPDFPrinting { 39 /** 40 * Constructor 41 */ URLPDFPrinting()42 public URLPDFPrinting() { 43 super(); 44 } 45 /** 46 * Starts the application. 47 */ main(java.lang.String[] args)48 public static void main(java.lang.String[] args) { 49 URLPDFPrinting pd = new URLPDFPrinting(); 50 PrintService service[] = null, defService = null; 51 52 service = PrintServiceLookup.lookupPrintServices(DocFlavor.URL.PDF, null); 53 if (service.length == 0) { 54 System.out.println("No PrintService support DocFlavor.URL.PDF"); 55 return; 56 } else { 57 defService = service[0]; 58 System.out.println("Print Service which supports DocFlavor.URL.PDF: "+defService); 59 } 60 61 System.out.println("is DocFlavor.URL.PDF supported? "+defService.isDocFlavorSupported(DocFlavor.URL.PDF)); 62 HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet(); 63 prSet.add(new Destination(new File("./dest.prn").toURI())); 64 65 DocPrintJob pj = defService.createPrintJob(); 66 PrintDocument prDoc = new PrintDocument(); 67 try { 68 pj.print(prDoc, prSet); 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } 72 73 } 74 } 75 76 class PrintDocument implements Doc { 77 InputStream fStream = null; 78 DocFlavor flavor = null; 79 HashDocAttributeSet docSet = new HashDocAttributeSet(); 80 URL url = null; 81 PrintDocument()82 public PrintDocument() { 83 try { 84 url = PrintDocument.class.getResource("hello.pdf"); 85 try{ Thread.sleep(6000); }catch(Exception e){ e.printStackTrace();} 86 fStream = url.openStream(); 87 System.out.println("URL input stream "+fStream); 88 } catch(Exception e) { 89 e.printStackTrace(); 90 } 91 docSet.add(OrientationRequested.LANDSCAPE); 92 } 93 getAttributes()94 public DocAttributeSet getAttributes() { 95 System.out.println("getAttributes called"); 96 return docSet; 97 } 98 getDocFlavor()99 public DocFlavor getDocFlavor() { 100 System.out.println("getDocFlavor called"); 101 return DocFlavor.URL.PDF; 102 } 103 getPrintData()104 public Object getPrintData(){ 105 System.out.println("getPrintData called"); 106 return url; 107 } 108 getReaderForText()109 public Reader getReaderForText() { 110 return null; 111 } 112 getStreamForBytes()113 public InputStream getStreamForBytes() { 114 System.out.println("getStreamForBytes called"); 115 return fStream; 116 } 117 } 118