Skip to main content
summaryrefslogtreecommitdiffstats
blob: a3b886fab8ae4f349e7fa8d7cc711f0900cba20a (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
/*******************************************************************************
 * Copyright (c) 2006, 2007 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.css2.marker;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author mengbo
 */
public abstract class EnumerableCounter implements ICounter {
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.list.ICounter#getNextObject(org.w3c.dom.Element,
	 *      java.lang.String)
	 */
	public final Object getNextObject(Element element, int type) {
		int index = calculateIndex(element);
		return getNextString(index, type);
	}

	/**
	 * @param element
	 * @return the index of the element in its parent
	 */
	public final int calculateIndex(Element element) {
		final String tag = element.getLocalName();
		final Node parent = element.getParentNode();
		final NodeList children = parent.getChildNodes();
		int index = 1;
		for (int i = 0; i < children.getLength(); i++) {
			final Node child = children.item(i);
			if (child == element) {
				break;
			}
			if (child.getLocalName().equalsIgnoreCase(tag)) {
				index++;
			}
		}
		return index;
	}

	/**
	 * @param index
	 * @param type
	 * @return the string for the next index
	 */
	public abstract String getNextString(int index, int type);
}

Back to the top