Source for gnu.javax.print.ipp.attribute.UnknownAttribute

   1: /* UnknownAttribute.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 gnu.javax.print.ipp.attribute;
  40: 
  41: import gnu.javax.print.ipp.IppUtilities;
  42: import gnu.javax.print.ipp.IppValueTag;
  43: 
  44: import java.net.URI;
  45: import java.net.URISyntaxException;
  46: 
  47: import javax.print.attribute.Attribute;
  48: 
  49: /**
  50:  * UnknownAttribute holds all the parsed Attribute information.
  51:  * It provides methods to get the value-tag, name and value.
  52:  *
  53:  * @author Wolfgang Baer (WBaer@gmx.de)
  54:  */
  55: public final class UnknownAttribute implements Attribute
  56: {
  57:   private byte tag;
  58:   private String name;
  59:   private byte[] value;
  60: 
  61:   /**
  62:    * Creates a <code>UnknownAttribute</code> object with the given values.
  63:    *
  64:    * @param tag the value tag
  65:    * @param name the attribute name
  66:    * @param value the byte[] with the value
  67:    */
  68:   public UnknownAttribute(byte tag, String name, byte[] value)
  69:   {
  70:     this.tag = tag;
  71:     this.name = name;
  72:     this.value = value;
  73:   }
  74: 
  75:   /**
  76:    * Returns category of this class.
  77:    *
  78:    * @return The class <code>UnknownAttribute</code> itself.
  79:    */
  80:   public Class<? extends Attribute> getCategory()
  81:   {
  82:     return UnknownAttribute.class;
  83:   }
  84: 
  85:   /**
  86:    * Returns the name of this attribute.
  87:    *
  88:    * @return The name attributes IPP name.
  89:    */
  90:   public String getName()
  91:   {
  92:     return name;
  93:   }
  94: 
  95:   /**
  96:    * Returns the value tag
  97:    * @return The tag.
  98:    *
  99:    * @see gnu.javax.print.ipp.IppValueTag
 100:    */
 101:   public byte getValueTag()
 102:   {
 103:     return tag;
 104:   }
 105: 
 106:   /**
 107:    * Returns the name of the attribute.
 108:    * @return The name.
 109:    */
 110:   public String getAttributeName()
 111:   {
 112:     return name;
 113:   }
 114: 
 115:   /**
 116:    * Returns the attribute value origin byte array.
 117:    * @return The value.
 118:    */
 119:   public byte[] getAttributeValue()
 120:   {
 121:     return value;
 122:   }
 123: 
 124:   /**
 125:    * Returns the attribute value decoded as String.
 126:    * @return The value as String.
 127:    */
 128:   public String getAttributeValueAsString()
 129:   {
 130:     return new String(value);
 131:   }
 132: 
 133:   /**
 134:    * Returns the attribute value decoded as int.
 135:    * @return The value as int.
 136:    */
 137:   public int getAttributeValueAsInt()
 138:   {
 139:     return IppUtilities.convertToInt(value);
 140:   }
 141: 
 142:   /**
 143:    * Returns the attribute value decoded as an URI.
 144:    * @return The value as URI.
 145:    */
 146:   public URI getAttributeValueAsUri()
 147:   {
 148:     try
 149:       {
 150:         return new URI(new String(value));
 151:       }
 152:     catch (URISyntaxException e)
 153:       {
 154:         return null;
 155:       }
 156:   }
 157: 
 158:   /**
 159:    * Provides a string representation for some default
 160:    * tag types (e.g. int, rangeofinteger, string, uri).
 161:    * For other more complex types "No conversion found."
 162:    * is returned.
 163:    */
 164:   public String toString()
 165:   {
 166:     switch (tag)
 167:       {
 168:       case IppValueTag.INTEGER:
 169:         return "" + getAttributeValueAsInt();
 170:       case IppValueTag.RANGEOFINTEGER:
 171:         int lower = IppUtilities.convertToInt(value[0], value[1],
 172:                                               value[2], value[3]);
 173:         int upper = IppUtilities.convertToInt(value[4], value[5],
 174:                                               value[6], value[7]);
 175:         return lower + "-" + upper;
 176:       case IppValueTag.URI:
 177:         return getAttributeValueAsUri().toString();
 178:       case IppValueTag.KEYWORD:
 179:       case IppValueTag.URI_SCHEME:
 180:       case IppValueTag.CHARSET:
 181:       case IppValueTag.NATURAL_LANGUAGE:
 182:       case IppValueTag.MIME_MEDIA_TYPE:
 183:       case IppValueTag.NAME_WITHOUT_LANGUAGE:
 184:       case IppValueTag.TEXT_WITHOUT_LANGUAGE:
 185:         return getAttributeValueAsString();
 186:       default:
 187:         return "No conversion found.";
 188:       }
 189:   }
 190: }