/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2015 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.report;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

import org.eclipse.birt.report.model.api.IResourceLocator;
import org.eclipse.birt.report.model.api.ModuleHandle;
import org.eclipse.datatools.connectivity.oda.flatfile.ResourceLocator;

import com.axelor.app.AppSettings;
import com.axelor.common.ClassUtils;

/**
 * This is a {@link ResourceLocator} that first searches external reports
 * directory for the resources and fallbacks to module resources if not found.
 *
 */
public class ReportResourceLocator implements IResourceLocator {

	public static final String CONFIG_REPORT_DIR = "axelor.report.dir";
	public static final String DEFAULT_REPORT_DIR = "{user.home}/axelor/reports";

	private Path searchPath;

	public ReportResourceLocator() {
		final String dir = AppSettings.get().getPath(CONFIG_REPORT_DIR,
				DEFAULT_REPORT_DIR);
		this.searchPath = Paths.get(dir);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public URL findResource(ModuleHandle moduleHandle, String fileName,
			int type, Map appContext) {
		return findResource(moduleHandle, fileName, type);
	}

	@Override
	public URL findResource(ModuleHandle moduleHandle, String fileName, int type) {

		String sub = ".";

		switch (type) {
		case IResourceLocator.LIBRARY:
			sub = "lib";
			break;
		case IResourceLocator.IMAGE:
			sub = "img";
			break;
		case IResourceLocator.CASCADING_STYLE_SHEET:
			sub = "css";
			break;
		}

		// first search in the top directory
		File found = searchPath.resolve(fileName).toFile();

		// else search in sub directory
		if (!found.exists()) {
			found = searchPath.resolve(sub).normalize().resolve(fileName).toFile();
		}

		if (found.exists()) {
			try {
				return found.toURI().toURL();
			} catch (MalformedURLException e) {
			}
		}

		// otherwise locate from the modules
		URL url = ClassUtils.getResource(Paths.get("reports", fileName).normalize().toString());
		if (url == null) {
			url = ClassUtils.getResource(Paths.get("reports", sub, fileName).normalize().toString());
		}

		return url;
	}

}

@@ -883,16 +883,48 @@
 til.Map;
+%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -1175,16 +1175,52 @@
 ssUtils;
+%0Aimport com.axelor.common.FileUtils;
 %0A%0A/**%0A *
@@ -1592,16 +1592,112 @@
 orts%22;%0A%0A
+%09private static final Pattern URL_PATTERN = Pattern.compile(%22%5E(file%7Cjar%7Chttp%7Chttps%7Cftp):/.*%22);%0A%0A
 %09private
@@ -2410,20 +2410,248 @@
 %7D%0A%0A%09%09// 
-firs
+if already an url%0A%09%09if (URL_PATTERN.matcher(fileName).matches()) %7B%0A%09%09%09try %7B%0A%09%09%09%09return new URL(fileName);%0A%09%09%09%7D catch (MalformedURLException e) %7B%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09// first resolve absolute path%0A%09%09File found = new File(fileName);%0A%0A%09%09// nex
 t search
@@ -2674,21 +2674,42 @@
 ctory%0A%09%09
-File 
+if (!found.exists()) %7B%0A%09%09%09
 found = 
@@ -2746,16 +2746,20 @@
 oFile();
+%0A%09%09%7D
 %0A%0A%09%09// e
@@ -3077,33 +3077,41 @@
 getResource(
-Path
+FileUtil
 s.get
+File
 (%22reports%22, 
@@ -3120,37 +3120,43 @@
 leName).
-normalize().toString(
+getPath().replace(%22%5C%5C%22, %22/%22
 ));%0A%09%09if
@@ -3204,25 +3204,33 @@
 esource(
-Path
+FileUtil
 s.get
+File
 (%22report
@@ -3252,29 +3252,35 @@
 me).
-normalize().toString(
+getPath().replace(%22%5C%5C%22, %22/%22
 ));%0A

Fix report engine resource locator