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