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