001/*
002 * ====================================================================
003 * 
004 * The Apache Software License, Version 1.1
005 * 
006 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights reserved.
007 * 
008 * Redistribution and use in source and binary forms, with or without
009 * modification, are permitted provided that the following conditions are met:
010 * 
011 * 1. Redistributions of source code must retain the above copyright notice,
012 * this list of conditions and the following disclaimer.
013 * 
014 * 2. Redistributions in binary form must reproduce the above copyright notice,
015 * this list of conditions and the following disclaimer in the documentation
016 * and/or other materials provided with the distribution.
017 * 
018 * 3. The end-user documentation included with the redistribution, if any, must
019 * include the following acknowledgement: "This product includes software
020 * developed by the Apache Software Foundation (http://www.apache.org/)."
021 * Alternately, this acknowledgement may appear in the software itself, if and
022 * wherever such third-party acknowledgements normally appear.
023 * 
024 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
025 * Foundation" must not be used to endorse or promote products derived from this
026 * software without prior written permission. For written permission, please
027 * contact apache@apache.org.
028 * 
029 * 5. Products derived from this software may not be called "Apache" nor may
030 * "Apache" appear in their names without prior written permission of the Apache
031 * Software Foundation.
032 * 
033 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
034 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE
036 * SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
037 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
038 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
039 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
040 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
041 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
042 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
043 * ====================================================================
044 * 
045 * This software consists of voluntary contributions made by many individuals on
046 * behalf of the Apache Software Foundation. For more information on the Apache
047 * Software Foundation, please see <http://www.apache.org/>.
048 * 
049 */
050
051package org.apache.wicket.util.diff;
052
053import java.io.BufferedReader;
054import java.io.StringReader;
055import java.util.LinkedList;
056import java.util.List;
057
058/**
059 * This class delegates handling of the to a StringBuilder based version.
060 * 
061 * @version $Revision: 1.1 $ $Date: 2006/03/12 00:24:21 $
062 * @author <a href="mailto:juanco@suigeneris.org">Juanco Anez</a>
063 */
064public class ToString
065{
066        /**
067         * Construct.
068         */
069        public ToString()
070        {
071        }
072
073        /**
074         * Default implementation of the {@link java.lang.Object#toString toString() } method that
075         * delegates work to a {@link java.lang.StringBuffer StringBuffer} base version.
076         * 
077         * @return String
078         */
079        @Override
080        public String toString()
081        {
082                StringBuilder s = new StringBuilder();
083                toString(s);
084                return s.toString();
085        }
086
087        /**
088         * Place a string image of the object in a StringBuffer.
089         * 
090         * @param s
091         *            the string buffer.
092         */
093        public void toString(final StringBuilder s)
094        {
095                s.append(super.toString());
096        }
097
098        /**
099         * Breaks a string into an array of strings. Use the value of the <code>line.separator</code>
100         * system property as the linebreak character.
101         * 
102         * @param value
103         *            the string to convert.
104         * @return String[]
105         */
106        public static String[] stringToArray(final String value)
107        {
108                BufferedReader reader = new BufferedReader(new StringReader(value));
109                List<String> l = new LinkedList<>();
110                String s;
111                try
112                {
113                        while ((s = reader.readLine()) != null)
114                        {
115                                l.add(s);
116                        }
117                }
118                catch (java.io.IOException ignored)
119                {
120                }
121                return l.toArray(new String[l.size()]);
122        }
123
124        /**
125         * Converts an array of {@link Object Object} to a string Use the value of the
126         * <code>line.separator</code> system property the line separator.
127         * 
128         * @param o
129         *            the array of objects.
130         * @return String
131         */
132        public static String arrayToString(final Object[] o)
133        {
134                return arrayToString(o, System.getProperty("line.separator"));
135        }
136
137        /**
138         * Converts an array of {@link Object Object} to a string using the given line separator.
139         * 
140         * @param o
141         *            the array of objects.
142         * @param EOL
143         *            the string to use as line separator.
144         * @return String
145         */
146        public static String arrayToString(final Object[] o, final String EOL)
147        {
148                StringBuilder buf = new StringBuilder();
149                for (int i = 0; i < o.length - 1; i++)
150                {
151                        buf.append(o[i]);
152                        buf.append(EOL);
153                }
154                buf.append(o[o.length - 1]);
155                return buf.toString();
156        }
157}