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.building;
20  
21  import java.io.File;
22  import java.net.URI;
23  
24  import org.apache.maven.building.FileSource;
25  import org.apache.maven.model.locator.ModelLocator;
26  
27  /**
28   * Wraps an ordinary {@link File} as a model source.
29   *
30   */
31  public class FileModelSource extends FileSource implements ModelSource3 {
32  
33      /**
34       * Creates a new model source backed by the specified file.
35       *
36       * @param pomFile The POM file, must not be {@code null}.
37       */
38      public FileModelSource(File pomFile) {
39          super(pomFile);
40      }
41  
42      /**
43       *
44       * @return the file of this source
45       *
46       * @deprecated instead use {@link #getFile()}
47       */
48      @Deprecated
49      public File getPomFile() {
50          return getFile();
51      }
52  
53      @Override
54      public ModelSource3 getRelatedSource(ModelLocator locator, String relPath) {
55          relPath = relPath.replace('\\', File.separatorChar).replace('/', File.separatorChar);
56  
57          File path = new File(getFile().getParentFile(), relPath);
58  
59          File relatedPom = locator.locateExistingPom(path);
60  
61          if (relatedPom != null) {
62              return new FileModelSource(relatedPom.toPath().normalize().toFile());
63          }
64  
65          return null;
66      }
67  
68      @Override
69      public URI getLocationURI() {
70          return getFile().toURI();
71      }
72  
73      @Override
74      public boolean equals(Object obj) {
75          if (this == obj) {
76              return true;
77          }
78  
79          if (obj == null) {
80              return false;
81          }
82  
83          if (!FileModelSource.class.equals(obj.getClass())) {
84              return false;
85          }
86          FileModelSource other = (FileModelSource) obj;
87          return getFile().equals(other.getFile());
88      }
89  
90      @Override
91      public int hashCode() {
92          return getFile().hashCode();
93      }
94  }