/*
 * Copyright 2002-2013 SCOOP Software GmbH
 *
 * 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 de.scoopgmbh.copper.wfrepo;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class URLClassloaderClasspathProvider implements CompilerOptionsProvider {

	@Override
	public Collection<String> getOptions() {
		StringBuilder buf = new StringBuilder();
		URLClassLoader loader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
		for (URL url : loader.getURLs() ) {
			File f = null;
			try {
				//convert the URL to a URI to remove the HTML encoding, if it exists.
				f = new File(url.toURI().getPath());
			} 
			catch (URISyntaxException e) {
				throw new RuntimeException("failed to convert the classpath URL '"+url+"' to a URI",e);
			}
			buf.append(f.getAbsolutePath()).append(File.pathSeparator);
		}
		List<String> options = new ArrayList<String>();
		options.add("-classpath");
		options.add(buf.toString());
		return options;
	}

}

@@ -1041,19 +1041,16 @@
 r();%0D%0A%09%09
-URL
 ClassLoa
@@ -1063,19 +1063,16 @@
 ader = (
-URL
 ClassLoa
@@ -1131,29 +1131,168 @@
 %0D%0A%09%09
-for (URL url : loader
+while (loader != null) %7B%0D%0A%09%09%09if (loader instanceof URLClassLoader) %7B%0D%0A%09%09%09%09StringBuilder buf2 = new StringBuilder();%0D%0A%09%09%09%09for (URL url : ((URLClassLoader)loader)
 .get
@@ -1306,16 +1306,18 @@
 ) %7B%0D%0A%09%09%09
+%09
+%09
 File f =
@@ -1327,16 +1327,18 @@
 ll;%0D%0A%09%09%09
+%09%09
 try %7B%0D%0A%09
@@ -1340,16 +1340,18 @@
  %7B%0D%0A%09%09%09%09
+%09
+%09
 //conver
@@ -1417,16 +1417,18 @@
 s.%0D%0A%09%09%09%09
+%09%09
 f = new 
@@ -1464,12 +1464,16 @@
 %0A%09%09%09
+%09
+%09
 %7D %0D%0A
+%09%09
 %09%09%09c
@@ -1507,16 +1507,18 @@
  %7B%0D%0A%09%09%09%09
+%09
+%09
 throw ne
@@ -1605,17 +1605,22 @@
 %0A%09%09%09
+%09%09
 %7D%0D%0A%09%09%09
+%09
+%09
 buf
+2
 .app
@@ -1673,16 +1673,104 @@
 ator);%0D%0A
+%09%09%09%09%7D%09%09%09%09%0D%0A%09%09%09%09buf.insert(0,  buf2.toString());%0D%0A%09%09%09%7D%0D%0A%09%09%09loader = loader.getParent();%0D%0A
 %09%09%7D%0D%0A%09%09L

add parent classloaders' classpathes