Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 857078dc2464a4d9054940a43d2f26718c83d59e (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
/**
 *  Copyright (c) 2016 Angelo ZERR 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:
 *  Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
 *  Alina Marin <alina@mx1.ibm.com> - fixed some stuff to improve the synch between the editor and the model.
 */
package org.eclipse.wst.json.core.internal.document;

import org.eclipse.wst.json.core.document.IJSONNode;
import org.eclipse.wst.json.core.document.IJSONPair;
import org.eclipse.wst.json.core.document.IJSONStructure;
import org.eclipse.wst.json.core.document.IJSONValue;
import org.eclipse.wst.json.core.document.JSONException;
import org.eclipse.wst.json.core.regions.JSONRegionContexts;
import org.eclipse.wst.json.core.util.JSONUtil;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;

public class JSONPairImpl extends JSONStructureImpl implements IJSONPair {

	private String name;
	private ITextRegion nameRegion = null;
	private ITextRegion equalRegion = null;
	private JSONObjectImpl ownerObject = null;
	private IJSONValue value;

	@Override
	public String getName() {
		return name;
	}

	protected void setName(String name) {
		name = name.substring(1, name.length() - 1); // remove start/end quote
		String oldName = this.name;
		this.name = name;
		notify(CHANGE, this, oldName, name, getStartOffset());
	}

	@Override
	public String getNodeName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getNodeValue() throws JSONException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IJSONNode cloneNode(boolean deep) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public short getNodeType() {
		return PAIR_NODE;
	}

	public void setOwnerObject(JSONObjectImpl ownerObject) {
		this.ownerObject = ownerObject;
	}

	public JSONObjectImpl getOwnerObject() {
		return ownerObject;
	}

	@Override
	public IJSONValue getValue() {
		return value;
	}

	public void setNameRegion(ITextRegion nameRegion) {
		this.nameRegion = nameRegion;
	}

	@Override
	public ITextRegion getNameRegion() {
		return nameRegion;
	}

	@Override
	public int getStartOffset() {
		if (this.getStartStructuredDocumentRegion() == null)
			return 0;
		int offset = this.getStartStructuredDocumentRegion().getStartOffset();
		// if (this.value != null) {
		// if (value.getNodeType() == IJSONNode.ARRAY_NODE)
		// return (offset + this.value.getStartOffset());
		// //return (offset);
		// }
		// if (this.nameRegion != null) {
		// return (offset + this.nameRegion.getStart());
		// }
		// if (this.equalRegion != null) {
		// return (offset + this.equalRegion.getStart());
		// }
		// if (this.value != null) {
		// return (offset + this.value.getStartOffset());
		// }
		return offset;
	}

	@Override
	public int getEndOffset() {
		if (this.getStartStructuredDocumentRegion() == null)
			return 0;
		int offset = this.getStartStructuredDocumentRegion().getEndOffset();
		if (this.value != null) {
			// if (value.getNodeType() == IJSONNode.OBJECT_NODE ||
			// value.getNodeType() == IJSONNode.ARRAY_NODE )
			return (this.value.getEndOffset());
		}
		// if (this.equalRegion != null) {
		// return (offset + this.equalRegion.getEnd());
		// }
		// if (this.nameRegion != null) {
		// return (offset + this.nameRegion.getEnd());
		// }
		return offset;
	}

	public void setEqualRegion(ITextRegion equalRegion) {
		this.equalRegion = equalRegion;
	}

	@Override
	public short getNodeValueType() {
		if (value != null) {
			return value.getNodeType();
		}
		return -1;
	}

	@Override
	public String getSimpleValue() {
		if (value == null) {
			return null;
		}
		return value.getSimpleValue();
	}
	
	@Override
	public String getValueRegionType() {
		if (value != null) {
			if (value.getStartStructuredDocumentRegion() == null) {
				return null;
			}
			return value.getStartStructuredDocumentRegion().getType();
		}
		return null;
	}

	public void setValue(IJSONValue value) {
		((JSONValueImpl) value).setParentNode(ownerObject);
		((JSONValueImpl) value).setOwnerPairNode(this);
		this.value = value;
		notify(CHANGE, this, null, this.value, getStartOffset());
	}
	
	public void updateValue(IJSONValue value) {
		IJSONValue oldValue = this.value;
		((JSONValueImpl) value).setParentNode(ownerObject);
		((JSONValueImpl) value).setOwnerPairNode(this);
		this.value = value;
		notify(CHANGE, null, oldValue, this.value, getStartOffset());
	}

	@Override
	public IStructuredDocumentRegion getEndStructuredDocumentRegion() {
		if (value != null) {
			return ((IJSONStructure) value).getStartStructuredDocumentRegion();
		}
		return super.getEndStructuredDocumentRegion();
	}

	@Override
	public String toString() {
		StringBuilder buffer = new StringBuilder();
		String tagName = getName();
		buffer.append(tagName);
		IStructuredDocumentRegion startStructuredDocumentRegion = getStartStructuredDocumentRegion();
		if (startStructuredDocumentRegion != null) {
			buffer.append('@');
			buffer.append(startStructuredDocumentRegion.toString());
		}
		IStructuredDocumentRegion endStructuredDocumentRegion = getEndStructuredDocumentRegion();
		if (endStructuredDocumentRegion != null) {
			buffer.append('@');
			buffer.append(endStructuredDocumentRegion.toString());
		}
		return buffer.toString();

	}
}

Back to the top