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