private String addressToString()

in geronimo-mail_2.1_spec/src/main/java/jakarta/mail/internet/AddressParser.java [1414:1472]


    private String addressToString(final TokenStream tokens) {
        final StringBuffer buffer = new StringBuffer();

        // this flag controls whether we insert a blank delimiter between tokens as
        // we advance through the list.  Blanks are only inserted between consequtive value tokens.
        // Initially, this is false, then we flip it to true whenever we add a value token, and
        // back to false for any special character token.
        boolean spaceRequired = false;

        // we use nextToken rather than nextRealToken(), since we need to process the comments also.
        AddressToken token = tokens.nextToken();

        // now add each of the tokens
        while (token.type != END_OF_TOKENS) {
            switch (token.type) {
                // the word tokens are the only ones where we need to worry about adding
                // whitespace delimiters.
                case ATOM:
                case QUOTED_LITERAL:
                    // was the last token also a word?  Insert a blank first.
                    if (spaceRequired) {
                        buffer.append(' ');
                    }
                    addTokenValue(token, buffer);
                    // let the next iteration know we just added a word to the list.
                    spaceRequired = true;
                    break;

                // these special characters are just added in.  The constants for the character types
                // were carefully selected to be the character value in question.  This allows us to
                // just append the value.
                case LEFT_ANGLE:
                case RIGHT_ANGLE:
                case COMMA:
                case COLON:
                case AT_SIGN:
                case SEMICOLON:
                case PERIOD:
                    buffer.append((char)token.type);
                    // no spaces around specials
                    spaceRequired = false;
                    break;

                // Domain literals self delimiting...we can just append them and turn off the space flag.
                case DOMAIN_LITERAL:
                    addTokenValue(token, buffer);
                    spaceRequired = false;
                    break;

                // Comments are also self delimitin.
                case COMMENT:
                    addTokenValue(token, buffer);
                    spaceRequired = false;
                    break;
            }
            token = tokens.nextToken();
        }
        return buffer.toString();
    }