Skip to content

Commit 75e534c

Browse files
committed
chore: move pom parser to parsers module
Signed-off-by: Ben Selwyn-Smith <[email protected]>
1 parent c9752b3 commit 75e534c

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

src/macaron/parsers/pomparser.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module contains the parser for POM files."""
5+
import logging
6+
from xml.etree.ElementTree import Element # nosec
7+
8+
import defusedxml.ElementTree
9+
from defusedxml.ElementTree import fromstring
10+
11+
logger: logging.Logger = logging.getLogger(__name__)
12+
13+
14+
def parse_pom_string(pom_string: str) -> Element | None:
15+
"""
16+
Parse the passed POM string using defusedxml.
17+
18+
Parameters
19+
----------
20+
pom_string : str
21+
The contents of a POM file as a string.
22+
23+
Returns
24+
-------
25+
Element | None
26+
The parsed element representing the POM's XML hierarchy.
27+
"""
28+
try:
29+
# Stored here first to help with type checking.
30+
pom: Element = fromstring(pom_string)
31+
return pom
32+
except defusedxml.ElementTree.ParseError as error:
33+
logger.debug("Failed to parse XML: %s", error)
34+
return None

src/macaron/repo_finder/repo_finder_java.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
import re
77
from xml.etree.ElementTree import Element # nosec
88

9-
import defusedxml.ElementTree
10-
from defusedxml.ElementTree import fromstring
119
from packageurl import PackageURL
1210

1311
from macaron.config.defaults import defaults
12+
from macaron.parsers.pomparser import parse_pom_string
1413
from macaron.repo_finder.repo_finder_base import BaseRepoFinder
1514
from macaron.repo_finder.repo_validator import find_valid_repository_url
1615
from macaron.util import send_get_http_raw
@@ -168,34 +167,13 @@ def _read_pom(self, pom: str) -> list[str]:
168167
return []
169168

170169
# Parse POM using defusedxml
171-
pom_element = self._parse_pom(pom)
170+
pom_element = parse_pom_string(pom)
172171
if pom_element is None:
173172
return []
174173

175174
# Attempt to extract SCM data and return URL
176175
return self._find_scm(pom_element, tags)
177176

178-
def _parse_pom(self, pom: str) -> Element | None:
179-
"""
180-
Parse the passed POM using defusedxml.
181-
182-
Parameters
183-
----------
184-
pom : str
185-
The contents of a POM file as a string.
186-
187-
Returns
188-
-------
189-
Element | None :
190-
The parsed element representing the POM's XML hierarchy.
191-
"""
192-
try:
193-
self.pom_element = fromstring(pom)
194-
return self.pom_element
195-
except defusedxml.ElementTree.ParseError as error:
196-
logger.debug("Failed to parse XML: %s", error)
197-
return None
198-
199177
def _find_scm(self, pom: Element, tags: list[str], resolve_properties: bool = True) -> list[str]:
200178
"""
201179
Parse the passed pom and extract the passed tags.

0 commit comments

Comments
 (0)