Path: blob/master/src/java.desktop/share/classes/javax/print/attribute/package-info.java
41159 views
/*1* Copyright (c) 2000, 2017, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/**26* Provides classes and interfaces that describe the types of Java Print27* Service attributes and how they can be collected into attribute sets.28*29* <h2>What is an Attribute?</h2>30* When setting up a print job, a client specifies two things: <b>print data</b>31* and <b>processing instructions.</b> The print data is the actual content to32* be printed. The processing instructions tell the printer how to print the33* print data, such as: what media to use, how many copies to print, and whether34* to print on one or both sides of a sheet. The client specifies these35* processing instructions with the attribute definitions of the Java Print36* Service API.37* <p>38* The print data and the processing instructions are separate entities. This39* means that:40* <ul>41* <li>You can print the same print data at different times using different42* processing instructions.43* <br>44* For example, you can print a slide presentation on US letter-sized white45* paper, double-sided, stapled, 20 copies to make handouts for a talk; and46* you could print the same slide presentation on US letter-sized47* transparencies, single-sided, one copy to make the actual slides for the48* talk.49* <li>You can use the same processing instructions at different times to50* print different data. For example, you could set your default processing51* instructions to: US letter-sized paper, double sided, stapled. Whenever you52* print a job, it prints with these settings, unless you explicitly override53* them.54* </ul>55* The processing instruction does not specify how the print job processes the56* request; each processing instruction is only a description of the results of57* a print job. The print job determines the manner in which it achieves the58* results specified by the processing instructions. Representing processing59* instructions as descriptive items provides more flexibility for implementing60* print jobs.61*62* <h3>Attribute Categories and Values</h3>63* Each printer has a set of capabilities, such as the ability to print on64* different paper sizes or the ability to print more than one copy. Each of the65* capabilities has a range of values. For example, a printer's orientation66* capability might have this range of values: [landscape, portrait]. For each67* print request, the capability is set to one of these values. The Java Print68* Service API uses the term <b>attribute category</b> to refer to a printer69* capability and the term <b>attribute value</b> to refer to the value of the70* capability.71* <p>72* In the Java Print Service API, an attribute category is represented by a Java73* class implementing the <a href="Attribute.html">Attribute</a> interface.74* Attribute values are instances of such a class or one of its subclasses. For75* example, to specify the number of copies, an application constructs an76* instance of the <a href="standard/Copies.html">Copies</a> class with the77* number of desired copies and uses the {@code Copies} instance as part of the78* print request. In this case, the {@code Copies} class represents the79* attribute category, and the {@code Copies} instance represents the attribute80* value.81*82* <h3><a id="role"></a>Attribute Roles</h3>83* When submitting a print job to a printer, the client provides the attributes84* describing the characteristics of the print data, such as the document name,85* and how the print data should be printed, such as double-sided, five copies.86* If a print job consists of multiple pieces of print data, different pieces87* might have different processing instructions, such as 8 x 11 inch media for88* the first document, and 11 x 17 inch media for another document.89* <p>90* Once the printer starts processing the print job, additional information91* about the job becomes available, which might include: the job state (such as92* <i>completed</i> or <i>queued</i>) and the number of pages printed so far.93* These pieces of information are also attributes. Attributes can also describe94* the printer itself, such as: the printer name, the printer location, and the95* number of jobs queued.96* <p>97* The Java Print Service API defines these different kinds of attributes with98* five subinterfaces of {@code Attribute}:99* <ul>100* <li><a href="DocAttribute.html">DocAttribute</a> specifies a characteristic101* of an individual document and the print job settings to be applied to an102* individual document.103* <li><a href="PrintRequestAttribute.html">PrintRequestAttribute</a>104* specifies a setting applied to a whole print job and to all the documents105* in the print job.106* <li><a href="PrintJobAttribute.html">PrintJobAttribute</a> reports the107* status of a print job.108* <li><a href="PrintServiceAttribute.html">PrintServiceAttribute</a> reports109* the status of a print service.110* <li><a href="SupportedValuesAttribute.html">SupportedValuesAttribute</a>111* gives the supported values for another attribute.112* </ul>113* Each attribute class implements one or more of these tagging subinterfaces to114* indicate where the attribute can be used in the API. If an attribute class115* implements multiple tagging subinterfaces, the attribute can be used in116* multiple contexts. For example, the media attribute can apply to one document117* in a print job as a {@code DocAttribute} or to an entire print job as a118* {@code PrintRequestAttribute}. Certain low-level attributes are never used on119* their own but are always aggregated into higher-level attributes. These120* low-level attribute classes only implement interface121* <a href="Attribute.html">Attribute</a>, not any of the tagging subinterfaces.122* <p>123* The Java Print Service API defines a group of standard attribute classes124* modeled upon the attributes in the Internet Printing Protocol (IPP) version125* 1.1. The standard attribute classes are in the subpackage126* {@code javax.print.attribute.standard} to keep the actual attribute classes127* conceptually separate from the generic apparatus defined in package128* {@code javax.print.attribute}.129*130* <h2>Attribute Sets</h2>131* A client usually needs to provide more than one processing instruction when132* submitting a print job. For example, the client might need to specify a media133* size of A4 and a landscape orientation. To send more than one processing134* instruction, the client collects the attributes into an attribute set, which135* the Java Print Service API represents with the136* <a href="AttributeSet.html">AttributeSet</a> interface.137* <p>138* The {@code AttributeSet} interface is similar to the139* {@link java.util.Map Map} interface: it provides a map of140* key to values, in which each key is unique and can contain no more than one141* value. However, the {@code AttributeSet} interface is designed to142* specifically support the needs of the Java Print Service API. An143* {@code AttributeSet} requires that:144* <ol type=1>145* <li>Each key in an {@code AttributeSet} corresponds to a category, and the146* value of the key can only be one of the attribute values that belong to the147* category represented by the key. Thus, unlike a {@code Map}, an148* {@code AttributeSet} restricts the possible values of a key: an attribute149* category cannot be set to an attribute value that does not belong to that150* category.151* <li>No two attributes from the same category can exist in the same set. For152* example, an attribute collection must not contain both a "one-sided"153* attribute and a "two-sided" attribute because these two attributes give the154* printer conflicting instructions.155* <li>Only attributes implementing the {@code Attribute} interface can be156* added to the set.157* </ol>158* The {@code javax.print.attribute} package includes159* <a href="HashAttributeSet.html">HashAttributeSet</a> as a concrete160* implementation of the attribute set interface. {@code HashAttributeSet}161* provides an attribute set based on a hash map. You can use this162* implementation or provide your own implementation of interface163* {@code AttributeSet}.164* <p>165* The Java Print Service API provides four specializations of an attribute set166* that are restricted to contain just one of the four kinds of attributes, as167* discussed in the <a href="#role">Attribute Roles</a> section:168* <ul>169* <li><a href="DocAttributeSet.html">DocAttributeSet</a>170* <li><a href="PrintRequestAttributeSet.html">PrintRequestAttributeSet</a>171* <li><a href="PrintJobAttributeSet.html"> PrintJobAttributeSet</a>172* <li><a href="PrintServiceAttributeSet.html">PrintServiceAttributeSet</a>173* </ul>174* Notice that only four kinds of attribute sets are listed here, but there are175* five kinds of attributes. Interface176* <a href="SupportedValuesAttribute.html">SupportedValuesAttribute</a> denotes177* an attribute that gives the supported values for another attribute.178* Supported-values attributes are never aggregated into attribute sets, so179* there is no attribute set subinterface defined for them.180* <p>181* In some contexts, an attribute set is read-only, which means that the client182* is only allowed to examine an attribute set's contents but not change them.183* In other contexts, the attribute set is read-write, which means that the184* client is allowed both to examine and to change an attribute set's contents.185* For a read-only attribute set, calling a mutating operation throws an186* {@code UnmodifiableSetException}.187* <p>188* Package {@code javax.print.attribute} includes one concrete implementation of189* each of the attribute set subinterfaces:190* <ul>191* <li><a href="HashDocAttributeSet.html"> HashDocAttributeSet</a>192* <li><a href="HashPrintRequestAttributeSet.html">193* HashPrintRequestAttributeSet</a>,194* <li><a href="HashPrintJobAttributeSet.html">HashPrintJobAttributeSet</a>,195* <li><a href="HashPrintServiceAttributeSet.html">196* HashPrintServiceAttributeSet</a>.197* </ul>198* All of these classes extend199* <a href="HashAttributeSet.html">HashAttributeSet</a> and enforce the200* restriction that the attribute set is only allowed to contain the201* corresponding kind of attribute.202*203* <h2>Attribute Class Design</h2>204* An attribute value is a small, atomic data item, such as an integer or an205* enumerated value. The Java Print Service API does not use primitive data206* types, such as int, to represent attribute values for these reasons:207* <ul>208* <li>Primitive data types are not type-safe. For example, a compiler should209* not allow a "copies" attribute value to be used for a "sides" attribute.210* <li>Some attributes must be represented as a record of several values. One211* example is printer resolution, which requires two numbers, such as 600 and212* 300 representing 600 x 300 dpi.213* </ul>214* For type-safety and to represent all attributes uniformly, the Java Print215* Service API defines each attribute category as a class, such as class216* {@code Copies}, class <a href="standard/Sides.html">Sides</a>, and class217* <a href="standard/PrinterResolution.html">PrinterResolution</a>. Each218* attribute class wraps one or more primitive data items containing the219* attribute's value. Attribute set operations perform frequent comparisons220* between attribute category objects when adding attributes, finding existing221* attributes in the same category, and looking up an attribute given its222* category. Because an attribute category is represented by a class, fast223* attribute-value comparisons can be performed with the {@code Class.equals}224* method.225* <p>226* Even though the Java Print Service API includes a large number of different227* attribute categories, there are only a few different types of attribute228* values. Most attributes can be represented by a small number of data types,229* such as: integer values, integer ranges, text, or an enumeration of integer230* values. The type of the attribute value that a category accepts is called the231* attribute's abstract syntax. To provide consistency and reduce code232* duplication, the Java Print Service API defines abstract syntax classes to233* represent each abstract syntax, and these classes are used as the parent of234* standard attributes whenever possible. The abstract syntax classes are:235* <ul>236* <li><a href="EnumSyntax.html">EnumSyntax</a> provides a type-safe237* enumeration in which enumerated values are represented as singleton238* objects. Each enumeration singleton is an instance of the enumeration class239* that wraps a hidden int value.240* <li><a href="IntegerSyntax.html">IntegerSyntax</a> is the abstract syntax241* for integer-valued attributes.242* <li><a href="TextSyntax.html">TextSyntax</a> is the abstract syntax for243* text-valued attributes, and includes a locale giving the text string's244* natural language.245* <li><a href="SetOfIntegerSyntax.html">SetOfIntegerSyntax</a> is the246* abstract syntax for attributes representing a range or set of integers247* <li><a href="ResolutionSyntax.html">ResolutionSyntax</a> is the abstract248* syntax for attributes representing resolution values, such as 600x300249* dpi.250* <li><a href="Size2DSyntax.html">Size2DSyntax</a> is the abstract syntax for251* attributes representing a two-dimensional size, such as a paper size of252* 8.5 x 11 inches.253* <li><a href="DateTimeSyntax.html">DateTimeSyntax</a> is the abstract syntax254* for attributes whose value is a date and time.255* <li><a href="URISyntax.html">URISyntax</a> is the abstract syntax for256* attributes whose value is a Uniform Resource Indicator.257* </ul>258* The abstract syntax classes are independent of the attributes that use them.259* In fact, applications that have nothing to do with printing can use the260* abstract syntax classes. Although most of the standard attribute classes261* extend one of the abstract syntax classes, no attribute class is required to262* extend one of these classes. The abstract syntax classes merely provide a263* convenient implementation that can be shared by many attribute classes.264* <p>265* Each attribute class implements the {@code Attribute} interface, either266* directly or indirectly, to mark it as a printing attribute. An attribute267* class that can appear in restricted attribute sets in certain contexts also268* implements one or more subinterfaces of {@code Attribute}. Most attribute269* classes also extend the appropriate abstract syntax class to get the270* implementation. Consider the {@code Sides} attribute class:271* <blockquote>272* <pre>{@code273* public class Sides274* extends EnumSyntax275* implements DocAttribute, PrintRequestAttribute, PrintJobAttribute276* {277* public final Object getCategory()278* {279* return Sides.class;280* }281* ...282* }}283* </pre>284* </blockquote>285* <p>286* Since every attribute class implements {@code Attribute}, every attribute287* class must provide an implementation for the288* {@link javax.print.attribute.Attribute#getCategory() getCategory} method,289* which returns the attribute category. In the case of {@code Sides}, the290* {@code getCategory} method returns {@code Sides.class}. The291* {@code getCategory} method is final to ensure that any vendor-defined292* subclasses of a standard attribute class appear in the same category. Every293* attribute object is immutable once constructed so that attribute object294* references can be passed around freely. To get a different attribute value,295* construct a different attribute object.296*297* <h2>Attribute Vendors</h2>298* The Java Print Service API is designed so that vendors can:299* <ul>300* <li>define new vendor-specific values for any standard attribute defined in301* <a href="standard/package-summary.html">javax.print.attribute.standard</a>.302* <li>define new attribute categories representing the vendor printer's303* proprietary capabilities not already supported by the standard attributes.304* </ul>305* To define a new value for an attribute, a client can construct instances of306* such attributes with arbitrary values at runtime. However, an enumerated307* attribute using an abstract syntax class of {@code EnumSyntax} specifies all308* the possible attribute values at compile time as singleton instances of the309* attribute class. This means that new enumerated values cannot be constructed310* at run time. To define new vendor-specific values for a standard enumerated311* attribute, the vendor must define a new attribute class specifying the new312* singleton instances. To ensure that the new attribute values fall in the same313* category as the standard attribute values, the new attribute class must be a314* subclass of the standard attribute class.315* <p>316* To define a new attribute category, a vendor defines a new attribute class.317* This attribute class, like the standard attribute classes, implements318* {@code Attribute} or one of its subinterfaces and extends an abstract syntax319* class. The vendor can either use an existing abstract syntax class or define320* a new one. The new vendor-defined attribute can be used wherever an321* {@code Attribute} is used, such as in an {@code AttributeSet}.322*323* <h2>Using Attributes</h2>324* A typical printing application uses the {@code PrintRequestAttributeSet}325* because print-request attributes are the types of attributes that client326* usually specifies. This example demonstrates creating an attribute set of327* print-request attributes and locating a printer that can print the document328* according to the specified attributes:329* <blockquote>330* <pre>{@code331* FileInputStream psStream;332* try {333* psstream = new FileInputStream("file.ps");334* } catch (FileNotFoundException ffne) {335* }336* if (psstream == null) {337* return;338* }339* //Set the document type. See the DocFlavor documentation for340* //more information.341* DocFlavor psInFormat = DocFlavor.INPUT_STREAM.POSTSCRIPT;342* Doc myDoc = new SimpleDoc(pstream, psInFormat, null);343* PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();344* aset.add(new Copies(5));345* aset.add(MediaSize.A4);346* aset.add(Sides.DUPLEX);347* PrintService[] services =348* PrintServiceLookup.lookupPrintServices(psInFormat, aset);349* if (services.length > 0) {350* DocPrintJob job = services[0].createPrintJob();351* try {352* job.print(myDoc, aset);353* } catch (PrintException pe) {}354* }355* }</pre>356* </blockquote>357* <p>358* Please note: In the {@code javax.print} APIs, a {@code null} reference359* parameter to methods is incorrect unless explicitly documented on the method360* as having a meaningful interpretation. Usage to the contrary is incorrect361* coding and may result in a run time exception either immediately or at some362* later time. {@code IllegalArgumentException} and {@code NullPointerException}363* are examples of typical and acceptable run time exceptions for such cases.364*365* @since 1.4366*/367package javax.print.attribute;368369370