xref: /OpenGrok/tools/src/main/python/opengrok_tools/utils/xml.py (revision 2d57dc692b4dd10e696ca6922510f6cecded1bfd)
1*2d57dc69SVladimir Kotal#!/usr/bin/env python3
2*2d57dc69SVladimir Kotal
3*2d57dc69SVladimir Kotal# CDDL HEADER START
4*2d57dc69SVladimir Kotal#
5*2d57dc69SVladimir Kotal# The contents of this file are subject to the terms of the
6*2d57dc69SVladimir Kotal# Common Development and Distribution License (the "License").
7*2d57dc69SVladimir Kotal# You may not use this file except in compliance with the License.
8*2d57dc69SVladimir Kotal#
9*2d57dc69SVladimir Kotal# See LICENSE.txt included in this distribution for the specific
10*2d57dc69SVladimir Kotal# language governing permissions and limitations under the License.
11*2d57dc69SVladimir Kotal#
12*2d57dc69SVladimir Kotal# When distributing Covered Code, include this CDDL HEADER in each
13*2d57dc69SVladimir Kotal# file and include the License file at LICENSE.txt.
14*2d57dc69SVladimir Kotal# If applicable, add the following below this CDDL HEADER, with the
15*2d57dc69SVladimir Kotal# fields enclosed by brackets "[]" replaced with your own identifying
16*2d57dc69SVladimir Kotal# information: Portions Copyright [yyyy] [name of copyright owner]
17*2d57dc69SVladimir Kotal#
18*2d57dc69SVladimir Kotal# CDDL HEADER END
19*2d57dc69SVladimir Kotal
20*2d57dc69SVladimir Kotal#
21*2d57dc69SVladimir Kotal# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
22*2d57dc69SVladimir Kotal#
23*2d57dc69SVladimir Kotal
24*2d57dc69SVladimir Kotalimport xml.etree.ElementTree as ET
25*2d57dc69SVladimir Kotal
26*2d57dc69SVladimir Kotal
27*2d57dc69SVladimir Kotalclass XMLProcessingException(Exception):
28*2d57dc69SVladimir Kotal    pass
29*2d57dc69SVladimir Kotal
30*2d57dc69SVladimir Kotal
31*2d57dc69SVladimir Kotaldef insert_file(input_xml, insert_xml_file):
32*2d57dc69SVladimir Kotal    """
33*2d57dc69SVladimir Kotal    inserts sub-root elements of XML file under root of input XML
34*2d57dc69SVladimir Kotal    :param input_xml: input XML string
35*2d57dc69SVladimir Kotal    :param insert_xml_file: path to file to insert
36*2d57dc69SVladimir Kotal    :return: string with resulting XML
37*2d57dc69SVladimir Kotal    """
38*2d57dc69SVladimir Kotal
39*2d57dc69SVladimir Kotal    # This avoids resulting XML to have namespace prefixes in elements.
40*2d57dc69SVladimir Kotal    ET.register_namespace('', "http://xmlns.jcp.org/xml/ns/javaee")
41*2d57dc69SVladimir Kotal
42*2d57dc69SVladimir Kotal    root = ET.fromstring(input_xml)
43*2d57dc69SVladimir Kotal    try:
44*2d57dc69SVladimir Kotal        insert_tree = ET.parse(insert_xml_file)
45*2d57dc69SVladimir Kotal    except ET.ParseError as e:
46*2d57dc69SVladimir Kotal        raise XMLProcessingException("Cannot parse file '{}' as XML".
47*2d57dc69SVladimir Kotal                                     format(insert_xml_file)) from e
48*2d57dc69SVladimir Kotal    except (PermissionError, IOError) as e:
49*2d57dc69SVladimir Kotal        raise XMLProcessingException("Cannot read file '{}'".
50*2d57dc69SVladimir Kotal                                     format(insert_xml_file)) from e
51*2d57dc69SVladimir Kotal
52*2d57dc69SVladimir Kotal    insert_root = insert_tree.getroot()
53*2d57dc69SVladimir Kotal
54*2d57dc69SVladimir Kotal    for elem in list(insert_root.findall('.')):
55*2d57dc69SVladimir Kotal        root.extend(list(elem))
56*2d57dc69SVladimir Kotal
57*2d57dc69SVladimir Kotal    return ET.tostring(root, encoding="utf8", method='xml').decode("utf-8")
58