/
WeightedAttrComparator.java
58 lines (47 loc) · 1.29 KB
/
WeightedAttrComparator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package net.erdfelt.maven.xmlfresh.util;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class WeightedAttrComparator implements Comparator<String>
{
public static final WeightedAttrComparator INSTANCE = new WeightedAttrComparator();
private List<String> importantNames = new ArrayList<String>();
@Override
public int compare(String o1, String o2)
{
String s1 = o1 == null?"":o1;
String s2 = o2 == null?"":o2;
int diff = s1.compareTo(s2);
diff = diff - weighted(s1);
diff = diff + weighted(s2);
return diff;
}
private int weighted(String name)
{
// xmlns is grouped first
if (name.startsWith("xmlns:"))
{
return 1000000; // always first
}
int idx = importantNames.indexOf(name);
if (idx >= 0)
{
return 100000 + idx;
}
// non-namespaced are next group
if (name.indexOf(':') < 0)
{
return 1000;
}
// namespaced is next
return 0;
}
public List<String> getImportantNames()
{
return importantNames;
}
public void setImportantNames(List<String> importantNames)
{
this.importantNames = importantNames;
}
}