|
| 1 | +// Copyright 2024 The casbin Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package org.casbin.util; |
| 16 | + |
| 17 | +import org.xml.sax.Attributes; |
| 18 | +import org.xml.sax.SAXException; |
| 19 | +import org.xml.sax.helpers.DefaultHandler; |
| 20 | + |
| 21 | +/*** |
| 22 | + * A custom SAX handler for parsing Maven POM files to retrieve the version |
| 23 | + * of a specific dependency based on its groupId and artifactId. |
| 24 | + */ |
| 25 | +public class DependencyHandler extends DefaultHandler { |
| 26 | + private final String targetGroupId; |
| 27 | + private final String targetArtifactId; |
| 28 | + private String currentElement = ""; |
| 29 | + private String currentGroupId = null; |
| 30 | + private String currentArtifactId = null; |
| 31 | + private String version = null; |
| 32 | + private boolean isDependency = false; |
| 33 | + |
| 34 | + /** |
| 35 | + * Constructs a DependencyHandler instance. |
| 36 | + * |
| 37 | + * @param groupId the groupId of the dependency to search for. |
| 38 | + * @param artifactId the artifactId of the dependency to search for. |
| 39 | + */ |
| 40 | + public DependencyHandler(String groupId, String artifactId) { |
| 41 | + this.targetGroupId = groupId; |
| 42 | + this.targetArtifactId = artifactId; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Gets the version of the target dependency. |
| 47 | + * |
| 48 | + * @return the version of the dependency if found, or null if not found. |
| 49 | + */ |
| 50 | + public String getVersion() { |
| 51 | + return version; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Called when the start of an XML element is encountered. |
| 56 | + * |
| 57 | + * @param uri the Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed. |
| 58 | + * @param localName the local name (without prefix), or the empty string if Namespace processing is not being performed. |
| 59 | + * @param qName the qualified name (with prefix, if present). |
| 60 | + * @param attributes the attributes attached to the element. If there are no attributes, it shall be an empty Attributes object. |
| 61 | + * @throws SAXException if a SAX error occurs. |
| 62 | + */ |
| 63 | + @Override |
| 64 | + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
| 65 | + currentElement = qName; |
| 66 | + |
| 67 | + if ("dependency".equals(qName)) { |
| 68 | + isDependency = true; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Called when the end of an XML element is encountered. |
| 74 | + * |
| 75 | + * @param uri the Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed. |
| 76 | + * @param localName the local name (without prefix), or the empty string if Namespace processing is not being performed. |
| 77 | + * @param qName the qualified name (with prefix, if present). |
| 78 | + * @throws SAXException if a SAX error occurs. |
| 79 | + */ |
| 80 | + @Override |
| 81 | + public void endElement(String uri, String localName, String qName) throws SAXException { |
| 82 | + if ("dependency".equals(qName)) { |
| 83 | + if (targetGroupId.equals(currentGroupId) && targetArtifactId.equals(currentArtifactId)) { |
| 84 | + isDependency = false; |
| 85 | + } |
| 86 | + |
| 87 | + currentGroupId = null; |
| 88 | + currentArtifactId = null; |
| 89 | + } |
| 90 | + currentElement = ""; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Called when character data inside an XML element is encountered. |
| 95 | + * |
| 96 | + * @param ch the characters. |
| 97 | + * @param start the start position in the character array. |
| 98 | + * @param length the number of characters to read from the array. |
| 99 | + * @throws SAXException if a SAX error occurs. |
| 100 | + */ |
| 101 | + @Override |
| 102 | + public void characters(char[] ch, int start, int length) throws SAXException { |
| 103 | + if (isDependency) { |
| 104 | + String content = new String(ch, start, length).trim(); |
| 105 | + |
| 106 | + switch (currentElement) { |
| 107 | + case "groupId": |
| 108 | + currentGroupId = content; |
| 109 | + break; |
| 110 | + case "artifactId": |
| 111 | + currentArtifactId = content; |
| 112 | + break; |
| 113 | + case "version": |
| 114 | + if (targetGroupId.equals(currentGroupId) && targetArtifactId.equals(currentArtifactId)) { |
| 115 | + version = content; |
| 116 | + } |
| 117 | + break; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments