xmlclass/xmlclass.py
author laurent
Thu, 24 Nov 2011 16:30:06 +0100
changeset 593 34c3569042db
parent 592 89ff2738ef20
child 594 41e62b3174dc
permissions -rwxr-xr-x
Fixing bug in xmlclass with empty CDATA. Empty CDATA aren't present in xml tree and prompt error when trying to load empty ANY tag using CDATA
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
     1
#!/usr/bin/env python
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
     3
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
     5
#based on the plcopen standard. 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
     6
#
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
     8
#
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
     9
#See COPYING file for copyrights details.
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    10
#
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    15
#
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    19
#General Public License for more details.
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    20
#
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    24
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
    25
import os, sys
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    26
import re
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    27
import datetime
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    28
from types import *
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    29
from xml.dom import minidom
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    30
from xml.sax.saxutils import escape, unescape, quoteattr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    31
from new import classobj
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    32
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    33
def CreateNode(name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    34
    node = minidom.Node()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    35
    node.nodeName = name
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    36
    node._attrs = {}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    37
    node.childNodes = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    38
    return node
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    39
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    40
def NodeRenameAttr(node, old_name, new_name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    41
    node._attrs[new_name] = node._attrs.pop(old_name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    42
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    43
def NodeSetAttr(node, name, value):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    44
    attr = minidom.Attr(name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    45
    text = minidom.Text()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    46
    text.data = value
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    47
    attr.childNodes[0] = text
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    48
    node._attrs[name] = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    49
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    50
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    51
Regular expression models for checking all kind of string values defined in XML
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    52
standard
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    53
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    54
Name_model = re.compile('([a-zA-Z_\:][\w\.\-\:]*)$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    55
Names_model = re.compile('([a-zA-Z_\:][\w\.\-\:]*(?: [a-zA-Z_\:][\w\.\-\:]*)*)$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    56
NMToken_model = re.compile('([\w\.\-\:]*)$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    57
NMTokens_model = re.compile('([\w\.\-\:]*(?: [\w\.\-\:]*)*)$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    58
QName_model = re.compile('((?:[a-zA-Z_][\w]*:)?[a-zA-Z_][\w]*)$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    59
QNames_model = re.compile('((?:[a-zA-Z_][\w]*:)?[a-zA-Z_][\w]*(?: (?:[a-zA-Z_][\w]*:)?[a-zA-Z_][\w]*)*)$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    60
NCName_model = re.compile('([a-zA-Z_][\w]*)$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    61
URI_model = re.compile('((?:http://|/)?(?:[\w.-]*/?)*)$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    62
LANGUAGE_model = re.compile('([a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*)$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    63
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    64
ONLY_ANNOTATION = re.compile("((?:annotation )?)")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    65
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    66
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    67
Regular expression models for extracting dates and times from a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    68
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    69
time_model = re.compile('([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]*)?)(?:Z)?$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    70
date_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})((?:[\-\+][0-9]{2}:[0-9]{2})|Z)?$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    71
datetime_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})[ T]([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]*)?)((?:[\-\+][0-9]{2}:[0-9]{2})|Z)?$')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    72
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    73
class xml_timezone(datetime.tzinfo):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    74
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    75
    def SetOffset(self, offset):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    76
        if offset == "Z":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    77
            self.__offset = timedelta(minutes = 0)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    78
            self.__name = "UTC"
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    79
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    80
            sign = {"-" : -1, "+" : 1}[offset[0]]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    81
            hours, minutes = [int(val) for val in offset[1:].split(":")]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    82
            self.__offset = timedelta(minutes=sign * (hours * 60 + minutes))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    83
            self.__name = ""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    84
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    85
    def utcoffset(self, dt):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    86
        return self.__offset
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    87
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    88
    def tzname(self, dt):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    89
        return self.__name
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    90
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    91
    def dst(self, dt):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    92
        return ZERO
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    93
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    94
[SYNTAXELEMENT, SYNTAXATTRIBUTE, SIMPLETYPE, COMPLEXTYPE, COMPILEDCOMPLEXTYPE, 
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
    95
 ATTRIBUTESGROUP, ELEMENTSGROUP, ATTRIBUTE, ELEMENT, CHOICE, ANY, TAG, CONSTRAINT,
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
    96
] = range(13)
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    97
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    98
def NotSupportedYet(type):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
    99
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   100
    Function that generates a function that point out to user that datatype
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   101
    used is not supported by xmlclass yet
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   102
    @param type: data type
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   103
    @return: function generated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   104
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   105
    def GetUnknownValue(attr):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   106
        raise ValueError("\"%s\" type isn't supported by \"xmlclass\" yet!" % \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   107
                         type)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   108
    return GetUnknownValue
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   109
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   110
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   111
This function calculates the number of whitespace for indentation
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   112
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   113
def getIndent(indent, balise):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   114
    first = indent * 2
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   115
    second = first + len(balise) + 1
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   116
    return u'\t'.expandtabs(first), u'\t'.expandtabs(second)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   117
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   118
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   119
def GetAttributeValue(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   120
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   121
    Function that extracts data from a tree node
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   122
    @param attr: tree node containing data to extract
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   123
    @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   124
    @return: data extracted as string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   125
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   126
    if not extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   127
        return attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   128
    if len(attr.childNodes) == 1:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   129
        return unescape(attr.childNodes[0].data.encode("utf-8"))
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   130
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   131
        # content is a CDATA
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   132
        text = ""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   133
        for node in attr.childNodes:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   134
            if node.nodeName != "#text":
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   135
                text += node.data.encode("utf-8")
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   136
        return text
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   137
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   138
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   139
def GetNormalizedString(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   140
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   141
    Function that normalizes a string according to XML 1.0. Replace  
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   142
    tabulations, line feed and carriage return by white space
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   143
    @param attr: tree node containing data to extract or data to normalize
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   144
    @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   145
    @return: data normalized as string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   146
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   147
    if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   148
        value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   149
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   150
        value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   151
    return value.replace("\t", " ").replace("\r", " ").replace("\n", " ")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   152
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   153
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   154
def GetToken(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   155
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   156
    Function that tokenizes a string according to XML 1.0. Remove any leading  
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   157
    and trailing white space and replace internal sequence of two or more 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   158
    spaces by only one white space
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   159
    @param attr: tree node containing data to extract or data to tokenize
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   160
    @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   161
    @return: data tokenized as string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   162
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   163
    return " ".join([part for part in 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   164
                     GetNormalizedString(attr, extract).split(" ")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   165
                     if part])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   166
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   167
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   168
def GetHexInteger(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   169
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   170
    Function that extracts an hexadecimal integer from a tree node or a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   171
    @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   172
    @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   173
    @return: data as an integer
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   174
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   175
    if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   176
        value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   177
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   178
        value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   179
    if len(value) % 2 != 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   180
        raise ValueError("\"%s\" isn't a valid hexadecimal integer!" % value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   181
    try:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   182
        return int(value, 16)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   183
    except:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   184
        raise ValueError("\"%s\" isn't a valid hexadecimal integer!" % value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   185
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   186
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   187
def GenerateIntegerExtraction(minInclusive=None, maxInclusive=None, 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   188
                              minExclusive=None, maxExclusive=None):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   189
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   190
    Function that generates an extraction function for integer defining min and
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   191
    max of integer value
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   192
    @param minInclusive: inclusive minimum
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   193
    @param maxInclusive: inclusive maximum
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   194
    @param minExclusive: exclusive minimum
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   195
    @param maxExclusive: exclusive maximum
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   196
    @return: function generated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   197
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   198
    def GetInteger(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   199
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   200
        Function that extracts an integer from a tree node or a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   201
        @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   202
        @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   203
        @return: data as an integer
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   204
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   205
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   206
        if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   207
            value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   208
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   209
            value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   210
        try:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   211
            # TODO: permit to write value like 1E2
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   212
            value = int(value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   213
        except:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   214
            raise ValueError("\"%s\" isn't a valid integer!" % value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   215
        if minInclusive is not None and value < minInclusive:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   216
            raise ValueError("\"%d\" isn't greater or equal to %d!" % \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   217
                             (value, minInclusive))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   218
        if maxInclusive is not None and value > maxInclusive:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   219
            raise ValueError("\"%d\" isn't lesser or equal to %d!" % \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   220
                             (value, maxInclusive))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   221
        if minExclusive is not None and value <= minExclusive:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   222
            raise ValueError("\"%d\" isn't greater than %d!" % \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   223
                             (value, minExclusive))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   224
        if maxExclusive is not None and value >= maxExclusive:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   225
            raise ValueError("\"%d\" isn't lesser than %d!" % \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   226
                             (value, maxExclusive))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   227
        return value
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   228
    return GetInteger
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   229
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   230
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   231
def GenerateFloatExtraction(type, extra_values=[]):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   232
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   233
    Function that generates an extraction function for float
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   234
    @param type: name of the type of float
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   235
    @return: function generated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   236
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   237
    def GetFloat(attr, extract = True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   238
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   239
        Function that extracts a float from a tree node or a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   240
        @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   241
        @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   242
        @return: data as a float
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   243
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   244
        if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   245
            value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   246
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   247
            value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   248
        if value in extra_values:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   249
            return value
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   250
        try:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   251
            return float(value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   252
        except:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   253
            raise ValueError("\"%s\" isn't a valid %s!" % (value, type))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   254
    return GetFloat
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   255
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   256
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   257
def GetBoolean(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   258
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   259
    Function that extracts a boolean from a tree node or a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   260
    @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   261
    @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   262
    @return: data as a boolean
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   263
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   264
    if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   265
        value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   266
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   267
        value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   268
    if value == "true" or value == "1":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   269
        return True
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   270
    elif value == "false" or value == "0":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   271
        return False
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   272
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   273
        raise ValueError("\"%s\" isn't a valid boolean!" % value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   274
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   275
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   276
def GetTime(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   277
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   278
    Function that extracts a time from a tree node or a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   279
    @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   280
    @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   281
    @return: data as a time
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   282
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   283
    if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   284
        value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   285
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   286
        value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   287
    result = time_model.match(value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   288
    if result:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   289
        values = result.groups()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   290
        time_values = [int(v) for v in values[:2]]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   291
        seconds = float(values[2])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   292
        time_values.extend([int(seconds), int((seconds % 1) * 1000000)])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   293
        return datetime.time(*time_values)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   294
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   295
        raise ValueError("\"%s\" isn't a valid time!" % value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   296
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   297
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   298
def GetDate(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   299
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   300
    Function that extracts a date from a tree node or a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   301
    @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   302
    @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   303
    @return: data as a date
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   304
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   305
    if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   306
        value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   307
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   308
        value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   309
    result = date_model.match(value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   310
    if result:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   311
        values = result.groups()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   312
        date_values = [int(v) for v in values[:3]]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   313
        if values[3] is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   314
            tz = xml_timezone()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   315
            tz.SetOffset(values[3])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   316
            date_values.append(tz)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   317
        return datetime.date(*date_values)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   318
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   319
        raise ValueError("\"%s\" isn't a valid date!" % value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   320
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   321
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   322
def GetDateTime(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   323
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   324
    Function that extracts date and time from a tree node or a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   325
    @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   326
    @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   327
    @return: data as date and time
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   328
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   329
    if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   330
        value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   331
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   332
        value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   333
    result = datetime_model.match(value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   334
    if result:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   335
        values = result.groups()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   336
        datetime_values = [int(v) for v in values[:5]]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   337
        seconds = float(values[5])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   338
        datetime_values.extend([int(seconds), int((seconds % 1) * 1000000)])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   339
        if values[6] is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   340
            tz = xml_timezone()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   341
            tz.SetOffset(values[6])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   342
            datetime_values.append(tz)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   343
        return datetime.datetime(*datetime_values)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   344
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   345
        raise ValueError("\"%s\" isn't a valid datetime!" % value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   346
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   347
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   348
def GenerateModelNameExtraction(type, model):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   349
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   350
    Function that generates an extraction function for string matching a model
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   351
    @param type: name of the data type
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   352
    @param model: model that data must match
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   353
    @return: function generated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   354
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   355
    def GetModelName(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   356
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   357
        Function that extracts a string from a tree node or not and check that
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   358
        string extracted or given match the model
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   359
        @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   360
        @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   361
        @return: data as a string if matching
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   362
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   363
        if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   364
            value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   365
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   366
            value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   367
        result = model.match(value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   368
        if not result:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   369
            raise ValueError("\"%s\" isn't a valid %s!" % (value, type))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   370
        return value
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   371
    return GetModelName
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   372
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   373
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   374
def GenerateLimitExtraction(min=None, max=None, unbounded=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   375
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   376
    Function that generates an extraction function for integer defining min and
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   377
    max of integer value
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   378
    @param min: minimum limit value
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   379
    @param max: maximum limit value
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   380
    @param unbounded: value can be "unbounded" or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   381
    @return: function generated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   382
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   383
    def GetLimit(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   384
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   385
        Function that extracts a string from a tree node or not and check that
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   386
        string extracted or given is in a list of values
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   387
        @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   388
        @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   389
        @return: data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   390
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   391
        if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   392
            value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   393
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   394
            value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   395
        if value == "unbounded":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   396
            if unbounded:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   397
                return value
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   398
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   399
                raise ValueError("Member limit can't be defined to \"unbounded\"!")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   400
        try:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   401
            limit = int(value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   402
        except:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   403
            raise ValueError("\"%s\" isn't a valid value for this member limit!" % value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   404
        if limit < 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   405
            raise ValueError("Member limit can't be negative!")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   406
        elif min is not None and limit < min:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   407
            raise ValueError("Member limit can't be lower than \"%d\"!" % min)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   408
        elif max is not None and limit > max:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   409
            raise ValueError("Member limit can't be upper than \"%d\"!" % max)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   410
        return limit
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   411
    return GetLimit
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   412
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   413
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   414
def GenerateEnumeratedExtraction(type, list):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   415
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   416
    Function that generates an extraction function for enumerated values
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   417
    @param type: name of the data type
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   418
    @param list: list of possible values
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   419
    @return: function generated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   420
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   421
    def GetEnumerated(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   422
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   423
        Function that extracts a string from a tree node or not and check that
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   424
        string extracted or given is in a list of values
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   425
        @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   426
        @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   427
        @return: data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   428
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   429
        if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   430
            value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   431
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   432
            value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   433
        if value in list:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   434
            return value
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   435
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   436
            raise ValueError("\"%s\" isn't a valid value for %s!" % \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   437
                             (value, type))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   438
    return GetEnumerated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   439
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   440
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   441
def GetNamespaces(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   442
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   443
    Function that extracts a list of namespaces from a tree node or a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   444
    @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   445
    @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   446
    @return: list of namespaces
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   447
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   448
    if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   449
        value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   450
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   451
        value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   452
    if value == "":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   453
        return []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   454
    elif value == "##any" or value == "##other":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   455
        namespaces = [value]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   456
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   457
        namespaces = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   458
        for item in value.split(" "):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   459
            if item == "##targetNamespace" or item == "##local":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   460
                namespaces.append(item)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   461
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   462
                result = URI_model.match(item)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   463
                if result is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   464
                    namespaces.append(item)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   465
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   466
                    raise ValueError("\"%s\" isn't a valid value for namespace!" % value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   467
    return namespaces
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   468
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   469
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   470
def GenerateGetList(type, list):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   471
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   472
    Function that generates an extraction function for a list of values
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   473
    @param type: name of the data type
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   474
    @param list: list of possible values
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   475
    @return: function generated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   476
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   477
    def GetLists(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   478
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   479
        Function that extracts a list of values from a tree node or a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   480
        @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   481
        @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   482
        @return: list of values
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   483
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   484
        if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   485
            value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   486
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   487
            value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   488
        if value == "":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   489
            return []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   490
        elif value == "#all":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   491
            return [value]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   492
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   493
            values = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   494
            for item in value.split(" "):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   495
                if item in list:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   496
                    values.append(item)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   497
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   498
                    raise ValueError("\"%s\" isn't a valid value for %s!" % \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   499
                                     (value, type))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   500
            return values
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   501
    return GetLists
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   502
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   503
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   504
def GenerateModelNameListExtraction(type, model):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   505
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   506
    Function that generates an extraction function for list of string matching
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   507
    a model
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   508
    @param type: name of the data type
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   509
    @param model: model that list elements must match
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   510
    @return: function generated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   511
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   512
    def GetModelNameList(attr, extract=True):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   513
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   514
        Function that extracts a list of string from a tree node or not and
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   515
        check that all extracted items match the model
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   516
        @param attr: tree node containing data to extract or data as a string
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   517
        @param extract: attr is a tree node or not
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   518
        @return: data as a list of string if matching 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   519
        """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   520
        if extract:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   521
            value = GetAttributeValue(attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   522
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   523
            value = attr
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   524
        values = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   525
        for item in value.split(" "):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   526
            result = model.match(item)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   527
            if result is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   528
                values.append(item)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   529
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   530
                raise ValueError("\"%s\" isn't a valid value for %s!" % \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   531
                                 (value, type))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   532
        return values
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   533
    return GetModelNameList
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   534
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   535
def GenerateAnyInfos(infos):
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   536
    def ExtractAny(tree):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   537
        if tree.nodeName == "#cdata-section":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   538
            return tree.data.encode("utf-8")
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   539
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   540
            return tree
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   541
    
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   542
    def GenerateAny(value, name=None, indent=0):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   543
        if isinstance(value, (StringType, UnicodeType)):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   544
            try:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   545
                value = value.decode("utf-8")
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   546
            except:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   547
                pass
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   548
            return u'<![CDATA[%s]]>\n' % value
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   549
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   550
            return value.toprettyxml(indent=" "*indent, encoding="utf-8")
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   551
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   552
    return {
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   553
        "type": COMPLEXTYPE, 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   554
        "extract": ExtractAny,
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   555
        "generate": GenerateAny,
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   556
        "initial": lambda: "",
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   557
        "check": lambda x: isinstance(x, (StringType, UnicodeType, minidom.Node))
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   558
    }
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   559
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   560
def GenerateTagInfos(name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   561
    def ExtractTag(tree):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   562
        if len(tree._attrs) > 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   563
            raise ValueError("\"%s\" musn't have attributes!" % name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   564
        if len(tree.childNodes) > 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   565
            raise ValueError("\"%s\" musn't have children!" % name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   566
        return None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   567
    
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   568
    def GenerateTag(value, name=None, indent=0):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   569
        if name is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   570
            ind1, ind2 = getIndent(indent, name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   571
            return ind1 + "<%s/>\n" % name
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   572
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   573
            return ""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   574
    
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   575
    return {
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   576
        "type": TAG, 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   577
        "extract": ExtractTag,
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   578
        "generate": GenerateTag,
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   579
        "initial": lambda: None,
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   580
        "check": lambda x: x == None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   581
    }
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   582
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   583
def FindTypeInfos(factory, infos):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   584
    if isinstance(infos, (UnicodeType, StringType)):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   585
        namespace, name = DecomposeQualifiedName(infos)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   586
        return factory.GetQualifiedNameInfos(name, namespace)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   587
    return infos
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   588
    
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   589
def GetElementInitialValue(factory, infos):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   590
    infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   591
    if infos["minOccurs"] == 0 and infos["maxOccurs"] == 1:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   592
        if infos.has_key("default"):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   593
            return infos["elmt_type"]["extract"](infos["default"], False)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   594
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   595
            return None
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   596
    elif infos["minOccurs"] == 1 and infos["maxOccurs"] == 1:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   597
        return infos["elmt_type"]["initial"]()
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   598
    else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   599
        return [infos["elmt_type"]["initial"]() for i in xrange(infos["minOccurs"])]
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   600
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   601
def HandleError(message, raise_exception):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   602
    if raise_exception:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   603
        raise ValueError(message)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   604
    return False
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   605
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   606
def CheckElementValue(factory, name, infos, value, raise_exception=True):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   607
    infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   608
    if value is None and raise_exception:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   609
        if not (infos["minOccurs"] == 0 and infos["maxOccurs"] == 1):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   610
            return HandleError("Attribute '%s' isn't optional." % name, raise_exception)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   611
    elif infos["maxOccurs"] == "unbounded" or infos["maxOccurs"] > 1:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   612
        if not isinstance(value, ListType):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   613
            return HandleError("Attribute '%s' must be a list." % name, raise_exception)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   614
        if len(value) < infos["minOccurs"] or infos["maxOccurs"] != "unbounded" and len(value) > infos["maxOccurs"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   615
            return HandleError("List out of bounds for attribute '%s'." % name, raise_exception)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   616
        if not reduce(lambda x, y: x and y, map(infos["elmt_type"]["check"], value), True):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   617
            return HandleError("Attribute '%s' must be a list of valid elements." % name, raise_exception)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   618
    elif infos.has_key("fixed") and value != infos["fixed"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   619
        return HandleError("Value of attribute '%s' can only be '%s'." % (name, str(infos["fixed"])), raise_exception)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   620
    else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   621
        return infos["elmt_type"]["check"](value)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   622
    return True
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   623
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   624
def GetContentInfos(name, choices):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   625
    for choice_infos in choices:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   626
        if choices_infos["type"] == "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   627
            for element_infos in choices_infos["elements"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   628
                if element_infos["type"] == CHOICE:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   629
                    if GetContentInfos(name, element_infos["choices"]):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   630
                        return choices_infos
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   631
                elif element_infos["name"] == name:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   632
                    return choices_infos
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   633
        elif choice_infos["name"] == name:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   634
            return choices_infos
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   635
    return None
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   636
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   637
def ComputeContentChoices(factory, name, infos):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   638
    choices = []
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   639
    for choice in infos["choices"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   640
        if choice["type"] == "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   641
            choice["name"] = "sequence"
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   642
            for sequence_element in choice["elements"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   643
                if sequence_element["type"] != CHOICE:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   644
                    element_infos = factory.ExtractTypeInfos(sequence_element["name"], name, sequence_element["elmt_type"])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   645
                    if element_infos is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   646
                        sequence_element["elmt_type"] = element_infos
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   647
        elif choice["elmt_type"] == "tag":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   648
            choice["elmt_type"] = GenerateTagInfos(choice["name"])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   649
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   650
            choice_infos = factory.ExtractTypeInfos(choice["name"], name, choice["elmt_type"])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   651
            if choice_infos is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   652
                choice["elmt_type"] = choice_infos
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   653
        choices.append((choice["name"], choice))
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   654
    return choices
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   655
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   656
def ExtractContentElement(factory, tree, infos, content):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   657
    infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   658
    if infos["maxOccurs"] == "unbounded" or infos["maxOccurs"] > 1:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   659
        if isinstance(content, ListType) and len(content) > 0 and \
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   660
           content[-1]["name"] == tree.nodeName:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   661
            content_item = content.pop(-1)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   662
            content_item["value"].append(infos["elmt_type"]["extract"](tree))
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   663
            return content_item
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   664
        elif not isinstance(content, ListType) and \
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   665
             content is not None and \
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   666
             content["name"] == tree.nodeName:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   667
            return {"name": tree.nodeName, 
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   668
                    "value": content["value"] + [infos["elmt_type"]["extract"](tree)]}
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   669
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   670
            return {"name": tree.nodeName, 
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   671
                    "value": [infos["elmt_type"]["extract"](tree)]}
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   672
    else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   673
        return {"name": tree.nodeName, 
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   674
                "value": infos["elmt_type"]["extract"](tree)}
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   675
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   676
def GenerateContentInfos(factory, name, choices):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   677
    choices_dict = {}
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   678
    for choice_name, infos in choices:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   679
        if choice_name == "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   680
            for element in infos["elements"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   681
                if element["type"] == CHOICE:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   682
                    element["elmt_type"] = GenerateContentInfos(factory, name, ComputeContentChoices(factory, name, element))
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   683
                elif choices_dict.has_key(element["name"]):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   684
                    raise ValueError("'%s' element defined two times in choice" % choice_name)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   685
                else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   686
                    choices_dict[element["name"]] = infos
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   687
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   688
            if choices_dict.has_key(choice_name):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   689
                raise ValueError("'%s' element defined two times in choice" % choice_name)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   690
            choices_dict[choice_name] = infos
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   691
    
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   692
    def GetContentInitial():
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   693
        content_name, infos = choices[0]
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   694
        if content_name == "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   695
            content_value = []
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   696
            for i in xrange(infos["minOccurs"]):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   697
                for element_infos in infos["elements"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   698
                    value = GetElementInitialValue(factory, element_infos)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   699
                    if value is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   700
                        if element_infos["type"] == CHOICE:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   701
                            content_value.append(value)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   702
                        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   703
                            content_value.append({"name": element_infos["name"], "value": value})
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   704
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   705
            content_value = GetElementInitialValue(factory, infos)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   706
        return {"name": content_name, "value": content_value}
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   707
        
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   708
    def CheckContent(value):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   709
        if value["name"] != "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   710
            infos = choices_dict.get(value["name"], None)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   711
            if infos is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   712
                return CheckElementValue(factory, value["name"], infos, value["value"], False)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   713
        elif len(value["value"]) > 0:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   714
            infos = choices_dict.get(value["value"][0]["name"], None)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   715
            if infos is None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   716
                for choice_name, infos in choices:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   717
                    if infos["type"] == "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   718
                        for element_infos in infos["elements"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   719
                            if element_infos["type"] == CHOICE:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   720
                                infos = GetContentInfos(value["value"][0]["name"], element_infos["choices"])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   721
            if infos is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   722
                sequence_number = 0
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   723
                element_idx = 0
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   724
                while element_idx < len(value["value"]):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   725
                    for element_infos in infos["elements"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   726
                        if element_infos["name"] == value["value"][element_idx]["name"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   727
                            element_value = value["value"][element_idx]["value"]
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   728
                            element_idx += 1
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   729
                        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   730
                            element_value = None
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   731
                        if not CheckElementValue(factory, element_infos["name"], element_infos, element_value, False):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   732
                            raise ValueError("Invalid sequence value in attribute 'content'")
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   733
                    sequence_number += 1
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   734
                if sequence_number < infos["minOccurs"] or infos["maxOccurs"] != "unbounded" and sequence_number > infos["maxOccurs"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   735
                    raise ValueError("Invalid sequence value in attribute 'content'")
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   736
                return True
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   737
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   738
            for element_name, infos in choices:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   739
                if element_name == "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   740
                    required = 0
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   741
                    for element in infos["elements"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   742
                        if element["minOccurs"] > 0:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   743
                            required += 1
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   744
                    if required == 0:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   745
                        return True
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   746
        return False
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   747
    
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   748
    def ExtractContent(tree, content):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   749
        infos = choices_dict.get(tree.nodeName, None)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   750
        if infos is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   751
            if infos["name"] == "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   752
                sequence_dict = dict([(element_infos["name"], element_infos) for element_infos in infos["elements"] if element_infos["type"] != CHOICE])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   753
                element_infos = sequence_dict.get(tree.nodeName)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   754
                if content is not None and \
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   755
                   content["name"] == "sequence" and \
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   756
                   len(content["value"]) > 0 and \
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   757
                   choices_dict.get(content["value"][-1]["name"]) == infos:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   758
                    return {"name": "sequence",
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   759
                            "value": content["value"] + [ExtractContentElement(factory, tree, element_infos, content["value"][-1])]}
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   760
                else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   761
                    return {"name": "sequence",
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   762
                            "value": [ExtractContentElement(factory, tree, element_infos, None)]}
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   763
            else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   764
                return ExtractContentElement(factory, tree, infos, content)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   765
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   766
            for choice_name, infos in choices:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   767
                if infos["type"] == "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   768
                    for element_infos in infos["elements"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   769
                        if element_infos["type"] == CHOICE:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   770
                            try:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   771
                                if content is not None and \
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   772
                                    content["name"] == "sequence" and \
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   773
                                    len(content["value"]) > 0:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   774
                                    return {"name": "sequence",
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   775
                                            "value": content["value"] + [element_infos["elmt_type"]["extract"](tree, content["value"][-1])]}
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   776
                                else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   777
                                    return {"name": "sequence",
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   778
                                            "value": [element_infos["elmt_type"]["extract"](tree, None)]}
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   779
                            except:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   780
                                pass
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   781
        raise ValueError("Invalid element \"%s\" for content!" % tree.nodeName)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   782
    
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   783
    def GenerateContent(value, name=None, indent=0):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   784
        text = ""
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   785
        if value["name"] != "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   786
            infos = choices_dict.get(value["name"], None)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   787
            if infos is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   788
                infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   789
                if infos["maxOccurs"] == "unbounded" or infos["maxOccurs"] > 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   790
                    for item in value["value"]:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   791
                        text += infos["elmt_type"]["generate"](item, value["name"], indent)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   792
                else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   793
                    text += infos["elmt_type"]["generate"](value["value"], value["name"], indent)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   794
        elif len(value["value"]) > 0:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   795
            infos = choices_dict.get(value["value"][0]["name"], None)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   796
            if infos is None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   797
                for choice_name, infos in choices:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   798
                    if infos["type"] == "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   799
                        for element_infos in infos["elements"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   800
                            if element_infos["type"] == CHOICE:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   801
                                infos = GetContentInfos(value["value"][0]["name"], element_infos["choices"])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   802
            if infos is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   803
                sequence_dict = dict([(element_infos["name"], element_infos) for element_infos in infos["elements"]]) 
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   804
                for element_value in value["value"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   805
                    element_infos = sequence_dict.get(element_value["name"])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   806
                    if element_infos["maxOccurs"] == "unbounded" or element_infos["maxOccurs"] > 1:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   807
                        for item in element_value["value"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   808
                            text += element_infos["elmt_type"]["generate"](item, element_value["name"], indent)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   809
                    else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   810
                        text += element_infos["elmt_type"]["generate"](element_value["value"], element_infos["name"], indent)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   811
        return text
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   812
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   813
    return {
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   814
        "type": COMPLEXTYPE,
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   815
        "initial": GetContentInitial,
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   816
        "check": CheckContent,
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   817
        "extract": ExtractContent,
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   818
        "generate": GenerateContent
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   819
    }
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   820
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   821
#-------------------------------------------------------------------------------
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   822
#                           Structure extraction functions
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   823
#-------------------------------------------------------------------------------
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   824
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   825
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   826
def DecomposeQualifiedName(name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   827
    result = QName_model.match(name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   828
    if not result:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   829
        raise ValueError("\"%s\" isn't a valid QName value!" % name) 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   830
    parts = result.groups()[0].split(':')
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   831
    if len(parts) == 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   832
        return None, parts[0]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   833
    return parts
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   834
    
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   835
def GenerateElement(element_name, attributes, elements_model, 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   836
                    accept_text=False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   837
    def ExtractElement(factory, node):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   838
        attrs = factory.ExtractNodeAttrs(element_name, node, attributes)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   839
        children_structure = ""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   840
        children_infos = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   841
        children = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   842
        for child in node.childNodes:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   843
            if child.nodeName not in ["#comment", "#text"]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   844
                namespace, childname = DecomposeQualifiedName(child.nodeName)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   845
                children_structure += "%s "%childname
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   846
        result = elements_model.match(children_structure)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   847
        if not result:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   848
            raise ValueError("Invalid structure for \"%s\" children!. First element invalid." % node.nodeName)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   849
        valid = result.groups()[0]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   850
        if len(valid) < len(children_structure):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   851
            raise ValueError("Invalid structure for \"%s\" children!. Element number %d invalid." % (node.nodeName, len(valid.split(" ")) - 1))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   852
        for child in node.childNodes:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   853
            if child.nodeName != "#comment" and \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   854
               (accept_text or child.nodeName != "#text"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   855
                if child.nodeName == "#text":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   856
                    children.append(GetAttributeValue(node))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   857
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   858
                    namespace, childname = DecomposeQualifiedName(child.nodeName)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   859
                    infos = factory.GetQualifiedNameInfos(childname, namespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   860
                    if infos["type"] != SYNTAXELEMENT:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   861
                        raise ValueError("\"%s\" can't be a member child!" % name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   862
                    if infos["extract"].has_key(element_name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   863
                        children.append(infos["extract"][element_name](factory, child))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   864
                    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   865
                        children.append(infos["extract"]["default"](factory, child))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   866
        return node.nodeName, attrs, children
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   867
    return ExtractElement
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   868
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   869
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   870
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   871
Class that generate class from an XML Tree
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   872
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   873
class ClassFactory:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   874
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   875
    def __init__(self, document, filepath=None, debug=False):
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   876
        self.Document = document
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   877
        if filepath is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   878
            self.BaseFolder, self.FileName = os.path.split(filepath)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   879
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   880
            self.BaseFolder = self.FileName = None
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   881
        self.Debug = debug
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   882
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   883
        # Dictionary for stocking Classes and Types definitions created from
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   884
        # the XML tree
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   885
        self.XMLClassDefinitions = {}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   886
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   887
        self.DefinedNamespaces = {}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   888
        self.Namespaces = {}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   889
        self.SchemaNamespace = None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   890
        self.TargetNamespace = None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   891
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   892
        self.CurrentCompilations = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   893
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   894
        # Dictionaries for stocking Classes and Types generated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   895
        self.ComputeAfter = []
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   896
        if self.FileName is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   897
            self.ComputedClasses = {self.FileName: {}}
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   898
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   899
            self.ComputedClasses = {}
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   900
        self.ComputedClassesInfos = {}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   901
        self.AlreadyComputed = {}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   902
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   903
    def GetQualifiedNameInfos(self, name, namespace=None, canbenone=False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   904
        if namespace is None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   905
            if self.Namespaces[self.SchemaNamespace].has_key(name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   906
                return self.Namespaces[self.SchemaNamespace][name]
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
   907
            for space, elements in self.Namespaces.iteritems():
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   908
                if space != self.SchemaNamespace and elements.has_key(name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   909
                    return elements[name]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   910
            parts = name.split("_", 1)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   911
            if len(parts) > 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   912
                group = self.GetQualifiedNameInfos(parts[0], namespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   913
                if group is not None and group["type"] == ELEMENTSGROUP:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   914
                    elements = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   915
                    if group.has_key("elements"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   916
                        elements = group["elements"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   917
                    elif group.has_key("choices"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   918
                        elements = group["choices"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   919
                    for element in elements:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   920
                        if element["name"] == parts[1]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   921
                            return element
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   922
            if not canbenone:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   923
                raise ValueError("Unknown element \"%s\" for any defined namespaces!" % name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   924
        elif self.Namespaces.has_key(namespace):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   925
            if self.Namespaces[namespace].has_key(name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   926
                return self.Namespaces[namespace][name]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   927
            parts = name.split("_", 1)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   928
            if len(parts) > 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   929
                group = self.GetQualifiedNameInfos(parts[0], namespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   930
                if group is not None and group["type"] == ELEMENTSGROUP:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   931
                    elements = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   932
                    if group.has_key("elements"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   933
                        elements = group["elements"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   934
                    elif group.has_key("choices"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   935
                        elements = group["choices"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   936
                    for element in elements:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   937
                        if element["name"] == parts[1]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   938
                            return element
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   939
            if not canbenone:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   940
                raise ValueError("Unknown element \"%s\" for namespace \"%s\"!" % (name, namespace))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   941
        elif not canbenone:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   942
            raise ValueError("Unknown namespace \"%s\"!" % namespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   943
        return None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   944
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   945
    def SplitQualifiedName(self, name, namespace=None, canbenone=False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   946
        if namespace is None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   947
            if self.Namespaces[self.SchemaNamespace].has_key(name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   948
                return name, None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   949
            for space, elements in self.Namespaces.items():
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   950
                if space != self.SchemaNamespace and elements.has_key(name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   951
                    return name, None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   952
            parts = name.split("_", 1)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   953
            if len(parts) > 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   954
                group = self.GetQualifiedNameInfos(parts[0], namespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   955
                if group is not None and group["type"] == ELEMENTSGROUP:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   956
                    elements = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   957
                    if group.has_key("elements"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   958
                        elements = group["elements"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   959
                    elif group.has_key("choices"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   960
                        elements = group["choices"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   961
                    for element in elements:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   962
                        if element["name"] == parts[1]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   963
                            return part[1], part[0]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   964
            if not canbenone:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   965
                raise ValueError("Unknown element \"%s\" for any defined namespaces!" % name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   966
        elif self.Namespaces.has_key(namespace):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   967
            if self.Namespaces[namespace].has_key(name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   968
                return name, None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   969
            parts = name.split("_", 1)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   970
            if len(parts) > 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   971
                group = self.GetQualifiedNameInfos(parts[0], namespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   972
                if group is not None and group["type"] == ELEMENTSGROUP:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   973
                    elements = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   974
                    if group.has_key("elements"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   975
                        elements = group["elements"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   976
                    elif group.has_key("choices"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   977
                        elements = group["choices"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   978
                    for element in elements:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   979
                        if element["name"] == parts[1]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   980
                            return parts[1], parts[0]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   981
            if not canbenone:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   982
                raise ValueError("Unknown element \"%s\" for namespace \"%s\"!" % (name, namespace))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   983
        elif not canbenone:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   984
            raise ValueError("Unknown namespace \"%s\"!" % namespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   985
        return None, None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   986
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   987
    def ExtractNodeAttrs(self, element_name, node, valid_attrs):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   988
        attrs = {}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   989
        for qualified_name, attr in node._attrs.items():
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   990
            namespace, name =  DecomposeQualifiedName(qualified_name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   991
            if name in valid_attrs:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   992
                infos = self.GetQualifiedNameInfos(name, namespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   993
                if infos["type"] != SYNTAXATTRIBUTE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   994
                    raise ValueError("\"%s\" can't be a member attribute!" % name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   995
                elif name in attrs:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   996
                    raise ValueError("\"%s\" attribute has been twice!" % name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   997
                elif element_name in infos["extract"]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   998
                    attrs[name] = infos["extract"][element_name](attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
   999
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1000
                    attrs[name] = infos["extract"]["default"](attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1001
            elif namespace == "xmlns":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1002
                infos = self.GetQualifiedNameInfos("anyURI", self.SchemaNamespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1003
                self.DefinedNamespaces[infos["extract"](attr)] = name
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1004
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1005
                raise ValueError("Invalid attribute \"%s\" for member \"%s\"!" % (qualified_name, node.nodeName))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1006
        for attr in valid_attrs:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1007
            if attr not in attrs and \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1008
               self.Namespaces[self.SchemaNamespace].has_key(attr) and \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1009
               self.Namespaces[self.SchemaNamespace][attr].has_key("default"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1010
                if self.Namespaces[self.SchemaNamespace][attr]["default"].has_key(element_name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1011
                    default = self.Namespaces[self.SchemaNamespace][attr]["default"][element_name]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1012
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1013
                    default = self.Namespaces[self.SchemaNamespace][attr]["default"]["default"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1014
                if default is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1015
                    attrs[attr] = default
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1016
        return attrs
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1017
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1018
    def ReduceElements(self, elements, schema=False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1019
        result = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1020
        for child_infos in elements:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1021
            if child_infos is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1022
                if child_infos[1].has_key("name") and schema:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1023
                    self.CurrentCompilations.append(child_infos[1]["name"])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1024
                namespace, name = DecomposeQualifiedName(child_infos[0])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1025
                infos = self.GetQualifiedNameInfos(name, namespace)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1026
                if infos["type"] != SYNTAXELEMENT:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1027
                    raise ValueError("\"%s\" can't be a member child!" % name)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1028
                element = infos["reduce"](self, child_infos[1], child_infos[2])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1029
                if element is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1030
                    result.append(element)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1031
                if child_infos[1].has_key("name") and schema:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1032
                    self.CurrentCompilations.pop(-1)
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1033
        annotations = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1034
        children = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1035
        for element in result:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1036
            if element["type"] == "annotation":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1037
                annotations.append(element)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1038
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1039
                children.append(element)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1040
        return annotations, children
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1041
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1042
    def AddComplexType(self, typename, infos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1043
        if not self.XMLClassDefinitions.has_key(typename):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1044
            self.XMLClassDefinitions[typename] = infos
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1045
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1046
            raise ValueError("\"%s\" class already defined. Choose another name!" % typename)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1047
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1048
    def ParseSchema(self):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1049
        pass
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1050
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1051
    def ExtractTypeInfos(self, name, parent, typeinfos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1052
        if isinstance(typeinfos, (StringType, UnicodeType)):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1053
            namespace, name = DecomposeQualifiedName(typeinfos)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1054
            infos = self.GetQualifiedNameInfos(name, namespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1055
            if infos["type"] == COMPLEXTYPE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1056
                name, parent = self.SplitQualifiedName(name, namespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1057
                result = self.CreateClass(name, parent, infos)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1058
                if result is not None and not isinstance(result, (UnicodeType, StringType)):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1059
                    self.Namespaces[self.TargetNamespace][result["name"]] = result
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1060
                return result
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1061
            elif infos["type"] == ELEMENT and infos["elmt_type"]["type"] == COMPLEXTYPE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1062
                name, parent = self.SplitQualifiedName(name, namespace)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1063
                result = self.CreateClass(name, parent, infos["elmt_type"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1064
                if result is not None and not isinstance(result, (UnicodeType, StringType)):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1065
                    self.Namespaces[self.TargetNamespace][result["name"]] = result
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1066
                return result
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1067
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1068
                return infos
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1069
        elif typeinfos["type"] == COMPLEXTYPE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1070
            return self.CreateClass(name, parent, typeinfos)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1071
        elif typeinfos["type"] == SIMPLETYPE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1072
            return typeinfos
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1073
            
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1074
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1075
    Methods that generates the classes
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1076
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1077
    def CreateClasses(self):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1078
        self.ParseSchema()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1079
        for name, infos in self.Namespaces[self.TargetNamespace].items():
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1080
            if infos["type"] == ELEMENT:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1081
                if not isinstance(infos["elmt_type"], (UnicodeType, StringType)) and \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1082
                   infos["elmt_type"]["type"] == COMPLEXTYPE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1083
                    self.ComputeAfter.append((name, None, infos["elmt_type"], True))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1084
                    while len(self.ComputeAfter) > 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1085
                        result = self.CreateClass(*self.ComputeAfter.pop(0))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1086
                        if result is not None and not isinstance(result, (UnicodeType, StringType)):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1087
                            self.Namespaces[self.TargetNamespace][result["name"]] = result
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1088
            elif infos["type"] == COMPLEXTYPE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1089
                self.ComputeAfter.append((name, None, infos))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1090
                while len(self.ComputeAfter) > 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1091
                    result = self.CreateClass(*self.ComputeAfter.pop(0))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1092
                    if result is not None and \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1093
                       not isinstance(result, (UnicodeType, StringType)):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1094
                        self.Namespaces[self.TargetNamespace][result["name"]] = result
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1095
            elif infos["type"] == ELEMENTSGROUP:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1096
                elements = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1097
                if infos.has_key("elements"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1098
                    elements = infos["elements"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1099
                elif infos.has_key("choices"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1100
                    elements = infos["choices"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1101
                for element in elements:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1102
                    if not isinstance(element["elmt_type"], (UnicodeType, StringType)) and \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1103
                       element["elmt_type"]["type"] == COMPLEXTYPE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1104
                        self.ComputeAfter.append((element["name"], infos["name"], element["elmt_type"]))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1105
                        while len(self.ComputeAfter) > 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1106
                            result = self.CreateClass(*self.ComputeAfter.pop(0))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1107
                            if result is not None and \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1108
                               not isinstance(result, (UnicodeType, StringType)):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1109
                                self.Namespaces[self.TargetNamespace][result["name"]] = result
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1110
        return self.ComputedClasses
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1111
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1112
    def CreateClass(self, name, parent, classinfos, baseclass = False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1113
        if parent is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1114
            classname = "%s_%s" % (parent, name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1115
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1116
            classname = name
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1117
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1118
        # Checks that classe haven't been generated yet
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1119
        if self.AlreadyComputed.get(classname, False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1120
            if baseclass:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1121
                self.AlreadyComputed[classname].IsBaseClass = baseclass
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1122
            return self.ComputedClassesInfos.get(classname, None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1123
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1124
        # If base classes haven't been generated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1125
        bases = []
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1126
        base_infos = classinfos.get("base", None)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1127
        if base_infos is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1128
            result = self.ExtractTypeInfos("base", name, base_infos)
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1129
            if result is None:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1130
                namespace, base_name = DecomposeQualifiedName(base_infos)                
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1131
                if self.AlreadyComputed.get(base_name, False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1132
                    self.ComputeAfter.append((name, parent, classinfos))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1133
                    if self.TargetNamespace is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1134
                        return "%s:%s" % (self.TargetNamespace, classname)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1135
                    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1136
                        return classname
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1137
            elif result is not None:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1138
                if self.FileName is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1139
                    classinfos["base"] = self.ComputedClasses[self.FileName].get(result["name"], None)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1140
                    if classinfos["base"] is None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1141
                        for filename, classes in self.ComputedClasses.iteritems():
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1142
                            if filename != self.FileName:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1143
                                classinfos["base"] = classes.get(result["name"], None)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1144
                                if classinfos["base"] is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1145
                                    break
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1146
                else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1147
                    classinfos["base"] = self.ComputedClasses.get(result["name"], None)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1148
                if classinfos["base"] is None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1149
                    raise ValueError("No class found for base type")
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1150
                bases.append(classinfos["base"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1151
        bases.append(object)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1152
        bases = tuple(bases)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1153
        classmembers = {"__doc__": classinfos.get("doc", ""), "IsBaseClass": baseclass}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1154
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1155
        self.AlreadyComputed[classname] = True
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1156
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1157
        for attribute in classinfos["attributes"]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1158
            infos = self.ExtractTypeInfos(attribute["name"], name, attribute["attr_type"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1159
            if infos is not None:                    
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1160
                if infos["type"] != SIMPLETYPE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1161
                    raise ValueError("\"%s\" type is not a simple type!" % attribute["attr_type"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1162
                attrname = attribute["name"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1163
                if attribute["use"] == "optional":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1164
                    classmembers[attrname] = None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1165
                    classmembers["add%s"%attrname] = generateAddMethod(attrname, self, attribute)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1166
                    classmembers["delete%s"%attrname] = generateDeleteMethod(attrname)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1167
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1168
                    classmembers[attrname] = infos["initial"]()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1169
                classmembers["set%s"%attrname] = generateSetMethod(attrname)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1170
                classmembers["get%s"%attrname] = generateGetMethod(attrname)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1171
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1172
                raise ValueError("\"%s\" type unrecognized!" % attribute["attr_type"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1173
            attribute["attr_type"] = infos
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1174
        
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1175
        for element in classinfos["elements"]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1176
            if element["type"] == CHOICE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1177
                elmtname = element["name"]
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1178
                choices = ComputeContentChoices(self, name, element)
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1179
                classmembers["get%schoices"%elmtname] = generateGetChoicesMethod(element["choices"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1180
                if element["maxOccurs"] == "unbounded" or element["maxOccurs"] > 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1181
                    classmembers["append%sbytype" % elmtname] = generateAppendChoiceByTypeMethod(element["maxOccurs"], self, element["choices"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1182
                    classmembers["insert%sbytype" % elmtname] = generateInsertChoiceByTypeMethod(element["maxOccurs"], self, element["choices"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1183
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1184
                    classmembers["set%sbytype" % elmtname] = generateSetChoiceByTypeMethod(self, element["choices"])
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1185
                infos = GenerateContentInfos(self, name, choices)
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1186
            elif element["type"] == ANY:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1187
                elmtname = element["name"] = "text"
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1188
                element["minOccurs"] = element["maxOccurs"] = 1
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1189
                infos = GenerateAnyInfos(element)
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1190
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1191
                elmtname = element["name"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1192
                infos = self.ExtractTypeInfos(element["name"], name, element["elmt_type"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1193
            if infos is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1194
                element["elmt_type"] = infos
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1195
            if element["maxOccurs"] == "unbounded" or element["maxOccurs"] > 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1196
                classmembers[elmtname] = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1197
                classmembers["append%s" % elmtname] = generateAppendMethod(elmtname, element["maxOccurs"], self, element)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1198
                classmembers["insert%s" % elmtname] = generateInsertMethod(elmtname, element["maxOccurs"], self, element)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1199
                classmembers["remove%s" % elmtname] = generateRemoveMethod(elmtname, element["minOccurs"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1200
                classmembers["count%s" % elmtname] = generateCountMethod(elmtname)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1201
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1202
                if element["minOccurs"] == 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1203
                    classmembers[elmtname] = None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1204
                    classmembers["add%s" % elmtname] = generateAddMethod(elmtname, self, element)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1205
                    classmembers["delete%s" % elmtname] = generateDeleteMethod(elmtname)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1206
                elif not isinstance(element["elmt_type"], (UnicodeType, StringType)):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1207
                    classmembers[elmtname] = element["elmt_type"]["initial"]()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1208
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1209
                    classmembers[elmtname] = None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1210
            classmembers["set%s" % elmtname] = generateSetMethod(elmtname)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1211
            classmembers["get%s" % elmtname] = generateGetMethod(elmtname)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1212
            
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1213
        classmembers["__init__"] = generateInitMethod(self, classinfos)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1214
        classmembers["__setattr__"] = generateSetattrMethod(self, classinfos)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1215
        classmembers["getStructure"] = generateStructureMethod(classinfos)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1216
        classmembers["loadXMLTree"] = generateLoadXMLTree(self, classinfos)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1217
        classmembers["generateXMLText"] = generateGenerateXMLText(self, classinfos)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1218
        classmembers["getElementAttributes"] = generateGetElementAttributes(self, classinfos)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1219
        classmembers["getElementInfos"] = generateGetElementInfos(self, classinfos)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1220
        classmembers["setElementValue"] = generateSetElementValue(self, classinfos)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1221
        classmembers["singleLineAttributes"] = True
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1222
        classmembers["compatibility"] = lambda x, y: None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1223
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1224
        class_definition = classobj(str(classname), bases, classmembers)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1225
        class_infos = {"type": COMPILEDCOMPLEXTYPE,
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1226
                "name": classname,
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1227
                "check": generateClassCheckFunction(class_definition),
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1228
                "initial": generateClassCreateFunction(class_definition),
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1229
                "extract": generateClassExtractFunction(class_definition),
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1230
                "generate": class_definition.generateXMLText}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1231
        
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1232
        if self.FileName is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1233
            self.ComputedClasses[self.FileName][classname] = class_definition
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1234
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1235
            self.ComputedClasses[classname] = class_definition
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1236
        self.ComputedClassesInfos[classname] = class_infos
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1237
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1238
        return class_infos
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1239
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1240
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1241
    Methods that print the classes generated
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1242
    """
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1243
    def PrintClasses(self):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1244
        items = self.ComputedClasses.items()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1245
        items.sort()
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1246
        if self.FileName is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1247
            for filename, classes in items:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1248
                print "File '%s':" % filename
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1249
                class_items = classes.items()
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1250
                class_items.sort()
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1251
                for classname, xmlclass in class_items:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1252
                    print "%s: %s" % (classname, str(xmlclass))
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1253
        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1254
            for classname, xmlclass in items:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1255
                print "%s: %s" % (classname, str(xmlclass))
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1256
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1257
    def PrintClassNames(self):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1258
        classnames = self.XMLClassDefinitions.keys()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1259
        classnames.sort()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1260
        for classname in classnames:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1261
            print classname
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1262
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1263
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1264
Method that generate the method for checking a class instance
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1265
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1266
def generateClassCheckFunction(class_definition):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1267
    def classCheckfunction(instance):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1268
        return isinstance(instance, class_definition)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1269
    return classCheckfunction
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1270
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1271
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1272
Method that generate the method for creating a class instance
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1273
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1274
def generateClassCreateFunction(class_definition):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1275
    def classCreatefunction():
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1276
        return class_definition()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1277
    return classCreatefunction
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1278
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1279
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1280
Method that generate the method for extracting a class instance
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1281
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1282
def generateClassExtractFunction(class_definition):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1283
    def classExtractfunction(node):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1284
        instance = class_definition()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1285
        instance.loadXMLTree(node)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1286
        return instance
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1287
    return classExtractfunction
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1288
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1289
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1290
Method that generate the method for loading an xml tree by following the
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1291
attributes list defined
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1292
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1293
def generateSetattrMethod(factory, classinfos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1294
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1295
    optional_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "optional"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1296
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1297
    
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1298
    def setattrMethod(self, name, value):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1299
        if attributes.has_key(name):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1300
            attributes[name]["attr_type"] = FindTypeInfos(factory, attributes[name]["attr_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1301
            if value is None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1302
                if optional_attributes.get(name, False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1303
                    return object.__setattr__(self, name, None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1304
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1305
                    raise ValueError("Attribute '%s' isn't optional." % name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1306
            elif attributes[name].has_key("fixed") and value != attributes[name]["fixed"]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1307
                raise ValueError, "Value of attribute '%s' can only be '%s'."%(name, str(attributes[name]["fixed"]))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1308
            elif attributes[name]["attr_type"]["check"](value):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1309
                return object.__setattr__(self, name, value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1310
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1311
                raise ValueError("Invalid value for attribute '%s'." % (name))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1312
        elif elements.has_key(name):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1313
            if CheckElementValue(factory, name, elements[name], value):
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1314
                return object.__setattr__(self, name, value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1315
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1316
                raise ValueError("Invalid value for attribute '%s'." % (name))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1317
        elif classinfos.has_key("base"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1318
            return classinfos["base"].__setattr__(self, name, value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1319
        elif self.__class__.__dict__.has_key(name):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1320
            return object.__setattr__(self, name, value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1321
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1322
            raise AttributeError("'%s' can't have an attribute '%s'." % (self.__class__.__name__, name))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1323
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1324
    return setattrMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1325
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1326
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1327
Method that generate the method for generating the xml tree structure model by 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1328
following the attributes list defined
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1329
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1330
def ComputeMultiplicity(name, infos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1331
    if infos["minOccurs"] == 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1332
        if infos["maxOccurs"] == "unbounded":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1333
            return "(?:%s)*" % name
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1334
        elif infos["maxOccurs"] == 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1335
            return "(?:%s)?" % name
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1336
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1337
            return "(?:%s){,%d}" % (name, infos["maxOccurs"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1338
    elif infos["minOccurs"] == 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1339
        if infos["maxOccurs"] == "unbounded":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1340
            return "(?:%s)+" % name
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1341
        elif infos["maxOccurs"] == 1:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1342
            return "(?:%s)" % name
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1343
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1344
            return "(?:%s){1,%d}" % (name, infos["maxOccurs"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1345
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1346
        if infos["maxOccurs"] == "unbounded":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1347
            return "(?:%s){%d,}" % (name, infos["minOccurs"], name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1348
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1349
            return "(?:%s){%d,%d}" % (name, infos["minOccurs"], 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1350
                                       infos["maxOccurs"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1351
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1352
def GetStructure(classinfos):
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1353
    elements = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1354
    for element in classinfos["elements"]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1355
        if element["type"] == ANY:
593
34c3569042db Fixing bug in xmlclass with empty CDATA. Empty CDATA aren't present in xml tree and prompt error when trying to load empty ANY tag using CDATA
laurent
parents: 592
diff changeset
  1356
            infos = element.copy()
34c3569042db Fixing bug in xmlclass with empty CDATA. Empty CDATA aren't present in xml tree and prompt error when trying to load empty ANY tag using CDATA
laurent
parents: 592
diff changeset
  1357
            infos["minOccurs"] = 0
34c3569042db Fixing bug in xmlclass with empty CDATA. Empty CDATA aren't present in xml tree and prompt error when trying to load empty ANY tag using CDATA
laurent
parents: 592
diff changeset
  1358
            elements.append(ComputeMultiplicity("#cdata-section |\w* ", infos))
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1359
        elif element["type"] == CHOICE:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1360
            choices = []
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1361
            for infos in element["choices"]:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1362
                if infos["type"] == "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1363
                    structure = "(?:%s)" % GetStructure(infos)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1364
                else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1365
                    structure = "%s " % infos["name"]
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1366
                choices.append(ComputeMultiplicity(structure, infos))
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1367
            elements.append(ComputeMultiplicity("|".join(choices), element))
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1368
        elif element["name"] == "content" and element["elmt_type"]["type"] == SIMPLETYPE:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1369
            elements.append("(?:#cdata-section )?")
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1370
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1371
            elements.append(ComputeMultiplicity("%s " % element["name"], element))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1372
    if classinfos.get("order", True) or len(elements) == 0:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1373
        return "".join(elements)
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1374
    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1375
        raise ValueError("XSD structure not yet supported!")
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1376
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1377
def generateStructureMethod(classinfos):
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1378
    def getStructureMethod(self):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1379
        structure = GetStructure(classinfos)
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1380
        if classinfos.has_key("base"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1381
            return classinfos["base"].getStructure(self) + structure
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1382
        return structure
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1383
    return getStructureMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1384
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1385
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1386
Method that generate the method for loading an xml tree by following the
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1387
attributes list defined
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1388
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1389
def generateLoadXMLTree(factory, classinfos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1390
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1391
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1392
    
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1393
    def loadXMLTreeMethod(self, tree, extras=[], derived=False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1394
        self.compatibility(tree)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1395
        if not derived:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1396
            children_structure = ""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1397
            for node in tree.childNodes:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1398
                if node.nodeName not in ["#comment", "#text"]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1399
                    children_structure += "%s " % node.nodeName
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1400
            structure_pattern = self.getStructure()
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1401
            if structure_pattern != "":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1402
                structure_model = re.compile("(%s)$" % structure_pattern)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1403
                result = structure_model.match(children_structure)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1404
                if not result:
593
34c3569042db Fixing bug in xmlclass with empty CDATA. Empty CDATA aren't present in xml tree and prompt error when trying to load empty ANY tag using CDATA
laurent
parents: 592
diff changeset
  1405
                    print structure_model.pattern, children_structure
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1406
                    raise ValueError("Invalid structure for \"%s\" children!." % tree.nodeName)
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1407
        required_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "required"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1408
        if classinfos.has_key("base"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1409
            extras.extend([attr["name"] for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1410
            classinfos["base"].loadXMLTree(self, tree, extras, True)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1411
        for attrname, attr in tree._attrs.iteritems():
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1412
            if attributes.has_key(attrname):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1413
                attributes[attrname]["attr_type"] = FindTypeInfos(factory, attributes[attrname]["attr_type"])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1414
                object.__setattr__(self, attrname, attributes[attrname]["attr_type"]["extract"](attr))
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1415
            elif not classinfos.has_key("base") and attrname not in extras:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1416
                raise ValueError("Invalid attribute \"%s\" for \"%s\" element!" % (attrname, tree.nodeName))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1417
            required_attributes.pop(attrname, None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1418
        if len(required_attributes) > 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1419
            raise ValueError("Required attributes %s missing for \"%s\" element!" % (", ".join(["\"%s\""%name for name in required_attributes]), tree.nodeName))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1420
        first = {}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1421
        for node in tree.childNodes:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1422
            name = node.nodeName
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1423
            if name in ["#text", "#comment"]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1424
                continue
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1425
            elif elements.has_key(name):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1426
                elements[name]["elmt_type"] = FindTypeInfos(factory, elements[name]["elmt_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1427
                if elements[name]["maxOccurs"] == "unbounded" or elements[name]["maxOccurs"] > 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1428
                    if first.get(name, True):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1429
                        object.__setattr__(self, name, [elements[name]["elmt_type"]["extract"](node)])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1430
                        first[name] = False
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1431
                    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1432
                        getattr(self, name).append(elements[name]["elmt_type"]["extract"](node))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1433
                else:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1434
                    object.__setattr__(self, name, elements[name]["elmt_type"]["extract"](node))
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1435
            elif elements.has_key("text"):
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1436
                if elements["text"]["maxOccurs"] == "unbounded" or elements["text"]["maxOccurs"] > 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1437
                    if first.get("text", True):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1438
                        object.__setattr__(self, "text", [elements["text"]["elmt_type"]["extract"](node)])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1439
                        first["text"] = False
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1440
                    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1441
                        getattr(self, "text").append(elements["text"]["elmt_type"]["extract"](node))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1442
                else:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1443
                    object.__setattr__(self, "text", elements["text"]["elmt_type"]["extract"](node))
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1444
            elif elements.has_key("content"):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1445
                if name == "#cdata-section":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1446
                    if elements["content"]["elmt_type"]["type"] == SIMPLETYPE:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1447
                        object.__setattr__(self, "content", elements["content"]["elmt_type"]["extract"](node.data, False))
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1448
                else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1449
                    content = getattr(self, "content")
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1450
                    if elements["content"]["maxOccurs"] == "unbounded" or elements["content"]["maxOccurs"] > 1:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1451
                        if first.get("content", True):
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1452
                            object.__setattr__(self, "content", [elements["content"]["elmt_type"]["extract"](node, None)])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1453
                            first["content"] = False
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1454
                        else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1455
                            content.append(elements["content"]["elmt_type"]["extract"](node, content))
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1456
                    else:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1457
                        object.__setattr__(self, "content", elements["content"]["elmt_type"]["extract"](node, content))
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1458
    return loadXMLTreeMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1459
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1460
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1461
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1462
Method that generates the method for generating an xml text by following the
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1463
attributes list defined
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1464
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1465
def generateGenerateXMLText(factory, classinfos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1466
    def generateXMLTextMethod(self, name, indent=0, extras={}, derived=False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1467
        ind1, ind2 = getIndent(indent, name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1468
        if not derived:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1469
            text = ind1 + u'<%s' % name
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1470
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1471
            text = u''
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1472
        
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1473
        first = True
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1474
        if not classinfos.has_key("base"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1475
            for attr, value in extras.items():
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1476
                if not first and not self.singleLineAttributes:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1477
                    text += u'\n%s' % (ind2)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1478
                text += u' %s=%s' % (attr, quoteattr(value))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1479
                first = False
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1480
            extras.clear()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1481
        for attr in classinfos["attributes"]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1482
            if attr["use"] != "prohibited":
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1483
                attr["attr_type"] = FindTypeInfos(factory, attr["attr_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1484
                value = getattr(self, attr["name"], None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1485
                if value != None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1486
                    computed_value = attr["attr_type"]["generate"](value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1487
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1488
                    computed_value = None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1489
                if attr["use"] != "optional" or (value != None and \
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1490
                   computed_value != attr.get("default", attr["attr_type"]["generate"](attr["attr_type"]["initial"]()))):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1491
                    if classinfos.has_key("base"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1492
                        extras[attr["name"]] = computed_value
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1493
                    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1494
                        if not first and not self.singleLineAttributes:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1495
                            text += u'\n%s' % (ind2)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1496
                        text += ' %s=%s' % (attr["name"], quoteattr(computed_value))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1497
                    first = False
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1498
        if classinfos.has_key("base"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1499
            first, new_text = classinfos["base"].generateXMLText(self, name, indent, extras, True)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1500
            text += new_text
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1501
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1502
            first = True
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1503
        for element in classinfos["elements"]:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1504
            element["elmt_type"] = FindTypeInfos(factory, element["elmt_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1505
            value = getattr(self, element["name"], None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1506
            if element["minOccurs"] == 0 and element["maxOccurs"] == 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1507
                if value is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1508
                    if first:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1509
                        text += u'>\n'
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1510
                        first = False
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1511
                    text += element["elmt_type"]["generate"](value, element["name"], indent + 1)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1512
            elif element["minOccurs"] == 1 and element["maxOccurs"] == 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1513
                if first:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1514
                    text += u'>\n'
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1515
                    first = False
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1516
                if element["name"] == "content" and element["elmt_type"]["type"] == SIMPLETYPE:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1517
                    text += element["elmt_type"]["generate"](value)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1518
                else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1519
                    text += element["elmt_type"]["generate"](value, element["name"], indent + 1)
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1520
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1521
                if first and len(value) > 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1522
                    text += u'>\n'
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1523
                    first = False
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1524
                for item in value:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1525
                    text += element["elmt_type"]["generate"](item, element["name"], indent + 1)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1526
        if not derived:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1527
            if first:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1528
                text += u'/>\n'
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1529
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1530
                text += ind1 + u'</%s>\n' % (name)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1531
            return text
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1532
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1533
            return first, text
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1534
    return generateXMLTextMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1535
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1536
def gettypeinfos(name, facets):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1537
    if facets.has_key("enumeration") and facets["enumeration"][0] is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1538
        return facets["enumeration"][0]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1539
    elif facets.has_key("maxInclusive"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1540
        limits = {"max" : None, "min" : None}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1541
        if facets["maxInclusive"][0] is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1542
            limits["max"] = facets["maxInclusive"][0]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1543
        elif facets["maxExclusive"][0] is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1544
            limits["max"] = facets["maxExclusive"][0] - 1
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1545
        if facets["minInclusive"][0] is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1546
            limits["min"] = facets["minInclusive"][0]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1547
        elif facets["minExclusive"][0] is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1548
            limits["min"] = facets["minExclusive"][0] + 1
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1549
        if limits["max"] is not None or limits["min"] is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1550
            return limits
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1551
    return name
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1552
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1553
def generateGetElementAttributes(factory, classinfos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1554
    def getElementAttributes(self):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1555
        attr_list = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1556
        if classinfos.has_key("base"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1557
            attr_list.extend(classinfos["base"].getElementAttributes(self))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1558
        for attr in classinfos["attributes"]:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1559
            if attr["use"] != "prohibited":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1560
                attr_params = {"name" : attr["name"], "use" : attr["use"], 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1561
                    "type" : gettypeinfos(attr["attr_type"]["basename"], attr["attr_type"]["facets"]),
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1562
                    "value" : getattr(self, attr["name"], "")}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1563
                attr_list.append(attr_params)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1564
        return attr_list
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1565
    return getElementAttributes
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1566
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1567
def generateGetElementInfos(factory, classinfos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1568
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1569
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1570
    
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1571
    def getElementInfos(self, name, path=None, derived=False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1572
        attr_type = "element"
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1573
        value = None
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1574
        use = "required"
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1575
        children = []
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1576
        if path is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1577
            parts = path.split(".", 1)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1578
            if attributes.has_key(parts[0]):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1579
                if len(parts) != 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1580
                    raise ValueError("Wrong path!")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1581
                attr_type = gettypeinfos(attributes[parts[0]]["attr_type"]["basename"], 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1582
                                         attributes[parts[0]]["attr_type"]["facets"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1583
                value = getattr(self, parts[0], "")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1584
            elif elements.has_key(parts[0]):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1585
                if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1586
                    if len(parts) != 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1587
                        raise ValueError("Wrong path!")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1588
                    attr_type = gettypeinfos(elements[parts[0]]["elmt_type"]["basename"], 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1589
                                             elements[parts[0]]["elmt_type"]["facets"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1590
                    value = getattr(self, parts[0], "")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1591
                elif parts[0] == "content":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1592
                    return self.content["value"].getElementInfos(self.content["name"], path)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1593
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1594
                    attr = getattr(self, parts[0], None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1595
                    if attr is None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1596
                        raise ValueError("Wrong path!")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1597
                    if len(parts) == 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1598
                        return attr.getElementInfos(parts[0])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1599
                    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1600
                        return attr.getElementInfos(parts[0], parts[1])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1601
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1602
                raise ValueError("Wrong path!")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1603
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1604
            if not derived:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1605
                children.extend(self.getElementAttributes())
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1606
            if classinfos.has_key("base"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1607
                children.extend(classinfos["base"].getElementInfos(self, name, derived=True)["children"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1608
            for element_name, element in elements.items():
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1609
                if element["minOccurs"] == 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1610
                    use = "optional"
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1611
                if element_name == "content" and element["type"] == CHOICE:
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1612
                    attr_type = [(choice["name"], None) for choice in element["choices"]]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1613
                    if self.content is None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1614
                        value = ""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1615
                    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1616
                        value = self.content["name"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1617
                        if self.content["value"] is not None:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1618
                            if self.content["name"] == "sequence":
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1619
                                choices_dict = dict([(choice["name"], choice) for choice in element["choices"]])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1620
                                sequence_infos = choices_dict.get("sequence", None)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1621
                                if sequence_infos is not None:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1622
                                    children.extend([item.getElementInfos(infos["name"]) for item, infos in zip(self.content["value"], sequence_infos["elements"])])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1623
                            else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1624
                                children.extend(self.content["value"].getElementInfos(self.content["name"])["children"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1625
                elif element["elmt_type"]["type"] == SIMPLETYPE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1626
                    children.append({"name": element_name, "require": element["minOccurs"] != 0, 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1627
                        "type": gettypeinfos(element["elmt_type"]["basename"], 
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1628
                                              element["elmt_type"]["facets"]),
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1629
                        "value": getattr(self, element_name, None)})
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1630
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1631
                    instance = getattr(self, element_name, None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1632
                    if instance is None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1633
                        instance = element["elmt_type"]["initial"]()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1634
                    children.append(instance.getElementInfos(element_name))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1635
        return {"name": name, "type": attr_type, "value": value, "use": use, "children": children}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1636
    return getElementInfos
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1637
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1638
def generateSetElementValue(factory, classinfos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1639
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1640
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1641
    
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1642
    def setElementValue(self, path, value):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1643
        if path is not None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1644
            parts = path.split(".", 1)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1645
            if attributes.has_key(parts[0]):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1646
                if len(parts) != 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1647
                    raise ValueError("Wrong path!")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1648
                if attributes[parts[0]]["attr_type"]["basename"] == "boolean":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1649
                    setattr(self, parts[0], value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1650
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1651
                    setattr(self, parts[0], attributes[parts[0]]["attr_type"]["extract"](value, False))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1652
            elif elements.has_key(parts[0]):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1653
                if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1654
                    if len(parts) != 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1655
                        raise ValueError("Wrong path!")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1656
                    if elements[parts[0]]["elmt_type"]["basename"] == "boolean":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1657
                        setattr(self, parts[0], value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1658
                    else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1659
                        setattr(self, parts[0], elements[parts[0]]["elmt_type"]["extract"](value, False))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1660
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1661
                    instance = getattr(self, parts[0], None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1662
                    if instance != None:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1663
                        if len(parts) > 1:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1664
                            instance.setElementValue(parts[1], value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1665
                        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1666
                            instance.setElementValue(None, value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1667
            elif elements.has_key("content"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1668
                if len(parts) > 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1669
                    self.content["value"].setElementValue(path, value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1670
            elif classinfos.has_key("base"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1671
                classinfos["base"].setElementValue(self, path, value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1672
        elif elements.has_key("content"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1673
            if value == "":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1674
                if elements["content"]["minOccurs"] == 0:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1675
                    self.setcontent(None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1676
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1677
                    raise ValueError("\"content\" element is required!")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1678
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1679
                self.setcontentbytype(value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1680
    return setElementValue
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1681
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1682
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1683
Methods that generates the different methods for setting and getting the attributes
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1684
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1685
def generateInitMethod(factory, classinfos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1686
    def initMethod(self):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1687
        if classinfos.has_key("base"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1688
            classinfos["base"].__init__(self)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1689
        for attribute in classinfos["attributes"]:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1690
            attribute["attr_type"] = FindTypeInfos(factory, attribute["attr_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1691
            if attribute["use"] == "required":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1692
                setattr(self, attribute["name"], attribute["attr_type"]["initial"]())
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1693
            elif attribute["use"] == "optional":
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1694
                if attribute.has_key("default"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1695
                    setattr(self, attribute["name"], attribute["attr_type"]["extract"](attribute["default"], False))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1696
                else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1697
                    setattr(self, attribute["name"], None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1698
        for element in classinfos["elements"]:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1699
            setattr(self, element["name"], GetElementInitialValue(factory, element))
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1700
    return initMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1701
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1702
def generateSetMethod(attr):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1703
    def setMethod(self, value):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1704
        setattr(self, attr, value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1705
    return setMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1706
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1707
def generateGetMethod(attr):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1708
    def getMethod(self):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1709
        return getattr(self, attr, None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1710
    return getMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1711
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1712
def generateAddMethod(attr, factory, infos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1713
    def addMethod(self):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1714
        if infos["type"] == ATTRIBUTE:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1715
            infos["attr_type"] = FindTypeInfos(factory, infos["attr_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1716
            initial = infos["attr_type"]["initial"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1717
            extract = infos["attr_type"]["extract"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1718
        elif infos["type"] == ELEMENT:
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1719
            infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1720
            initial = infos["elmt_type"]["initial"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1721
            extract = infos["elmt_type"]["extract"]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1722
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1723
            raise ValueError("Invalid class attribute!")
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1724
        if infos.has_key("default"):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1725
            setattr(self, attr, extract(infos["default"], False))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1726
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1727
            setattr(self, attr, initial())
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1728
    return addMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1729
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1730
def generateDeleteMethod(attr):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1731
    def deleteMethod(self):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1732
        setattr(self, attr, None)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1733
    return deleteMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1734
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1735
def generateAppendMethod(attr, maxOccurs, factory, infos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1736
    def appendMethod(self, value):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1737
        infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1738
        attr_list = getattr(self, attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1739
        if maxOccurs == "unbounded" or len(attr_list) < maxOccurs:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1740
            if infos["elmt_type"]["check"](value):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1741
                attr_list.append(value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1742
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1743
                raise ValueError("\"%s\" value isn't valid!" % attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1744
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1745
            raise ValueError("There can't be more than %d values in \"%s\"!" % (maxOccurs, attr))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1746
    return appendMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1747
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1748
def generateInsertMethod(attr, maxOccurs, factory, infos):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1749
    def insertMethod(self, index, value):
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1750
        infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1751
        attr_list = getattr(self, attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1752
        if maxOccurs == "unbounded" or len(attr_list) < maxOccurs:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1753
            if infos["elmt_type"]["check"](value):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1754
                attr_list.insert(index, value)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1755
            else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1756
                raise ValueError("\"%s\" value isn't valid!" % attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1757
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1758
            raise ValueError("There can't be more than %d values in \"%s\"!" % (maxOccurs, attr))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1759
    return insertMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1760
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1761
def generateGetChoicesMethod(choice_types):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1762
    def getChoicesMethod(self):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1763
        return [choice["name"] for choice in choice_types]
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1764
    return getChoicesMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1765
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1766
def generateSetChoiceByTypeMethod(factory, choice_types):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1767
    choices = dict([(choice["name"], choice) for choice in choice_types])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1768
    def setChoiceMethod(self, type):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1769
        if not choices.has_key(type):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1770
            raise ValueError("Unknown \"%s\" choice type for \"content\"!" % type)
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1771
        choices[type]["elmt_type"] = FindTypeInfos(factory, choices[type]["elmt_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1772
        new_element = choices[type]["elmt_type"]["initial"]()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1773
        self.content = {"name": type, "value": new_element}
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1774
        return new_element
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1775
    return setChoiceMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1776
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1777
def generateAppendChoiceByTypeMethod(maxOccurs, factory, choice_types):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1778
    choices = dict([(choice["name"], choice) for choice in choice_types])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1779
    def appendChoiceMethod(self, type):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1780
        if not choices.has_key(type):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1781
            raise ValueError("Unknown \"%s\" choice type for \"content\"!" % type)
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1782
        choices[type]["elmt_type"] = FindTypeInfos(factory, choices[type]["elmt_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1783
        if maxOccurs == "unbounded" or len(self.content) < maxOccurs:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1784
            new_element = choices[type]["elmt_type"]["initial"]()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1785
            self.content.append({"name": type, "value": new_element})
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1786
            return new_element
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1787
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1788
            raise ValueError("There can't be more than %d values in \"content\"!" % maxOccurs)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1789
    return appendChoiceMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1790
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1791
def generateInsertChoiceByTypeMethod(maxOccurs, factory, choice_types):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1792
    choices = dict([(choice["name"], choice) for choice in choice_types])
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1793
    def insertChoiceMethod(self, index, type):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1794
        if not choices.has_key(type):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1795
            raise ValueError("Unknown \"%s\" choice type for \"content\"!" % type)
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1796
        choices[type]["elmt_type"] = FindTypeInfos(factory, choices[type]["elmt_type"])
565
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1797
        if maxOccurs == "unbounded" or len(self.content) < maxOccurs:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1798
            new_element = choices[type]["elmt_type"]["initial"]()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1799
            self.content.insert(index, {"name" : type, "value" : new_element})
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1800
            return new_element
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1801
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1802
            raise ValueError("There can't be more than %d values in \"content\"!" % maxOccurs)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1803
    return insertChoiceMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1804
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1805
def generateRemoveMethod(attr, minOccurs):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1806
    def removeMethod(self, index):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1807
        attr_list = getattr(self, attr)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1808
        if len(attr_list) > minOccurs:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1809
            getattr(self, attr).pop(index)
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1810
        else:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1811
            raise ValueError("There can't be less than %d values in \"%s\"!" % (minOccurs, attr))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1812
    return removeMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1813
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1814
def generateCountMethod(attr):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1815
    def countMethod(self):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1816
        return len(getattr(self, attr))
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1817
    return countMethod
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1818
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1819
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1820
This function generate the classes from a class factory
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1821
"""
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1822
def GenerateClasses(factory, declare=False):
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1823
    ComputedClasses = factory.CreateClasses()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1824
    #factory.PrintClasses()
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1825
    if declare:
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1826
        for ClassName, Class in pluginClasses.items():
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1827
            sys._getframe(1).f_locals[ClassName] = Class
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1828
        for TypeName, Type in pluginTypes.items():
94c11207aa6f Moving xmlclass and docutils into plcopeneditor
laurent
parents:
diff changeset
  1829
            sys._getframe(1).f_locals[TypeName] = Type
592
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1830
    if factory.FileName is not None and len(ComputedClasses) == 1:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1831
        globals().update(ComputedClasses[factory.FileName])
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1832
        return ComputedClasses[factory.FileName]
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1833
    else:
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1834
        globals().update(ComputedClasses)
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1835
        return ComputedClasses
89ff2738ef20 Adding support in xmlclass for handling some not yet supported XML syntaxes
laurent
parents: 565
diff changeset
  1836