/*
 * Copyright (C) 2004-2012  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.servletutil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;

import javax.servlet.ServletContext;

import com.exedio.cope.util.PrefixSource;
import com.exedio.cope.util.Properties.Source;

public final class ServletSource
{
	private ServletSource()
	{
		// prevent instantiation
	}

	public static final Source create(final ServletContext context)
	{
		final String contextPath = context.getContextPath();
		final String prefix;
		if(contextPath==null)
			prefix = null;
		else if("".equals(contextPath))
			prefix = "root.";
		else if(contextPath.startsWith("/"))
			prefix = contextPath.substring(1) + '.';
		else
			prefix = contextPath + '.';

		final Source initParam = PrefixSource.wrap(new Source(){
					public String get(final String key)
					{
						return context.getInitParameter(key);
					}

					public Collection<String> keySet()
					{
						final ArrayList<String> result = new ArrayList<String>();
						for(final Enumeration<?> e = context.getInitParameterNames(); e.hasMoreElements(); )
							result.add((String)e.nextElement());
						return result;
					}

					public String getDescription()
					{
						return toString();
					}

					@Override
					public String toString()
					{
						return "ServletContext '" + contextPath + '\'';
					}
				},
				prefix);

		return new Source(){
			public String get(final String key)
			{
				if("contextPath".equals(key))
					return contextPath;

				return initParam.get(key);
			}

			public Collection<String> keySet()
			{
				final ArrayList<String> result = new ArrayList<String>();
				result.addAll(initParam.keySet());
				result.add("contextPath");
				return result;
			}

			public String getDescription()
			{
				return initParam.getDescription();
			}

			@Override
			public String toString()
			{
				return initParam.toString();
			}
		};
	}
}

@@ -2217,24 +2217,243 @@
 g key)%0A%09%09%09%7B%0A
+%09%09%09%09// TODO use Sources#checkKey instead, once its available here%0A%09%09%09%09if(key==null)%0A%09%09%09%09%09throw new NullPointerException(%22key%22);%0A%09%09%09%09if(key.length()==0)%0A%09%09%09%09%09throw new IllegalArgumentException(%22key must not be empty%22);%0A%0A
 %09%09%09%09if(%22cont

check key according to Sources#checkKey