1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model;
20
21 import java.util.stream.Collectors;
22
23
24
25
26
27
28 @SuppressWarnings("all")
29 public final class InputLocation implements java.io.Serializable, Cloneable, InputLocationTracker {
30
31
32
33
34
35
36
37
38
39 private int lineNumber = -1;
40
41
42
43
44
45 private int columnNumber = -1;
46
47
48
49
50 private InputSource source;
51
52
53
54
55 private java.util.Map<Object, InputLocation> locations;
56
57
58
59
60 private InputLocation location;
61
62
63
64
65 private InputLocation importedFrom;
66
67
68
69
70
71 public InputLocation(org.apache.maven.api.model.InputLocation location) {
72 this.lineNumber = location.getLineNumber();
73 this.columnNumber = location.getColumnNumber();
74 this.source = location.getSource() != null ? new InputSource(location.getSource()) : null;
75 this.locations = location.getLocations().isEmpty()
76 ? null
77 : location.getLocations().entrySet().stream()
78 .collect(Collectors.toMap(
79 e -> e.getKey(),
80 e -> e.getValue() == location ? this : new InputLocation(e.getValue())));
81 this.importedFrom = location.getImportedFrom() != null ? new InputLocation(location.getImportedFrom()) : null;
82 }
83
84 public InputLocation(int lineNumber, int columnNumber) {
85 this.lineNumber = lineNumber;
86 this.columnNumber = columnNumber;
87 }
88
89 public InputLocation(int lineNumber, int columnNumber, InputSource source) {
90 this.lineNumber = lineNumber;
91 this.columnNumber = columnNumber;
92 this.source = source;
93 }
94
95
96
97
98
99
100
101
102
103
104 public InputLocation clone() {
105 try {
106 InputLocation copy = (InputLocation) super.clone();
107
108 if (copy.locations != null) {
109 copy.locations = new java.util.LinkedHashMap(copy.locations);
110 }
111
112 return copy;
113 } catch (Exception ex) {
114 throw (RuntimeException)
115 new UnsupportedOperationException(getClass().getName() + " does not support clone()").initCause(ex);
116 }
117 }
118
119
120
121
122
123
124
125 public int getColumnNumber() {
126 return this.columnNumber;
127 }
128
129
130
131
132
133
134
135 public int getLineNumber() {
136 return this.lineNumber;
137 }
138
139
140
141
142
143
144
145 public InputLocation getLocation(Object key) {
146 if (key instanceof String) {
147 switch ((String) key) {
148 case "": {
149 return this.location;
150 }
151 default: {
152 return getOtherLocation(key);
153 }
154 }
155 } else {
156 return getOtherLocation(key);
157 }
158 }
159
160
161
162
163
164
165 public java.util.Map<Object, InputLocation> getLocations() {
166 return locations;
167 }
168
169
170
171
172
173
174
175 public void setLocation(Object key, InputLocation location) {
176 if (key instanceof String) {
177 switch ((String) key) {
178 case "": {
179 this.location = location;
180 return;
181 }
182 default: {
183 setOtherLocation(key, location);
184 return;
185 }
186 }
187 } else {
188 setOtherLocation(key, location);
189 }
190 }
191
192
193
194
195
196
197
198 public void setOtherLocation(Object key, InputLocation location) {
199 if (location != null) {
200 if (this.locations == null) {
201 this.locations = new java.util.LinkedHashMap<>();
202 }
203 this.locations.put(key, location);
204 }
205 }
206
207
208
209
210
211
212
213 private InputLocation getOtherLocation(Object key) {
214 return (locations != null) ? locations.get(key) : null;
215 }
216
217
218
219
220
221
222 public InputSource getSource() {
223 return this.source;
224 }
225
226
227
228
229
230
231
232
233 public InputLocation getImportedFrom() {
234 return importedFrom;
235 }
236
237
238
239
240
241
242 public void setImportedFrom(InputLocation importedFrom) {
243 this.importedFrom = importedFrom;
244 }
245
246
247
248
249
250
251
252
253
254 public static InputLocation merge(InputLocation target, InputLocation source, boolean sourceDominant) {
255 if (source == null) {
256 return target;
257 } else if (target == null) {
258 return source;
259 }
260
261 InputLocation result = new InputLocation(target.getLineNumber(), target.getColumnNumber(), target.getSource());
262
263 java.util.Map<Object, InputLocation> locations;
264 java.util.Map<Object, InputLocation> sourceLocations = source.getLocations();
265 java.util.Map<Object, InputLocation> targetLocations = target.getLocations();
266 if (sourceLocations == null) {
267 locations = targetLocations;
268 } else if (targetLocations == null) {
269 locations = sourceLocations;
270 } else {
271 locations = new java.util.LinkedHashMap();
272 locations.putAll(sourceDominant ? targetLocations : sourceLocations);
273 locations.putAll(sourceDominant ? sourceLocations : targetLocations);
274 }
275 result.setLocations(locations);
276
277 return result;
278 }
279
280
281
282
283
284
285
286
287
288 public static InputLocation merge(
289 InputLocation target, InputLocation source, java.util.Collection<Integer> indices) {
290 if (source == null) {
291 return target;
292 } else if (target == null) {
293 return source;
294 }
295
296 InputLocation result = new InputLocation(target.getLineNumber(), target.getColumnNumber(), target.getSource());
297
298 java.util.Map<Object, InputLocation> locations;
299 java.util.Map<Object, InputLocation> sourceLocations = source.getLocations();
300 java.util.Map<Object, InputLocation> targetLocations = target.getLocations();
301 if (sourceLocations == null) {
302 locations = targetLocations;
303 } else if (targetLocations == null) {
304 locations = sourceLocations;
305 } else {
306 locations = new java.util.LinkedHashMap<>();
307 for (java.util.Iterator<Integer> it = indices.iterator(); it.hasNext(); ) {
308 InputLocation location;
309 Integer index = it.next();
310 if (index.intValue() < 0) {
311 location = sourceLocations.get(Integer.valueOf(~index.intValue()));
312 } else {
313 location = targetLocations.get(index);
314 }
315 locations.put(Integer.valueOf(locations.size()), location);
316 }
317 }
318 result.setLocations(locations);
319
320 return result;
321 }
322
323
324
325
326
327
328 public void setLocations(java.util.Map<Object, InputLocation> locations) {
329 this.locations = locations;
330 }
331
332 public org.apache.maven.api.model.InputLocation toApiLocation() {
333 if (locations != null && locations.values().contains(this)) {
334 if (locations.size() == 1 && locations.values().iterator().next() == this) {
335 return new org.apache.maven.api.model.InputLocation(
336 lineNumber,
337 columnNumber,
338 source != null ? source.toApiSource() : null,
339 locations.keySet().iterator().next());
340 } else {
341 return new org.apache.maven.api.model.InputLocation(
342 lineNumber, columnNumber, source != null ? source.toApiSource() : null);
343 }
344 } else {
345 return new org.apache.maven.api.model.InputLocation(
346 lineNumber,
347 columnNumber,
348 source != null ? source.toApiSource() : null,
349 locations != null
350 ? locations.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()
351 .toApiLocation()))
352 : null);
353 }
354 }
355
356
357
358
359
360
361
362
363
364
365 public abstract static class StringFormatter {
366
367
368
369
370
371
372
373
374
375
376
377 public abstract String toString(InputLocation location);
378 }
379
380 @Override
381 public String toString() {
382 return getLineNumber() + " : " + getColumnNumber() + ", " + getSource();
383 }
384 }