package de.chandre.admintool.core.thymeleaf;

import java.util.Comparator;

/**
 * Comparator expects the full url path of template<br>
 * Templates found in core will have the lowest order
 * @author Andre
 * @since 1.0.1
 *
 */
public class TemplateUrlComparator implements Comparator<String> {
	
	private static final String[] CORE_LIB = new String[]{"/admin-tools-core/", "/admin-tools-core-"};
	
	private static int NEG = -1;
	private static int POS = 1;

	@Override
	public int compare(String o1, String o2) {
		boolean o1FoundInCore = foundInCore(o1);
		boolean o2FoundInCore = foundInCore(o2);
		
		if (o1FoundInCore) {
			return POS;
		}
		if (o2FoundInCore) {
			return NEG;
		}
		return o2.compareTo(o1);
	}
	
	private boolean foundInCore(String url) {
		boolean foundInCore = false;
		for (String lib : CORE_LIB) {
			foundInCore |= url.contains(lib);
		}
		return foundInCore;
	}

}

@@ -67,16 +67,134 @@
 parator;
+%0Aimport java.util.regex.Pattern;%0A%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;
 %0A%0A/**%0A *
@@ -436,64 +436,198 @@
 nal 
-String%5B%5D CORE_LIB = new String%5B%5D%7B%22/admin-tools-core/%22, %22
+Log LOGGER = LogFactory.getLog(TemplateUrlComparator.class);%0A%09%0A%09private static final Pattern%5B%5D CORE_LIB = new Pattern%5B%5D%7B %0A%09%09%09Pattern.compile(%22.*/admin-tools-core/.*%22),%0A%09%09%09Pattern.compile(%22.*
 /adm
@@ -640,17 +640,25 @@
 ls-core-
-%22
+%5C%5Cd.*%22)%0A%09
 %7D;%0A%09%0A%09pr
@@ -1059,18 +1059,26 @@
 or (
-String lib
+Pattern libPattern
  : C
@@ -1110,24 +1110,163 @@
  %7C= 
-url.contains(lib
+libPattern.matcher(url).matches();%0A%09%09%7D%0A%09%09if (LOGGER.isTraceEnabled()) %7B%0A%09%09%09LOGGER.trace(String.format(%22template found in core %25s for %25s%22, foundInCore, url)
 );%0A%09
@@ -1294,10 +1294,9 @@
 re;%0A
+
 %09%7D%0A
-%0A
 %7D%0A

Fix #1 resource loading ordered in wrong way (Tomcat)