summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/codegenerators/xmlschemawriter.h
blob: 18c668e5545d2ac12004def98c5a7b88a94458b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   copyright (C) 2003 Brian Thomas <brian.thomas@gsfc.nasa.gov>          *
 *   copyright (C) 2004-2007                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

#ifndef XMLSCHEMAWRITER_H
#define XMLSCHEMAWRITER_H

#include "../codegenerator.h"
#include "../attribute.h"
#include "../association.h"
#include "../umlclassifierlist.h"
#include "../umlattributelist.h"
#include "../umlobjectlist.h"
#include "../umlassociationlist.h"
#include "simplecodegenerator.h"

/**
  * Class XMLSchemaWriter is a code generator for UMLClassifier objects.
  * Create an instance of this class, and feed it a UMLClassifier when
  * calling writeClass and it will generate a XMLschema source file for
  * that concept
  *
  * Our basic approach is to map UMLClassifiers (classes/interfaces) into
  * XML elements (or nodes). We declare these element in the schema either
  * as complexType or as groups based on whether they are concrete or abstract
  * in nature. This is not a perfect decision, but thats life with XML Schema...
  * you cant fully represent Objects in the XML world ..yet. -b.t.
  */

class XMLSchemaWriter : public SimpleCodeGenerator
{
    Q_OBJECT
  
public:

    /**
     * Constructor, initialises a couple of variables
     */
    XMLSchemaWriter();

    /**
     * Destructor, empty
     */
    virtual ~XMLSchemaWriter();

    /**
     * call this method to generate XMLschema code for a UMLClassifier
     * @param c the class to generate code for
     */
    virtual void writeClass(UMLClassifier *c);

    /**
     * returns "XMLSchema"
     */
    virtual Uml::Programming_Language getLanguage();

    /**
     * get list of reserved keywords
     */
    virtual const TQStringList reservedKeywords() const;

private:

    /**
     * Writes concept's documentation then  guts
     */
    void writeClassifier(UMLClassifier *c, TQTextStream &XMLSchema);
    void writeAbstractClassifier(UMLClassifier *c, TQTextStream &XMLSchema);
    void writeConcreteClassifier(UMLClassifier *c, TQTextStream &XMLSchema);

    /**
     * write a <complexType> declaration for this classifier
     */
    void writeComplexTypeClassifierDecl(UMLClassifier *c,
                                        UMLAssociationList associations,
                                        UMLAssociationList aggregations,
                                        UMLAssociationList compositions,
                                        UMLClassifierList superclassifiers,
                                        TQTextStream &XMLSchema);

    /**
     * write a <group> declaration for this classifier. Used for interfaces to classes with
     * inheriting children.
     */
    void writeGroupClassifierDecl(UMLClassifier *c,
                                  UMLClassifierList superclassifiers,
                                  TQTextStream &XMLSchema);

    /**
     * find if the classifier would have any Child elements.
     */
    bool determineIfHasChildNodes( UMLClassifier *c);

    /**
     * write all attributes for a given class
     * @param c the class for which we are generating code
     * @param j the stream associated with the output file
     */
    void writeAttributes(UMLClassifier *c, TQTextStream &j);

    /**
     * write an element declaration.
     */
    void writeElementDecl( const TQString &elementName, const TQString &elementTypeName, TQTextStream &XMLschema);

    /**
     * writes the Attribute declarations
     * @param attribs List of attributes
     * @param XMLschema text stream
     */
    void writeAttributeDecls(UMLAttributeList &attribs, TQTextStream &XMLschema );

    /**
     * write an element attribute.
     */
    void writeAttributeDecl(UMLAttribute *attrib, TQTextStream &XMLschema );

    /**
     * Find all attributes for this concept.
     */
    UMLAttributeList findAttributes (UMLClassifier *c);

    /**
     * Discover the string name of all the attribute groups (which are child nodes)
     * of this concept (err.. element)
     */
    TQStringList findAttributeGroups (UMLClassifier *c);

    /**
     * Searches a list of associations for appropriate ones to write out as attributes.
     * This works well for compositions, aggregations and self-associations but will
     * not work right for plain associations between 2 different classes.
     */
    bool writeAssociationDecls(UMLAssociationList associations, bool noRoleOK, bool didOne,
                               Uml::IDType id, TQTextStream &XMLschema);

    /**
     * Find all attributes that  belong in group
     */
    void writeAttributeGroupDecl(const TQString &elementName, UMLAttributeList &attribs, TQTextStream &XMLschema );

    /**
     * Writes out an association as an attribute using Vector
     */
    void writeAssociationRoleDecl(UMLClassifier *c, const TQString &multi, TQTextStream &XMLschema);

    /**
     * Construct an element tag with the schema namespace
     */
    TQString makeSchemaTag ( TQString tagName );

    /**
     * Construct an element tag with the package namespace
     */
    TQString makePackageTag ( TQString tagName );

    /**
     * Writes a comment
     */
    void writeComment(const TQString &text, TQTextStream &XMLschema);

    /**
     * Find and return a list of child UMLObjects pointed to by the associations
     * in this UMLClassifier.
     */
    UMLObjectList findChildObjsInAssociations (UMLClassifier *c, UMLAssociationList associations);

    /**
     * Replaces `string' with `String' and `bool' with `boolean'
     */
    TQString fixTypeName(const TQString& string);

    /**
     * check that initial values of strings DONT have quotes around them
     * (we get double quoting then!!
     */
    TQString fixInitialStringDeclValue( TQString value, const TQString &type);

    /**
     * Find the element node name for this concept.
     */
    TQString getElementName(UMLClassifier *c);

    /**
     * Find the element node "type" name. Used in the "complexType" which
     * might define that element node.
     */
    TQString getElementTypeName(UMLClassifier *c);

    /**
     * Find the group node "type" name. Used for elements which define an interface/are abstract.
     */
    TQString getElementGroupTypeName(UMLClassifier *c);

    /**
     * Find all the child objects in this association and make sure they get
     * written out (if they havent already been)
     */
    void writeChildObjsInAssociation (UMLClassifier *c, UMLAssociationList assoc, TQTextStream &s);

    /**
     * Quick check to see if we have written the declaration for this concept yet.
     */
    bool hasBeenWritten(UMLClassifier *c);

    /**
     * mark a concept as written, so it is not repeatedly re-declared in the schema
     */
    void markAsWritten(UMLClassifier *c);

    /**
     * The basic schemaNamespace tag
     */
    TQString schemaNamespaceTag;

    /**
     * The basic schemaNamespace tag
     */
    TQString packageNamespaceTag;

    /*
     * The basic schemaNamespace URI
     */
    TQString schemaNamespaceURI;

    /**
     * The basic schemaNamespace URI
     */
    TQString packageNamespaceURI;

    /**
     * A \n, used at the end of each line
     */
    TQString startline;

    /**
     * a list of UMLClassifiers we have already written
     */
    UMLClassifierList writtenClassifiers;
};

#endif // XMLSCHEMAWRITER_H