/*
 * Copyright 2011 Greg Haines
 * 
 * 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.
 */
/*
 * Copyright 2011 Greg Haines
 * 
 * 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.
 */
package net.greghaines.jesque.web.utils;

import static net.greghaines.jesque.utils.JesqueUtils.createBacktrace;
import static net.greghaines.jesque.utils.JesqueUtils.join;
import static net.greghaines.jesque.utils.ResqueConstants.COLON;

import java.io.IOException;
import java.util.Date;
import java.util.regex.Pattern;

import net.greghaines.jesque.json.ObjectMapperFactory;
import net.greghaines.jesque.utils.ResqueDateFormatThreadLocal;

/**
 * JSP helper methods since JSTL sucks.
 * 
 * @author Greg Haines
 */
public final class TagHelper
{
	private static final String newLine = "\n";
	private static final Pattern colonPattern = Pattern.compile(COLON);

	public static String formatDate(final Date date)
	{
		return (date == null) ? null : ResqueDateFormatThreadLocal.getInstance().format(date);
	}

	public static String toJson(final Object obj)
	throws IOException
	{
		return ObjectMapperFactory.get().writeValueAsString(obj);
	}

	public static String showArgs(final Object[] args)
	throws IOException
	{
		if (args == null)
		{
			return null;
		}
		final StringBuilder sb = new StringBuilder();
		String s = "";
		for (final Object arg : args)
		{
			sb.append(s).append(toJson(arg));
			s = newLine;
		}
		return sb.toString();
	}
	
	public static String asBacktrace(final Throwable t)
	{
		return (t == null) ? null : join(newLine, createBacktrace(t));
	}
	
	public static String workerShortName(final String workerName)
	{
		if (workerName == null)
		{
			return null;
		}
		final String[] nameParts = colonPattern.split(workerName);
		return nameParts[0] + COLON + nameParts[1];
	}
	
	public static String newLine()
	{
		return newLine;
	}

	private TagHelper(){}
}

@@ -1,601 +1,4 @@
-/*%0A * Copyright 2011 Greg Haines%0A * %0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A * %0A *    http://www.apache.org/licenses/LICENSE-2.0%0A * %0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 /*%0A 
@@ -1168,23 +1168,24 @@
  String 
-newLine
+NEW_LINE
  = %22%5Cn%22;
@@ -1215,28 +1215,29 @@
 Pattern 
-colonPattern
+COLON_PATTERN
  = Patte
@@ -1800,23 +1800,24 @@
 %0A%09%09%09s = 
-newLine
+NEW_LINE
 ;%0A%09%09%7D%0A%09%09
@@ -1934,23 +1934,24 @@
  : join(
-newLine
+NEW_LINE
 , create
@@ -2119,20 +2119,21 @@
 s = 
-colonPattern
+COLON_PATTERN
 .spl
@@ -2243,23 +2243,24 @@
 %09return 
-newLine
+NEW_LINE
 ;%0A%09%7D%0A%0A%09p

Remove duplicate copyright header.