View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model;
20  
21  import java.util.stream.Collectors;
22  
23  /**
24   * Class InputLocation.
25   *
26   * @version $Revision$ $Date$
27   */
28  @SuppressWarnings("all")
29  public final class InputLocation implements java.io.Serializable, Cloneable, InputLocationTracker {
30  
31      // --------------------------/
32      // - Class/Member Variables -/
33      // --------------------------/
34  
35      /**
36       * The one-based line number. The value will be non-positive if
37       * unknown.
38       */
39      private int lineNumber = -1;
40  
41      /**
42       * The one-based column number. The value will be non-positive
43       * if unknown.
44       */
45      private int columnNumber = -1;
46  
47      /**
48       * Field source.
49       */
50      private InputSource source;
51  
52      /**
53       * Field locations.
54       */
55      private java.util.Map<Object, InputLocation> locations;
56  
57      /**
58       * Field location.
59       */
60      private InputLocation location;
61  
62      /**
63       * Field importedFrom.
64       */
65      private InputLocation importedFrom;
66  
67      // ----------------/
68      // - Constructors -/
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      } // -- org.apache.maven.model.InputLocation(int, int)
88  
89      public InputLocation(int lineNumber, int columnNumber, InputSource source) {
90          this.lineNumber = lineNumber;
91          this.columnNumber = columnNumber;
92          this.source = source;
93      } // -- org.apache.maven.model.InputLocation(int, int, InputSource)
94  
95      // -----------/
96      // - Methods -/
97      // -----------/
98  
99      /**
100      * Method clone.
101      *
102      * @return InputLocation
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     } // -- InputLocation clone()
118 
119     /**
120      * Get the one-based column number. The value will be
121      * non-positive if unknown.
122      *
123      * @return int
124      */
125     public int getColumnNumber() {
126         return this.columnNumber;
127     } // -- int getColumnNumber()
128 
129     /**
130      * Get the one-based line number. The value will be
131      * non-positive if unknown.
132      *
133      * @return int
134      */
135     public int getLineNumber() {
136         return this.lineNumber;
137     } // -- int getLineNumber()
138 
139     /**
140      *
141      *
142      * @param key
143      * @return InputLocation
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     } // -- InputLocation getLocation( Object )
159 
160     /**
161      *
162      *
163      * @return Map
164      */
165     public java.util.Map<Object, InputLocation> getLocations() {
166         return locations;
167     } // -- java.util.Map<Object, InputLocation> getLocations()
168 
169     /**
170      *
171      *
172      * @param key
173      * @param location
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     } // -- void setLocation( Object, InputLocation )
191 
192     /**
193      *
194      *
195      * @param key
196      * @param location
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     } // -- void setOtherLocation( Object, InputLocation )
206 
207     /**
208      *
209      *
210      * @param key
211      * @return InputLocation
212      */
213     private InputLocation getOtherLocation(Object key) {
214         return (locations != null) ? locations.get(key) : null;
215     } // -- InputLocation getOtherLocation( Object )
216 
217     /**
218      * Get the source field.
219      *
220      * @return InputSource
221      */
222     public InputSource getSource() {
223         return this.source;
224     } // -- InputSource getSource()
225 
226     /**
227      * Gets the parent InputLocation where this InputLocation may have been imported from.
228      * Can return {@code null}.
229      *
230      * @return InputLocation
231      * @since 4.0.0
232      */
233     public InputLocation getImportedFrom() {
234         return importedFrom;
235     }
236 
237     /**
238      * Set the imported from location.
239      *
240      * @param importedFrom
241      */
242     public void setImportedFrom(InputLocation importedFrom) {
243         this.importedFrom = importedFrom;
244     }
245 
246     /**
247      * Method merge.
248      *
249      * @param target
250      * @param sourceDominant
251      * @param source
252      * @return InputLocation
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     } // -- InputLocation merge( InputLocation, InputLocation, boolean )
279 
280     /**
281      * Method merge.
282      *
283      * @param target
284      * @param indices
285      * @param source
286      * @return InputLocation
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     } // -- InputLocation merge( InputLocation, InputLocation, java.util.Collection )
322 
323     /**
324      *
325      *
326      * @param locations
327      */
328     public void setLocations(java.util.Map<Object, InputLocation> locations) {
329         this.locations = locations;
330     } // -- void setLocations( java.util.Map )
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     // - Inner Classes -/
358     // -----------------/
359 
360     /**
361      * Class StringFormatter.
362      *
363      * @version $Revision$ $Date$
364      */
365     public abstract static class StringFormatter {
366 
367         // -----------/
368         // - Methods -/
369         // -----------/
370 
371         /**
372          * Method toString.
373          *
374          * @param location
375          * @return String
376          */
377         public abstract String toString(InputLocation location);
378     }
379 
380     @Override
381     public String toString() {
382         return getLineNumber() + " : " + getColumnNumber() + ", " + getSource();
383     }
384 }