{
List<FacesConfig> sortedList = new ArrayList<FacesConfig>();
for (int i=0; i < preOrderedList.size(); i++)
{
FacesConfig resource = preOrderedList.get(i);
if (resource.getOrdering() != null)
{
if (resource.getOrdering().getBeforeList().isEmpty() &&
resource.getOrdering().getAfterList().isEmpty())
{
//No order rules, just put it as is
sortedList.add(resource);
}
else if (resource.getOrdering().getBeforeList().isEmpty())
{
//Only after rules
applyAfterRule(sortedList, resource);
}
else if (resource.getOrdering().getAfterList().isEmpty())
{
//Only before rules
//Resolve if there is a later reference to this node before
//apply it
boolean referenceNode = false;
for (int j = i+1; j < preOrderedList.size(); j++)
{
FacesConfig pointingResource = preOrderedList.get(j);
for (OrderSlot slot : pointingResource.getOrdering().getBeforeList())
{
if (slot instanceof FacesConfigNameSlot &&
resource.getName().equals(((FacesConfigNameSlot)slot).getName()) )
{
referenceNode = true;
}
if (slot instanceof ConfigOthersSlot)
{
//No matter if there is a reference, because this rule
//is not strict and before other ordering is unpredictable.
//
referenceNode = false;
break;
}
}
if (referenceNode)
{
break;
}
for (OrderSlot slot : pointingResource.getOrdering().getAfterList())
{
if (slot instanceof FacesConfigNameSlot &&
resource.getName().equals(((FacesConfigNameSlot)slot).getName()) )
{
referenceNode = true;
break;
}
}
}
applyBeforeRule(sortedList, resource, referenceNode);
}
else
{
//Both before and after rules
//In this case we should compare before and after rules
//and the one with names takes precedence over the other one.
//It both have names references, before rules takes
//precedence over after
//after some action is applied a check of the condition is made.
int beforeWeight = 0;
int afterWeight = 0;
for (OrderSlot slot : resource.getOrdering()
.getBeforeList())
{
if (slot instanceof FacesConfigNameSlot)
{
beforeWeight++;
}
}
for (OrderSlot slot : resource.getOrdering()
.getAfterList())
{
if (slot instanceof FacesConfigNameSlot)
{
afterWeight++;
}
}
if (beforeWeight >= afterWeight)
{
applyBeforeRule(sortedList, resource,false);
}
else
{
applyAfterRule(sortedList, resource);
}
}
}
else
{
//No order rules, just put it as is
sortedList.add(resource);
}
}
//Check
for (int i = 0; i < sortedList.size(); i++)
{
FacesConfig resource = sortedList.get(i);
if (resource.getOrdering() != null)
{
for (OrderSlot slot : resource.getOrdering().getBeforeList())
{
if (slot instanceof FacesConfigNameSlot)
{
String name = ((FacesConfigNameSlot) slot).getName();
if (name != null && !"".equals(name))
{
boolean founded = false;
for (int j = i-1; j >= 0; j--)
{
if (name.equals(sortedList.get(j).getName()))
{
founded=true;
break;
}
}
if (founded)
{
//Cyclic reference
return null;
}
}
}
}
for (OrderSlot slot : resource.getOrdering().getAfterList())
{
if (slot instanceof FacesConfigNameSlot)
{
String name = ((FacesConfigNameSlot) slot).getName();
if (name != null && !"".equals(name))