Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9f3af73e3532ac6d9831134ad77ee8c84293a00c (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package org.eclipse.jpt.jaxb.eclipselink.core.internal.context.java;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.common.core.internal.utility.SimpleTextRange;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.common.utility.internal.CollectionTools;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.jaxb.core.context.java.JavaContextNode;
import org.eclipse.jpt.jaxb.core.internal.context.java.AbstractJavaContextNode;
import org.eclipse.jpt.jaxb.eclipselink.core.context.java.ELXmlPath;
import org.eclipse.jpt.jaxb.eclipselink.core.internal.validation.ELJaxbValidationMessageBuilder;
import org.eclipse.jpt.jaxb.eclipselink.core.internal.validation.ELJaxbValidationMessages;
import org.eclipse.jpt.jaxb.eclipselink.core.resource.java.XmlPathAnnotation;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;


public class ELJavaXmlPath
		extends AbstractJavaContextNode
		implements ELXmlPath {
		
	protected String value;
	
	protected Context context;
	
	protected List<Step> steps;
	
	
	public ELJavaXmlPath(JavaContextNode parent, Context context) {
		super(parent);
		this.context = context;
		initValue();
	}
	
	
	// ***** sync/update *****
	
	@Override
	public void synchronizeWithResourceModel() {
		super.synchronizeWithResourceModel();
		syncValue();
	}
	
	
	// ***** value *****
	
	public String getValue() {
		return this.value;
	}
	
	public void setValue(String value) {
		getAnnotation().setValue(value);
		setValue_(value);
	}
	
	protected void setValue_(String value) {
		String old = this.value;
		this.value = value;
		firePropertyChanged(VALUE_PROPERTY, old, this.value);
	}
	
	protected void initValue() {
		this.value = getAnnotation().getValue();
		initSteps();
	}
	
	protected void syncValue() {
		setValue_(getAnnotation().getValue());
		syncSteps();
		
	}
	
	protected XmlPathAnnotation getAnnotation() {
		return this.context.getAnnotation();
	}
	
	
	// ***** steps *****
	
	protected void initSteps() {
		this.steps = new Vector<Step>();
		CollectionTools.addAll(this.steps, parse(this.value));
	}
	
	protected void syncSteps() {
		String oldXpath = getCompleteXpath();
		if (! StringTools.stringsAreEqual(this.value, oldXpath)) {
			this.steps.clear();
			CollectionTools.addAll(this.steps, parse(this.value));
		}
	}
	
	protected String getCompleteXpath() {
		StringBuffer sb = new StringBuffer();
		for (Iterator<Step> stream = this.steps.iterator(); stream.hasNext(); ) {
			sb.append(stream.next().value);
			if (stream.hasNext()) {
				sb.append(DELIM);
			}
		}
		return sb.toString();
	}
	
	
	// ***** validation *****
	
	@Override
	public TextRange getValidationTextRange(CompilationUnit astRoot) {
		return getAnnotation().getTextRange(astRoot);
	}
	
	@Override
	public void validate(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) {
		super.validate(messages, reporter, astRoot);
		
		String completeXpath = getCompleteXpath();
		if (completeXpath.startsWith(DELIM)) {
			messages.add(
					ELJaxbValidationMessageBuilder.buildMessage(
								IMessage.HIGH_SEVERITY,
								ELJaxbValidationMessages.XML_PATH__ROOT_NOT_SUPPORTED,
								new String[] { this.value },
								ELJavaXmlPath.this,
								getValueTextRange(astRoot)));
			return;
		}
		
		IMessage firstFormError = null;
		Iterator<Step> stream = this.steps.iterator();
		while (firstFormError == null && stream.hasNext() ) {
			firstFormError = stream.next().getFirstFormError(messages, astRoot);
		}
		if (firstFormError != null) {
			messages.add(firstFormError);
			return;
		}
	}
	
	protected TextRange getValueTextRange(CompilationUnit astRoot) {
		// should never be null
		return getAnnotation().getValueTextRange(astRoot);
	}
	
	protected TextRange buildTextRange(Step step, CompilationUnit astRoot) {
		TextRange entireTextRange = getValueTextRange(astRoot);
		int start = 1; // move to right one since initial text range includes first quote
		for (Step each : this.steps) {
			int length = each.value.length();
				if (each == step) {
				if (length == 0) {
					if (this.steps.size() > 1) {
						// expand text range to include either leading or trailing "/"
						length += 1;
						if (step.index != 0) {
							// if step is not first step, use leading "/"
							start --;
						}
					}
					else {
						return entireTextRange;
					}
				}
				return new SimpleTextRange(entireTextRange.getOffset() + start, length, entireTextRange.getLineNumber());
			}
			else {
				start += length + 1; // step plus "/"
			}
		}
		throw new IllegalArgumentException("Step must be in list of this XmlPath's steps.");
	}
	
	
	// ***** xpath parsing *****
	
	protected static String DELIM = "/";
	
	protected static String TEXT = "text()";
	
	protected static String SELF = ".";
	
	protected static String ATT_PREFIX = "@";
	
	protected Step[] parse(String xpath) {
		if (xpath == null) {
			return new Step[0];
		}
		String[] segments = xpath.split(DELIM, -1);
		Step[] steps = new Step[segments.length];
		for (int i = 0; i < segments.length; i ++ ) {
			steps[i] = new Step(segments[i], i);
		}
		return steps;
	}
	
	
	public interface Context {
		
		XmlPathAnnotation getAnnotation();
	}
	
	
	protected class Step {
		
		protected String value;
		
		protected int index;
		
		protected Flavor flavor;
		
		
		protected Step(String value, int index) {
			this.value = value;
			this.index = index;
			this.flavor = determineFlavor();
		}
		
		protected Flavor determineFlavor() {
			if (StringTools.stringIsEmpty(this.value)) {
				return Flavor.EMPTY;
			}
			else if (TEXT.equals(this.value)) {
				return Flavor.TEXT;
			}
			else if (SELF.equals(this.value)) {
				return Flavor.SELF;
			}
			else if (this.value.startsWith(ATT_PREFIX)) {
				return Flavor.ATTRIBUTE;
			}
			else {
				return Flavor.ELEMENT;
			}
		}
		
		protected IMessage getFirstFormError(List<IMessage> messages, CompilationUnit astRoot) {
			switch (this.flavor) {
				case EMPTY :
					return ELJaxbValidationMessageBuilder.buildMessage(
							IMessage.HIGH_SEVERITY,
							ELJaxbValidationMessages.XML_PATH__INVALID_FORM_ILLEGAL_SEGMENT,
							new String[] { this.value },
							ELJavaXmlPath.this,
							ELJavaXmlPath.this.buildTextRange(this, astRoot));
				case SELF :
					if (this.index != 0) {
						return ELJaxbValidationMessageBuilder.buildMessage(
							IMessage.HIGH_SEVERITY,
							ELJaxbValidationMessages.XML_PATH__SELF_SEGMENT_MUST_BE_FIRST_SEGMENT,
							ELJavaXmlPath.this,
							ELJavaXmlPath.this.buildTextRange(this, astRoot));
					}
					return null;
				case TEXT :
					if (this.index != ELJavaXmlPath.this.steps.size() - 1) {
						return ELJaxbValidationMessageBuilder.buildMessage(
							IMessage.HIGH_SEVERITY,
							ELJaxbValidationMessages.XML_PATH__TEXT_SEGMENT_MUST_BE_LAST_SEGMENT,
							ELJavaXmlPath.this,
							ELJavaXmlPath.this.buildTextRange(this, astRoot));
					}
					return null;
				case ATTRIBUTE :
					if (this.index != ELJavaXmlPath.this.steps.size() - 1) {
						return ELJaxbValidationMessageBuilder.buildMessage(
							IMessage.HIGH_SEVERITY,
							ELJaxbValidationMessages.XML_PATH__ATTRIBUTE_SEGMENT_MUST_BE_LAST_SEGMENT,
							ELJavaXmlPath.this,
							ELJavaXmlPath.this.buildTextRange(this, astRoot));
					}

				default :
					return null;
			}
		}
	}
	
	
	protected enum Flavor {
		
		EMPTY,
		
		SELF,
		
		TEXT,
		
		ELEMENT,
		
		ATTRIBUTE
	}
}

Back to the top