12d57dc69SVladimir Kotal#!/usr/bin/env python3 22d57dc69SVladimir Kotal 32d57dc69SVladimir Kotal# CDDL HEADER START 42d57dc69SVladimir Kotal# 52d57dc69SVladimir Kotal# The contents of this file are subject to the terms of the 62d57dc69SVladimir Kotal# Common Development and Distribution License (the "License"). 72d57dc69SVladimir Kotal# You may not use this file except in compliance with the License. 82d57dc69SVladimir Kotal# 92d57dc69SVladimir Kotal# See LICENSE.txt included in this distribution for the specific 102d57dc69SVladimir Kotal# language governing permissions and limitations under the License. 112d57dc69SVladimir Kotal# 122d57dc69SVladimir Kotal# When distributing Covered Code, include this CDDL HEADER in each 132d57dc69SVladimir Kotal# file and include the License file at LICENSE.txt. 142d57dc69SVladimir Kotal# If applicable, add the following below this CDDL HEADER, with the 152d57dc69SVladimir Kotal# fields enclosed by brackets "[]" replaced with your own identifying 162d57dc69SVladimir Kotal# information: Portions Copyright [yyyy] [name of copyright owner] 172d57dc69SVladimir Kotal# 182d57dc69SVladimir Kotal# CDDL HEADER END 192d57dc69SVladimir Kotal 202d57dc69SVladimir Kotal# 212d57dc69SVladimir Kotal# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 222d57dc69SVladimir Kotal# 232d57dc69SVladimir Kotal 242d57dc69SVladimir Kotalimport xml.etree.ElementTree as ET 252d57dc69SVladimir Kotal 262d57dc69SVladimir Kotal 272d57dc69SVladimir Kotalclass XMLProcessingException(Exception): 282d57dc69SVladimir Kotal pass 292d57dc69SVladimir Kotal 302d57dc69SVladimir Kotal 312d57dc69SVladimir Kotaldef insert_file(input_xml, insert_xml_file): 322d57dc69SVladimir Kotal """ 332d57dc69SVladimir Kotal inserts sub-root elements of XML file under root of input XML 342d57dc69SVladimir Kotal :param input_xml: input XML string 352d57dc69SVladimir Kotal :param insert_xml_file: path to file to insert 362d57dc69SVladimir Kotal :return: string with resulting XML 372d57dc69SVladimir Kotal """ 382d57dc69SVladimir Kotal 392d57dc69SVladimir Kotal # This avoids resulting XML to have namespace prefixes in elements. 40*a700e5b4SVladimir Kotal ET.register_namespace('', "https://jakarta.ee/xml/ns/jakartaee") 412d57dc69SVladimir Kotal 422d57dc69SVladimir Kotal root = ET.fromstring(input_xml) 432d57dc69SVladimir Kotal try: 442d57dc69SVladimir Kotal insert_tree = ET.parse(insert_xml_file) 452d57dc69SVladimir Kotal except ET.ParseError as e: 462d57dc69SVladimir Kotal raise XMLProcessingException("Cannot parse file '{}' as XML". 472d57dc69SVladimir Kotal format(insert_xml_file)) from e 482d57dc69SVladimir Kotal except (PermissionError, IOError) as e: 492d57dc69SVladimir Kotal raise XMLProcessingException("Cannot read file '{}'". 502d57dc69SVladimir Kotal format(insert_xml_file)) from e 512d57dc69SVladimir Kotal 522d57dc69SVladimir Kotal insert_root = insert_tree.getroot() 532d57dc69SVladimir Kotal 542d57dc69SVladimir Kotal for elem in list(insert_root.findall('.')): 552d57dc69SVladimir Kotal root.extend(list(elem)) 562d57dc69SVladimir Kotal 572d57dc69SVladimir Kotal return ET.tostring(root, encoding="utf8", method='xml').decode("utf-8") 58