001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.print;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.CharArrayReader;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.Reader;
025: import java.io.StringReader;
026: import javax.print.attribute.DocAttributeSet;
027:
028: public final class SimpleDoc implements Doc {
029: private Object printdata;
030:
031: private DocFlavor flavor;
032:
033: private DocAttributeSet attributes;
034:
035: private Reader reader;
036:
037: private InputStream instream;
038:
039: public SimpleDoc(Object printData, DocFlavor docflavor,
040: DocAttributeSet docattributes) {
041: /*
042: * IllegalArgumentException - if flavor or printData is null, or the
043: * printData does not correspond to the specified doc flavor--for
044: * example, the data is not of the type specified as the representation
045: * in the DocFlavor.
046: */
047: if (docflavor == null || printData == null) {
048: throw new IllegalArgumentException("Argument is null");
049: }
050: try {
051: Class<?> clazz = Class.forName(docflavor
052: .getRepresentationClassName());
053: if (!clazz.isInstance(printData)) {
054: throw new IllegalArgumentException("");
055: }
056: } catch (Exception e) {
057: throw new IllegalArgumentException(
058: "Wrong type of print data");
059: }
060: this .printdata = printData;
061: this .flavor = docflavor;
062: this .attributes = docattributes;
063: this .reader = null;
064: this .instream = null;
065: }
066:
067: public DocAttributeSet getAttributes() {
068: return attributes;
069: }
070:
071: public DocFlavor getDocFlavor() {
072: return flavor;
073: }
074:
075: public Object getPrintData() throws IOException {
076: return printdata;
077: }
078:
079: public Reader getReaderForText() throws IOException {
080: synchronized (this ) {
081: if (reader != null) {
082: return reader;
083: }
084: if (printdata instanceof Reader) {
085: reader = (Reader) printdata;
086: } else if (printdata instanceof char[]) {
087: reader = new CharArrayReader((char[]) printdata);
088: } else if (printdata instanceof String) {
089: reader = new StringReader((String) printdata);
090: }
091: }
092: return reader;
093: }
094:
095: public InputStream getStreamForBytes() throws IOException {
096: synchronized (this ) {
097: if (instream != null) {
098: return instream;
099: }
100: if (printdata instanceof InputStream) {
101: instream = (InputStream) printdata;
102: } else if (printdata instanceof byte[]) {
103: instream = new ByteArrayInputStream((byte[]) printdata);
104: }
105: }
106: return instream;
107: }
108: }
|