Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: b12067d1bba27a8d1d62fd3c51304ca3d5590ccb (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/***************************************************************************************************
 * Copyright (c) 2003, 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
 **************************************************************************************************/
/*
 * Created on Sep 21, 2003
 *  
 */
package org.eclipse.wst.common.internal.emf.resource;

import org.eclipse.emf.ecore.EObject;


/*
 * Translator filters can be used to differentiate between Object and Attribute Translators without
 * breaking them out into seperate data structures. The Filter will rely on the underlying data
 * structure used to store the children of the given Translator.
 * 
 * getNextChild() will return null when no other translators of the given genre are available.
 * 
 * The processing hints which are created and then passed to getNext*Translator() store information
 * that needs to be persisted between calls. The createProcessingHints() will create a properly
 * initialized array. The constants NEXT_START_HINT_INDX and MODE_HINT_INDX provide pointers into
 * the array for the NEXT_START position and the proper mode to operate in
 * (STANDARD_TRANSLATORS_MODE or
 *  
 */
public abstract class TranslatorFilter {

	protected final Translator translator;
	protected final int version;

	public static final int NEXT_START_HINT_INDX = 0;
	public static final int MODE_HINT_INDX = 1;

	public static final int STANDARD_TRANSLATORS_MODE = 0;
	public static final int VARIABLE_TRANSLATORS_MODE = 1;

	/*
	 * These TranslatorFilters are used in a stateless mode. Only their scanNextTranslator() methods
	 * will be invoked
	 */
	private static final TranslatorFilter objectTranslatorFilter = new ObjectTranslatorFilter(null, -1);
	private static final TranslatorFilter attributeTranslatorFilter = new AttributeTranslatorFilter(null, -1);


	protected int mode = STANDARD_TRANSLATORS_MODE;
	protected int index = -1;

	protected Translator cachedPeekAheadTranslator = null;

	public TranslatorFilter(Translator trans, int version) {
		this.translator = trans;
		this.version = version;
	}

	/**
	 * Calling peek twice will advance the current child
	 */
	public Translator peekNextChild(EObject target) {
		cachedPeekAheadTranslator = getNextChild(target);
		return cachedPeekAheadTranslator;
	}

	/**
	 * getNextChild() will return null when no other translators of the given genre are available.
	 */
	public Translator getNextChild(EObject target) {

		Translator result = null;
		if (cachedPeekAheadTranslator != null) {
			result = cachedPeekAheadTranslator;
			cachedPeekAheadTranslator = null;
			return result;
		}

		int found = 0;
		Translator children[] = null;
		switch (mode) {
			case STANDARD_TRANSLATORS_MODE :
				children = this.translator.getChildren(target, this.version);

				/* Look for the next Attribute Translator */
				found = scanNextTranslator(children, this.index);

				if (found >= 0) {
					/*
					 * If found, (1) update the result, (2) update the index so we can skip ahead on
					 * the next invocation (3) escape the VARIABLE_TRANSLATORS processing
					 */
					result = children[found];
					this.index = found;
					break;
				}
				/*
				 * Reset the index. DO NOT BREAK. Allow entry into VARIABLE_TRANSLATORS case
				 */
				this.index = -1;
				/*
				 * update the mode to VARIABLE_TRANSLATORS so we can skip to it directly next time
				 */
				this.mode = VARIABLE_TRANSLATORS_MODE;
			case VARIABLE_TRANSLATORS_MODE :
				children = this.translator.getVariableChildren(target, this.version);
				found = scanNextTranslator(children, this.index);
				if (found >= 0) {
					/*
					 * If found, (1) update the result, (2) update the index so we can skip ahead on
					 * the next invocation
					 */
					result = children[found];
					this.index = found;
				}

		}

		return result;
	}

	public static final int[] createProcessingHints() {
		return new int[]{-1, STANDARD_TRANSLATORS_MODE};
	}

	public static final Translator getNextAttributeTranslator(Translator translator, int startHint, int[] nextHint, EObject target, int version) {

		return TranslatorFilter.getNextChild(translator, startHint, nextHint, target, version, attributeTranslatorFilter);
	}

	public static final Translator getNextObjectTranslator(Translator translator, int startHint, int[] nextHint, EObject target, int version) {

		return TranslatorFilter.getNextChild(translator, startHint, nextHint, target, version, objectTranslatorFilter);
	}

	/**
	 * getNextChild() takes hints on where to begin in the children array of the given Translator.
	 * When it finds the translator, it will update the hints array with the start hint for the next
	 * invocation(hints[0]) and when necessary it will use update the mode (hints[1]) to either
	 * STANDARD_TRANSLATORS or VARIABLE_TRANSLATORS.
	 * 
	 * @param translator
	 * @param startHint
	 * @param hints
	 *            a two-element array: hints[0] will be updated with the next startHint and hints[1]
	 *            will be used to store the mode.
	 * @param target
	 * @param version
	 * @param translatorFilter
	 * @return
	 */
	public static final Translator getNextChild(Translator translator, int startHint, int[] hints, EObject target, int version, TranslatorFilter translatorFilter) {

		Translator result = null;

		int index = startHint;
		Translator children[] = null;

		switch (hints[MODE_HINT_INDX]) {
			case STANDARD_TRANSLATORS_MODE :
				children = translator.getChildren(target, version);
				if (children != null && startHint < children.length) {

					/* Look for the next Attribute Translator */
					index = translatorFilter.scanNextTranslator(children, index);

					if (index >= 0) {
						/*
						 * If found, (1) update the result, (2) update the index so we can skip
						 * ahead on the next invocation (3) escape the VARIABLE_TRANSLATORS
						 * processing
						 */
						result = children[index];
						break;
					}
					/*
					 * DO NOT BREAK we will default to VARIABLE TRANSLATORS MODE so we must reset
					 * the startHint appropriately
					 */
					startHint = -1;
				}

			case VARIABLE_TRANSLATORS_MODE :
				hints[MODE_HINT_INDX] = VARIABLE_TRANSLATORS_MODE;
				/*
				 * Reset the index.
				 */
				index = startHint;
				children = translator.getVariableChildren(target, version);
				if (children != null && children.length > 0 && startHint < children.length) {
					index = translatorFilter.scanNextTranslator(children, index);
					result = (index >= 0) ? children[index] : null;
				}
		}

		hints[NEXT_START_HINT_INDX] = (result == null && children != null) ? children.length : index;

		return result;
	}


	public abstract int scanNextTranslator(Translator[] children, int start);

	/**
	 * @return
	 */
	public int getIndex() {
		return index;
	}

	/**
	 * @return
	 */
	public int getMode() {
		return mode;
	}

}

Back to the top