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