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.util.List;
22  
23  import org.apache.maven.model.Model;
24  import org.apache.maven.model.Profile;
25  
26  /**
27   * Holds a model along with some auxiliary information. This internal utility class assists the model builder during POM
28   * processing by providing a means to transport information that cannot be (easily) extracted from the model itself.
29   *
30   * @author Benjamin Bentmann
31   */
32  class ModelData {
33      private final ModelSource source;
34  
35      private Model model;
36  
37      private Model rawModel;
38  
39      private List<Profile> activeProfiles;
40  
41      private String groupId;
42  
43      private String artifactId;
44  
45      private String version;
46  
47      /**
48       * Creates a new container for the specified model.
49       *
50       * @param model The model to wrap, may be {@code null}.
51       */
52      ModelData(ModelSource source, Model model) {
53          this.source = source;
54          this.model = model;
55      }
56  
57      /**
58       * Creates a new container for the specified model.
59       *
60       * @param model The model to wrap, may be {@code null}.
61       * @param groupId The effective group identifier of the model, may be {@code null}.
62       * @param artifactId The effective artifact identifier of the model, may be {@code null}.
63       * @param version The effective version of the model, may be {@code null}.
64       */
65      ModelData(ModelSource source, Model model, String groupId, String artifactId, String version) {
66          this.source = source;
67          this.model = model;
68          setGroupId(groupId);
69          setArtifactId(artifactId);
70          setVersion(version);
71      }
72  
73      public ModelSource getSource() {
74          return source;
75      }
76  
77      /**
78       * Gets the model being wrapped.
79       *
80       * @return The model or {@code null} if not set.
81       */
82      public Model getModel() {
83          return model;
84      }
85  
86      /**
87       * Sets the model being wrapped.
88       *
89       * @param model The model, may be {@code null}.
90       */
91      public void setModel(Model model) {
92          this.model = model;
93      }
94  
95      /**
96       * Gets the raw model being wrapped.
97       *
98       * @return The raw model or {@code null} if not set.
99       */
100     public Model getRawModel() {
101         return rawModel;
102     }
103 
104     /**
105      * Sets the raw model being wrapped.
106      *
107      * @param rawModel The raw model, may be {@code null}.
108      */
109     public void setRawModel(Model rawModel) {
110         this.rawModel = rawModel;
111     }
112 
113     /**
114      * Gets the active profiles from the model.
115      *
116      * @return The active profiles or {@code null} if not set.
117      */
118     public List<Profile> getActiveProfiles() {
119         return activeProfiles;
120     }
121 
122     /**
123      * Sets the active profiles from the model.
124      *
125      * @param activeProfiles The active profiles, may be {@code null}.
126      */
127     public void setActiveProfiles(List<Profile> activeProfiles) {
128         this.activeProfiles = activeProfiles;
129     }
130 
131     /**
132      * Gets the effective group identifier of the model.
133      *
134      * @return The effective group identifier of the model or an empty string if unknown, never {@code null}.
135      */
136     public String getGroupId() {
137         return (groupId != null) ? groupId : "";
138     }
139 
140     /**
141      * Sets the effective group identifier of the model.
142      *
143      * @param groupId The effective group identifier of the model, may be {@code null}.
144      */
145     public void setGroupId(String groupId) {
146         this.groupId = groupId;
147     }
148 
149     /**
150      * Gets the effective artifact identifier of the model.
151      *
152      * @return The effective artifact identifier of the model or an empty string if unknown, never {@code null}.
153      */
154     public String getArtifactId() {
155         return (artifactId != null) ? artifactId : "";
156     }
157 
158     /**
159      * Sets the effective artifact identifier of the model.
160      *
161      * @param artifactId The effective artifact identifier of the model, may be {@code null}.
162      */
163     public void setArtifactId(String artifactId) {
164         this.artifactId = artifactId;
165     }
166 
167     /**
168      * Gets the effective version of the model.
169      *
170      * @return The effective version of the model or an empty string if unknown, never {@code null}.
171      */
172     public String getVersion() {
173         return (version != null) ? version : "";
174     }
175 
176     /**
177      * Sets the effective version of the model.
178      *
179      * @param version The effective version of the model, may be {@code null}.
180      */
181     public void setVersion(String version) {
182         this.version = version;
183     }
184 
185     /**
186      * Gets the effective identifier of the model in the form {@code <groupId>:<artifactId>:<version>}.
187      *
188      * @return The effective identifier of the model, never {@code null}.
189      */
190     public String getId() {
191         StringBuilder buffer = new StringBuilder(128);
192 
193         buffer.append(getGroupId())
194                 .append(':')
195                 .append(getArtifactId())
196                 .append(':')
197                 .append(getVersion());
198 
199         return buffer.toString();
200     }
201 
202     @Override
203     public String toString() {
204         return String.valueOf(model);
205     }
206 }