Exemplo n.º 1
0
  /**
   * Constructs an {@code AttributedString} from an {@code AttributedCharacterIterator}, which
   * represents attributed text.
   *
   * @param iterator the {@code AttributedCharacterIterator} that contains the text for this
   *     attributed string.
   */
  public AttributedString(AttributedCharacterIterator iterator) {
    if (iterator.getBeginIndex() > iterator.getEndIndex()) {
      // text.0A=Invalid substring range
      throw new IllegalArgumentException(Messages.getString("text.0A")); // $NON-NLS-1$
    }
    StringBuilder buffer = new StringBuilder();
    for (int i = iterator.getBeginIndex(); i < iterator.getEndIndex(); i++) {
      buffer.append(iterator.current());
      iterator.next();
    }
    text = buffer.toString();
    Set<AttributedCharacterIterator.Attribute> attributes = iterator.getAllAttributeKeys();
    if (attributes == null) {
      return;
    }
    attributeMap = new HashMap<Attribute, List<Range>>((attributes.size() * 4 / 3) + 1);

    Iterator<Attribute> it = attributes.iterator();
    while (it.hasNext()) {
      AttributedCharacterIterator.Attribute attribute = it.next();
      iterator.setIndex(0);
      while (iterator.current() != CharacterIterator.DONE) {
        int start = iterator.getRunStart(attribute);
        int limit = iterator.getRunLimit(attribute);
        Object value = iterator.getAttribute(attribute);
        if (value != null) {
          addAttribute(attribute, value, start, limit);
        }
        iterator.setIndex(limit);
      }
    }
  }
  /** Returns a Set of the attribute identifiers at <code>index</code>. */
  Map getAttributes(int index) {
    if (isValidMask()) {
      AttributedCharacterIterator iterator = getIterator();

      if (index >= 0 && index <= iterator.getEndIndex()) {
        iterator.setIndex(index);
        return iterator.getAttributes();
      }
    }
    return null;
  }
Exemplo n.º 3
0
  private AttributedString(
      AttributedCharacterIterator iterator, int start, int end, Set<Attribute> attributes) {
    if (start < iterator.getBeginIndex() || end > iterator.getEndIndex() || start > end) {
      throw new IllegalArgumentException();
    }

    if (attributes == null) {
      return;
    }

    StringBuilder buffer = new StringBuilder();
    iterator.setIndex(start);
    while (iterator.getIndex() < end) {
      buffer.append(iterator.current());
      iterator.next();
    }
    text = buffer.toString();
    attributeMap = new HashMap<Attribute, List<Range>>((attributes.size() * 4 / 3) + 1);

    Iterator<Attribute> it = attributes.iterator();
    while (it.hasNext()) {
      AttributedCharacterIterator.Attribute attribute = it.next();
      iterator.setIndex(start);
      while (iterator.getIndex() < end) {
        Object value = iterator.getAttribute(attribute);
        int runStart = iterator.getRunStart(attribute);
        int limit = iterator.getRunLimit(attribute);
        if ((value instanceof Annotation && runStart >= start && limit <= end)
            || (value != null && !(value instanceof Annotation))) {
          addAttribute(
              attribute,
              value,
              (runStart < start ? start : runStart) - start,
              (limit > end ? end : limit) - start);
        }
        iterator.setIndex(limit);
      }
    }
  }