summaryrefslogtreecommitdiff
path: root/src/com/memberwebs/ldapxml/helpers/LXComparator.java
blob: c25326c01e06ca1248126776f183298aa6866ca1 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.memberwebs.ldapxml.helpers;

import java.util.*;

import org.w3c.dom.*;

/**
 * Used to sort XML elements according to a specified sort order
 *
 * @author nielsen@memberwebs.com
 * @version 0.5
 */
public class LXComparator
        implements Comparator
{
    /**
     * Construct a new LXComparator
     *
     * @param attrs The element or attribute names to sort by
     * @param directions The sort direction for each attr
     */
    public LXComparator(String[] attrs, boolean[] directions)
    {
        m_attrs = attrs;
        m_directions = directions;
    }

    /**
     * Compare two objects and return -1, 0 or 1
     *
     * @param o1 The first object
     * @param o2 The second object
     */
    public int compare(Object o1, Object o2)
    {
        Element e1 = (Element)o1;
        Element e2 = (Element)o2;

        for(int i = 0; i < m_attrs.length; i++)
        {
            String v1 = getSubElementValue(e1, m_attrs[i]);
            String v2 = getSubElementValue(e2, m_attrs[i]);

            int ret;

            if(v1 == null && v2 == null)
                ret = 0;
            else if(v1 == null)
                ret = -1;
            else if(v2 == null)
                ret = 1;
            else
                ret = v1.compareTo(v2);

            if(ret != 0)
                return m_directions[i] ? ret : -ret;
        }

        return 0;
    }

    /**
     * Check whether another comparator is equal to this one.
     *
     * @param obj The other comparator
     * @return Whether equal or not
     */
    public boolean equals(Object obj)
    {
        LXComparator other = (LXComparator)obj;
        return m_attrs.equals(other.m_attrs) &&
                m_directions.equals(other.m_directions);
    }

    /**
     * Get a value from an element. The value is either retrieved
     * from a sub element of the appropriate name or an attribute
     * of the same name.
     *
     * @param el The element to retrieve from.
     * @param attr The value name
     */
    private static String getSubElementValue(Element el, String attr)
    {
        // First we search sub elements and then attributes
        if(el.hasChildNodes())
        {
            NodeList children = el.getChildNodes();
            for(int i = 0; i < children.getLength(); i++)
            {
                Node child = children.item(i);
                if(child != null &&
                   child.getNodeType() == Node.ELEMENT_NODE &&
                   child.getNodeName().equals(attr))
                {
                    return getElementValue(el);
                }
            }
        }

        // Otherwise try to get the attribute by the same name
        return el.getAttribute(attr);
    }

    /**
     * Get the complete text value of an element.
     *
     * @param el The element to retrieve value for
     * @return The value.
     */
    private static String getElementValue(Element el)
    {
        String value = "";

        if(el.hasChildNodes())
        {
            NodeList children = el.getChildNodes();
            for(int i = 0; i < children.getLength(); i++)
            {
                Node child = children.item(i);
                if(child != null)
                {
                    switch(child.getNodeType())
                    {
                    case Node.ELEMENT_NODE:
                        value += getElementValue((Element)child);
                        break;
                    case Node.TEXT_NODE:
                        value += child.getNodeValue();
                        break;
                    }
                }
            }
        }

        return value;
    }

    private String[] m_attrs;
    private boolean[] m_directions;
}