1# This program is free software; you can redistribute it and/or modify
2# it under the terms of the (LGPL) GNU Lesser General Public License as
3# published by the Free Software Foundation; either version 3 of the
4# License, or (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9# GNU Library Lesser General Public License for more details at
10# ( http://www.gnu.org/licenses/lgpl.html ).
11#
12# You should have received a copy of the GNU Lesser General Public License
13# along with this program; if not, write to the Free Software
14# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15# written by: Jeff Ortel ( jortel@redhat.com )
16
17"""
18Provides I{marshaller} core classes.
19"""
20
21from suds import *
22from suds.mx import *
23from suds.mx.appender import ContentAppender
24from suds.sax.document import Document
25
26from logging import getLogger
27log = getLogger(__name__)
28
29
30class Core:
31    """
32    An I{abstract} marshaller.  This class implement the core
33    functionality of the marshaller.
34    @ivar appender: A content appender.
35    @type appender: L{ContentAppender}
36    """
37
38    def __init__(self):
39        """
40        """
41        self.appender = ContentAppender(self)
42
43    def process(self, content):
44        """
45        Process (marshal) the tag with the specified value using the
46        optional type information.
47        @param content: The content to process.
48        @type content: L{Object}
49        """
50        log.debug('processing:\n%s', content)
51        self.reset()
52        if content.tag is None:
53            content.tag = content.value.__class__.__name__
54        document = Document()
55        self.append(document, content)
56        return document.root()
57
58    def append(self, parent, content):
59        """
60        Append the specified L{content} to the I{parent}.
61        @param parent: The parent node to append to.
62        @type parent: L{Element}
63        @param content: The content to append.
64        @type content: L{Object}
65        """
66        log.debug('appending parent:\n%s\ncontent:\n%s', parent, content)
67        if self.start(content):
68            self.appender.append(parent, content)
69            self.end(parent, content)
70
71    def reset(self):
72        """
73        Reset the marshaller.
74        """
75        pass
76
77    def node(self, content):
78        """
79        Create and return an XML node.
80        @param content: Content information for the new node.
81        @type content: L{Content}
82        @return: An element.
83        @rtype: L{Element}
84        """
85        raise NotImplementedError
86
87    def start(self, content):
88        """
89        Appending this content has started.
90        @param content: The content for which processing has started.
91        @type content: L{Content}
92        @return: True to continue appending
93        @rtype: boolean
94        """
95        return True
96
97    def suspend(self, content):
98        """
99        Appending this content has suspended.
100        @param content: The content for which processing has been suspended.
101        @type content: L{Content}
102        """
103        pass
104
105    def resume(self, content):
106        """
107        Appending this content has resumed.
108        @param content: The content for which processing has been resumed.
109        @type content: L{Content}
110        """
111        pass
112
113    def end(self, parent, content):
114        """
115        Appending this content has ended.
116        @param parent: The parent node ending.
117        @type parent: L{Element}
118        @param content: The content for which processing has ended.
119        @type content: L{Content}
120        """
121        pass
122
123    def setnil(self, node, content):
124        """
125        Set the value of the I{node} to nill.
126        @param node: A I{nil} node.
127        @type node: L{Element}
128        @param content: The content to set nil.
129        @type content: L{Content}
130        """
131        pass
132
133    def setdefault(self, node, content):
134        """
135        Set the value of the I{node} to a default value.
136        @param node: A I{nil} node.
137        @type node: L{Element}
138        @param content: The content to set the default value.
139        @type content: L{Content}
140        @return: The default.
141        """
142        pass
143
144    def optional(self, content):
145        """
146        Get whether the specified content is optional.
147        @param content: The content which to check.
148        @type content: L{Content}
149        """
150        return False
151