Skip to main content
summaryrefslogtreecommitdiffstats
blob: 87cf1052d8d00dc50f0bc0fed9862e159bdf6cd7 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.utils;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.w3c.dom.Node;

/**
 * To sort the location of tags, this comparator is used to compare tags' order.
 * 
 * @author mengbo
 */
public class NodeLocationComparator implements Comparator {
	private final static Map orders = new HashMap();

	private final static Integer DEFAULT_ORDER = new Integer(Integer.MAX_VALUE);

	private static NodeLocationComparator _instance = new NodeLocationComparator();
	static {
		orders.put("taglib", new Integer(0));
		orders.put("directive.taglib", new Integer(0));
		orders.put("head", new Integer(1));
	}

	private NodeLocationComparator() {
        // no external instantiation
	}

	public static NodeLocationComparator getInstance() {
		return _instance;
	}

	/**
	 * The object to be compared could be Node or tag name.
	 * 
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare(Object o1, Object o2) {
		Assert.isTrue((o1 instanceof Node || o1 instanceof String)
				&& (o2 instanceof Node || o2 instanceof String));
		Integer i1 = getOrder(o1);
		Integer i2 = getOrder(o2);
		return i1.compareTo(i2);
	}

	private Integer getOrder(Object n) {
		String name = null;
		if (n instanceof Node) {
			name = ((Node) n).getLocalName();
		} else {
			name = (String) n;
		}
		if (name != null) {
			Object order = orders.get(name);
			if (order != null) {
				return (Integer) order;
			}
		}
		return DEFAULT_ORDER;
	}
}

Back to the top