# Copyright (c) 2000-2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

""" HornAbstraction module.

Object classes manipulated by Horn
"""
__revision__ = "$Id: HornAbstraction.py,v 1.6 2002/01/04 14:13:05 syt Exp $"

from xml.dom.ext import StripXml, PrettyPrint
from cStringIO import StringIO


# Elements factory ############################################################
class HORNElementsFactory:
    """ Factory for horn elements """
    def __init__(self):
        self.reader = DOMReader(validate=0, keepAllWs=0, catName=None)
        self.doc = None

    def element_from_stream(self, xmlstream):
        e = self.reader.fromStream(xmlstream, ownerDoc=self.doc)
        return complete(self._document_node(e))

    def element_from_string(self, xmlstring):
        e = self.reader.fromString(xmlstring, ownerDoc=self.doc)
        return complete(self._document_node(e))

    def element_from_file(self, xmlfile):
        f = open(xmlfile)
        doc = self.reader.fromStream(f, ownerDoc=self.doc)
        f.close()
        return complete(self._document_node(doc))
    
    def elements_from_stream(self, xmlstream):
        e = self.reader.fromStream(xmlstream, ownerDoc=self.doc)
        return self._childs(self._document_node(e))

    def elements_from_string(self, xmlstring):
        e = self.reader.fromString(xmlstring, ownerDoc=self.doc)
        return self._childs(self._document_node(e))
    
    def elements_from_file(self, xmlfile):
        f = open(xmlfile)
        e = self.reader.fromStream(f, ownerDoc=self.doc)
        elements = self._childs(self._document_node(e))
        f.close()
        return elements

    def create_dom_document(self) :
         self.doc = Document()
         return self.doc
    
    def release_domtree(self, node):
        self.reader.releaseNode(node)
        
    # Private #################################################################
    def _childs(self, e):
        l = []
        for n in e.childNodes:
            if n.nodeType == n.ELEMENT_NODE:
                l.append(complete(n))
        return l
    
    def _document_node(self, e):
        for e in e.childNodes:
            if e.nodeType == e.ELEMENT_NODE:
                return e
        raise Exception('no root node')

def complete(e):
    e.as_xml = lambda n=e, en='UTF-8': as_xml(n, en)
    return StripXml(e)
    
def as_xml(self, encoding='UTF-8'):
    b = StringIO()
    PrettyPrint(self, b, encoding=encoding)
    data = b.getvalue()
    b.close()
    return data

#  ############################################################################
from xml.dom.ext.reader.Sax2 import Reader as DOMReader
from xml.dom.Document import Document as DOMDocument
class Document(DOMDocument):
    def __init__(self):
        DOMDocument.__init__(self, None)

