1183N/A/*
2362N/A * Copyright (c) 2001, 2009, Oracle and/or its affiliates. All rights reserved.
1183N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1183N/A *
1183N/A * This code is free software; you can redistribute it and/or modify it
1183N/A * under the terms of the GNU General Public License version 2 only, as
1183N/A * published by the Free Software Foundation.
1183N/A *
1183N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1183N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1183N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1183N/A * version 2 for more details (a copy is included in the LICENSE file that
1183N/A * accompanied this code).
1183N/A *
1183N/A * You should have received a copy of the GNU General Public License version
1183N/A * 2 along with this work; if not, write to the Free Software Foundation,
1183N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1183N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1183N/A */
1183N/A
1183N/A/*
1183N/A * @test
1183N/A * @bug 4468109
1183N/A * @summary Test for printing AUTOSENSE DocFlavor. No exception should be thrown.
1183N/A * @run main PrintAutoSenseData
1183N/A*/
1183N/A
1183N/Aimport java.io.*;
1183N/Aimport javax.print.*;
1183N/Aimport javax.print.attribute.*;
1183N/Aimport javax.print.attribute.standard.*;
1183N/Aimport java.net.URL;
1183N/A
1183N/Apublic class PrintAutoSenseData
1183N/A{
1183N/A private DocFlavor flavor = DocFlavor.URL.AUTOSENSE; //represents the docflavor.
1183N/A private PrintService[] service = PrintServiceLookup.lookupPrintServices(flavor, null);
1183N/A
1183N/A
1183N/A public PrintAutoSenseData()
1183N/A {
1183N/A if (service.length == 0)
1183N/A {
1183N/A System.out.println("No print service available...");
1183N/A return;
1183N/A }
1183N/A
1183N/A System.out.println("selected PrintService: " + this.service[0]);
1183N/A if (service[0].isDocFlavorSupported(flavor)) {
1183N/A System.out.println("DocFlavor.URL.AUTOSENSE supported");
1183N/A } else {
1183N/A System.out.println("DocFlavor.URL.AUTOSENSE not supported. Testing aborted !!");
1183N/A return;
1183N/A }
1183N/A
1183N/A DocPrintJob job = service[0].createPrintJob();
1183N/A this.print();
1183N/A }
1183N/A
1183N/A // The print method prints sample file with DocFlavor.URL.AUTOSENSE.
1183N/A void print()
1183N/A {
1183N/A String fileName = "./sample.txt";
1183N/A DocPrintJob job = service[0].createPrintJob();
1183N/A
1183N/A // The representation class is a URL.
1183N/A System.out.println("printing " + fileName + " using doc flavor: " + this.flavor);
1183N/A System.out.println("Rep. class name: " + this.flavor.getRepresentationClassName() + " MimeType: " + this.flavor.getMimeType());
1183N/A
1183N/A Doc doc = new URLDoc(fileName, this.flavor);
1183N/A HashPrintRequestAttributeSet prSet =
1183N/A new HashPrintRequestAttributeSet();
1183N/A prSet.add(new Destination(new File("./dest.prn").toURI()));
1183N/A //print the document.
1183N/A try {
1183N/A job.print(doc, prSet);
1183N/A } catch ( Exception e ) {
1183N/A e.printStackTrace();
1183N/A }
1183N/A }
1183N/A
1183N/A public static void main(String[] args) {
1183N/A new PrintAutoSenseData();
1183N/A }
1183N/A
1183N/A}
1183N/A
1183N/A/* This class is for reading autosense data with URL representation class */
1183N/A
1183N/Aclass URLDoc implements Doc
1183N/A{
1183N/A protected String fileName = null;
1183N/A protected DocFlavor flavor = null;
1183N/A protected Object printData = null;
1183N/A protected InputStream instream = null;
1183N/A
1183N/A public URLDoc(String filename, DocFlavor docFlavor)
1183N/A {
1183N/A this.fileName = filename;
1183N/A this.flavor = docFlavor;
1183N/A }
1183N/A
1183N/A public DocFlavor getDocFlavor() {
1183N/A return DocFlavor.URL.AUTOSENSE;
1183N/A }
1183N/A
1183N/A public DocAttributeSet getAttributes()
1183N/A {
1183N/A HashDocAttributeSet hset = new HashDocAttributeSet();
1183N/A return hset;
1183N/A }
1183N/A
1183N/A public Object getPrintData()
1183N/A {
1183N/A if ( this.printData == null )
1183N/A {
1183N/A this.printData = URLDoc.class.getResource(this.fileName);
1183N/A System.out.println("getPrintData(): " + this.printData);
1183N/A }
1183N/A return this.printData;
1183N/A }
1183N/A
1183N/A public Reader getReaderForText()
1183N/A {
1183N/A return null;
1183N/A }
1183N/A
1183N/A public InputStream getStreamForBytes()
1183N/A {
1183N/A System.out.println("getStreamForBytes(): " + this.printData);
1183N/A try
1183N/A {
1183N/A if ( (this.printData != null) && (this.printData instanceof URL) )
1183N/A {
1183N/A this.instream = ((URL)this.printData).openStream();
1183N/A }
1183N/A if (this.instream == null)
1183N/A {
1183N/A URL url = URLDoc.class.getResource(this.fileName);
1183N/A this.instream = url.openStream();
1183N/A }
1183N/A }
1183N/A catch ( IOException ie )
1183N/A {
1183N/A System.out.println("URLDoc: exception: " + ie.toString());
1183N/A }
1183N/A return this.instream;
1183N/A }
1183N/A}