Paras

1532 ワード

public class Paras
{
	private Paras(){}
//	private static Logger log = Logger.getLogger(Paras.class);
	
	public static String[] toParas(String paras)
	{
		if(paras == null)
		{
			return null;
		}
		
		class A
		{
			private StringBuffer sbr = new StringBuffer();
			
			String execute(String paras)
			{
				paras = paras.trim();
				
				if(paras.length() == 0)
				{
					return sbr.toString();
				}
				
				String t ;
				try
				{
					if('"' == paras.charAt(0))
					{
						t = paras.substring(1, paras.indexOf('"',1));
						sbr.append(t).append("|");
						paras = paras.substring(t.length()+2);
					}
					else
					{
						if(paras.indexOf(' ') == -1)
						{
							sbr.append(paras).append("|");
							paras = "";
							
						}
						else
						{
							t = paras.substring(0, paras.indexOf(' '));
							sbr.append(t).append("|");
							paras = paras.substring(t.length());
						}
					}
				}
				catch (RuntimeException e)
				{
					throw new IndexOutOfBoundsException(" !");
				}
				return execute(paras);
			}
		}

		return new A().execute(paras).split("\\|");
	}
	
}