Source for gnu.javax.print.ipp.IppUtilities

   1: /* IppUtilities.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;
  40: 
  41: import gnu.javax.print.ipp.attribute.DetailedStatusMessage;
  42: import gnu.javax.print.ipp.attribute.DocumentAccessError;
  43: import gnu.javax.print.ipp.attribute.StatusMessage;
  44: import gnu.javax.print.ipp.attribute.defaults.CopiesDefault;
  45: import gnu.javax.print.ipp.attribute.defaults.FinishingsDefault;
  46: import gnu.javax.print.ipp.attribute.defaults.JobHoldUntilDefault;
  47: import gnu.javax.print.ipp.attribute.defaults.JobPriorityDefault;
  48: import gnu.javax.print.ipp.attribute.defaults.JobSheetsDefault;
  49: import gnu.javax.print.ipp.attribute.defaults.MediaDefault;
  50: import gnu.javax.print.ipp.attribute.defaults.MultipleDocumentHandlingDefault;
  51: import gnu.javax.print.ipp.attribute.defaults.NumberUpDefault;
  52: import gnu.javax.print.ipp.attribute.defaults.OrientationRequestedDefault;
  53: import gnu.javax.print.ipp.attribute.defaults.PrintQualityDefault;
  54: import gnu.javax.print.ipp.attribute.defaults.SidesDefault;
  55: import gnu.javax.print.ipp.attribute.job.JobDetailedStatusMessages;
  56: import gnu.javax.print.ipp.attribute.job.JobDocumentAccessErrors;
  57: import gnu.javax.print.ipp.attribute.job.JobId;
  58: import gnu.javax.print.ipp.attribute.job.JobStateMessage;
  59: import gnu.javax.print.ipp.attribute.printer.MultipleOperationTimeOut;
  60: import gnu.javax.print.ipp.attribute.printer.PrinterStateMessage;
  61: import gnu.javax.print.ipp.attribute.printer.PrinterUpTime;
  62: import gnu.javax.print.ipp.attribute.supported.CompressionSupported;
  63: import gnu.javax.print.ipp.attribute.supported.FinishingsSupported;
  64: import gnu.javax.print.ipp.attribute.supported.IppVersionsSupported;
  65: import gnu.javax.print.ipp.attribute.supported.JobHoldUntilSupported;
  66: import gnu.javax.print.ipp.attribute.supported.JobSheetsSupported;
  67: import gnu.javax.print.ipp.attribute.supported.MediaSupported;
  68: import gnu.javax.print.ipp.attribute.supported.MultipleDocumentHandlingSupported;
  69: import gnu.javax.print.ipp.attribute.supported.MultipleDocumentJobsSupported;
  70: import gnu.javax.print.ipp.attribute.supported.OperationsSupported;
  71: import gnu.javax.print.ipp.attribute.supported.OrientationRequestedSupported;
  72: import gnu.javax.print.ipp.attribute.supported.PageRangesSupported;
  73: import gnu.javax.print.ipp.attribute.supported.PrintQualitySupported;
  74: import gnu.javax.print.ipp.attribute.supported.PrinterResolutionSupported;
  75: import gnu.javax.print.ipp.attribute.supported.SidesSupported;
  76: import gnu.javax.print.ipp.attribute.supported.UriAuthenticationSupported;
  77: import gnu.javax.print.ipp.attribute.supported.UriSecuritySupported;
  78: 
  79: import java.lang.reflect.Constructor;
  80: import java.lang.reflect.Field;
  81: import java.lang.reflect.InvocationTargetException;
  82: import java.util.HashMap;
  83: import java.util.Locale;
  84: 
  85: import javax.print.attribute.Attribute;
  86: import javax.print.attribute.EnumSyntax;
  87: import javax.print.attribute.SupportedValuesAttribute;
  88: import javax.print.attribute.standard.Chromaticity;
  89: import javax.print.attribute.standard.ColorSupported;
  90: import javax.print.attribute.standard.Compression;
  91: import javax.print.attribute.standard.Copies;
  92: import javax.print.attribute.standard.CopiesSupported;
  93: import javax.print.attribute.standard.Fidelity;
  94: import javax.print.attribute.standard.Finishings;
  95: import javax.print.attribute.standard.JobHoldUntil;
  96: import javax.print.attribute.standard.JobImpressionsCompleted;
  97: import javax.print.attribute.standard.JobKOctetsProcessed;
  98: import javax.print.attribute.standard.JobMediaSheetsCompleted;
  99: import javax.print.attribute.standard.JobMessageFromOperator;
 100: import javax.print.attribute.standard.JobName;
 101: import javax.print.attribute.standard.JobOriginatingUserName;
 102: import javax.print.attribute.standard.JobPriority;
 103: import javax.print.attribute.standard.JobPrioritySupported;
 104: import javax.print.attribute.standard.JobSheets;
 105: import javax.print.attribute.standard.JobState;
 106: import javax.print.attribute.standard.JobStateReason;
 107: import javax.print.attribute.standard.Media;
 108: import javax.print.attribute.standard.MediaSizeName;
 109: import javax.print.attribute.standard.MultipleDocumentHandling;
 110: import javax.print.attribute.standard.NumberOfInterveningJobs;
 111: import javax.print.attribute.standard.NumberUp;
 112: import javax.print.attribute.standard.NumberUpSupported;
 113: import javax.print.attribute.standard.OrientationRequested;
 114: import javax.print.attribute.standard.OutputDeviceAssigned;
 115: import javax.print.attribute.standard.PDLOverrideSupported;
 116: import javax.print.attribute.standard.PageRanges;
 117: import javax.print.attribute.standard.PagesPerMinute;
 118: import javax.print.attribute.standard.PagesPerMinuteColor;
 119: import javax.print.attribute.standard.PresentationDirection;
 120: import javax.print.attribute.standard.PrintQuality;
 121: import javax.print.attribute.standard.PrinterInfo;
 122: import javax.print.attribute.standard.PrinterIsAcceptingJobs;
 123: import javax.print.attribute.standard.PrinterLocation;
 124: import javax.print.attribute.standard.PrinterMakeAndModel;
 125: import javax.print.attribute.standard.PrinterMessageFromOperator;
 126: import javax.print.attribute.standard.PrinterName;
 127: import javax.print.attribute.standard.PrinterResolution;
 128: import javax.print.attribute.standard.PrinterState;
 129: import javax.print.attribute.standard.PrinterStateReason;
 130: import javax.print.attribute.standard.QueuedJobCount;
 131: import javax.print.attribute.standard.ReferenceUriSchemesSupported;
 132: import javax.print.attribute.standard.Severity;
 133: import javax.print.attribute.standard.SheetCollate;
 134: import javax.print.attribute.standard.Sides;
 135: 
 136: /**
 137:  * Collection of static utilities methods used in
 138:  * IPP response parsing and all over the place.
 139:  * <p>
 140:  * Also provides mapping from the attribute name values to
 141:  * the actual class object. Used to construct objects via reflection.
 142:  * </p>
 143:  *
 144:  * @author Wolfgang Baer (WBaer@gmx.de)
 145:  */
 146: public final class IppUtilities
 147: {
 148:   // These are reused in the reflection code to not instantiate an array everytime
 149:   private static Object[] INTEGER_ATT_VALUE = new Object[1];
 150:   private static Class<?>[] INTEGER_CLASS_ARRAY = new Class[] {int.class};
 151:   private static Object[] TEXT_ATT_VALUE = new Object[2];
 152:   private static Class<?>[] TEXT_CLASS_ARRAY = new Class[] {String.class, Locale.class};
 153: 
 154:   // The map -> Attribute name to Attribute class
 155:   private static HashMap<String,Class<? extends Attribute>> classesByName =
 156:                                                 new HashMap<String,Class<? extends Attribute>>();
 157:   // The map -> StandardAttribute class to SupportedAttribute category name
 158:   private static HashMap<Class<? extends Attribute>,SupportedValuesAttribute> instanceByClass =
 159:                                          new HashMap<Class<? extends Attribute>,SupportedValuesAttribute>();
 160: 
 161:   /**
 162:    * All the currently needed attributes
 163:    */
 164:   static
 165:     {
 166:       // enums
 167:       classesByName.put(JobState.ABORTED.getName(), JobState.class);
 168:       classesByName.put(Sides.DUPLEX.getName(), Sides.class);
 169:       classesByName.put(SheetCollate.COLLATED.getName(), SheetCollate.class);
 170:       classesByName.put(Severity.ERROR.getName(), Severity.class);
 171:       classesByName.put(JobSheets.NONE.getName(), JobSheets.class);
 172:       classesByName.put(Finishings.BIND.getName(), Finishings.class);
 173:       classesByName.put(Fidelity.FIDELITY_FALSE.getName(), Fidelity.class);
 174:       classesByName.put(Compression.GZIP.getName(), Compression.class);
 175:       classesByName.put(Chromaticity.COLOR.getName(), Chromaticity.class);
 176:       classesByName.put(PrintQuality.DRAFT.getName(), PrintQuality.class);
 177:       classesByName.put(PrinterState.IDLE.getName(), PrinterState.class);
 178:       classesByName.put(SidesDefault.ONE_SIDED.getName(), SidesDefault.class);
 179:       classesByName.put(ReferenceUriSchemesSupported.FILE.getName(),
 180:                         ReferenceUriSchemesSupported.class);
 181:       classesByName.put(PrinterStateReason.DOOR_OPEN.getName(),
 182:                         PrinterStateReason.class);
 183:       classesByName.put(PresentationDirection.TOLEFT_TOTOP.getName(),
 184:                         PresentationDirection.class);
 185:       classesByName.put(PDLOverrideSupported.ATTEMPTED.getName(),
 186:                         PDLOverrideSupported.class);
 187:       classesByName.put(OrientationRequested.PORTRAIT.getName(),
 188:                         OrientationRequested.class);
 189:       classesByName.put(MultipleDocumentHandling.SINGLE_DOCUMENT.getName(),
 190:                         MultipleDocumentHandling.class);
 191:       classesByName.put(JobStateReason.JOB_QUEUED.getName(),
 192:                         JobStateReason.class);
 193:       classesByName.put(UriAuthenticationSupported.NONE.getName(),
 194:                         UriAuthenticationSupported.class);
 195:       classesByName.put(OperationsSupported.GET_JOBS.getName(),
 196:                         OperationsSupported.class);
 197:       classesByName.put(UriSecuritySupported.NONE.getName(),
 198:                         UriSecuritySupported.class);
 199:       classesByName.put(FinishingsSupported.NONE.getName(),
 200:                         FinishingsSupported.class);
 201:       classesByName.put(FinishingsDefault.NONE.getName(),
 202:                         FinishingsDefault.class);
 203:       classesByName.put(IppVersionsSupported.V_1_0.getName(),
 204:                         IppVersionsSupported.class);
 205:       classesByName.put(MultipleDocumentHandlingSupported.SINGLE_DOCUMENT.getName(),
 206:                         MultipleDocumentHandlingSupported.class);
 207:       classesByName.put(MultipleDocumentHandlingDefault.SINGLE_DOCUMENT.getName(),
 208:                         MultipleDocumentHandlingDefault.class);
 209:       classesByName.put(CompressionSupported.NONE.getName(),
 210:                         CompressionSupported.class);
 211:       classesByName.put(OrientationRequestedSupported.PORTRAIT.getName(),
 212:                         OrientationRequestedSupported.class);
 213:       classesByName.put(OrientationRequestedDefault.PORTRAIT.getName(),
 214:                         OrientationRequestedDefault.class);
 215:       classesByName.put(SidesSupported.ONE_SIDED.getName(),
 216:                         SidesSupported.class);
 217:       classesByName.put(PrintQualityDefault.DRAFT.getName(),
 218:                         PrintQualityDefault.class);
 219:       classesByName.put(PrintQualitySupported.DRAFT.getName(),
 220:                         PrintQualitySupported.class);
 221:       classesByName.put(ReferenceUriSchemesSupported.FTP.getName(),
 222:                         ReferenceUriSchemesSupported.class);
 223: 
 224:       // the boolean types
 225:       classesByName.put(ColorSupported.SUPPORTED.getName(), ColorSupported.class);
 226:       classesByName.put(PrinterIsAcceptingJobs.ACCEPTING_JOBS.getName(),
 227:                         PrinterIsAcceptingJobs.class);
 228:       classesByName.put(MultipleDocumentJobsSupported.SUPPORTED.getName(),
 229:                         MultipleDocumentJobsSupported.class);
 230:       classesByName.put(PageRangesSupported.SUPPORTED.getName(),
 231:                         PageRangesSupported.class);
 232: 
 233:       // TextSyntax derived attributes
 234:       classesByName.put("media-default", MediaDefault.class);
 235:       classesByName.put("media-supported", MediaSupported.class);
 236:       classesByName.put("media", MediaSizeName.class);
 237:       classesByName.put("printer-location", PrinterLocation.class);
 238:       classesByName.put("printer-info", PrinterInfo.class);
 239:       classesByName.put("printer-make-and-model", PrinterMakeAndModel.class);
 240:       classesByName.put("printer-state-message", PrinterStateMessage.class);
 241:       classesByName.put("job-state-message", JobStateMessage.class);
 242:       classesByName.put("job-sheets-default", JobSheetsDefault.class);
 243:       classesByName.put("job-sheets-supported", JobSheetsSupported.class);
 244:       classesByName.put("job-name", JobName.class);
 245:       classesByName.put("printer-name", PrinterName.class);
 246:       classesByName.put("status-message", StatusMessage.class);
 247:       classesByName.put("detailed-status-message", DetailedStatusMessage.class);
 248:       classesByName.put("document-access-error", DocumentAccessError.class);
 249:       classesByName.put("output-device-assigned", OutputDeviceAssigned.class);
 250:       classesByName.put("job-hold-until-default", JobHoldUntilDefault.class);
 251:       classesByName.put("job-originating-user-name",
 252:                         JobOriginatingUserName.class);
 253:       classesByName.put("job-hold-until-supported",
 254:                         JobHoldUntilSupported.class);
 255:       classesByName.put("job-message-from-operator",
 256:                         JobMessageFromOperator.class);
 257:       classesByName.put("printer-message-from-operator",
 258:                         PrinterMessageFromOperator.class);
 259:       classesByName.put("job-detailed-status-messages",
 260:                         JobDetailedStatusMessages.class);
 261:       classesByName.put("job-document-access-errors",
 262:                         JobDocumentAccessErrors.class);
 263: 
 264:       // IntegerSyntax derived Attributes
 265:       classesByName.put("copies-default", CopiesDefault.class);
 266:       classesByName.put("job-id", JobId.class);
 267:       classesByName.put("job-priority-supported", JobPrioritySupported.class);
 268:       classesByName.put("job-priority-default", JobPriorityDefault.class);
 269:       classesByName.put("number-up-supported", NumberUpSupported.class);
 270:       classesByName.put("number-up-default", NumberUpDefault.class);
 271:       classesByName.put("queued-job-count", QueuedJobCount.class);
 272:       classesByName.put("printer-up-time", PrinterUpTime.class);
 273:       classesByName.put("pages-per-minute", PagesPerMinute.class);
 274:       classesByName.put("pages-per-minute-color", PagesPerMinuteColor.class);
 275:       classesByName.put("job-k-octets-processed", JobKOctetsProcessed.class);
 276:       classesByName.put("number-of-intervening-jobs",
 277:                         NumberOfInterveningJobs.class);
 278:       classesByName.put("job-impressions-completed",
 279:                         JobImpressionsCompleted.class);
 280:       classesByName.put("job-media-sheets-completed",
 281:                         JobMediaSheetsCompleted.class);
 282:       classesByName.put("multiple-operation-time-out",
 283:                         MultipleOperationTimeOut.class);
 284: 
 285: 
 286:       // 4.2 job template attributes
 287:       instanceByClass.put(JobPriority.class, new JobPrioritySupported(1));
 288:       instanceByClass.put(JobHoldUntil.class, new JobHoldUntilSupported("", null));
 289:       instanceByClass.put(JobSheets.class, new JobSheetsSupported("", null));
 290:       instanceByClass.put(MultipleDocumentHandling.class, MultipleDocumentHandlingSupported.SINGLE_DOCUMENT);
 291:       instanceByClass.put(Copies.class, new CopiesSupported(1));
 292:       instanceByClass.put(Finishings.class, FinishingsSupported.BIND);
 293:       instanceByClass.put(PageRanges.class, PageRangesSupported.SUPPORTED);
 294:       instanceByClass.put(Sides.class, SidesSupported.DUPLEX);
 295:       instanceByClass.put(NumberUp.class, new NumberUpSupported(1));
 296:       instanceByClass.put(OrientationRequested.class, OrientationRequestedSupported.LANDSCAPE);
 297:       instanceByClass.put(Media.class, new MediaSupported("", null));
 298:       instanceByClass.put(PrinterResolution.class, new PrinterResolutionSupported(1,1,1));
 299:       instanceByClass.put(PrintQuality.class, PrintQualitySupported.DRAFT);
 300: 
 301:       // 4.4 printer attributes
 302:       instanceByClass.put(Compression.class, CompressionSupported.COMPRESS);
 303:     }
 304: 
 305:   private IppUtilities()
 306:   {
 307:     // not to be instantiated
 308:   }
 309: 
 310:   /**
 311:    * Returns the implementing class object for given
 312:    * attribute name objects.
 313:    *
 314:    * @param name the attribute name
 315:    * @return The <code>Class</code> object.
 316:    */
 317:   public static Class<? extends Attribute> getClass(String name)
 318:   {
 319:     return classesByName.get(name);
 320:   }
 321: 
 322:   /**
 323:    * Returns the name of the supported attribute
 324:    * based on the given standard attribute category.
 325:    *
 326:    * @param clazz the standard attribute category
 327:    * @return The name of the supported attribute category.
 328:    */
 329:   public static String getSupportedAttrName(Class<? extends Attribute> clazz)
 330:   {
 331:     return instanceByClass.get(clazz).getName();
 332:   }
 333: 
 334:   /**
 335:    * Returns the category of the supported attribute
 336:    * based on the given standard attribute category.
 337:    *
 338:    * @param clazz the standard attribute category
 339:    * @return The supported attribute category.
 340:    */
 341:   public static Class<? extends Attribute> getSupportedCategory(Class<? extends Attribute> clazz)
 342:   {
 343:     return instanceByClass.get(clazz).getCategory();
 344:   }
 345: 
 346:   /**
 347:    * Helper method to convert to an int.
 348:    * @param b the byte array
 349:    * @return The converted int.
 350:    */
 351:   public static int convertToInt(byte[] b)
 352:   {
 353:     return (((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16)
 354:             | ((b[2] & 0xff) << 8) | (b[3] & 0xff));
 355:   }
 356: 
 357:   /**
 358:    * Helper method to convert to an int.
 359:    * @param b1 the 1th byte
 360:    * @param b2 the 2th byte
 361:    * @param b3 the 3th byte
 362:    * @param b4 the 4th byte
 363:    * @return The converted int.
 364:    */
 365:   public static int convertToInt(byte b1, byte b2, byte b3, byte b4)
 366:   {
 367:     return (((b1 & 0xff) << 24) | ((b2 & 0xff) << 16)
 368:             | ((b3 & 0xff) << 8) | (b4 & 0xff));
 369:   }
 370: 
 371:   /**
 372:    * Helper method to convert to a short.
 373:    * @param b1 the 1th byte
 374:    * @param b2 the 2th byte
 375:    * @return The converted short.
 376:    */
 377:   public static short convertToShort(byte b1, byte b2)
 378:   {
 379:     return (short) ((b1 << 8) | (b2 & 0xff));
 380:   }
 381: 
 382:   /**
 383:    * Instantiates an <code>EnumSyntax</code> based attribute with the given IPP
 384:    * name and the given value (Enums maybe int or String based).
 385:    *
 386:    * @param name the attribute name of the subclass.
 387:    * @param value the integer value of the specific enum.
 388:    * @return The Attribute (a subclass of EnumSyntax)
 389:    */
 390:   public static Attribute getEnumAttribute(String name, Object value)
 391:   {
 392:     Class<?> attrClass = getClass(name);
 393: 
 394:     // There might be unknown enums we have no mapped class for
 395:     if (attrClass ==  null)
 396:       return null;
 397: 
 398:     try
 399:       {
 400:         Field[] fields = attrClass.getDeclaredFields();
 401:         for (int i = 0; i < fields.length; i++)
 402:           {
 403:             Field field = fields[i];
 404:             if (field.getType().equals(attrClass))
 405:               {
 406:                 EnumSyntax attr = (EnumSyntax) field.get(null);
 407:                 if (value instanceof Integer
 408:                     && attr.getValue() == ((Integer) value).intValue())
 409:                   return (Attribute) attr;
 410:                 else if (value instanceof String
 411:                          && attr.toString().equals(value))
 412:                   return (Attribute) attr;
 413:               }
 414:           }
 415:       }
 416:     catch (SecurityException e)
 417:       {
 418:         // should not happen
 419:       }
 420:     catch (IllegalArgumentException e)
 421:       {
 422:         // should not happen
 423:       }
 424:     catch (IllegalAccessException e)
 425:       {
 426:         // should not happen, all fields are public
 427:       }
 428: 
 429:     return null;
 430:   }
 431: 
 432: 
 433: 
 434:   /**
 435:    * Instantiates an <code>IntegerSyntax</code> based attribute with the
 436:    * given IPP name for the given int value.
 437:    *
 438:    * @param name the attribute name of the subclass.
 439:    * @param value the integer value
 440:    * @return The Attribute (a subclass of IntegerSyntax)
 441:    */
 442:   public static Attribute getIntegerAttribute(String name, int value)
 443:   {
 444:     Class<?> attrClass = getClass(name);
 445: 
 446:     // There might be unknown attributes we have no mapped class for
 447:     if (attrClass ==  null)
 448:       return null;
 449: 
 450:     try
 451:       {
 452:         INTEGER_ATT_VALUE[0] = Integer.valueOf(value);
 453:         Constructor<?> c = attrClass.getDeclaredConstructor(INTEGER_CLASS_ARRAY);
 454:         return (Attribute) c.newInstance(INTEGER_ATT_VALUE);
 455:       }
 456:     catch (SecurityException e)
 457:       {
 458:         // should not happen
 459:       }
 460:     catch (NoSuchMethodException e)
 461:       {
 462:         // should not happen
 463:       }
 464:     catch (IllegalAccessException e)
 465:       {
 466:         // should not happen, all fields are public
 467:       }
 468:     catch (InstantiationException e)
 469:     {
 470:       // should not happen, all fields are public
 471:     }
 472:     catch (InvocationTargetException e)
 473:     {
 474:       // should not happen, all fields are public
 475:     }
 476: 
 477:     return null;
 478:   }
 479: 
 480:   /**
 481:    * Instantiates an <code>TextSyntax</code> based attribute with the given
 482:    * IPP name for the given text value (will be decoded).
 483:    *
 484:    * @param name the attribute name of the subclass.
 485:    * @param tag the tag defined in {@link IppValueTag}
 486:    * @param value the byte[] value to be decoded based on the tag value.
 487:    * @return The Attribute (a subclass of TextSyntax)
 488:    */
 489:   public static Attribute getTextAttribute(String name, byte tag, byte[] value)
 490:   {
 491:     // without language tag is rather easy - default locale
 492:     if (tag == IppValueTag.NAME_WITHOUT_LANGUAGE
 493:         || tag == IppValueTag.TEXT_WITHOUT_LANGUAGE)
 494:       {
 495:         TEXT_ATT_VALUE[0] = new String(value);
 496:         TEXT_ATT_VALUE[1] = Locale.getDefault();
 497:       }
 498:     else
 499:       {
 500:         short langLength = convertToShort(value[0], value[1]);
 501:         byte[] tmp = new byte[langLength];
 502:         byte[] tmp2 = new byte[value.length - 4 - langLength];
 503:         System.arraycopy(value, 2, tmp, 0, langLength);
 504: 
 505:         // parse into language/region
 506:         String language = new String(tmp);
 507:         String text = new String(tmp2);
 508:         Locale locale = null;
 509: 
 510:         if (language.length() > 2)
 511:           locale = new Locale(language.substring(0, 2), language.substring(3));
 512:         else
 513:           locale = new Locale(language);
 514: 
 515:         TEXT_ATT_VALUE[0] = text;
 516:         TEXT_ATT_VALUE[1] = locale;
 517:       }
 518: 
 519:     Class<?> attrClass = getClass(name);
 520: 
 521:     // There might be unknown attributes we have no mapped class for
 522:     if (attrClass ==  null)
 523:       return null;
 524: 
 525:     try
 526:       {
 527:         Constructor<?> c = attrClass.getDeclaredConstructor(TEXT_CLASS_ARRAY);
 528:         return (Attribute) c.newInstance(TEXT_ATT_VALUE);
 529:       }
 530:     catch (SecurityException e)
 531:       {
 532:         // should not happen
 533:       }
 534:     catch (NoSuchMethodException e)
 535:       {
 536:         // should not happen
 537:       }
 538:     catch (IllegalAccessException e)
 539:       {
 540:         // should not happen, all fields are public
 541:       }
 542:     catch (InstantiationException e)
 543:       {
 544:         // should not happen, all fields are public
 545:       }
 546:     catch (InvocationTargetException e)
 547:       {
 548:         // should not happen, all fields are public
 549:       }
 550: 
 551:     return null;
 552:   }
 553: }