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  
26  /**
27   * Wraps an ordinary {@link File} as a model source.
28   *
29   * @author Benjamin Bentmann
30   */
31  public class FileModelSource extends FileSource implements ModelSource2 {
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 ModelSource2 getRelatedSource(String relPath) {
55          relPath = relPath.replace('\\', File.separatorChar).replace('/', File.separatorChar);
56  
57          File relatedPom = new File(getFile().getParentFile(), relPath);
58  
59          if (relatedPom.isDirectory()) {
60              // TODO figure out how to reuse ModelLocator.locatePom(File) here
61              relatedPom = new File(relatedPom, "pom.xml");
62          }
63  
64          if (relatedPom.isFile() && relatedPom.canRead()) {
65              return new FileModelSource(new File(relatedPom.toURI().normalize()));
66          }
67  
68          return null;
69      }
70  
71      @Override
72      public URI getLocationURI() {
73          return getFile().toURI();
74      }
75  
76      @Override
77      public boolean equals(Object obj) {
78          if (this == obj) {
79              return true;
80          }
81  
82          if (obj == null) {
83              return false;
84          }
85  
86          if (!FileModelSource.class.equals(obj.getClass())) {
87              return false;
88          }
89          FileModelSource other = (FileModelSource) obj;
90          return getFile().equals(other.getFile());
91      }
92  
93      @Override
94      public int hashCode() {
95          return getFile().hashCode();
96      }
97  }