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 "qxsdschemadebugger_p.h"
41 
42 QT_BEGIN_NAMESPACE
43 
44 using namespace QPatternist;
45 
XsdSchemaDebugger(const NamePool::Ptr & namePool)46 XsdSchemaDebugger::XsdSchemaDebugger(const NamePool::Ptr &namePool)
47     : m_namePool(namePool)
48 {
49 }
50 
dumpParticle(const XsdParticle::Ptr & particle,int level)51 void XsdSchemaDebugger::dumpParticle(const XsdParticle::Ptr &particle, int level)
52 {
53     QString prefix; prefix.fill(QLatin1Char(' '), level);
54 
55     qDebug("%s min=%s max=%s", qPrintable(prefix), qPrintable(QString::number(particle->minimumOccurs())),
56                                qPrintable(particle->maximumOccursUnbounded() ? QLatin1String("unbounded") : QString::number(particle->maximumOccurs())));
57 
58     if (particle->term()->isElement()) {
59         qDebug("%selement (%s)", qPrintable(prefix), qPrintable(XsdElement::Ptr(particle->term())->displayName(m_namePool)));
60     } else if (particle->term()->isModelGroup()) {
61         const XsdModelGroup::Ptr group(particle->term());
62         if (group->compositor() == XsdModelGroup::SequenceCompositor) {
63             qDebug("%ssequence", qPrintable(prefix));
64         } else if (group->compositor() == XsdModelGroup::AllCompositor) {
65             qDebug("%sall", qPrintable(prefix));
66         } else if (group->compositor() == XsdModelGroup::ChoiceCompositor) {
67             qDebug("%schoice", qPrintable(prefix));
68         }
69 
70         for (int i = 0; i < group->particles().count(); ++i)
71             dumpParticle(group->particles().at(i), level + 5);
72     } else if (particle->term()->isWildcard()) {
73         XsdWildcard::Ptr wildcard(particle->term());
74         qDebug("%swildcard (process=%d)", qPrintable(prefix), wildcard->processContents());
75     }
76 }
77 
dumpInheritance(const SchemaType::Ptr & type,int level)78 void XsdSchemaDebugger::dumpInheritance(const SchemaType::Ptr &type, int level)
79 {
80     QString prefix; prefix.fill(QLatin1Char(' '), level);
81     qDebug("%s-->%s", qPrintable(prefix), qPrintable(type->displayName(m_namePool)));
82     if (type->wxsSuperType())
83         dumpInheritance(type->wxsSuperType(), ++level);
84 }
85 
dumpWildcard(const XsdWildcard::Ptr & wildcard)86 void XsdSchemaDebugger::dumpWildcard(const XsdWildcard::Ptr &wildcard)
87 {
88     QVector<QString> varietyNames;
89     varietyNames.append(QLatin1String("Any"));
90     varietyNames.append(QLatin1String("Enumeration"));
91     varietyNames.append(QLatin1String("Not"));
92 
93     QVector<QString> processContentsNames;
94     processContentsNames.append(QLatin1String("Strict"));
95     processContentsNames.append(QLatin1String("Lax"));
96     processContentsNames.append(QLatin1String("Skip"));
97 
98     qDebug("      processContents: %s", qPrintable(processContentsNames.at((int)wildcard->processContents())));
99     const XsdWildcard::NamespaceConstraint::Ptr constraint = wildcard->namespaceConstraint();
100     qDebug("      variety: %s", qPrintable(varietyNames.at((int)constraint->variety())));
101     if (constraint->variety() != XsdWildcard::NamespaceConstraint::Any)
102         qDebug() << "      namespaces:" << constraint->namespaces();
103 }
104 
dumpType(const SchemaType::Ptr & type)105 void XsdSchemaDebugger::dumpType(const SchemaType::Ptr &type)
106 {
107     if (type->isComplexType()) {
108         const XsdComplexType::Ptr complexType(type);
109         qDebug("\n+++ Complex Type +++");
110         qDebug("Name: %s (abstract: %s)", qPrintable(complexType->displayName(m_namePool)), complexType->isAbstract() ? "yes" : "no");
111         if (complexType->wxsSuperType())
112             qDebug("  base type: %s", qPrintable(complexType->wxsSuperType()->displayName(m_namePool)));
113         else
114             qDebug("  base type: (none)");
115         if (complexType->contentType()->variety() == XsdComplexType::ContentType::Empty)
116             qDebug("  content type: empty");
117         if (complexType->contentType()->variety() == XsdComplexType::ContentType::Simple)
118             qDebug("  content type: simple");
119         if (complexType->contentType()->variety() == XsdComplexType::ContentType::ElementOnly)
120             qDebug("  content type: element-only");
121         if (complexType->contentType()->variety() == XsdComplexType::ContentType::Mixed)
122             qDebug("  content type: mixed");
123         if (complexType->contentType()->variety() == XsdComplexType::ContentType::Simple) {
124             if (complexType->contentType()->simpleType())
125                 qDebug("  simple type: %s", qPrintable(complexType->contentType()->simpleType()->displayName(m_namePool)));
126             else
127                 qDebug("  simple type: (none)");
128         }
129 
130         const XsdAttributeUse::List uses = complexType->attributeUses();
131         qDebug("   %d attributes", uses.count());
132         for (int i = 0; i < uses.count(); ++i) {
133             qDebug("      attr: %s", qPrintable(uses.at(i)->attribute()->displayName(m_namePool)));
134         }
135         qDebug("   has attribute wildcard: %s", complexType->attributeWildcard() ? "yes" : "no");
136         if (complexType->attributeWildcard()) {
137             dumpWildcard(complexType->attributeWildcard());
138         }
139 
140         if (complexType->contentType()->particle()) {
141             dumpParticle(complexType->contentType()->particle(), 5);
142         }
143     } else {
144         qDebug("\n+++ Simple Type +++");
145         qDebug("Name: %s", qPrintable(type->displayName(m_namePool)));
146         if (type->isDefinedBySchema()) {
147             const XsdSimpleType::Ptr simpleType(type);
148             if (simpleType->primitiveType())
149                 qDebug("  primitive type: %s", qPrintable(simpleType->primitiveType()->displayName(m_namePool)));
150             else
151                 qDebug("  primitive type: (none)");
152         }
153         dumpInheritance(type, 0);
154     }
155 }
156 
157 
dumpElement(const XsdElement::Ptr & element)158 void XsdSchemaDebugger::dumpElement(const XsdElement::Ptr &element)
159 {
160     QStringList disallowedSubstGroup;
161     if (element->disallowedSubstitutions() & XsdElement::RestrictionConstraint)
162         disallowedSubstGroup << QLatin1String("restriction");
163     if (element->disallowedSubstitutions() & XsdElement::ExtensionConstraint)
164         disallowedSubstGroup << QLatin1String("extension");
165     if (element->disallowedSubstitutions() & XsdElement::SubstitutionConstraint)
166         disallowedSubstGroup << QLatin1String("substitution");
167 
168 
169     qDebug() << "Name:" << element->displayName(m_namePool);
170     qDebug() << "IsAbstract:" << (element->isAbstract() ? "yes" : "no");
171     qDebug() << "Type:" << element->type()->displayName(m_namePool);
172     qDebug() << "DisallowedSubstitutionGroups:" << disallowedSubstGroup.join(QLatin1String("' "));
173 }
174 
dumpAttribute(const XsdAttribute::Ptr & attribute)175 void XsdSchemaDebugger::dumpAttribute(const XsdAttribute::Ptr &attribute)
176 {
177     qDebug() << "Name:" << attribute->displayName(m_namePool);
178     qDebug() << "Type:" << attribute->type()->displayName(m_namePool);
179 }
180 
dumpSchema(const XsdSchema::Ptr & schema)181 void XsdSchemaDebugger::dumpSchema(const XsdSchema::Ptr &schema)
182 {
183     qDebug() << "------------------------------ Schema -------------------------------";
184 
185     // elements
186     {
187         qDebug() << "Global Elements:";
188         const XsdElement::List elements = schema->elements();
189         for (int i = 0; i < elements.count(); ++i) {
190             dumpElement(elements.at(i));
191         }
192     }
193 
194     // attributes
195     {
196         qDebug() << "Global Attributes:";
197         const XsdAttribute::List attributes = schema->attributes();
198         for (int i = 0; i < attributes.count(); ++i) {
199             dumpAttribute(attributes.at(i));
200         }
201     }
202 
203     // types
204     {
205         qDebug() << "Global Types:";
206         const SchemaType::List types = schema->types();
207         for (int i = 0; i < types.count(); ++i) {
208             dumpType(types.at(i));
209         }
210     }
211 
212     // anonymous types
213     {
214         qDebug() << "Anonymous Types:";
215         const SchemaType::List types = schema->anonymousTypes();
216         for (int i = 0; i < types.count(); ++i) {
217             dumpType(types.at(i));
218         }
219     }
220 
221     qDebug() << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
222 }
223 
224 QT_END_NAMESPACE
225