/*
 * $Id$ $Revision$
 * $Date$
 * 
 * ==============================================================================
 * 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 wicket.util.file;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;

import wicket.util.string.StringList;

/**
 * Mantains a list of folders as a path.
 * 
 * @author Johan Compagner
 */
public final class WebApplicationPath implements IResourceFinder
{
	/** The list of urls in the path */
	private final List webappPaths = new ArrayList();

	/** The list of folders in the path */
	private final List folders = new ArrayList();

	private final ServletContext servletContext;
	/**
	 * Constructor
	 * @param servletContext 
	 * 					The webapplication context where the resources must be loaded from
	 */
	public WebApplicationPath(ServletContext servletContext)
	{
		this.servletContext = servletContext;
	}

	/**
	 * @param path
	 *            add a path that is lookup through the servlet context
	 * @return The path, for invocation chaining
	 */
	public IResourceFinder add(String path)
	{
		if(!path.startsWith("/")) path = "/" + path;
		if(!path.endsWith("/")) path += "/";
		webappPaths.add(path);

		return this;
	}

	/**
	 * @param folder
	 * @return The WebApplicationResources, for invocation chaining
	 */
	public IResourceFinder add(final Folder folder)
	{
		folders.add(folder);
		return this;
	}

	/**
	 * Looks for a given pathname along this path
	 * 
	 * @param pathname
	 *            The filename with possible path
	 * @return The file located on the path
	 */
	public URL find(final String pathname)
	{
		for (final Iterator iterator = folders.iterator(); iterator.hasNext();)
		{
			Folder folder = (Folder)iterator.next();
			File file = new File(folder,pathname);
			if(file.exists())
			{
				try
				{
					return file.toURI().toURL();
				}
				catch (MalformedURLException ex)
				{
					// ignore
				}
			}
		}
		for (final Iterator iterator = webappPaths.iterator(); iterator.hasNext();)
		{
			final String path = (String)iterator.next();
			try
			{
				final URL file = servletContext.getResource(path + pathname);
				if (file != null)
				{
					return file;
				}
			}
			catch (Exception ex)
			{
				// ignore, file couldn't be found
			}
		}

		return null;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		return "[folders = " + StringList.valueOf(folders) + ", webapppaths: " + StringList.valueOf(webappPaths)+ "]";
	}
}

@@ -18,19 +18,16 @@
 ion$
-%0A *
  $Date$%0A
  * %0A
@@ -22,16 +22,16 @@
  $Date$%0A
+
  * %0A * =
@@ -1299,16 +1299,17 @@
 ontext;%0A
+%0A
 %09/**%0A%09 *
@@ -1321,16 +1321,21 @@
 tructor%0A
+%09 * %0A
 %09 * @par
@@ -1355,19 +1355,24 @@
 text
- 
 %0A%09 * 
-%09%09%09%09%09
+           
 The 
@@ -1428,16 +1428,31 @@
 e loaded
+%0A%09 *           
  from%0A%09 
@@ -1744,24 +1744,25 @@
 ath)%0A%09%7B%0A%09%09if
+ 
 (!path.start
@@ -1768,25 +1768,32 @@
 tsWith(%22/%22))
- 
+%0A%09%09%7B%0A%09%09%09
 path = %22/%22 +
@@ -1799,20 +1799,25 @@
 + path;%0A
+%09%09%7D%0A
 %09%09if
+ 
 (!path.e
@@ -1829,17 +1829,24 @@
 th(%22/%22))
- 
+%0A%09%09%7B%0A%09%09%09
 path += 
@@ -1850,16 +1850,20 @@
 += %22/%22;%0A
+%09%09%7D%0A
 %09%09webapp
@@ -1879,17 +1879,16 @@
 (path);%0A
-%0A
 %09%09return
@@ -2451,16 +2451,17 @@
 (folder,
+ 
 pathname
@@ -2468,16 +2468,17 @@
 );%0A%09%09%09if
+ 
 (file.ex
@@ -3126,17 +3126,21 @@
 paths: %22
- 
+%0A%09%09%09%09
 + String
@@ -3164,16 +3164,17 @@
 ppPaths)
+ 
 + %22%5D%22;%0A%09

Trivial formatting.