summaryrefslogtreecommitdiff
path: root/cpp/include/XMLTransform/XMLTransform.h
blob: 80da8a5d91ba051db872bfe5244dc594dac5bf52 (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
251
252
253
254
255
256
257
258
259
260
261
// **********************************************************************
//
// Copyright (c) 2002
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#ifndef XML_TRANSFORM_H
#define XML_TRANSFORM_H

#include <Freeze/DB.h>
#include <IceUtil/OutputUtil.h>

#include <xercesc/dom/DOM.hpp>

#include <map>

#ifdef WIN32
#   ifdef XML_TRANSFORM_API_EXPORTS
#       define XML_TRANSFORM_API __declspec(dllexport)
#   else
#       define XML_TRANSFORM_API __declspec(dllimport)
#   endif
#else
#   define XML_TRANSFORM_API /**/
#endif

namespace XMLTransform
{

//
// Raised in the case that the transform is illegal.
//
class XML_TRANSFORM_API IllegalTransform : public ::IceUtil::Exception
{
public:

    IllegalTransform(const char*, int);

    virtual ::std::string ice_name() const;
    virtual void ice_print(::std::ostream&) const;
    virtual ::IceUtil::Exception* ice_clone() const;
    virtual void ice_throw() const;

    ::std::string reason;
};

//
// Raised in the case that the given schema is incompatible with the old schema.
//
class XML_TRANSFORM_API IncompatibleSchema : public ::IceUtil::Exception
{
public:

    IncompatibleSchema(const char*, int);

    virtual ::std::string ice_name() const;
    virtual void ice_print(::std::ostream&) const;
    virtual ::IceUtil::Exception* ice_clone() const;
    virtual void ice_throw() const;

    ::std::string reason;
};

//
// Raised in the case that the given schema is not an XML schema.
//
class XML_TRANSFORM_API InvalidSchema : public ::IceUtil::Exception
{
public:

    InvalidSchema(const char*, int);

    virtual ::std::string ice_name() const;
    virtual void ice_print(::std::ostream&) const;
    virtual ::IceUtil::Exception* ice_clone() const;
    virtual void ice_throw() const;

    ::std::string reason;
};

//
// Raised in the case that the given XML instance document violates the associated schema.
//
class XML_TRANSFORM_API SchemaViolation : public ::IceUtil::Exception
{
public:

    SchemaViolation(const char*, int);

    virtual ::std::string ice_name() const;
    virtual void ice_print(::std::ostream&) const;
    virtual ::IceUtil::Exception* ice_clone() const;
    virtual void ice_throw() const;

    ::std::string reason;
};

//
// Raised if a type was not found in the new schema.
//
class XML_TRANSFORM_API MissingTypeException : public ::IceUtil::Exception
{
public:

    MissingTypeException(const char*, int);

    virtual ::std::string ice_name() const;
    virtual void ice_print(::std::ostream&) const;
    virtual ::IceUtil::Exception* ice_clone() const;
    virtual void ice_throw() const;

    ::std::string reason;
};

//
// Raised in the case of a general transformation failure.
//
class XML_TRANSFORM_API TransformException : public ::IceUtil::Exception
{
public:

    TransformException(const char*, int);

    virtual ::std::string ice_name() const;
    virtual void ice_print(::std::ostream&) const;
    virtual ::IceUtil::Exception* ice_clone() const;
    virtual void ice_throw() const;

    ::std::string reason;
};

//
// Map from namespace prefix to namespace URI.
//
typedef ::std::map< ::std::string, ::std::string> PrefixURIMap;

//
// This represents the information associated with an XML document. Note that the namespace information is
// only retrieved from the top-level node (which matches the layout of XML schema documents).
//
class XML_TRANSFORM_API DocumentInfo : public ::IceUtil::Shared
{
public:

    DocumentInfo(DOMDocument*, bool, DOMNode*, const ::std::string& = "");
    ~DocumentInfo();

    DOMDocument* getDocument() const;
    ::std::string findURI(const ::std::string& prefix) const;
    ::std::string getTargetNamespace() const;

private:

    DOMDocument* _document;
    bool _releaseDocument;
    PrefixURIMap _nsMap;
    ::std::string _targetNamespace;
};

typedef ::IceUtil::Handle<DocumentInfo> DocumentInfoPtr;

//
// Transform interface.
//
class XML_TRANSFORM_API Transform : public ::IceUtil::Shared
{
public:

    Transform();
    virtual ~Transform();

    typedef ::std::map< ::std::string, ::std::string > MissingTypeMap;

    virtual void transform(::IceUtil::XMLOutput&, const DocumentInfoPtr&, const ::std::string&, DOMNode*,
                           const MissingTypeMap&) = 0;
    virtual void checkMissingTypes(const DocumentInfoPtr&, DOMNode*, const MissingTypeMap&) { }
    virtual void collectMissingTypes(const DocumentInfoPtr&, DOMNode*, MissingTypeMap&) { }
    virtual ::std::ostream& print(::std::ostream&) = 0;
};

typedef ::IceUtil::Handle<Transform> TransformPtr;

//
// Map of string to transform type.
//
typedef ::std::map< ::std::string, TransformPtr> TransformMap;

//
// Given an old and new XML schema, this class will transform an instance document that corresponds to the old
// schema formation into a document that matches the new schema.
//
class XML_TRANSFORM_API Transformer
{
public:

    Transformer(const Ice::StringSeq&, const Ice::StringSeq&, const Ice::StringSeq&, const Ice::StringSeq&,
                DOMDocument*, DOMDocument*);
    ~Transformer();

    void transform(::IceUtil::XMLOutput&, DOMDocument*, const std::string&, bool, bool = true);

private:

    //
    // Map of local@uri element names to transforms. Needed for actual
    // transform.
    //
    TransformMap _elements;

    //
    // Map of local@uri class transforms (based on static type). This
    // information cached for creation of the transform.
    //
    TransformMap _staticClassTransforms;
};

class XML_TRANSFORM_API DBTransformer
{
public:

    DBTransformer(const Freeze::DBEnvironmentPtr&, const Freeze::DBPtr&, const Ice::StringSeq&, const Ice::StringSeq&,
                  const Ice::StringSeq&, const Ice::StringSeq&, bool);
    ~DBTransformer();

    //
    // This version allows the caller to specify the filenames for the
    // old and new schemas.
    //
    void transform(const std::string&, const std::string&);

    //
    // This version allows the caller to specify a single schema for
    // transformation. This is commonly used for schemas whose formal
    // types don't change, but whose actual (instance) types do change.
    //
    void transform(const std::string&);

private:

    void transform(DOMDocument*, DOMDocument*);

    Freeze::DBEnvironmentPtr _dbEnv;
    Freeze::DBPtr _db;
    Ice::StringSeq _loadOld;
    Ice::StringSeq _loadNew;
    Ice::StringSeq _pathOld;
    Ice::StringSeq _pathNew;
    bool _force;
};

} // End namespace XMLTransform

#endif