#!@PYTHON@
# -*- python -*-
# Copyright (c) 2000-2004, 2007 Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import sys
import ubf
import pprint
import types

class IndentingPrinter:
    def __init__(self, stream = sys.stdout, step = 2):
        self.stream = stream
        self.step = 2
        self.position = 0

    def indent(self):
        self.position = self.position + self.step

    def outdent(self):
        self.position = self.position - self.step

    def writeLine(self, str):
        self.stream.write(' ' * self.position + str + '\n')

inputstr = sys.stdin.read()
input = ubf.Decoder(inputstr).decode()

o = IndentingPrinter()

def xmlEscape(s):
    s = s.replace('&', '&amp;')
    s = s.replace('<', '&lt;')
    s = s.replace('>', '&gt;')
    return s

def walk(x):
    if type(x) == types.TupleType:
        if len(x):
            o.writeLine('<struct>')
            o.indent()
            for child in x: walk(child)
            o.outdent()
            o.writeLine('</struct>')
        else:
            o.writeLine('<struct/>')
    elif type(x) == types.ListType:
        if len(x):
            o.writeLine('<list>')
            o.indent()
            for child in x: walk(child)
            o.outdent()
            o.writeLine('</list>')
        else:
            o.writeLine('<list/>')
    elif isinstance(x, ubf.Tag):
        o.writeLine('<tag key="'+x.key+'">')
        o.indent()
        walk(x.value)
        o.outdent()
        o.writeLine('</tag>')
    elif type(x) in (types.IntType, types.LongType):
        o.writeLine('<number>'+str(x)+'</number>')
    elif type(x) == types.StringType:
        o.writeLine('<string>'+xmlEscape(x)+'</string>')
    elif isinstance(x, ubf.Symbol):
        o.writeLine('<symbol>'+xmlEscape(x.name)+'</symbol>')
    elif isinstance(x, ubf.Binary):
        o.writeLine('<binary length="'+str(len(x.content))+'">')
        o.indent()
        o.writeLine('<![CDATA['+x.content+']]>')
        o.outdent()
        o.writeLine('</binary>')
    else:
        raise ubf.FormatError('Unsupported term type in ubf2xml', x)

if len(sys.argv) > 1 and sys.argv[1] == '-p':
    pprint.pprint(input)
else:
    walk(input)
