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