Skip to main content
summaryrefslogtreecommitdiffstats
blob: d1bae1ef7607789e4a8de59b5721876c03b3e4fa (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
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
/*******************************************************************************
 * Copyright (c) 2004, 2006 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.html.core.internal.contentmodel.chtml;



import java.util.Iterator;
import java.util.Locale;

import org.eclipse.wst.xml.core.internal.contentmodel.CMNode;

/**
 * An implementation of the CMNamedNodeMap interface. This class is intented
 * to be used as a container of attribute declarations. If someone wants to
 * use this class for other purpose, he must pay attention to the fact that
 * this class is tolerant of the key name case. That is, this class does not
 * distinguish "name", "NAME", and "Name" as a key name.
 */
class CMNamedNodeMapImpl implements org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap {

	private java.util.Hashtable items = null;

	/**
	 */
	public CMNamedNodeMapImpl() {
		super();
		items = new java.util.Hashtable();
	}

	/**
	 * getLength method
	 * 
	 * @return int
	 */
	public int getLength() {
		return items.size();
	}

	/**
	 * getNamedItem method
	 * 
	 * @return CMNode <code>null</code> for unknown keys.
	 * @param name
	 *            java.lang.String
	 */
	public CMNode getNamedItem(String name) {
		String cookedName = makeCanonicalForm(name);
		if (!items.containsKey(cookedName))
			return null;
		return (CMNode) items.get(cookedName);
	}

	/**
	 * item method
	 * 
	 * @return CMNode
	 * @param index
	 *            int
	 */
	public CMNode item(int index) {
		Iterator iter = iterator();
		while (iter.hasNext()) {
			CMNode node = (CMNode) iter.next();
			if (--index < 0)
				return node;
		}
		return null;
	}

	/**
	 * @return java.util.Iterator
	 */
	public Iterator iterator() {
		return items.values().iterator();
	}

	/**
	 * @return java.lang.String
	 * @param rawForm
	 *            java.lang.String
	 */
	private String makeCanonicalForm(String raw) {
		// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=100152
		// we are able to "cheat" here a little and use US Locale
		// to get a good cononical form, since we are using this only
		// for HTML and JSP standard tags.
		// Long term, for similar needs with XML 1.1 (for example)
		// we should use a class such as com.ibm.icu.text.Normalizer
		return raw.toUpperCase(Locale.US);
	}

	/**
	 * @param key
	 *            java.lang.String
	 * @param item
	 *            java.lang.String
	 */
	void putNamedItem(String name, CMNode item) {
		String cookedName = makeCanonicalForm(name);
		if (items.containsKey(cookedName))
			return; // already registered.
		items.put(cookedName, item);
	}
}

Back to the top