Here you can find the source of printList(Collection
Parameter | Description |
---|---|
list | a parameter |
public static StringBuffer printList(Collection<String> list)
//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 < & and > 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, <b>...</b> 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("<"); break; case '>': retval.append(">"); break; case '&': retval.append("&"); break; case '\'': retval.append("'"); break; case '"': retval.append("""); break; default: retval.append(c); break; } } if (shortened) { retval.append("<b>...</b>"); } return retval.toString(); } }