ECode MessageFormat::FormatToCharacterIterator(
    /* [in] */ IInterface* object,
    /* [out] */ IAttributedCharacterIterator** characterIterator)
{
    VALIDATE_NOT_NULL(characterIterator);
    *characterIterator = NULL;
    VALIDATE_NOT_NULL(object);

    StringBuffer buffer;
    List<AutoPtr<FieldContainer> > fields;

    // format the message, and find fields
    AutoPtr<IFieldPosition> position;
    CFieldPosition::New(0, (IFieldPosition**)&position);
    AutoPtr<ArrayOf<IInterface*> > arr = ArrayOf<IInterface*>::Alloc(1);
    arr->Set(0, object);
    FormatImpl(arr, &buffer, position, &fields);

    // create an AttributedString with the formatted buffer
    AutoPtr<IAttributedString> as;
    String outstr;
    buffer.Substring(0, buffer.GetLength(),&outstr);
    CAttributedString::New(outstr, (IAttributedString**)&as);

    // add MessageFormat field attributes and values to the AttributedString
    List<AutoPtr<FieldContainer> >::Iterator fc = fields.Begin();
    for ( ; fc != fields.End(); ++fc) {
        FAIL_RETURN(as->AddAttribute((*fc)->mAttribute, (*fc)->mValue, (*fc)->mStart, (*fc)->mEnd));
    }

    // return the CharacterIterator from AttributedString
    return as->GetIterator(characterIterator);
}