Skip to main content
summaryrefslogtreecommitdiffstats
blob: 06c73a67e883115bf45631c3bfe13742757c856e (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
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
142
143
144
145
146
147
148
/*******************************************************************************
 * Copyright (c) 2004 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.css.core.internal.metamodel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.wst.css.core.metamodel.CSSMMNode;


abstract class CSSMMNodeImpl implements CSSMMNode {

	public CSSMMNodeImpl() {
		super();
	}

	public abstract String getType();

	public String getDescription() {
		return fDescription;
	}

	public String getAttribute(String name) {
		if (fAttributes == null) {
			return null;
		}
		return (String) fAttributes.get(name);
	}

	public Iterator getChildNodes() {
		if (fChildNodes == null) {
			return Collections.EMPTY_LIST.iterator();
		}
		else {
			return Collections.unmodifiableCollection(fChildNodes).iterator();
		}
	}

	public Iterator getDescendants() {
		List descendants = new ArrayList();
		Iterator iChild = getChildNodes();
		while (iChild.hasNext()) {
			CSSMMNode child = (CSSMMNode) iChild.next();
			Iterator iDescendant = child.getDescendants();
			if (iDescendant.hasNext()) {
				while (iDescendant.hasNext()) {
					CSSMMNode descendant = (CSSMMNode) iDescendant.next();
					if (!descendants.contains(descendant)) {
						descendants.add(descendant);
					}
				}
			}
			else {
				if (!descendants.contains(child)) {
					descendants.add(child);
				}
			}
		}
		return Collections.unmodifiableCollection(descendants).iterator();
	}

	void appendChild(CSSMMNode child) {
		if (child == null) {
			return;
		}
		if (fChildNodes == null) {
			fChildNodes = new ArrayList();
		}
		if (!fChildNodes.contains(child)) {
			fChildNodes.add(child);
			((CSSMMNodeImpl) child).fRefCount++;
		}
	}

	void removeChild(CSSMMNode child) {
		if (child == null || fChildNodes == null) {
			return;
		}
		Iterator i = fChildNodes.iterator();
		while (i.hasNext()) {
			if (i.next() == child) {
				i.remove();
				((CSSMMNodeImpl) child).fRefCount--;
				return;
			}
		}
	}

	void removeAllChildNodes() {
		if (fChildNodes == null) {
			return;
		}
		Iterator i = fChildNodes.iterator();
		while (i.hasNext()) {
			((CSSMMNodeImpl) i.next()).fRefCount--;
		}
		fChildNodes.clear();
	}

	abstract boolean canContain(CSSMMNode child);

	void initializeAttribute(Map attributes) throws IllegalArgumentException {
		if (fAttributes == null) {
			fAttributes = new HashMap();
		}
		fAttributes.putAll(attributes);
		postSetAttribute();
	}

	void setDescription(String description) {
		fDescription = description;
	}

	void postSetAttribute() throws IllegalArgumentException {
		// default : nop
	}

	abstract short getError();

	int getChildCount() {
		return (fChildNodes != null) ? fChildNodes.size() : 0;
	}

	int getReferenceCount() {
		return fRefCount;
	}


	private List fChildNodes = null;
	private String fDescription = null;
	private Map fAttributes = null;
	private int fRefCount = 0; // for error check

	static final String ATTR_NAME = "name"; //$NON-NLS-1$
	static final String NAME_NOT_AVAILABLE = ""; //$NON-NLS-1$
}

Back to the top