Java List Print printList(Collection list)

Here you can find the source of printList(Collection list)

Description

Escapes the items of the Collection and returns them in a StringBuffer, separated by commas.

License

Apache License

Parameter

Parameter Description
list a parameter

Return

list of the elements of the collection, sepparated by commas

Declaration

public static StringBuffer printList(Collection<String> list) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2006 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./*from   w ww.j  ava  2  s .  co  m*/
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Escapes the items of the Collection and returns them in a StringBuffer,
     * separated by commas.
     * 
     * @param list
     * @return list of the elements of the collection, sepparated by commas
     */
    public static StringBuffer printList(Collection<String> list) {
        StringBuffer sb = new StringBuffer();
        if (list != null && list.size() > 0) {
            Iterator<String> iter = list.iterator();
            while (true) {
                sb.append(escape(iter.next()));
                if (iter.hasNext()) {
                    sb.append(", ");
                } else {
                    break;
                }
            }
        }
        return sb;
    }

    /**
     * Simple HTML escaping of the String version of the specified Object.
     * 
     * @param obj Object that has a meaningful toString() value
     * @return string with some escaped characters
     */
    public static String escape(Object obj) {
        if (obj == null) {
            return "";
        }
        return escapeAndShorten(obj.toString(), -1);
    }

    /**
     * Simple HTML escaping.
     * 
     * Escapes &lt; &amp; and &gt; and leaves the rest as it is.
     * 
     * @param raw
     * @return string with some escaped characters
     */
    public static String escape(String raw) {
        return escapeAndShorten(raw, -1);
    }

    /**
     * Escape HTML data and shorted the result if necessary.
     * 
     * If the text is escaped, &lt;b&gt;...&lt;/b&gt; will be 
     * appended. 
     * 
     * @param raw raw text to escape 
     * @param maxLength maximum output length 
     * @return HTML code
     */
    public static String escapeAndShorten(String raw, int maxLength) {
        if (raw == null) {
            return "";
        }
        StringBuilder retval = new StringBuilder();
        int length = raw.length();
        boolean shortened = false;
        if (maxLength != -1 && length > maxLength) {
            length = maxLength;
            shortened = true;
        }
        for (int i = 0; i < length; i++) {
            char c = raw.charAt(i);
            switch (c) {
            case '<':
                retval.append("&lt;");
                break;
            case '>':
                retval.append("&gt;");
                break;
            case '&':
                retval.append("&amp;");
                break;
            case '\'':
                retval.append("&#039;");
                break;
            case '"':
                retval.append("&#034;");
                break;
            default:
                retval.append(c);
                break;
            }
        }
        if (shortened) {
            retval.append("<b>...</b>");
        }
        return retval.toString();
    }
}

Related

  1. printIntegerArrayList(ArrayList arr)
  2. printIntegerList(List array)
  3. printItemList(String label, List itemList)
  4. printLines(List lines)
  5. printLinkedList(String prefix, LinkedList list)
  6. printList(final List list)
  7. printList(List l)
  8. printList(List l)
  9. printList(List results)