Source for javax.print.DocFlavor

   1: /* DocFlavor.java --
   2:    Copyright (C) 2004 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.Serializable;
  42: import java.util.HashMap;
  43: import java.util.Iterator;
  44: import java.util.Map;
  45: 
  46: /**
  47:  * @author Michael Koch (konqueror@gmx.de)
  48:  */
  49: public class DocFlavor implements Cloneable, Serializable
  50: {
  51:   /**
  52:    * @author Michael Koch (konqueror@gmx.de)
  53:    */
  54:   public static class BYTE_ARRAY
  55:     extends DocFlavor
  56:   {
  57:     private static final long serialVersionUID = -9065578006593857475L;
  58: 
  59:     public static final BYTE_ARRAY AUTOSENSE = new BYTE_ARRAY("application/octet-stream");
  60:     public static final BYTE_ARRAY GIF = new BYTE_ARRAY("image/gif");
  61:     public static final BYTE_ARRAY JPEG = new BYTE_ARRAY("image/jpeg");
  62:     public static final BYTE_ARRAY PCL = new BYTE_ARRAY("application/vnd.hp-PCL");
  63:     public static final BYTE_ARRAY PDF = new BYTE_ARRAY("application/pdf");
  64:     public static final BYTE_ARRAY PNG = new BYTE_ARRAY("image/png");
  65:     public static final BYTE_ARRAY POSTSCRIPT = new BYTE_ARRAY("application/postscript");
  66:     public static final BYTE_ARRAY TEXT_HTML_HOST = new BYTE_ARRAY("text/html");
  67:     public static final BYTE_ARRAY TEXT_HTML_US_ASCII = new BYTE_ARRAY("text/html; charset=us-ascii");
  68:     public static final BYTE_ARRAY TEXT_HTML_UTF_16 = new BYTE_ARRAY("text/html; charset=utf-16");
  69:     public static final BYTE_ARRAY TEXT_HTML_UTF_16BE = new BYTE_ARRAY("text/html; charset=utf-16be");
  70:     public static final BYTE_ARRAY TEXT_HTML_UTF_16LE = new BYTE_ARRAY("text/html; charset=utf-16le");
  71:     public static final BYTE_ARRAY TEXT_HTML_UTF_8 = new BYTE_ARRAY("text/html; charset=utf-8");
  72:     public static final BYTE_ARRAY TEXT_PLAIN_HOST = new BYTE_ARRAY("text/plain");
  73:     public static final BYTE_ARRAY TEXT_PLAIN_US_ASCII = new BYTE_ARRAY("text/plain; charset=us-ascii");
  74:     public static final BYTE_ARRAY TEXT_PLAIN_UTF_16 = new BYTE_ARRAY("text/plain; charset=utf-16");
  75:     public static final BYTE_ARRAY TEXT_PLAIN_UTF_16BE = new BYTE_ARRAY("text/plain; charset=utf-16be");
  76:     public static final BYTE_ARRAY TEXT_PLAIN_UTF_16LE = new BYTE_ARRAY("text/plain; charset=utf-16le");
  77:     public static final BYTE_ARRAY TEXT_PLAIN_UTF_8 = new BYTE_ARRAY("text/plain; charset=utf-8");
  78:     
  79:     public BYTE_ARRAY(String mimeType)
  80:     {
  81:       super(mimeType, "[B");
  82:     }
  83:   }
  84:   
  85:   /**
  86:    * @author Michael Koch (konqueror@gmx.de)
  87:    */
  88:   public static class CHAR_ARRAY
  89:     extends DocFlavor
  90:   {
  91:     private static final long serialVersionUID = -8720590903724405128L;
  92:     
  93:     public static final DocFlavor.CHAR_ARRAY TEXT_HTML = new CHAR_ARRAY("text/html; charset=utf-16");
  94:     public static final DocFlavor.CHAR_ARRAY TEXT_PLAIN = new CHAR_ARRAY("text/plain; charset=utf-16");
  95: 
  96:     public CHAR_ARRAY(String mimeType)
  97:     {
  98:       super(mimeType, "[C");
  99:     }
 100:   }
 101:   
 102:   /**
 103:    * @author Michael Koch (konqueror@gmx.de)
 104:    */
 105:   public static class INPUT_STREAM
 106:     extends DocFlavor
 107:   {
 108:     private static final long serialVersionUID = -7045842700749194127L;
 109: 
 110:     public static final INPUT_STREAM AUTOSENSE = new INPUT_STREAM("application/octet-stream");
 111:     public static final INPUT_STREAM GIF = new INPUT_STREAM("image/gif");
 112:     public static final INPUT_STREAM JPEG = new INPUT_STREAM("image/jpeg");
 113:     public static final INPUT_STREAM PCL = new INPUT_STREAM("application/vnd.hp-PCL");
 114:     public static final INPUT_STREAM PDF = new INPUT_STREAM("application/pdf");
 115:     public static final INPUT_STREAM PNG = new INPUT_STREAM("image/png");
 116:     public static final INPUT_STREAM POSTSCRIPT = new INPUT_STREAM("application/postscript");
 117:     public static final INPUT_STREAM TEXT_HTML_HOST = new INPUT_STREAM("text/html");
 118:     public static final INPUT_STREAM TEXT_HTML_US_ASCII = new INPUT_STREAM("text/html; charset=us-ascii");
 119:     public static final INPUT_STREAM TEXT_HTML_UTF_16 = new INPUT_STREAM("text/html; charset=utf-16");
 120:     public static final INPUT_STREAM TEXT_HTML_UTF_16BE = new INPUT_STREAM("text/html; charset=utf-16be");
 121:     public static final INPUT_STREAM TEXT_HTML_UTF_16LE = new INPUT_STREAM("text/html; charset=utf-16le");
 122:     public static final INPUT_STREAM TEXT_HTML_UTF_8 = new INPUT_STREAM("text/html; charset=utf-8");
 123:     public static final INPUT_STREAM TEXT_PLAIN_HOST = new INPUT_STREAM("text/plain");
 124:     public static final INPUT_STREAM TEXT_PLAIN_US_ASCII = new INPUT_STREAM("text/plain; charset=us-ascii");
 125:     public static final INPUT_STREAM TEXT_PLAIN_UTF_16 = new INPUT_STREAM("text/plain; charset=utf-16");
 126:     public static final INPUT_STREAM TEXT_PLAIN_UTF_16BE = new INPUT_STREAM("text/plain; charset=utf-16be");
 127:     public static final INPUT_STREAM TEXT_PLAIN_UTF_16LE = new INPUT_STREAM("text/plain; charset=utf-16le");
 128:     public static final INPUT_STREAM TEXT_PLAIN_UTF_8 = new INPUT_STREAM("text/plain; charset=utf-8");
 129:     
 130:     public INPUT_STREAM(String mimeType)
 131:     {
 132:       super(mimeType, "java.io.InputStream");
 133:     }
 134:   }
 135:   
 136:   /**
 137:    * @author Michael Koch (konqueror@gmx.de)
 138:    */
 139:   public static class READER
 140:     extends DocFlavor
 141:   {
 142:     private static final long serialVersionUID = 7100295812579351567L;
 143: 
 144:     public static final DocFlavor.READER TEXT_HTML = new READER("text/html; charset=utf-16");
 145:     public static final DocFlavor.READER TEXT_PLAIN = new READER("text/plain; charset=utf-16");
 146:     
 147:     public READER(String mimeType)
 148:     {
 149:       super(mimeType, "java.io.Reader");
 150:     }
 151:   }
 152:   
 153:   /**
 154:    * @author Michael Koch (konqueror@gmx.de)
 155:    */
 156:   public static class SERVICE_FORMATTED
 157:     extends DocFlavor
 158:   {
 159:     private static final long serialVersionUID = 6181337766266637256L;
 160: 
 161:     public static final DocFlavor.SERVICE_FORMATTED PAGEABLE = new SERVICE_FORMATTED("java.awt.print.Pageable");
 162:     public static final DocFlavor.SERVICE_FORMATTED PRINTABLE = new SERVICE_FORMATTED("java.awt.print.Printable");
 163:     public static final DocFlavor.SERVICE_FORMATTED RENDERABLE_IMAGE = new SERVICE_FORMATTED("java.awt.image.renderable.RenderableImage");
 164:     
 165:     public SERVICE_FORMATTED(String className)
 166:     {
 167:       super("application/x-java-jvm-local-objectref", className);
 168:     }
 169:   }
 170:   
 171:   /**
 172:    * @author Michael Koch (konqueror@gmx.de)
 173:    */
 174:   public static class STRING
 175:     extends DocFlavor
 176:   {
 177:     private static final long serialVersionUID = 4414407504887034035L;
 178: 
 179:     public static final DocFlavor.STRING TEXT_HTML = new STRING("text/html; charset=utf-16");
 180:     public static final DocFlavor.STRING TEXT_PLAIN = new STRING("text/plain; charset=utf-16");
 181:     
 182:     public STRING(String mimeType)
 183:     {
 184:       super(mimeType, "java.lang.String");
 185:     }
 186:   }
 187:   
 188:   /**
 189:    * @author Michael Koch (konqueror@gmx.de)
 190:    */
 191:   public static class URL
 192:     extends DocFlavor
 193:   {
 194:     private static final long serialVersionUID = 2936725788144902062L;
 195: 
 196:     public static final DocFlavor.URL AUTOSENSE = new URL("application/octet-stream");
 197:     public static final DocFlavor.URL GIF = new URL("image/gif");
 198:     public static final DocFlavor.URL JPEG = new URL("image/jpeg");
 199:     public static final DocFlavor.URL PCL = new URL("application/vnd.hp-PCL");
 200:     public static final DocFlavor.URL PDF = new URL("application/pdf");
 201:     public static final DocFlavor.URL PNG = new URL("image/png");
 202:     public static final DocFlavor.URL POSTSCRIPT = new URL("application/postscript");
 203:     public static final DocFlavor.URL TEXT_HTML_HOST = new URL("text/html");
 204:     public static final DocFlavor.URL TEXT_HTML_US_ASCII = new URL("text/html; charset=us-ascii");
 205:     public static final DocFlavor.URL TEXT_HTML_UTF_16 = new URL("text/html; charset=utf-16");
 206:     public static final DocFlavor.URL TEXT_HTML_UTF_16BE = new URL("text/html; charset=utf-16be");
 207:     public static final DocFlavor.URL TEXT_HTML_UTF_16LE = new URL("text/html; charset=utf-16le");
 208:     public static final DocFlavor.URL TEXT_HTML_UTF_8 = new URL("text/html; charset=utf-8");
 209:     public static final DocFlavor.URL TEXT_PLAIN_HOST = new URL("text/plain");
 210:     public static final DocFlavor.URL TEXT_PLAIN_US_ASCII = new URL("text/plain; charset=us-ascii");
 211:     public static final DocFlavor.URL TEXT_PLAIN_UTF_16 = new URL("text/plain; charset=utf-16");
 212:     public static final DocFlavor.URL TEXT_PLAIN_UTF_16BE = new URL("text/plain; charset=utf-16be");
 213:     public static final DocFlavor.URL TEXT_PLAIN_UTF_16LE = new URL("text/plain; charset=utf-16le");
 214:     public static final DocFlavor.URL TEXT_PLAIN_UTF_8 = new URL("text/plain; charset=utf-8");
 215:     
 216:     public URL(String mimeType)
 217:     {
 218:       super(mimeType, "java.net.URL");
 219:     }
 220:   }
 221:   
 222:   private static final long serialVersionUID = -4512080796965449721L;
 223:   
 224:   // FIXME: Get the host encoding from somewhere. Note that the new String is to make
 225:   // sure the field won't be a compile time constant.
 226:   public static final String hostEncoding = new String("US-ASCII");
 227: 
 228:   private String mediaSubtype;
 229:   private String mediaType;
 230:   private String className;
 231:   private HashMap params = new HashMap();
 232:   
 233:   public DocFlavor(String mimeType, String className)
 234:   {
 235:     if (mimeType == null || className == null)
 236:       throw new NullPointerException();
 237: 
 238:     parseMimeType(mimeType);
 239:     this.className = className;
 240:   }
 241: 
 242:   private void parseMimeType(String mimeType)
 243:   {
 244:     // FIXME: This method is know to be not completely correct, but it works for now.
 245:     
 246:     int pos = mimeType.indexOf(';');
 247: 
 248:     if (pos != -1)
 249:       {
 250:     String tmp = mimeType.substring(pos + 2);
 251:     mimeType = mimeType.substring(0, pos);
 252:     pos = tmp.indexOf('=');
 253:     params.put(tmp.substring(0, pos), tmp.substring(pos + 1));
 254:       }
 255: 
 256:     pos = mimeType.indexOf('/');
 257: 
 258:     if (pos == -1)
 259:       throw new IllegalArgumentException();
 260: 
 261:     mediaType = mimeType.substring(0, pos);
 262:     mediaSubtype = mimeType.substring(pos + 1);
 263:   }
 264:   
 265:   public boolean equals(Object obj)
 266:   {
 267:     if (! (obj instanceof DocFlavor))
 268:       return false;
 269: 
 270:     DocFlavor tmp = (DocFlavor) obj;
 271: 
 272:     return (getMimeType().equals(tmp.getMimeType())
 273:         && getRepresentationClassName().equals(tmp.getRepresentationClassName()));
 274:   }
 275: 
 276:   public String getMediaSubtype()
 277:   {
 278:     return mediaSubtype;
 279:   }
 280: 
 281:   public String getMediaType()
 282:   {
 283:     return mediaType;
 284:   }
 285: 
 286:   public String getMimeType()
 287:   {
 288:     // FIXME: Check if this algorithm is correct.
 289:     
 290:     String mimeType = getMediaType() + "/" + getMediaSubtype();
 291:     Iterator it = params.entrySet().iterator();
 292: 
 293:     while (it.hasNext())
 294:       {
 295:     Map.Entry entry = (Map.Entry) it.next();
 296:     mimeType += "; " + entry.getKey() + "=\"" + entry.getValue() + "\"";
 297:       }
 298: 
 299:     return mimeType;
 300:   }
 301: 
 302:   public String getParameter(String paramName)
 303:   {
 304:     if (paramName == null)
 305:       throw new NullPointerException();
 306:     
 307:     return (String) params.get(paramName); 
 308:   }
 309: 
 310:   public String getRepresentationClassName()
 311:   {
 312:     return className;
 313:   }
 314: 
 315:   public int hashCode()
 316:   {
 317:     return ((mediaType.hashCode()
 318:          * mediaSubtype.hashCode()
 319:          * className.hashCode()) ^ params.hashCode());
 320:   }
 321: 
 322:   public String toString()
 323:   {
 324:     return getMimeType();
 325:   }
 326: }