Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f7dd6da11db33680231796bf1af63fba0824e7d8 (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
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems 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:
 * QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.qt.core.qmltypes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.internal.qt.core.Activator;
import org.eclipse.cdt.qt.core.qmljs.IJSArrayExpression;
import org.eclipse.cdt.qt.core.qmljs.IJSExpression;
import org.eclipse.cdt.qt.core.qmljs.IQmlASTNode;
import org.eclipse.cdt.qt.core.qmljs.IQmlBinding;
import org.eclipse.cdt.qt.core.qmljs.IQmlProgram;
import org.eclipse.cdt.qt.core.qmljs.IQmlPropertyBinding;
import org.eclipse.cdt.qt.core.qmljs.IQmlQualifiedID;
import org.eclipse.cdt.qt.core.qmljs.IQmlRootObject;
import org.eclipse.cdt.qt.core.qmljs.IQmlScriptBinding;
import org.eclipse.cdt.qt.core.qmljs.QMLExpressionEvaluator;
import org.eclipse.cdt.qt.core.qmljs.QMLExpressionEvaluator.InvalidExpressionException;

public class QMLModelBuilder {

	private final Map<String, QMLModuleInfo> moduleMap = new HashMap<>();

	public QMLModelBuilder() {
	}

	public QMLModuleInfo addModule(String module, IQmlASTNode ast) {
		QMLModuleInfo info = moduleMap.get(module);
		if (!moduleMap.containsKey(module)) {
			if (ensureNode(ast, IQmlProgram.class)) {
				IQmlRootObject obj = ((IQmlProgram) ast).getRootObject();
				if (ensureNode(obj, IQmlRootObject.class)) {
					info = new QMLModuleInfo(this, obj);
					moduleMap.put(module, info);
				}
			}
		}
		return info;
	}

	public QMLModuleInfo getModule(String module) {
		return moduleMap.get(module);
	}

	public boolean hasModule(String module) {
		return moduleMap.get(module) != null;
	}

	boolean ensureIdentifier(IQmlQualifiedID actual, String expected) {
		if (!actual.getName().equals(expected)) {
			Activator.log("[QmlTypes] Unexpected node identifier: expected '" + expected + "', but was '" //$NON-NLS-1$ //$NON-NLS-2$
					+ actual.getName() + "'"); //$NON-NLS-1$
			return false;
		}
		return true;
	}

	boolean ensureNode(IQmlASTNode actual, Class<? extends IQmlASTNode> expected) {
		if (!expected.isInstance(actual)) {
			Activator.log("[QmlTypes] Expected node '" + expected + "', but was '" + actual.getClass().getInterfaces()[0] + "'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			return false;
		}
		return true;
	}

	boolean ensureValue(Object actual, Class<?> expected) {
		if (!expected.isInstance(actual)) {
			Activator.log("[QmlTypes] Unexpected value: expected '" + expected + "', but was '" //$NON-NLS-1$ //$NON-NLS-2$
					+ actual.getClass().getInterfaces()[0] + "'"); //$NON-NLS-1$
			return false;
		}
		return true;
	}

	void unexpectedNode(IQmlASTNode node) {
		Activator.log("[QmlTypes] Unexpected node '" + node.getClass().getInterfaces()[0] + "'"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	String getStringBinding(IQmlPropertyBinding prop) {
		IQmlBinding b = prop.getBinding();
		if (ensureNode(b, IQmlScriptBinding.class)) {
			IQmlScriptBinding sb = (IQmlScriptBinding) b;
			if (ensureNode(sb.getScript(), IJSExpression.class)) {
				try {
					Object value = QMLExpressionEvaluator.evaluateConstExpr((IJSExpression) sb.getScript());
					if (value instanceof String) {
						return (String) value;
					}
				} catch (InvalidExpressionException e) {
					handleException(e);
				}
			}
		}
		return null;
	}

	String[] getStringArrayBinding(IQmlPropertyBinding prop) {
		ArrayList<String> result = new ArrayList<>();
		IQmlBinding b = prop.getBinding();
		if (ensureNode(b, IQmlScriptBinding.class)) {
			IQmlScriptBinding sb = (IQmlScriptBinding) b;
			if (ensureNode(sb.getScript(), IJSArrayExpression.class)) {
				IJSArrayExpression arrExpr = (IJSArrayExpression) sb.getScript();
				for (IJSExpression expr : arrExpr.getElements()) {
					try {
						Object value = QMLExpressionEvaluator.evaluateConstExpr(expr);
						if (value instanceof String) {
							result.add((String) value);
						}
					} catch (InvalidExpressionException e) {
						handleException(e);
					}
				}
			}
		}
		return result.toArray(new String[result.size()]);
	}

	public Integer[] getIntegerArrayBinding(IQmlPropertyBinding prop) {
		ArrayList<Integer> result = new ArrayList<>();
		IQmlBinding b = prop.getBinding();
		if (ensureNode(b, IQmlScriptBinding.class)) {
			IQmlScriptBinding sb = (IQmlScriptBinding) b;
			if (ensureNode(sb.getScript(), IJSArrayExpression.class)) {
				IJSArrayExpression arrExpr = (IJSArrayExpression) sb.getScript();
				for (IJSExpression expr : arrExpr.getElements()) {
					try {
						Object value = QMLExpressionEvaluator.evaluateConstExpr(expr);
						if (value instanceof Number) {
							result.add(((Number) value).intValue());
						}
					} catch (InvalidExpressionException e) {
						handleException(e);
					}
				}
			}
		}
		return result.toArray(new Integer[result.size()]);
	}

	boolean getBooleanBinding(IQmlPropertyBinding prop) {
		IQmlBinding b = prop.getBinding();
		if (ensureNode(b, IQmlScriptBinding.class)) {
			IQmlScriptBinding sb = (IQmlScriptBinding) b;
			if (ensureNode(sb.getScript(), IJSExpression.class)) {
				try {
					Object value = QMLExpressionEvaluator.evaluateConstExpr((IJSExpression) sb.getScript());
					if (value instanceof Number) {
						return (Boolean) value;
					}
				} catch (InvalidExpressionException e) {
					handleException(e);
				}
			}
		}
		return false;
	}

	public Integer getIntegerBinding(IQmlPropertyBinding prop) {
		IQmlBinding b = prop.getBinding();
		if (ensureNode(b, IQmlScriptBinding.class)) {
			IQmlScriptBinding sb = (IQmlScriptBinding) b;
			if (ensureNode(sb.getScript(), IJSExpression.class)) {
				try {
					Object value = QMLExpressionEvaluator.evaluateConstExpr((IJSExpression) sb.getScript());
					if (value instanceof Number) {
						return ((Number) value).intValue();
					}
				} catch (InvalidExpressionException e) {
					handleException(e);
				}
			}
		}
		return 0;
	}

	public void handleException(Throwable t) {
		Activator.log("[QmlTypes] " + t.getMessage()); //$NON-NLS-1$
	}

}

Back to the top