1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qxsdschemamerger_p.h"
41 
42 QT_BEGIN_NAMESPACE
43 
44 using namespace QPatternist;
45 
XsdSchemaMerger(const XsdSchema::Ptr & schema,const XsdSchema::Ptr & otherSchema)46 XsdSchemaMerger::XsdSchemaMerger(const XsdSchema::Ptr &schema, const XsdSchema::Ptr &otherSchema)
47 {
48     merge(schema, otherSchema);
49 }
50 
mergedSchema() const51 XsdSchema::Ptr XsdSchemaMerger::mergedSchema() const
52 {
53     return m_mergedSchema;
54 }
55 
merge(const XsdSchema::Ptr & schema,const XsdSchema::Ptr & otherSchema)56 void XsdSchemaMerger::merge(const XsdSchema::Ptr &schema, const XsdSchema::Ptr &otherSchema)
57 {
58     m_mergedSchema = XsdSchema::Ptr(new XsdSchema(otherSchema->namePool()));
59 
60     // first fill the merged schema with the values from schema
61     if (schema) {
62         const XsdElement::List elements = schema->elements();
63         for (int i = 0; i < elements.count(); ++i) {
64             m_mergedSchema->addElement(elements.at(i));
65         }
66 
67         const XsdAttribute::List attributes = schema->attributes();
68         for (int i = 0; i < attributes.count(); ++i) {
69             m_mergedSchema->addAttribute(attributes.at(i));
70         }
71 
72         const SchemaType::List types = schema->types();
73         for (int i = 0; i < types.count(); ++i) {
74             m_mergedSchema->addType(types.at(i));
75         }
76 
77         const SchemaType::List anonymousTypes = schema->anonymousTypes();
78         for (int i = 0; i < anonymousTypes.count(); ++i) {
79             m_mergedSchema->addAnonymousType(anonymousTypes.at(i));
80         }
81 
82         const XsdModelGroup::List elementGroups = schema->elementGroups();
83         for (int i = 0; i < elementGroups.count(); ++i) {
84             m_mergedSchema->addElementGroup(elementGroups.at(i));
85         }
86 
87         const XsdAttributeGroup::List attributeGroups = schema->attributeGroups();
88         for (int i = 0; i < attributeGroups.count(); ++i) {
89             m_mergedSchema->addAttributeGroup(attributeGroups.at(i));
90         }
91 
92         const XsdNotation::List notations = schema->notations();
93         for (int i = 0; i < notations.count(); ++i) {
94             m_mergedSchema->addNotation(notations.at(i));
95         }
96 
97         const XsdIdentityConstraint::List identityConstraints = schema->identityConstraints();
98         for (int i = 0; i < identityConstraints.count(); ++i) {
99             m_mergedSchema->addIdentityConstraint(identityConstraints.at(i));
100         }
101     }
102 
103     // then merge in the values from the otherSchema
104     {
105         const XsdElement::List elements = otherSchema->elements();
106         for (int i = 0; i < elements.count(); ++i) {
107             if (!m_mergedSchema->element(elements.at(i)->name(otherSchema->namePool())))
108                 m_mergedSchema->addElement(elements.at(i));
109         }
110 
111         const XsdAttribute::List attributes = otherSchema->attributes();
112         for (int i = 0; i < attributes.count(); ++i) {
113             if (!m_mergedSchema->attribute(attributes.at(i)->name(otherSchema->namePool())))
114                 m_mergedSchema->addAttribute(attributes.at(i));
115         }
116 
117         const SchemaType::List types = otherSchema->types();
118         for (int i = 0; i < types.count(); ++i) {
119             if (!m_mergedSchema->type(types.at(i)->name(otherSchema->namePool())))
120                 m_mergedSchema->addType(types.at(i));
121         }
122 
123         const SchemaType::List anonymousTypes = otherSchema->anonymousTypes();
124         for (int i = 0; i < anonymousTypes.count(); ++i) {
125             // add anonymous type as they are
126             m_mergedSchema->addAnonymousType(anonymousTypes.at(i));
127         }
128 
129         const XsdModelGroup::List elementGroups = otherSchema->elementGroups();
130         for (int i = 0; i < elementGroups.count(); ++i) {
131             if (!m_mergedSchema->elementGroup(elementGroups.at(i)->name(otherSchema->namePool())))
132                 m_mergedSchema->addElementGroup(elementGroups.at(i));
133         }
134 
135         const XsdAttributeGroup::List attributeGroups = otherSchema->attributeGroups();
136         for (int i = 0; i < attributeGroups.count(); ++i) {
137             if (!m_mergedSchema->attributeGroup(attributeGroups.at(i)->name(otherSchema->namePool())))
138                 m_mergedSchema->addAttributeGroup(attributeGroups.at(i));
139         }
140 
141         const XsdNotation::List notations = otherSchema->notations();
142         for (int i = 0; i < notations.count(); ++i) {
143             if (!m_mergedSchema->notation(notations.at(i)->name(otherSchema->namePool())))
144                 m_mergedSchema->addNotation(notations.at(i));
145         }
146 
147         const XsdIdentityConstraint::List identityConstraints = otherSchema->identityConstraints();
148         for (int i = 0; i < identityConstraints.count(); ++i) {
149             if (!m_mergedSchema->identityConstraint(identityConstraints.at(i)->name(otherSchema->namePool())))
150                 m_mergedSchema->addIdentityConstraint(identityConstraints.at(i));
151         }
152     }
153 }
154 
155 QT_END_NAMESPACE
156