~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/schema/GSParser.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# GNU Enterprise Common - GNUe Schema Definition - Sax base XML Parser
 
2
#
 
3
# Copyright 2001-2005 Free Software Foundation
 
4
#
 
5
# This file is part of GNU Enterprise
 
6
#
 
7
# GNU Enterprise is free software; you can redistribute it
 
8
# and/or modify it under the terms of the GNU General Public
 
9
# License as published by the Free Software Foundation; either
 
10
# version 2, or (at your option) any later version.
 
11
#
 
12
# GNU Enterprise is distributed in the hope that it will be
 
13
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
14
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
15
# PURPOSE. See the GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public
 
18
# License along with program; see the file COPYING. If not,
 
19
# write to the Free Software Foundation, Inc., 59 Temple Place
 
20
# - Suite 330, Boston, MA 02111-1307, USA.
 
21
#
 
22
# $Id: GSParser.py 6851 2005-01-03 20:59:28Z jcater $
 
23
 
 
24
import Objects
 
25
import copy
 
26
import types
 
27
 
 
28
from gnue.common.formatting import GTypecast
 
29
from gnue.common.definitions import GParser
 
30
 
 
31
 
 
32
xmlElements = None
 
33
 
 
34
# =============================================================================
 
35
# load an XML object tree from a given stream and return it's root object
 
36
# =============================================================================
 
37
 
 
38
def loadFile (buffer, app = None, initialize = True):
 
39
  """
 
40
  This function loads an XML object tree from a given stream and return it's
 
41
  root object.
 
42
  """
 
43
 
 
44
  return GParser.loadXMLObject (buffer, xmlSchemaHandler, 'GSSchema', 'schema',
 
45
           initialize, attributes = {'_app': app})
 
46
 
 
47
 
 
48
# =============================================================================
 
49
# Build a dictionary tree with all available XML elements
 
50
# =============================================================================
 
51
 
 
52
def getXMLelements ():
 
53
  """
 
54
  This function creates a dictionary tree with all valid xml elements. This
 
55
  dictionary tree is available via global variable 'xmlElements'
 
56
  """
 
57
 
 
58
  global xmlElements
 
59
 
 
60
  if xmlElements is None:
 
61
    xmlElements = {
 
62
      'schema':       {
 
63
         'BaseClass': Objects.GSSchema,
 
64
         'Required': 1,
 
65
         'SingleInstance': 1,
 
66
         'Attributes':  {
 
67
            'title':       {
 
68
               'Typecast': GTypecast.text },
 
69
            'author':       {
 
70
               'Typecast': GTypecast.text },
 
71
            'version':       {
 
72
               'Typecast': GTypecast.text },
 
73
            'description':       {
 
74
               'Typecast': GTypecast.text } } ,
 
75
         'ParentTags':  None },
 
76
 
 
77
      'tables':  {
 
78
         'BaseClass': Objects.GSTables,
 
79
         'SingleInstance': 1,
 
80
         'ParentTags':  ('schema',) },
 
81
 
 
82
      'table':    {
 
83
         'BaseClass': Objects.GSTable,
 
84
         'Importable': 1,
 
85
         'Attributes': {
 
86
            'name': {
 
87
               'Required': 1,
 
88
               'Unique': 1,
 
89
               'Typecast': GTypecast.name },
 
90
            'description': {
 
91
               'Typecast': GTypecast.text },
 
92
            'action': {
 
93
               'Typecast': GTypecast.text,
 
94
               'ValueSet': {
 
95
                  'create': {},
 
96
                  'extend': {} },
 
97
               'Default': 'create' }},
 
98
         'ParentTags':  ('tables',) },
 
99
 
 
100
      'fields':  {
 
101
         'BaseClass': Objects.GSFields,
 
102
         'Importable': 1,
 
103
         'SingleInstance': 1,
 
104
         'ParentTags':  ('table',) },
 
105
 
 
106
      'field':   {
 
107
         'BaseClass': Objects.GSField,
 
108
         'Importable': 1,
 
109
         'Attributes': {
 
110
            'name':          {
 
111
               'Required': 1,
 
112
               'Unique': 1,
 
113
               'Typecast': GTypecast.name },
 
114
            'description': {
 
115
               'Typecast': GTypecast.text },
 
116
            'type': {
 
117
               'Required': 1,
 
118
               'Typecast': GTypecast.name },
 
119
            'length': {
 
120
               'Typecast': GTypecast.whole },
 
121
            'precision': {
 
122
               'Typecast': GTypecast.whole,
 
123
               'Default': 0 },
 
124
            'auto': {
 
125
               'Typecast': GTypecast.boolean,
 
126
               'Default': 0 },
 
127
            'nullable':     {
 
128
               'Typecast': GTypecast.boolean,
 
129
               'Default': 1 },
 
130
            'default':     {
 
131
               'Typecast': GTypecast.text },
 
132
            'defaultwith':     {
 
133
               'Typecast': GTypecast.text,
 
134
               'ValueSet': {
 
135
                  'constant': {},
 
136
                  'timestamp': {},
 
137
                  'serial': {} },
 
138
               'Default': 'constant' },
 
139
               },
 
140
         'ParentTags':  ('fields',) },
 
141
 
 
142
      'primarykey':   {
 
143
         'BaseClass': Objects.GSPrimaryKey,
 
144
         'SingleInstance': 1,
 
145
         'Attributes': {
 
146
            'name':        {
 
147
               'Required': 1,
 
148
               'Typecast': GTypecast.name } },
 
149
         'ParentTags':  ('table',) },
 
150
 
 
151
      'pkfield':   {
 
152
         'BaseClass': Objects.GSPKField,
 
153
         'Attributes': {
 
154
            'name':        {
 
155
               'Required': 1,
 
156
               'Typecast': GTypecast.name } },
 
157
         'ParentTags':  ('primarykey',) },
 
158
 
 
159
      'constraints':   {
 
160
         'BaseClass': Objects.GSConstraints,
 
161
         'SingleInstance': 1,
 
162
         'ParentTags':  ('table',) },
 
163
 
 
164
      'constraint':    {
 
165
         'BaseClass': Objects.GSConstraint,
 
166
         'Attributes': {
 
167
            'name': {
 
168
               'Required': 1,
 
169
               'Typecast': GTypecast.name },
 
170
            'type': {
 
171
               'Typecast': GTypecast.name } },
 
172
         'ParentTags':  ('constraints',) },
 
173
 
 
174
      'constraintfield':   {
 
175
         'BaseClass': Objects.GSConstraintField,
 
176
         'Attributes': {
 
177
            'name':        {
 
178
               'Required': 1,
 
179
               'Typecast': GTypecast.name } },
 
180
         'ParentTags':  ('constraint',) },
 
181
 
 
182
      'constraintref':   {
 
183
         'BaseClass': Objects.GSConstraintRef,
 
184
         'Attributes': {
 
185
            'name':        {
 
186
               'Required': 1,
 
187
               'Typecast': GTypecast.name },
 
188
             'table':        {
 
189
               'Required': 1,
 
190
               'Typecast': GTypecast.name } },
 
191
         'ParentTags':  ('constraint',) },
 
192
 
 
193
      'indexes':   {
 
194
         'BaseClass': Objects.GSIndexes,
 
195
         'SingleInstance': 1,
 
196
         'ParentTags':  ('table',) },
 
197
      'index':    {
 
198
         'BaseClass': Objects.GSIndex,
 
199
         'Attributes': {
 
200
            'name': {
 
201
               'Required': 1,
 
202
               'Typecast': GTypecast.name },
 
203
            'unique': {
 
204
               'Default': 0,
 
205
               'Typecast': GTypecast.boolean } },
 
206
         'ParentTags':  ('indexes',) },
 
207
 
 
208
      'indexfield':   {
 
209
         'BaseClass': Objects.GSIndexField,
 
210
         'Attributes': {
 
211
            'name':        {
 
212
               'Required': 1,
 
213
               'Typecast': GTypecast.name } },
 
214
         'ParentTags':  ('index',) },
 
215
 
 
216
      'data':   {
 
217
         'BaseClass': Objects.GSData,
 
218
         'SingleInstance': 1,
 
219
         'ParentTags':  ('schema',) },
 
220
 
 
221
      'tabledata':   {
 
222
         'BaseClass': Objects.GSTableData,
 
223
         'Importable': 1,
 
224
         'Attributes': {
 
225
            'name':        {
 
226
               'Required': 1,
 
227
               'Typecast': GTypecast.name },
 
228
            'tablename':        {
 
229
               'Required': 1,
 
230
               'Typecast': GTypecast.name } },
 
231
         'ParentTags':  ('data',) },
 
232
 
 
233
      'definition': {
 
234
        'BaseClass': Objects.GSDefinition,
 
235
        'SingleInstance': True,
 
236
        'ParentTags': ('tabledata',) },
 
237
 
 
238
      'column': {
 
239
        'BaseClass': Objects.GSColumn,
 
240
        'Attributes': {
 
241
          'field': {
 
242
            'Required': True,
 
243
            'Typecast': GTypecast.name },
 
244
          'type': {
 
245
            'Required': True,
 
246
            'Typecast': GTypecast.name },
 
247
          'key': {
 
248
            'Typecast': GTypecast.boolean,
 
249
            'Default' : False},
 
250
          },
 
251
        'ParentTags': ('definition',)},
 
252
 
 
253
      'rows':   {
 
254
         'BaseClass': Objects.GSRows,
 
255
         'SingleInstance': 1,
 
256
         'ParentTags':  ('tabledata',) },
 
257
 
 
258
      'row':   {
 
259
         'BaseClass': Objects.GSRow,
 
260
         'ParentTags':  ('rows',) },
 
261
 
 
262
      'value':   {
 
263
         'BaseClass': Objects.GSValue,
 
264
         'Attributes': {
 
265
            'field':        {
 
266
               'Required': 0,
 
267
               'Typecast': GTypecast.name },
 
268
            'type':        {
 
269
               'Required': 0,
 
270
               'Typecast': GTypecast.name,
 
271
               'Default':  'text' },
 
272
            'key': {
 
273
               'Required': 0,
 
274
               'Typecast': GTypecast.boolean,
 
275
               'Default' : 0}
 
276
            },
 
277
         'ParentTags':  ('row',),
 
278
         'MixedContent': 1,
 
279
         'KeepWhitespace': 1},
 
280
 
 
281
      'description':   {
 
282
         'BaseClass': Objects.GSDescription,
 
283
         'SingleInstance': 1,
 
284
         'MixedContent': 1,
 
285
         'UsableBySiblings': 1,
 
286
         'ParentTags':  ('schema',) },
 
287
 
 
288
    }
 
289
 
 
290
  return GParser.buildImportableTags ('schema', xmlElements)
 
291
 
 
292
 
 
293
# =============================================================================
 
294
# Class called by the XML parser to process the XML file
 
295
# =============================================================================
 
296
 
 
297
class xmlSchemaHandler (GParser.xmlHandler):
 
298
 
 
299
  # ---------------------------------------------------------------------------
 
300
  # Constructor
 
301
  # ---------------------------------------------------------------------------
 
302
 
 
303
  def __init__ (self):
 
304
 
 
305
    GParser.xmlHandler.__init__(self)
 
306
    self.xmlElements = getXMLelements ()
 
307