// copyright => http://www.jxml.com/mdsax/License.txt

package com.jxml.quick;

/**
 * Supports new-line/line-separator conversion.
 */
public class NL
{
	/**
	 * The system line separator.
	 */
	public final static String LS;

	static
	{
		String t=null;
		try
		{
			t=System.getProperty("line.separator");
		} catch (SecurityException se)
		{
			t=null;
			se.printStackTrace();
			System.exit(1);
		}
		LS=t;
	}

	/**
	 * Convert a string containing lf's to line separators.
	 */
	public static String toLS(String s)
	{
		if ("\n".equals(LS))
			return s;
		int i=s.indexOf('\n');
		if (i==-1)
			return s;
		StringBuffer sb=new StringBuffer();
		int j=0;
		while (i>-1)
		{
			if (i!=j)
				sb.append(s.substring(j,i));
			sb.append(LS);
			j=i+1;
			i=s.indexOf('\n',j);
		}
		sb.append(s.substring(j));
		return sb.toString();
	}
}
