Source for javax.print.SimpleDoc

   1: /* SimpleDoc.java -- 
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.print;
  40: 
  41: import java.io.ByteArrayInputStream;
  42: import java.io.CharArrayReader;
  43: import java.io.IOException;
  44: import java.io.InputStream;
  45: import java.io.Reader;
  46: import java.io.StringReader;
  47: 
  48: import javax.print.attribute.AttributeSetUtilities;
  49: import javax.print.attribute.DocAttributeSet;
  50: 
  51: /**
  52:  * Simple implementation of the <code>Doc</code> interface capable of handling 
  53:  * the predefined document flavors of <code>DocFlavor</code>.
  54:  * <p>
  55:  * This implementation can construct a reader or stream for the service from 
  56:  * the print data and ensures that always the same object is returned on each
  57:  * method call. It does simple checks that the supplied data matches the 
  58:  * specified flavor of the doc object and supports thread safe access.
  59:  * </p> 
  60:  * 
  61:  * @author Wolfgang Baer (WBaer@gmx.de)
  62:  */
  63: public final class SimpleDoc implements Doc
  64: {  
  65:   private final Object printData;
  66:   private final DocFlavor flavor;
  67:   private final DocAttributeSet attributes;
  68:   
  69:   private InputStream stream;
  70:   private Reader reader;
  71: 
  72:   /**
  73:    * Constructs a SimpleDoc with the specified print data, doc flavor and doc attribute set.
  74:    * @param printData the object with the data to print.
  75:    * @param flavor the document flavor of the print data.
  76:    * @param attributes the attributes of the doc (may be <code>null</code>).
  77:    * 
  78:    * @throws IllegalArgumentException if either <code>printData</code> or
  79:    *   <code>flavor</code> are <code>null</code>, or the print data is not
  80:    *   supplied in the document format specified by the given flavor object.
  81:    */
  82:   public SimpleDoc(Object printData, DocFlavor flavor, 
  83:       DocAttributeSet attributes)
  84:   {
  85:     if (printData == null || flavor == null)
  86:       throw new IllegalArgumentException("printData/flavor may not be null");
  87:     
  88:     if (! (printData.getClass().getName().equals(
  89:            flavor.getRepresentationClassName())
  90:         || flavor.getRepresentationClassName().equals("java.io.Reader")
  91:            && printData instanceof Reader
  92:         || flavor.getRepresentationClassName().equals("java.io.InputStream")
  93:            && printData instanceof InputStream))
  94:       {
  95:         throw new IllegalArgumentException("data is not of declared flavor type");
  96:       }          
  97:     
  98:     this.printData = printData;
  99:     this.flavor = flavor;
 100:     
 101:     if (attributes != null)
 102:       this.attributes = AttributeSetUtilities.unmodifiableView(attributes);
 103:     else
 104:       this.attributes = null;
 105:     
 106:     stream = null;
 107:     reader = null;
 108:   }
 109: 
 110:   /**
 111:    * Returns the unmodifiable view of the attributes of this doc object.
 112:    * <p>
 113:    * The attributes of this doc's attributes set overrides attributes of 
 114:    * the same category in the print job's attribute set. If an attribute 
 115:    * is not available in this doc's attributes set or <code>null</code>
 116:    * is returned the attributes of the same category of the print job are
 117:    * used. 
 118:    * </p>
 119:    * 
 120:    * @return The unmodifiable attributes set, or <code>null</code>.
 121:    */
 122:   public DocAttributeSet getAttributes()
 123:   {
 124:     return attributes;
 125:   }
 126: 
 127:   /**
 128:    * Returns the flavor of this doc objects print data.
 129:    * 
 130:    * @return The document flavor.
 131:    */
 132:   public DocFlavor getDocFlavor()
 133:   {
 134:     return flavor;
 135:   }
 136: 
 137:   /**
 138:    * Returns the print data of this doc object.
 139:    * <p>
 140:    * The returned object is an instance as described by the associated
 141:    * document flavor ({@link DocFlavor#getRepresentationClassName()})
 142:    * and can be cast to this representation class.
 143:    * </p>
 144:    * 
 145:    * @return The print data in the representation class.
 146:    * @throws IOException if representation class is a stream and I/O
 147:    * exception occures.
 148:    */
 149:   public Object getPrintData() throws IOException
 150:   {
 151:     return printData;
 152:   }
 153: 
 154:   /**
 155:    * Returns a <code>Reader</code> object for extracting character print data
 156:    * from this document.
 157:    * <p>
 158:    * This method is supported if the document flavor is of type:
 159:    * <ul>
 160:    * <li><code>char[]</code></li>
 161:    * <li><code>java.lang.String</code></li>
 162:    * <li><code>java.io.Reader</code></li>
 163:    * </ul>
 164:    * otherwise this method returns <code>null</code>.
 165:    * </p> 
 166:    * 
 167:    * @return The <code>Reader</code> object, or <code>null</code>.
 168:    * 
 169:    * @throws IOException if an error occurs.
 170:    */
 171:   public Reader getReaderForText() throws IOException
 172:   {
 173:     synchronized (this)
 174:       {
 175:         // construct the reader if applicable on request
 176:         if (reader == null)
 177:           {
 178:             if (flavor instanceof DocFlavor.CHAR_ARRAY)
 179:               reader = new CharArrayReader((char[]) printData);
 180:             else if (flavor instanceof DocFlavor.STRING)
 181:               reader = new StringReader((String) printData);
 182:             else if (flavor instanceof DocFlavor.READER)
 183:               reader = (Reader) printData;
 184:           }
 185:         
 186:         return reader;
 187:       }   
 188:   }
 189: 
 190:   /**
 191:    * Returns an <code>InputStream</code> object for extracting byte print data
 192:    * from this document.
 193:    * <p>
 194:    * This method is supported if the document flavor is of type:
 195:    * <ul>
 196:    * <li><code>byte[]</code></li>
 197:    * <li><code>java.io.InputStream</code></li>
 198:    * </ul>
 199:    * otherwise this method returns <code>null</code>.
 200:    * </p> 
 201:    * 
 202:    * @return The <code>InputStream</code> object, or <code>null</code>.
 203:    * 
 204:    * @throws IOException if an error occurs.
 205:    */
 206:   public InputStream getStreamForBytes() throws IOException
 207:   {
 208:     synchronized (this)
 209:       {
 210:         // construct the stream if applicable on request
 211:         if (stream == null)
 212:           {
 213:             if (flavor instanceof DocFlavor.BYTE_ARRAY)
 214:               stream = new ByteArrayInputStream((byte[]) printData);
 215:             else if (flavor instanceof DocFlavor.INPUT_STREAM)
 216:               stream = (InputStream) printData;
 217:           }
 218:         
 219:         return stream;
 220:       }    
 221:   }
 222: 
 223: }