Exemplo n.º 1
0
  /**
   * ************************************************************************ Return a Iterator with
   * only the relevant attributes. Fixes implementation in AttributedString, which returns
   * everything
   *
   * @param aString attributed string
   * @param relevantAttributes relevant attributes
   * @return iterator
   */
  public static AttributedCharacterIterator getIterator(
      AttributedString aString, AttributedCharacterIterator.Attribute[] relevantAttributes) {
    AttributedCharacterIterator iter = aString.getIterator();
    Set set = iter.getAllAttributeKeys();
    //	System.out.println("AllAttributeKeys=" + set);
    if (set.size() == 0) return iter;
    //	Check, if there are unwanted attributes
    Set<AttributedCharacterIterator.Attribute> unwanted =
        new HashSet<AttributedCharacterIterator.Attribute>(iter.getAllAttributeKeys());
    for (int i = 0; i < relevantAttributes.length; i++) unwanted.remove(relevantAttributes[i]);
    if (unwanted.size() == 0) return iter;

    //	Create new String
    StringBuffer sb = new StringBuffer();
    for (char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next())
      sb.append(c);
    aString = new AttributedString(sb.toString());

    //	copy relevant attributes
    Iterator it = iter.getAllAttributeKeys().iterator();
    while (it.hasNext()) {
      AttributedCharacterIterator.Attribute att = (AttributedCharacterIterator.Attribute) it.next();
      if (!unwanted.contains(att)) {
        for (char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) {
          Object value = iter.getAttribute(att);
          if (value != null) {
            int start = iter.getRunStart(att);
            int limit = iter.getRunLimit(att);
            //	System.out.println("Attribute=" + att + " Value=" + value + " Start=" + start + "
            // Limit=" + limit);
            aString.addAttribute(att, value, start, limit);
            iter.setIndex(limit);
          }
        }
      }
      //	else
      //		System.out.println("Unwanted: " + att);
    }
    return aString.getIterator();
  } //	getIterator