xmlelement.py :  » Wiki » FelloWiki » FelloWiki-0.01a1.dev-r36 » fellowiki » util » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Wiki » FelloWiki 
FelloWiki » FelloWiki 0.01a1.dev r36 » fellowiki » util » xmlelement.py
# pylint: disable-msg=W0613,E0201
# W0702, W0706: allow argument magic (i.e. attributes_from_dict())

# Copyright (c) 2006 Jan Niklas Fingerle
# 
# 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.

"""

TODO
    
"""

from copy import copy
import elementtree.ElementTree as ElementTree
        
class XMLElement(object):
    def __init__(self, tag = None, content=None, prepend_to=None,
                 append_to=None, **attributes):
        self.tag = tag
        if content is None:
            self.content = []
        else:
            try:
                content.pop # check if it's a kind of list
                self.content = content
            except AttributeError:
                self.content = [content]
        self.attributes = {}
        
        for (key, val) in attributes.items():
            if key[-1:] == '_':
                self.attributes[key[:-1]] = val
            else:
                self.attributes[key] = val
            
        self.callback = []
        self.translations = []
        
        if prepend_to is not None:
            prepend_to.prepend(self)
        if append_to is not None:
            append_to.append(self)
        
    def clone(self, other):
        self.tag = other.tag
        self.content = other.content
        self.attributes = other.attributes
        self.callback = other.callback
        self.translations = other.translations
        
    def to_element_tree(self): 
        xhtml = ElementTree.Element(self.tag)
        sub_xhtml = None
        current_textlist = []
        content = copy(self.content)
        translations = []
        
        for (key, val) in self.attributes.items():
            xhtml.set(key, str(val))
        
        # I want to modify the list, therefore I write this a bit 
        # more explicitly:
        while content:
            item = content.pop(0) 
            try: # should work, if it's a text list
                current_textlist.extend(item)
            except TypeError: # should be an XMLElement
                if item.tag is None:
                    content = item.content + content
                else:
                    current_text = ''.join(current_textlist)
                    current_textlist = []
                    if sub_xhtml is None:
                        xhtml.text = current_text
                    else:
                        sub_xhtml.tail = current_text
                    sub_xhtml, sub_translations = item.to_element_tree()
                    xhtml.append(sub_xhtml)
                    translations.extend(sub_translations)
        current_text = ''.join(current_textlist)
        if sub_xhtml is None:
            xhtml.text = current_text
        else:
            sub_xhtml.tail = current_text
        for translation in self.translations:
            if translation[1] is None:
                translations.append((xhtml, 
                                     translation[0], 
                                     translation[2]))
            else:
                translation[1](xhtml, translation[2])
        return xhtml, translations
        
    def is_empty(self):
        return not bool(self.content)
        
    def is_not_empty(self):
        return bool(self.content)
            
    def append(self, *appendee):
        self.content.extend(appendee)
    
    def prepend(self, *prependee):
        self.content = list(prependee) + self.content
        
    def set(self, key, val):
        self.attributes[key] = val
        
    def get(self, key):
        return self.attributes[key]
        
def SubElement(parent, *args, **kwargs):
    return XMLElement(append_to=parent, *args, **kwargs)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.