Path: blob/master/test/jdk/javax/print/attribute/autosense/PrintAutoSenseData.java
41152 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4468109 802158326* @summary Test for printing AUTOSENSE DocFlavor. No exception should be thrown.27* @run main PrintAutoSenseData28*/2930import java.io.*;31import javax.print.*;32import javax.print.attribute.*;33import javax.print.attribute.standard.*;34import java.net.URL;3536public class PrintAutoSenseData37{38private DocFlavor flavor = DocFlavor.URL.AUTOSENSE; //represents the docflavor.39private PrintService[] service = PrintServiceLookup.lookupPrintServices(flavor, null);404142public PrintAutoSenseData()43{44if (service.length == 0)45{46System.out.println("No print service available...");47return;48}4950System.out.println("selected PrintService: " + this.service[0]);51if (service[0].isDocFlavorSupported(flavor)) {52System.out.println("DocFlavor.URL.AUTOSENSE supported");53} else {54System.out.println("DocFlavor.URL.AUTOSENSE not supported. Testing aborted !!");55return;56}5758DocPrintJob job = service[0].createPrintJob();59this.print();60}6162// The print method prints sample file with DocFlavor.URL.AUTOSENSE.63void print()64{65String fileName = "./sample.txt";66DocPrintJob job = service[0].createPrintJob();6768// The representation class is a URL.69System.out.println("printing " + fileName + " using doc flavor: " + this.flavor);70System.out.println("Rep. class name: " + this.flavor.getRepresentationClassName() + " MimeType: " + this.flavor.getMimeType());7172Doc doc = new URLDoc(fileName, this.flavor);73HashPrintRequestAttributeSet prSet =74new HashPrintRequestAttributeSet();75prSet.add(new Destination(new File("./dest.prn").toURI()));76//print the document.77try {78job.print(doc, prSet);79} catch ( Exception e ) {80e.printStackTrace();81}82}8384public static void main(String[] args) {85new PrintAutoSenseData();86}8788}8990/* This class is for reading autosense data with URL representation class */9192class URLDoc implements Doc93{94protected String fileName = null;95protected DocFlavor flavor = null;96protected Object printData = null;97protected InputStream instream = null;9899public URLDoc(String filename, DocFlavor docFlavor)100{101this.fileName = filename;102this.flavor = docFlavor;103}104105public DocFlavor getDocFlavor() {106return DocFlavor.URL.AUTOSENSE;107}108109public DocAttributeSet getAttributes()110{111HashDocAttributeSet hset = new HashDocAttributeSet();112return hset;113}114115public Object getPrintData()116{117if ( this.printData == null )118{119this.printData = URLDoc.class.getResource(this.fileName);120System.out.println("getPrintData(): " + this.printData);121}122return this.printData;123}124125public Reader getReaderForText()126{127return null;128}129130public InputStream getStreamForBytes()131{132System.out.println("getStreamForBytes(): " + this.printData);133try134{135if ( (this.printData != null) && (this.printData instanceof URL) )136{137this.instream = ((URL)this.printData).openStream();138}139if (this.instream == null)140{141URL url = URLDoc.class.getResource(this.fileName);142this.instream = url.openStream();143}144}145catch ( IOException ie )146{147System.out.println("URLDoc: exception: " + ie.toString());148}149return this.instream;150}151}152153154