#!/bin/env python
#
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
#

# $Id: gen_junit_report.py 1141953 2011-07-01 14:42:56Z rhuijben $
"""
gen_junit_report.py -- The script is to generate the junit report for
Subversion tests.  The script uses the log file, tests.log created by
"make check" process. It parses the log file and generate the junit
files for each test separately in the specified output directory. The
script can take --log-file and --output-dir arguments.
"""

import sys
import os
import getopt

def replace_from_map(data, encode):
    """replace substrings in DATA with replacements defined in ENCODING"""
    for pattern, replacement in encode.items():
        data = data.replace(pattern, replacement)
    return data

xml_encode_map = {
      '&': '&amp;',
      '<': '&lt;',
      '>': '&gt;',
      '"': '&quot;',
      "'": '&apos;',
      }

def xml_encode(data):
    """encode the xml characters in the data"""
    return replace_from_map(data, xml_encode_map)

special_encode_map = {
    ']]>': ']]]]><![CDATA[>', # CDATA terminator sequence
    '\000': '&#9216;',        # U+2400 SYMBOL FOR NULL
    '\001': '&#9217;',        # U+2401 SYMBOL FOR START OF HEADING