Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a7975bfd860c3c60b75e3566a128151d131004d1 (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
package org.eclipse.dltk.javascript.internal.debug;

import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IVariable;
import org.eclipse.dltk.debug.core.DLTKDebugPlugin;
import org.eclipse.dltk.debug.core.model.ArrayScriptType;
import org.eclipse.dltk.debug.core.model.AtomicScriptType;
import org.eclipse.dltk.debug.core.model.ComplexScriptType;
import org.eclipse.dltk.debug.core.model.IScriptType;
import org.eclipse.dltk.debug.core.model.IScriptTypeFactory;
import org.eclipse.dltk.debug.core.model.IScriptValue;
import org.eclipse.dltk.debug.core.model.StringScriptType;

public class JavaScriptTypeFactory implements IScriptTypeFactory {
	private static final String[] atomicTypes = { "number", "boolean", "date" };

	private static final ThreadLocal<Integer> detailsLevel = new ThreadLocal<Integer>() {
		protected Integer initialValue() {
			return Integer.valueOf(0);
		}
	};

	public JavaScriptTypeFactory() {

	}

	public IScriptType buildType(String type) {
		for (int i = 0; i < atomicTypes.length; ++i) {
			if (atomicTypes[i].equals(type)) {
				return new AtomicScriptType(type);
			}
		}

		if ("javaarray".equals(type) || "array".equals(type)) {
			return new ArrayScriptType() {
				protected String buildDetailString(IVariable variable)
						throws DebugException {
					Integer currentLevel = detailsLevel.get();
					detailsLevel
							.set(Integer.valueOf(currentLevel.intValue() + 1));
					try {
						if (variable.getValue() instanceof IScriptValue
								&& currentLevel.intValue() < 2) {
							IScriptValue value = (IScriptValue) variable
									.getValue();
							return value.getType().formatDetails(value);
						}
						return super.buildDetailString(variable);
					} finally {
						currentLevel = detailsLevel.get();
						detailsLevel.set(Integer.valueOf(currentLevel
								.intValue() - 1));
					}

				}
			};
		}

		if ("string".equals(type)) {
			return new StringScriptType("string");
		}
		return new ComplexScriptType(type) {
			public String formatValue(IScriptValue value) {
				StringBuffer sb = new StringBuffer();
				sb.append(value.getRawValue());
				String id = value.getInstanceId();
				if (id != null) {
					sb.append(" (id = " + id + ")"); // TODO add constant
				}

				return sb.toString();
			}

			/**
			 * @see org.eclipse.dltk.debug.core.model.ComplexScriptType#formatDetails(org.eclipse.dltk.debug.core.model.IScriptValue)
			 */
			public String formatDetails(IScriptValue value) {
				StringBuffer sb = new StringBuffer();
				Integer currentLevel = detailsLevel.get();
				detailsLevel.set(Integer.valueOf(currentLevel.intValue() + 1));
				try {
					IVariable[] variables = value.getVariables();
					if (variables.length > 0) {
						sb.append("{");
						for (int i = 0; i < variables.length; i++) {
							sb.append(variables[i].getName());
							sb.append(":");
							if (variables[i].getValue() instanceof IScriptValue) {
								if (currentLevel.intValue() < 2) {
									IScriptValue sv = (IScriptValue) variables[i]
											.getValue();
									sb.append(sv.getType().formatDetails(sv));
								} else
									sb.append("{...}");
							} else {
								sb.append(variables[i].getValue()
										.getValueString());
							}
							sb.append(",");
						}
						sb.setLength(sb.length() - 1);
						sb.append("}");
					} else {
						sb.append(value.getRawValue());
						String id = value.getInstanceId();
						if (id != null) {
							sb.append(" (id = " + id + ")");
						}
					}
				} catch (DebugException ex) {
					DLTKDebugPlugin.logWarning(
							"error creating variable details", ex);
				} finally {
					currentLevel = detailsLevel.get();
					detailsLevel
							.set(Integer.valueOf(currentLevel.intValue() - 1));
				}
				return sb.toString();
			}
		};
	}
}

Back to the top