|
| 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.helpers.DefaultHandler; |
| 18 | +import org.xml.sax.*; |
| 19 | + |
| 20 | +/*** |
| 21 | + * A custom handler for parsing XML dependencies (POM file format) using SAX (Simple API for XML). |
| 22 | + * This handler looks for a specific dependency identified by the group ID and artifact ID, |
| 23 | + * and extracts its version information. |
| 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; |
| 30 | + private String currentArtifactId; |
| 31 | + private String currentVersion; |
| 32 | + private String dependencyVersion; |
| 33 | + |
| 34 | + /** |
| 35 | + * Constructor to initialize the handler with the target groupId and artifactId. |
| 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 | + * Called when a new element is encountered during the XML parsing. |
| 47 | + * |
| 48 | + * @param uri The namespace URI (if any). |
| 49 | + * @param localName The local name of the element. |
| 50 | + * @param qName The qualified name of the element. |
| 51 | + * @param attributes The attributes of the element. |
| 52 | + */ |
| 53 | + @Override |
| 54 | + public void startElement(String uri, String localName, String qName, Attributes attributes) { |
| 55 | + currentElement = qName; |
| 56 | + if ("dependency".equals(currentElement)) { |
| 57 | + currentGroupId = null; |
| 58 | + currentArtifactId = null; |
| 59 | + currentVersion = null; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Called when the end of an element is reached during XML parsing. |
| 65 | + * |
| 66 | + * @param uri The namespace URI (if any). |
| 67 | + * @param localName The local name of the element. |
| 68 | + * @param qName The qualified name of the element. |
| 69 | + */ |
| 70 | + @Override |
| 71 | + public void endElement(String uri, String localName, String qName) { |
| 72 | + if ("dependency".equals(qName)) { |
| 73 | + if (targetGroupId.equals(currentGroupId) && targetArtifactId.equals(currentArtifactId)) { |
| 74 | + dependencyVersion = currentVersion; |
| 75 | + } |
| 76 | + } |
| 77 | + currentElement = null; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Called to process the character data inside an element during XML parsing. |
| 82 | + * |
| 83 | + * @param ch The character array containing the text. |
| 84 | + * @param start The start index of the text. |
| 85 | + * @param length The length of the text. |
| 86 | + */ |
| 87 | + @Override |
| 88 | + public void characters(char[] ch, int start, int length) { |
| 89 | + String content = new String(ch, start, length).trim(); |
| 90 | + if (content.isEmpty()) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if ("groupId".equals(currentElement)) { |
| 95 | + currentGroupId = content; |
| 96 | + } else if ("artifactId".equals(currentElement)) { |
| 97 | + currentArtifactId = content; |
| 98 | + } else if ("version".equals(currentElement)) { |
| 99 | + currentVersion = content; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns the version of the dependency if found, otherwise returns "Unknown". |
| 105 | + * |
| 106 | + * @return The version of the target dependency. |
| 107 | + */ |
| 108 | + public String getDependencyVersion() { |
| 109 | + return dependencyVersion != null ? dependencyVersion : "Unknown"; |
| 110 | + } |
| 111 | +} |
0 commit comments