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 "qxsdschema_p.h"
41 
42 #include <QtCore/QReadLocker>
43 #include <QtCore/QWriteLocker>
44 
45 QT_BEGIN_NAMESPACE
46 
47 using namespace QPatternist;
48 
XsdSchema(const NamePool::Ptr & namePool)49 XsdSchema::XsdSchema(const NamePool::Ptr &namePool)
50     : m_namePool(namePool)
51 {
52 }
53 
~XsdSchema()54 XsdSchema::~XsdSchema()
55 {
56 }
57 
namePool() const58 NamePool::Ptr XsdSchema::namePool() const
59 {
60     return m_namePool;
61 }
62 
setTargetNamespace(const QString & targetNamespace)63 void XsdSchema::setTargetNamespace(const QString &targetNamespace)
64 {
65     m_targetNamespace = targetNamespace;
66 }
67 
targetNamespace() const68 QString XsdSchema::targetNamespace() const
69 {
70     return m_targetNamespace;
71 }
72 
addElement(const XsdElement::Ptr & element)73 void XsdSchema::addElement(const XsdElement::Ptr &element)
74 {
75     const QWriteLocker locker(&m_lock);
76 
77     m_elements.insert(element->name(m_namePool), element);
78 }
79 
element(const QXmlName & name) const80 XsdElement::Ptr XsdSchema::element(const QXmlName &name) const
81 {
82     const QReadLocker locker(&m_lock);
83 
84     return m_elements.value(name);
85 }
86 
elements() const87 XsdElement::List XsdSchema::elements() const
88 {
89     const QReadLocker locker(&m_lock);
90 
91     return m_elements.values();
92 }
93 
addAttribute(const XsdAttribute::Ptr & attribute)94 void XsdSchema::addAttribute(const XsdAttribute::Ptr &attribute)
95 {
96     const QWriteLocker locker(&m_lock);
97 
98     m_attributes.insert(attribute->name(m_namePool), attribute);
99 }
100 
attribute(const QXmlName & name) const101 XsdAttribute::Ptr XsdSchema::attribute(const QXmlName &name) const
102 {
103     const QReadLocker locker(&m_lock);
104 
105     return m_attributes.value(name);
106 }
107 
attributes() const108 XsdAttribute::List XsdSchema::attributes() const
109 {
110     const QReadLocker locker(&m_lock);
111 
112     return m_attributes.values();
113 }
114 
addType(const SchemaType::Ptr & type)115 void XsdSchema::addType(const SchemaType::Ptr &type)
116 {
117     const QWriteLocker locker(&m_lock);
118 
119     m_types.insert(type->name(m_namePool), type);
120 }
121 
type(const QXmlName & name) const122 SchemaType::Ptr XsdSchema::type(const QXmlName &name) const
123 {
124     const QReadLocker locker(&m_lock);
125 
126     return m_types.value(name);
127 }
128 
types() const129 SchemaType::List XsdSchema::types() const
130 {
131     const QReadLocker locker(&m_lock);
132 
133     return m_types.values();
134 }
135 
simpleTypes() const136 XsdSimpleType::List XsdSchema::simpleTypes() const
137 {
138     QReadLocker locker(&m_lock);
139 
140     XsdSimpleType::List retval;
141 
142     const SchemaType::List types = m_types.values();
143     for (int i = 0; i < types.count(); ++i) {
144         if (types.at(i)->isSimpleType() && types.at(i)->isDefinedBySchema())
145             retval.append(types.at(i));
146     }
147 
148     return retval;
149 }
150 
complexTypes() const151 XsdComplexType::List XsdSchema::complexTypes() const
152 {
153     QReadLocker locker(&m_lock);
154 
155     XsdComplexType::List retval;
156 
157     const SchemaType::List types = m_types.values();
158     for (int i = 0; i < types.count(); ++i) {
159         if (types.at(i)->isComplexType() && types.at(i)->isDefinedBySchema())
160             retval.append(types.at(i));
161     }
162 
163     return retval;
164 }
165 
addAnonymousType(const SchemaType::Ptr & type)166 void XsdSchema::addAnonymousType(const SchemaType::Ptr &type)
167 {
168     const QWriteLocker locker(&m_lock);
169 
170     // search for not used anonymous type name
171     QXmlName typeName = type->name(m_namePool);
172     while (m_anonymousTypes.contains(typeName)) {
173         typeName = m_namePool->allocateQName(QString(), QLatin1String("merged_") + m_namePool->stringForLocalName(typeName.localName()), QString());
174     }
175 
176     m_anonymousTypes.insert(typeName, type);
177 }
178 
anonymousTypes() const179 SchemaType::List XsdSchema::anonymousTypes() const
180 {
181     const QReadLocker locker(&m_lock);
182 
183     return m_anonymousTypes.values();
184 }
185 
addAttributeGroup(const XsdAttributeGroup::Ptr & group)186 void XsdSchema::addAttributeGroup(const XsdAttributeGroup::Ptr &group)
187 {
188     const QWriteLocker locker(&m_lock);
189 
190     m_attributeGroups.insert(group->name(m_namePool), group);
191 }
192 
attributeGroup(const QXmlName name) const193 XsdAttributeGroup::Ptr XsdSchema::attributeGroup(const QXmlName name) const
194 {
195     const QReadLocker locker(&m_lock);
196 
197     return m_attributeGroups.value(name);
198 }
199 
attributeGroups() const200 XsdAttributeGroup::List XsdSchema::attributeGroups() const
201 {
202     const QReadLocker locker(&m_lock);
203 
204     return m_attributeGroups.values();
205 }
206 
addElementGroup(const XsdModelGroup::Ptr & group)207 void XsdSchema::addElementGroup(const XsdModelGroup::Ptr &group)
208 {
209     const QWriteLocker locker(&m_lock);
210 
211     m_elementGroups.insert(group->name(m_namePool), group);
212 }
213 
elementGroup(const QXmlName & name) const214 XsdModelGroup::Ptr XsdSchema::elementGroup(const QXmlName &name) const
215 {
216     const QReadLocker locker(&m_lock);
217 
218     return m_elementGroups.value(name);
219 }
220 
elementGroups() const221 XsdModelGroup::List XsdSchema::elementGroups() const
222 {
223     const QReadLocker locker(&m_lock);
224 
225     return m_elementGroups.values();
226 }
227 
addNotation(const XsdNotation::Ptr & notation)228 void XsdSchema::addNotation(const XsdNotation::Ptr &notation)
229 {
230     const QWriteLocker locker(&m_lock);
231 
232     m_notations.insert(notation->name(m_namePool), notation);
233 }
234 
notation(const QXmlName & name) const235 XsdNotation::Ptr XsdSchema::notation(const QXmlName &name) const
236 {
237     const QReadLocker locker(&m_lock);
238 
239     return m_notations.value(name);
240 }
241 
notations() const242 XsdNotation::List XsdSchema::notations() const
243 {
244     const QReadLocker locker(&m_lock);
245 
246     return m_notations.values();
247 }
248 
addIdentityConstraint(const XsdIdentityConstraint::Ptr & constraint)249 void XsdSchema::addIdentityConstraint(const XsdIdentityConstraint::Ptr &constraint)
250 {
251     const QWriteLocker locker(&m_lock);
252 
253     m_identityConstraints.insert(constraint->name(m_namePool), constraint);
254 }
255 
identityConstraint(const QXmlName & name) const256 XsdIdentityConstraint::Ptr XsdSchema::identityConstraint(const QXmlName &name) const
257 {
258     const QReadLocker locker(&m_lock);
259 
260     return m_identityConstraints.value(name);
261 }
262 
identityConstraints() const263 XsdIdentityConstraint::List XsdSchema::identityConstraints() const
264 {
265     const QReadLocker locker(&m_lock);
266 
267     return m_identityConstraints.values();
268 }
269 
270 QT_END_NAMESPACE
271