1/* 2 * Copyright (c) 2001, 2013, 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/* 25 * @test 26 * @bug 4468109 8021583 27 * @summary Test for printing AUTOSENSE DocFlavor. No exception should be thrown. 28 * @run main PrintAutoSenseData 29*/ 30 31import java.io.*; 32import javax.print.*; 33import javax.print.attribute.*; 34import javax.print.attribute.standard.*; 35import java.net.URL; 36 37public class PrintAutoSenseData 38{ 39 private DocFlavor flavor = DocFlavor.URL.AUTOSENSE; //represents the docflavor. 40 private PrintService[] service = PrintServiceLookup.lookupPrintServices(flavor, null); 41 42 43 public PrintAutoSenseData() 44 { 45 if (service.length == 0) 46 { 47 System.out.println("No print service available..."); 48 return; 49 } 50 51 System.out.println("selected PrintService: " + this.service[0]); 52 if (service[0].isDocFlavorSupported(flavor)) { 53 System.out.println("DocFlavor.URL.AUTOSENSE supported"); 54 } else { 55 System.out.println("DocFlavor.URL.AUTOSENSE not supported. Testing aborted !!"); 56 return; 57 } 58 59 DocPrintJob job = service[0].createPrintJob(); 60 this.print(); 61 } 62 63 // The print method prints sample file with DocFlavor.URL.AUTOSENSE. 64 void print() 65 { 66 String fileName = "./sample.txt"; 67 DocPrintJob job = service[0].createPrintJob(); 68 69 // The representation class is a URL. 70 System.out.println("printing " + fileName + " using doc flavor: " + this.flavor); 71 System.out.println("Rep. class name: " + this.flavor.getRepresentationClassName() + " MimeType: " + this.flavor.getMimeType()); 72 73 Doc doc = new URLDoc(fileName, this.flavor); 74 HashPrintRequestAttributeSet prSet = 75 new HashPrintRequestAttributeSet(); 76 prSet.add(new Destination(new File("./dest.prn").toURI())); 77 //print the document. 78 try { 79 job.print(doc, prSet); 80 } catch ( Exception e ) { 81 e.printStackTrace(); 82 } 83 } 84 85 public static void main(String[] args) { 86 new PrintAutoSenseData(); 87 } 88 89} 90 91/* This class is for reading autosense data with URL representation class */ 92 93class URLDoc implements Doc 94{ 95 protected String fileName = null; 96 protected DocFlavor flavor = null; 97 protected Object printData = null; 98 protected InputStream instream = null; 99 100 public URLDoc(String filename, DocFlavor docFlavor) 101 { 102 this.fileName = filename; 103 this.flavor = docFlavor; 104 } 105 106 public DocFlavor getDocFlavor() { 107 return DocFlavor.URL.AUTOSENSE; 108 } 109 110 public DocAttributeSet getAttributes() 111 { 112 HashDocAttributeSet hset = new HashDocAttributeSet(); 113 return hset; 114 } 115 116 public Object getPrintData() 117 { 118 if ( this.printData == null ) 119 { 120 this.printData = URLDoc.class.getResource(this.fileName); 121 System.out.println("getPrintData(): " + this.printData); 122 } 123 return this.printData; 124 } 125 126 public Reader getReaderForText() 127 { 128 return null; 129 } 130 131 public InputStream getStreamForBytes() 132 { 133 System.out.println("getStreamForBytes(): " + this.printData); 134 try 135 { 136 if ( (this.printData != null) && (this.printData instanceof URL) ) 137 { 138 this.instream = ((URL)this.printData).openStream(); 139 } 140 if (this.instream == null) 141 { 142 URL url = URLDoc.class.getResource(this.fileName); 143 this.instream = url.openStream(); 144 } 145 } 146 catch ( IOException ie ) 147 { 148 System.out.println("URLDoc: exception: " + ie.toString()); 149 } 150 return this.instream; 151 } 152} 153