Source for gnu.java.text.FormatCharacterIterator

   1: /* FormatCharacter.java -- Implementation of AttributedCharacterIterator for
   2:    formatters.
   3:    Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2012 Free Software Foundation, Inc.
   4: 
   5: This file is part of GNU Classpath.
   6: 
   7: GNU Classpath is free software; you can redistribute it and/or modify
   8: it under the terms of the GNU General Public License as published by
   9: the Free Software Foundation; either version 2, or (at your option)
  10: any later version.
  11: 
  12: GNU Classpath is distributed in the hope that it will be useful, but
  13: WITHOUT ANY WARRANTY; without even the implied warranty of
  14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15: General Public License for more details.
  16: 
  17: You should have received a copy of the GNU General Public License
  18: along with GNU Classpath; see the file COPYING.  If not, write to the
  19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20: 02110-1301 USA.
  21: 
  22: Linking this library statically or dynamically with other modules is
  23: making a combined work based on this library.  Thus, the terms and
  24: conditions of the GNU General Public License cover the whole
  25: combination.
  26: 
  27: As a special exception, the copyright holders of this library give you
  28: permission to link this library with independent modules to produce an
  29: executable, regardless of the license terms of these independent
  30: modules, and to copy and distribute the resulting executable under
  31: terms of your choice, provided that you also meet, for each linked
  32: independent module, the terms and conditions of the license of that
  33: module.  An independent module is a module which is not derived from
  34: or based on this library.  If you modify this library, you may extend
  35: this exception to your version of the library, but you are not
  36: obligated to do so.  If you do not wish to do so, delete this
  37: exception statement from your version. */
  38: package gnu.java.text;
  39: 
  40: import java.text.AttributedCharacterIterator;
  41: import java.util.ArrayList;
  42: import java.util.HashMap;
  43: import java.util.HashSet;
  44: import java.util.Iterator;
  45: import java.util.List;
  46: import java.util.Map;
  47: import java.util.Set;
  48: 
  49: import static java.text.AttributedCharacterIterator.Attribute;
  50: 
  51: /**
  52:  * This class should not be put public and it is only intended to the
  53:  * classes of the java.text package. Its aim is to build a segmented
  54:  * character iterator by appending strings and adding attributes to
  55:  * portions of strings. The code intends to do some optimization
  56:  * concerning memory consumption and attribute access but at the
  57:  * end it is only an AttributedCharacterIterator.
  58:  *
  59:  * @author Guilhem Lavaux <guilhem@kaffe.org>
  60:  * @date November 22, 2003
  61:  */
  62: public class FormatCharacterIterator implements AttributedCharacterIterator
  63: {
  64:   private String formattedString;
  65:   private int charIndex;
  66:   private int attributeIndex;
  67:   private int[] ranges;
  68:   private List<Map<Attribute,Object>> attributes;
  69:   private static final boolean DEBUG = false;
  70: 
  71:   /**
  72:    * This constructor builds an empty iterated strings. The attributes
  73:    * are empty and so is the string. However you may append strings
  74:    * and attributes to this iterator.
  75:    */
  76:   public FormatCharacterIterator()
  77:   {
  78:     formattedString = "";
  79:     ranges = new int[0];
  80:     attributes = new ArrayList<Map<Attribute,Object>>(0);
  81:   }
  82: 
  83:   /**
  84:    * This constructor take a string <code>s</code>, a set of ranges
  85:    * and the corresponding attributes. This is used to build an iterator.
  86:    * The array <code>ranges</code> should be formatted as follow:
  87:    * each element of <code>ranges</code> specifies the index in the string
  88:    * until which the corresponding map of attributes at the same position
  89:    * is applied. For example, if you have:
  90:    * <pre>
  91:    *   s = "hello";
  92:    *   ranges = new int[] { 2, 6 };
  93:    *   attributes = new ArrayList<Map<Attribute,Object>>(2);
  94:    * </pre>
  95:    * <code>"he"</code> will have the attributes <code>attributes.get(0)</code>,
  96:    * <code>"llo"</code> the <code>attributes.get(1)</code>.
  97:    */
  98:   public FormatCharacterIterator (String s, int[] ranges,
  99:                                   List<Map<Attribute,Object>> attributes)
 100:   {
 101:     formattedString = s;
 102:     this.ranges = ranges;
 103:     this.attributes = attributes;
 104:   }
 105: 
 106:   /*
 107:    * The following methods are inherited from AttributedCharacterIterator,
 108:    * and thus are already documented.
 109:    */
 110: 
 111:   public Set<Attribute> getAllAttributeKeys()
 112:   {
 113:     if (attributes != null && attributes.get(attributeIndex) != null)
 114:       return attributes.get(attributeIndex).keySet();
 115:     else
 116:       return new HashSet<Attribute>();
 117:   }
 118: 
 119:   public Map<Attribute,Object> getAttributes()
 120:   {
 121:     if (attributes != null && attributes.get(attributeIndex) != null)
 122:       return attributes.get(attributeIndex);
 123:     else
 124:       return new HashMap<Attribute,Object>();
 125:   }
 126: 
 127:   public Object getAttribute (Attribute attrib)
 128:   {
 129:     if (attributes != null && attributes.get(attributeIndex) != null)
 130:       return attributes.get(attributeIndex).get (attrib);
 131:     else
 132:       return null;
 133:   }
 134: 
 135:   public int getRunLimit(Set<? extends Attribute> reqAttrs)
 136:   {
 137:     if (attributes == null)
 138:       return formattedString.length();
 139: 
 140:     int currentAttrIndex = attributeIndex;
 141:     Set<Attribute> newKeys;
 142: 
 143:     do
 144:       {
 145:         currentAttrIndex++;
 146:         if (currentAttrIndex == attributes.size())
 147:           return formattedString.length();
 148:         Map<Attribute,Object> currentAttr =
 149:           attributes.get(currentAttrIndex);
 150:         if (currentAttr == null)
 151:           break;
 152:         newKeys = currentAttr.keySet();
 153:       }
 154:     while (newKeys.containsAll (reqAttrs));
 155: 
 156:     return ranges[currentAttrIndex-1];
 157:   }
 158: 
 159:   public int getRunLimit (Attribute attribute)
 160:   {
 161:     Set<Attribute> s = new HashSet<Attribute>();
 162: 
 163:     s.add (attribute);
 164:     return getRunLimit (s);
 165:   }
 166: 
 167:   public int getRunLimit()
 168:   {
 169:     if (attributes == null)
 170:       return formattedString.length();
 171:     if (attributes.get(attributeIndex) == null)
 172:       {
 173:         for (int i=attributeIndex+1;i<attributes.size();i++)
 174:           if (attributes.get(i) != null)
 175:             return ranges[i-1];
 176:         return formattedString.length();
 177:       }
 178: 
 179:     return getRunLimit (attributes.get(attributeIndex).keySet());
 180:   }
 181: 
 182:   public int getRunStart (Set<? extends Attribute> reqAttrs)
 183:   {
 184:     if (attributes == null)
 185:       return formattedString.length();
 186: 
 187:     int currentAttrIndex = attributeIndex;
 188:     Set<Attribute> newKeys = null;
 189: 
 190:     do
 191:       {
 192:         if (currentAttrIndex == 0)
 193:           return 0;
 194: 
 195:         currentAttrIndex--;
 196:         Map<Attribute,Object> currentAttr =
 197:           attributes.get(currentAttrIndex);
 198:         if (currentAttr == null)
 199:           break;
 200:         newKeys = currentAttr.keySet();
 201:       }
 202:     while (newKeys.containsAll (reqAttrs));
 203: 
 204:     return (currentAttrIndex > 0) ? ranges[currentAttrIndex-1] : 0;
 205:   }
 206: 
 207:   public int getRunStart()
 208:   {
 209:     if (attributes == null)
 210:       return 0;
 211: 
 212:     Map<Attribute,Object> attrib = attributes.get(attributeIndex);
 213:     if (attrib == null)
 214:       {
 215:         for (int i=attributeIndex;i>0;i--)
 216:           if (attributes.get(i) != null)
 217:             return ranges[attributeIndex-1];
 218:         return 0;
 219:       }
 220: 
 221:     return getRunStart (attrib.keySet());
 222:   }
 223: 
 224:   public int getRunStart (Attribute attribute)
 225:   {
 226:     Set<Attribute> s = new HashSet<Attribute>();
 227: 
 228:     s.add (attribute);
 229:     return getRunStart (s);
 230:   }
 231: 
 232:   public Object clone()
 233:   {
 234:     return new FormatCharacterIterator (formattedString, ranges, attributes);
 235:   }
 236: 
 237:   /*
 238:    * The following methods are inherited from CharacterIterator and thus
 239:    * are already documented.
 240:    */
 241: 
 242:   public char current()
 243:   {
 244:     return formattedString.charAt (charIndex);
 245:   }
 246: 
 247:   public char first()
 248:   {
 249:     charIndex = 0;
 250:     attributeIndex = 0;
 251:     return formattedString.charAt (0);
 252:   }
 253: 
 254:   public int getBeginIndex()
 255:   {
 256:     return 0;
 257:   }
 258: 
 259:   public int getEndIndex()
 260:   {
 261:     return formattedString.length();
 262:   }
 263: 
 264:   public int getIndex()
 265:   {
 266:     return charIndex;
 267:   }
 268: 
 269:   public char last()
 270:   {
 271:     charIndex = formattedString.length()-1;
 272:     if (attributes != null)
 273:       attributeIndex = attributes.size()-1;
 274:     return formattedString.charAt (charIndex);
 275:   }
 276: 
 277:   public char next()
 278:   {
 279:     charIndex++;
 280:     if (charIndex >= formattedString.length())
 281:       {
 282:         charIndex = getEndIndex();
 283:         return DONE;
 284:       }
 285:     if (attributes != null)
 286:       {
 287:         if (charIndex >= ranges[attributeIndex])
 288:           attributeIndex++;
 289:       }
 290:     return formattedString.charAt (charIndex);
 291:   }
 292: 
 293:   public char previous()
 294:   {
 295:     charIndex--;
 296:     if (charIndex < 0)
 297:       {
 298:         charIndex = 0;
 299:         return DONE;
 300:       }
 301: 
 302:     if (attributes != null)
 303:       {
 304:         if (charIndex < ranges[attributeIndex])
 305:           attributeIndex--;
 306:       }
 307:     return formattedString.charAt (charIndex);
 308:   }
 309: 
 310:   public char setIndex (int position)
 311:   {
 312:     if (position < 0 || position > formattedString.length())
 313:       throw new IllegalArgumentException ("position is out of range");
 314: 
 315:     charIndex = position;
 316:     if (attributes != null)
 317:       {
 318:         for (attributeIndex=0;attributeIndex<attributes.size();
 319:              attributeIndex++)
 320:           if (ranges[attributeIndex] > charIndex)
 321:             break;
 322:         attributeIndex--;
 323:       }
 324:     if (charIndex == formattedString.length())
 325:       return DONE;
 326:     else
 327:       return formattedString.charAt (charIndex);
 328:   }
 329: 
 330:   /**
 331:    * This method merge the specified attributes and ranges with the
 332:    * internal tables. This method is in charge of the optimization
 333:    * of tables. Two following sets of attributes are never the same.
 334:    *
 335:    * @see #FormatCharacterIterator()
 336:    *
 337:    * @param attributes the new array attributes to apply to the string.
 338:    */
 339:   public void mergeAttributes (List<Map<Attribute,Object>> attributes,
 340:                                int[] ranges)
 341:   {
 342:     List<Integer> newRanges = new ArrayList<Integer>();
 343:     List<Map<Attribute,Object>> newAttributes =
 344:       new ArrayList<Map<Attribute,Object>>();
 345:     int i = 0, j = 0;
 346: 
 347:     debug("merging " + attributes.size() + " attrs");
 348: 
 349:     while (i < this.ranges.length && j < ranges.length)
 350:       {
 351:         if (this.attributes.get(i) != null)
 352:           {
 353:             newAttributes.add (this.attributes.get(i));
 354:             if (attributes.get(j) != null)
 355:               this.attributes.get(i).putAll (attributes.get(j));
 356:           }
 357:         else
 358:           {
 359:             newAttributes.add (attributes.get(j));
 360:           }
 361:         if (this.ranges[i] == ranges[j])
 362:           {
 363:             newRanges.add (Integer.valueOf (ranges[j]));
 364:             i++;
 365:             j++;
 366:           }
 367:         else if (this.ranges[i] < ranges[j])
 368:           {
 369:             newRanges.add (Integer.valueOf (this.ranges[i]));
 370:             i++;
 371:           }
 372:         else
 373:           {
 374:             newRanges.add (Integer.valueOf (ranges[j]));
 375:             j++;
 376:           }
 377:      }
 378: 
 379:     if (i != this.ranges.length)
 380:       {
 381:         for (;i<this.ranges.length;i++)
 382:           {
 383:             newAttributes.add (this.attributes.get(i));
 384:             newRanges.add (Integer.valueOf (this.ranges[i]));
 385:           }
 386:       }
 387:     if (j != ranges.length)
 388:       {
 389:         for (;j<ranges.length;j++)
 390:           {
 391:             newAttributes.add (attributes.get(j));
 392:             newRanges.add (Integer.valueOf (ranges[j]));
 393:           }
 394:       }
 395: 
 396:     this.attributes = newAttributes;
 397:     this.ranges = new int[newRanges.size()];
 398: 
 399:     for (i=0;i<newRanges.size();i++)
 400:       {
 401:         this.ranges[i] = newRanges.get (i).intValue();
 402:       }
 403: 
 404:     dumpTable();
 405:   }
 406: 
 407:   /**
 408:    * This method appends to the internal attributed string the attributed
 409:    * string contained in the specified iterator.
 410:    *
 411:    * @param iterator the iterator which contains the attributed string to
 412:    * append to this iterator.
 413:    */
 414:   public void append (AttributedCharacterIterator iterator)
 415:   {
 416:     char c = iterator.first();
 417:     List<Integer> moreRanges = new ArrayList<Integer>();
 418:     List<Map<Attribute,Object>> moreAttributes =
 419:       new ArrayList<Map<Attribute,Object>>();
 420: 
 421:     do
 422:       {
 423:         formattedString = formattedString + String.valueOf (c);
 424:         // TODO: Reduce the size of the output array.
 425:         moreAttributes.add (iterator.getAttributes());
 426:         moreRanges.add (Integer.valueOf (formattedString.length()));
 427:         // END TOOD
 428:         c = iterator.next();
 429:       }
 430:     while (c != DONE);
 431: 
 432:     List<Map<Attribute,Object>> newAttributes =
 433:       new ArrayList<Map<Attribute,Object>>(attributes.size() + moreAttributes.size());
 434:     int[] newRanges = new int[ranges.length + moreRanges.size()];
 435: 
 436:     newAttributes.addAll(attributes);
 437:     newAttributes.addAll(moreAttributes);
 438: 
 439:     System.arraycopy (ranges, 0, newRanges, 0, ranges.length);
 440:     Integer[] newRangesArray = moreRanges.toArray(new Integer[moreRanges.size()]);
 441:     for (int i = 0; i < moreRanges.size();i++)
 442:       newRanges[i+ranges.length] = newRangesArray[i].intValue();
 443: 
 444:     attributes = newAttributes;
 445:     ranges = newRanges;
 446:   }
 447: 
 448:   /**
 449:    * This method appends an attributed string which attributes are specified
 450:    * directly in the calling parameters.
 451:    *
 452:    * @param text The string to append.
 453:    * @param localAttributes The attributes to put on this string in the
 454:    * iterator. If it is <code>null</code> the string will simply have no
 455:    * attributes.
 456:    */
 457:   public void append (String text, HashMap<? extends Attribute,? extends Object> localAttributes)
 458:   {
 459:     int[] newRanges = new int[ranges.length+1];
 460:     List<Map<Attribute,Object>> newAttributes =
 461:       new ArrayList<Map<Attribute,Object>>(attributes.size()+1);
 462: 
 463:     formattedString += text;
 464:     newAttributes.addAll(attributes);
 465:     System.arraycopy (ranges, 0, newRanges, 0, ranges.length);
 466:     newRanges[ranges.length] = formattedString.length();
 467:     newAttributes.add(new HashMap<Attribute,Object>(localAttributes));
 468: 
 469:     ranges = newRanges;
 470:     attributes = newAttributes;
 471:   }
 472: 
 473:   /**
 474:    * This method appends a string without attributes. It is completely
 475:    * equivalent to call {@link #append(String,HashMap)} with localAttributes
 476:    * equal to <code>null</code>.
 477:    *
 478:    * @param text The string to append to the iterator.
 479:    */
 480:   public void append (String text)
 481:   {
 482:     append (text, null);
 483:   }
 484: 
 485:   /**
 486:    * This method adds a set of attributes to a range of character. The
 487:    * bounds are always inclusive. In the case many attributes have to
 488:    * be added it is advised to directly use {@link #mergeAttributes(java.util.List;[I}
 489:    *
 490:    * @param attributes Attributes to merge into the iterator.
 491:    * @param rangeStart Lower bound of the range of characters which will receive the
 492:    * attribute.
 493:    * @param rangeEnd Upper bound of the range of characters which will receive the
 494:    * attribute.
 495:    *
 496:    * @throws IllegalArgumentException if ranges are out of bounds.
 497:    */
 498:   public void addAttributes(Map<? extends Attribute,? extends Object> attributes,
 499:                             int rangeStart, int rangeEnd)
 500:   {
 501:     List<Map<Attribute,Object>> mergeAttribs = new ArrayList<Map<Attribute,Object>>();
 502:     int[] mergeRanges;
 503: 
 504:     if (rangeStart == 0)
 505:         mergeRanges = new int[] { rangeEnd };
 506:     else
 507:       {
 508:         mergeRanges = new int[] { rangeStart, rangeEnd };
 509:         mergeAttribs.add(null);
 510:       }
 511:     mergeAttribs.add(new HashMap<Attribute,Object>(attributes));
 512:     mergeAttributes(mergeAttribs, mergeRanges);
 513:   }
 514: 
 515:   private void debug(String s)
 516:   {
 517:     if (DEBUG)
 518:       System.out.println(s);
 519:   }
 520: 
 521:   private void dumpTable()
 522:   {
 523:     int startRange = 0;
 524: 
 525:     if (!DEBUG)
 526:       return;
 527: 
 528:     System.out.println("Dumping internal table:");
 529:     for (int i = 0; i < ranges.length; i++)
 530:       {
 531:         System.out.print("\t" + startRange + " => " + ranges[i] + ":");
 532:         if (attributes.get(i) == null)
 533:           System.out.println("null");
 534:         else
 535:           {
 536:             Set<Attribute> keyset = attributes.get(i).keySet();
 537:             if (keyset != null)
 538:               {
 539:                 Iterator<Attribute> keys = keyset.iterator();
 540: 
 541:                 while (keys.hasNext())
 542:                   System.out.print(" " + keys.next());
 543:               }
 544:             else
 545:               System.out.println("keySet null");
 546:             System.out.println();
 547:           }
 548:       }
 549:     System.out.println();
 550:     System.out.flush();
 551:   }
 552: }