1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qxsdschemadebugger_p.h"
43 
44 QT_BEGIN_NAMESPACE
45 
46 using namespace QPatternist;
47 
XsdSchemaDebugger(const NamePool::Ptr & namePool)48 XsdSchemaDebugger::XsdSchemaDebugger(const NamePool::Ptr &namePool)
49     : m_namePool(namePool)
50 {
51 }
52 
dumpParticle(const XsdParticle::Ptr & particle,int level)53 void XsdSchemaDebugger::dumpParticle(const XsdParticle::Ptr &particle, int level)
54 {
55     QString prefix; prefix.fill(QLatin1Char(' '), level);
56 
57     qDebug("%s min=%s max=%s", qPrintable(prefix), qPrintable(QString::number(particle->minimumOccurs())),
58                                qPrintable(particle->maximumOccursUnbounded() ? QLatin1String("unbounded") : QString::number(particle->maximumOccurs())));
59 
60     if (particle->term()->isElement()) {
61         qDebug("%selement (%s)", qPrintable(prefix), qPrintable(XsdElement::Ptr(particle->term())->displayName(m_namePool)));
62     } else if (particle->term()->isModelGroup()) {
63         const XsdModelGroup::Ptr group(particle->term());
64         if (group->compositor() == XsdModelGroup::SequenceCompositor) {
65             qDebug("%ssequence", qPrintable(prefix));
66         } else if (group->compositor() == XsdModelGroup::AllCompositor) {
67             qDebug("%sall", qPrintable(prefix));
68         } else if (group->compositor() == XsdModelGroup::ChoiceCompositor) {
69             qDebug("%schoice", qPrintable(prefix));
70         }
71 
72         for (int i = 0; i < group->particles().count(); ++i)
73             dumpParticle(group->particles().at(i), level + 5);
74     } else if (particle->term()->isWildcard()) {
75         XsdWildcard::Ptr wildcard(particle->term());
76         qDebug("%swildcard (process=%d)", qPrintable(prefix), wildcard->processContents());
77     }
78 }
79 
dumpInheritance(const SchemaType::Ptr & type,int level)80 void XsdSchemaDebugger::dumpInheritance(const SchemaType::Ptr &type, int level)
81 {
82     QString prefix; prefix.fill(QLatin1Char(' '), level);
83     qDebug("%s-->%s", qPrintable(prefix), qPrintable(type->displayName(m_namePool)));
84     if (type->wxsSuperType())
85         dumpInheritance(type->wxsSuperType(), ++level);
86 }
87 
dumpWildcard(const XsdWildcard::Ptr & wildcard)88 void XsdSchemaDebugger::dumpWildcard(const XsdWildcard::Ptr &wildcard)
89 {
90     QVector<QString> varietyNames;
91     varietyNames.append(QLatin1String("Any"));
92     varietyNames.append(QLatin1String("Enumeration"));
93     varietyNames.append(QLatin1String("Not"));
94 
95     QVector<QString> processContentsNames;
96     processContentsNames.append(QLatin1String("Strict"));
97     processContentsNames.append(QLatin1String("Lax"));
98     processContentsNames.append(QLatin1String("Skip"));
99 
100     qDebug("      processContents: %s", qPrintable(processContentsNames.at((int)wildcard->processContents())));
101     const XsdWildcard::NamespaceConstraint::Ptr constraint = wildcard->namespaceConstraint();
102     qDebug("      variety: %s", qPrintable(varietyNames.at((int)constraint->variety())));
103     if (constraint->variety() != XsdWildcard::NamespaceConstraint::Any)
104         qDebug() << "      namespaces:" << constraint->namespaces();
105 }
106 
dumpType(const SchemaType::Ptr & type)107 void XsdSchemaDebugger::dumpType(const SchemaType::Ptr &type)
108 {
109     if (type->isComplexType()) {
110         const XsdComplexType::Ptr complexType(type);
111         qDebug("\n+++ Complex Type +++");
112         qDebug("Name: %s (abstract: %s)", qPrintable(complexType->displayName(m_namePool)), complexType->isAbstract() ? "yes" : "no");
113         if (complexType->wxsSuperType())
114             qDebug("  base type: %s", qPrintable(complexType->wxsSuperType()->displayName(m_namePool)));
115         else
116             qDebug("  base type: (none)");
117         if (complexType->contentType()->variety() == XsdComplexType::ContentType::Empty)
118             qDebug("  content type: empty");
119         if (complexType->contentType()->variety() == XsdComplexType::ContentType::Simple)
120             qDebug("  content type: simple");
121         if (complexType->contentType()->variety() == XsdComplexType::ContentType::ElementOnly)
122             qDebug("  content type: element-only");
123         if (complexType->contentType()->variety() == XsdComplexType::ContentType::Mixed)
124             qDebug("  content type: mixed");
125         if (complexType->contentType()->variety() == XsdComplexType::ContentType::Simple) {
126             if (complexType->contentType()->simpleType())
127                 qDebug("  simple type: %s", qPrintable(complexType->contentType()->simpleType()->displayName(m_namePool)));
128             else
129                 qDebug("  simple type: (none)");
130         }
131 
132         const XsdAttributeUse::List uses = complexType->attributeUses();
133         qDebug("   %d attributes", uses.count());
134         for (int i = 0; i < uses.count(); ++i) {
135             qDebug("      attr: %s", qPrintable(uses.at(i)->attribute()->displayName(m_namePool)));
136         }
137         qDebug("   has attribute wildcard: %s", complexType->attributeWildcard() ? "yes" : "no");
138         if (complexType->attributeWildcard()) {
139             dumpWildcard(complexType->attributeWildcard());
140         }
141 
142         if (complexType->contentType()->particle()) {
143             dumpParticle(complexType->contentType()->particle(), 5);
144         }
145     } else {
146         qDebug("\n+++ Simple Type +++");
147         qDebug("Name: %s", qPrintable(type->displayName(m_namePool)));
148         if (type->isDefinedBySchema()) {
149             const XsdSimpleType::Ptr simpleType(type);
150             if (simpleType->primitiveType())
151                 qDebug("  primitive type: %s", qPrintable(simpleType->primitiveType()->displayName(m_namePool)));
152             else
153                 qDebug("  primitive type: (none)");
154         }
155         dumpInheritance(type, 0);
156     }
157 }
158 
159 
dumpElement(const XsdElement::Ptr & element)160 void XsdSchemaDebugger::dumpElement(const XsdElement::Ptr &element)
161 {
162     QStringList disallowedSubstGroup;
163     if (element->disallowedSubstitutions() & XsdElement::RestrictionConstraint)
164         disallowedSubstGroup << QLatin1String("restriction");
165     if (element->disallowedSubstitutions() & XsdElement::ExtensionConstraint)
166         disallowedSubstGroup << QLatin1String("extension");
167     if (element->disallowedSubstitutions() & XsdElement::SubstitutionConstraint)
168         disallowedSubstGroup << QLatin1String("substitution");
169 
170 
171     qDebug() << "Name:" << element->displayName(m_namePool);
172     qDebug() << "IsAbstract:" << (element->isAbstract() ? "yes" : "no");
173     qDebug() << "Type:" << element->type()->displayName(m_namePool);
174     qDebug() << "DisallowedSubstitutionGroups:" << disallowedSubstGroup.join(QLatin1String("' "));
175 }
176 
dumpAttribute(const XsdAttribute::Ptr & attribute)177 void XsdSchemaDebugger::dumpAttribute(const XsdAttribute::Ptr &attribute)
178 {
179     qDebug() << "Name:" << attribute->displayName(m_namePool);
180     qDebug() << "Type:" << attribute->type()->displayName(m_namePool);
181 }
182 
dumpSchema(const XsdSchema::Ptr & schema)183 void XsdSchemaDebugger::dumpSchema(const XsdSchema::Ptr &schema)
184 {
185     qDebug() << "------------------------------ Schema -------------------------------";
186 
187     // elements
188     {
189         qDebug() << "Global Elements:";
190         const XsdElement::List elements = schema->elements();
191         for (int i = 0; i < elements.count(); ++i) {
192             dumpElement(elements.at(i));
193         }
194     }
195 
196     // attributes
197     {
198         qDebug() << "Global Attributes:";
199         const XsdAttribute::List attributes = schema->attributes();
200         for (int i = 0; i < attributes.count(); ++i) {
201             dumpAttribute(attributes.at(i));
202         }
203     }
204 
205     // types
206     {
207         qDebug() << "Global Types:";
208         const SchemaType::List types = schema->types();
209         for (int i = 0; i < types.count(); ++i) {
210             dumpType(types.at(i));
211         }
212     }
213 
214     // anonymous types
215     {
216         qDebug() << "Anonymous Types:";
217         const SchemaType::List types = schema->anonymousTypes();
218         for (int i = 0; i < types.count(); ++i) {
219             dumpType(types.at(i));
220         }
221     }
222 
223     qDebug() << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
224 }
225 
226 QT_END_NAMESPACE
227