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.cling.invoker.mvnup.goals;
20  
21  import org.jdom2.Document;
22  import org.jdom2.Element;
23  import org.jdom2.Namespace;
24  
25  import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.ModelVersions.MODEL_VERSION_4_0_0;
26  import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.ModelVersions.MODEL_VERSION_4_1_0;
27  import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.ModelVersions.MODEL_VERSION_4_2_0;
28  import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.Namespaces.MAVEN_4_0_0_NAMESPACE;
29  import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.Namespaces.MAVEN_4_1_0_NAMESPACE;
30  import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.Namespaces.MAVEN_4_2_0_NAMESPACE;
31  import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.SchemaLocations.MAVEN_4_1_0_SCHEMA_LOCATION;
32  import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.SchemaLocations.MAVEN_4_2_0_SCHEMA_LOCATION;
33  import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.XmlElements.MODEL_VERSION;
34  
35  /**
36   * Utility class for handling Maven model version operations during upgrades.
37   */
38  public final class ModelVersionUtils {
39  
40      private ModelVersionUtils() {
41          // Utility class
42      }
43  
44      /**
45       * Detects the model version from a POM document.
46       * Uses both the modelVersion element and namespace URI for detection.
47       *
48       * @param pomDocument the POM document
49       * @return the detected model version
50       */
51      public static String detectModelVersion(Document pomDocument) {
52          Element root = pomDocument.getRootElement();
53          Namespace namespace = root.getNamespace();
54  
55          // First try to get from modelVersion element
56          Element modelVersionElement = root.getChild(MODEL_VERSION, namespace);
57          if (modelVersionElement != null) {
58              String modelVersion = modelVersionElement.getTextTrim();
59              if (!modelVersion.isEmpty()) {
60                  return modelVersion;
61              }
62          }
63  
64          // Fallback to namespace URI detection
65          String namespaceUri = namespace.getURI();
66          if (MAVEN_4_2_0_NAMESPACE.equals(namespaceUri)) {
67              return MODEL_VERSION_4_2_0;
68          } else if (MAVEN_4_1_0_NAMESPACE.equals(namespaceUri)) {
69              return MODEL_VERSION_4_1_0;
70          } else if (MAVEN_4_0_0_NAMESPACE.equals(namespaceUri)) {
71              return MODEL_VERSION_4_0_0;
72          }
73  
74          // Default fallback
75          return MODEL_VERSION_4_0_0;
76      }
77  
78      /**
79       * Checks if a model version is valid for upgrade operations.
80       * Currently supports 4.0.0, 4.1.0, and 4.2.0.
81       *
82       * @param modelVersion the model version to validate
83       * @return true if the model version is valid
84       */
85      public static boolean isValidModelVersion(String modelVersion) {
86          return MODEL_VERSION_4_0_0.equals(modelVersion)
87                  || MODEL_VERSION_4_1_0.equals(modelVersion)
88                  || MODEL_VERSION_4_2_0.equals(modelVersion);
89      }
90  
91      /**
92       * Checks if an upgrade from one version to another is possible.
93       *
94       * @param fromVersion the source version
95       * @param toVersion the target version
96       * @return true if the upgrade is possible
97       */
98      public static boolean canUpgrade(String fromVersion, String toVersion) {
99          if (fromVersion == null || toVersion == null) {
100             return false;
101         }
102 
103         // Support upgrades: 4.0.0 → 4.1.0, 4.0.0 → 4.2.0, 4.1.0 → 4.2.0
104         if (MODEL_VERSION_4_0_0.equals(fromVersion)) {
105             return MODEL_VERSION_4_1_0.equals(toVersion) || MODEL_VERSION_4_2_0.equals(toVersion);
106         }
107         if (MODEL_VERSION_4_1_0.equals(fromVersion)) {
108             return MODEL_VERSION_4_2_0.equals(toVersion);
109         }
110 
111         return false;
112     }
113 
114     /**
115      * Checks if a model version is eligible for inference optimizations.
116      * Models 4.0.0+ are eligible (4.0.0 has limited inference, 4.1.0+ has full inference).
117      *
118      * @param modelVersion the model version to check
119      * @return true if eligible for inference
120      */
121     public static boolean isEligibleForInference(String modelVersion) {
122         return MODEL_VERSION_4_0_0.equals(modelVersion)
123                 || MODEL_VERSION_4_1_0.equals(modelVersion)
124                 || MODEL_VERSION_4_2_0.equals(modelVersion);
125     }
126 
127     /**
128      * Checks if a model version is newer than 4.1.0.
129      *
130      * @param modelVersion the model version to check
131      * @return true if newer than 4.1.0
132      */
133     public static boolean isNewerThan410(String modelVersion) {
134         if (modelVersion == null) {
135             return false;
136         }
137 
138         // Simple version comparison for now
139         // This could be enhanced with proper version parsing if needed
140         try {
141             String[] parts = modelVersion.split("\\.");
142             if (parts.length >= 2) {
143                 int major = Integer.parseInt(parts[0]);
144                 int minor = Integer.parseInt(parts[1]);
145 
146                 if (major > 4) {
147                     return true;
148                 }
149                 if (major == 4 && minor > 1) {
150                     return true;
151                 }
152                 if (major == 4 && minor == 1 && parts.length > 2) {
153                     int patch = Integer.parseInt(parts[2]);
154                     return patch > 0;
155                 }
156             }
157         } catch (NumberFormatException e) {
158             // If we can't parse it, assume it's not newer
159             return false;
160         }
161 
162         return false;
163     }
164 
165     /**
166      * Checks if a model version is greater than or equal to a target version.
167      *
168      * @param modelVersion the model version to check
169      * @param targetVersion the target version to compare against
170      * @return true if modelVersion >= targetVersion
171      */
172     public static boolean isVersionGreaterOrEqual(String modelVersion, String targetVersion) {
173         if (modelVersion == null || targetVersion == null) {
174             return false;
175         }
176 
177         // Handle exact equality first
178         if (modelVersion.equals(targetVersion)) {
179             return true;
180         }
181 
182         // For now, handle the specific cases we need
183         if (MODEL_VERSION_4_1_0.equals(targetVersion)) {
184             return MODEL_VERSION_4_1_0.equals(modelVersion) || isNewerThan410(modelVersion);
185         }
186 
187         // Default to false for unknown comparisons
188         return false;
189     }
190 
191     /**
192      * Updates the model version element in a POM document.
193      *
194      * @param pomDocument the POM document
195      * @param newVersion the new model version
196      */
197     public static void updateModelVersion(Document pomDocument, String newVersion) {
198         Element root = pomDocument.getRootElement();
199         Namespace namespace = root.getNamespace();
200 
201         Element modelVersionElement = root.getChild(MODEL_VERSION, namespace);
202         if (modelVersionElement != null) {
203             modelVersionElement.setText(newVersion);
204         } else {
205             // Create new modelVersion element if it doesn't exist
206             Element newModelVersionElement = new Element(MODEL_VERSION, namespace);
207             newModelVersionElement.setText(newVersion);
208 
209             // Insert at the beginning of the document
210             root.addContent(0, newModelVersionElement);
211         }
212     }
213 
214     /**
215      * Removes the model version element from a POM document.
216      * This is used during inference when the model version can be inferred.
217      *
218      * @param pomDocument the POM document
219      * @return true if the element was removed, false if it didn't exist
220      */
221     public static boolean removeModelVersion(Document pomDocument) {
222         Element root = pomDocument.getRootElement();
223         Namespace namespace = root.getNamespace();
224 
225         Element modelVersionElement = root.getChild(MODEL_VERSION, namespace);
226         if (modelVersionElement != null) {
227             return root.removeContent(modelVersionElement);
228         }
229         return false;
230     }
231 
232     /**
233      * Gets the schema location for a model version.
234      *
235      * @param modelVersion the model version
236      * @return the schema location
237      */
238     public static String getSchemaLocationForModelVersion(String modelVersion) {
239         if (MODEL_VERSION_4_2_0.equals(modelVersion)) {
240             return MAVEN_4_2_0_SCHEMA_LOCATION;
241         } else if (MODEL_VERSION_4_1_0.equals(modelVersion)) {
242             return MAVEN_4_1_0_SCHEMA_LOCATION;
243         } else if (isNewerThan410(modelVersion)) {
244             // For versions newer than 4.1.0 but not specifically 4.2.0, use 4.2.0 schema
245             return MAVEN_4_2_0_SCHEMA_LOCATION;
246         }
247         return UpgradeConstants.SchemaLocations.MAVEN_4_0_0_SCHEMA_LOCATION;
248     }
249 }