1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * @file
|
---|
44 | * @short This file is included by qcastingplatform_p.h.
|
---|
45 | * If you need includes in this file, put them in CasttingPlatform.h,
|
---|
46 | * outside of the namespace.
|
---|
47 | */
|
---|
48 |
|
---|
49 | template<typename TokenLookupClass,
|
---|
50 | typename LookupKey>
|
---|
51 | MaintainingReader<TokenLookupClass, LookupKey>::MaintainingReader(const typename ElementDescription<TokenLookupClass, LookupKey>::Hash &elementDescriptions,
|
---|
52 | const QSet<typename TokenLookupClass::NodeName> &standardAttributes,
|
---|
53 | const ReportContext::Ptr &context,
|
---|
54 | QIODevice *const queryDevice) : QXmlStreamReader(queryDevice)
|
---|
55 | , m_hasHandledStandardAttributes(false)
|
---|
56 | , m_context(context)
|
---|
57 | , m_elementDescriptions(elementDescriptions)
|
---|
58 | , m_standardAttributes(standardAttributes)
|
---|
59 | {
|
---|
60 | Q_ASSERT(m_context);
|
---|
61 | Q_ASSERT(!m_elementDescriptions.isEmpty());
|
---|
62 |
|
---|
63 | /* We start with stripping. */
|
---|
64 | m_stripWhitespace.push(true);
|
---|
65 | }
|
---|
66 |
|
---|
67 | template<typename TokenLookupClass,
|
---|
68 | typename LookupKey>
|
---|
69 | MaintainingReader<TokenLookupClass, LookupKey>::~MaintainingReader()
|
---|
70 | {
|
---|
71 | }
|
---|
72 |
|
---|
73 | template<typename TokenLookupClass,
|
---|
74 | typename LookupKey>
|
---|
75 | QSourceLocation MaintainingReader<TokenLookupClass, LookupKey>::currentLocation() const
|
---|
76 | {
|
---|
77 | return QSourceLocation(documentURI(),
|
---|
78 | lineNumber(),
|
---|
79 | columnNumber());
|
---|
80 | }
|
---|
81 |
|
---|
82 | template<typename TokenLookupClass,
|
---|
83 | typename LookupKey>
|
---|
84 | QXmlStreamReader::TokenType MaintainingReader<TokenLookupClass, LookupKey>::readNext()
|
---|
85 | {
|
---|
86 | const TokenType retval = QXmlStreamReader::readNext();
|
---|
87 |
|
---|
88 | switch(retval)
|
---|
89 | {
|
---|
90 | case StartElement:
|
---|
91 | {
|
---|
92 | m_currentElementName = TokenLookupClass::toToken(name());
|
---|
93 | m_currentAttributes = attributes();
|
---|
94 | m_hasHandledStandardAttributes = false;
|
---|
95 |
|
---|
96 | if(!m_currentAttributes.hasAttribute(QLatin1String("xml:space")))
|
---|
97 | m_stripWhitespace.push(m_stripWhitespace.top());
|
---|
98 | break;
|
---|
99 | }
|
---|
100 | case EndElement:
|
---|
101 | m_currentElementName = TokenLookupClass::toToken(name());
|
---|
102 | m_stripWhitespace.pop();
|
---|
103 | break;
|
---|
104 | default:
|
---|
105 | break;
|
---|
106 | }
|
---|
107 |
|
---|
108 | return retval;
|
---|
109 | }
|
---|
110 |
|
---|
111 | template<typename TokenLookupClass,
|
---|
112 | typename LookupKey>
|
---|
113 | bool MaintainingReader<TokenLookupClass, LookupKey>::isWhitespace() const
|
---|
114 | {
|
---|
115 | return QXmlStreamReader::isWhitespace()
|
---|
116 | || XPathHelper::isWhitespaceOnly(text());
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | template<typename TokenLookupClass,
|
---|
121 | typename LookupKey>
|
---|
122 | void MaintainingReader<TokenLookupClass, LookupKey>::error(const QString &message,
|
---|
123 | const ReportContext::ErrorCode code) const
|
---|
124 | {
|
---|
125 | m_context->error(message, code, currentLocation());
|
---|
126 | }
|
---|
127 |
|
---|
128 | template<typename TokenLookupClass,
|
---|
129 | typename LookupKey>
|
---|
130 | void MaintainingReader<TokenLookupClass, LookupKey>::warning(const QString &message) const
|
---|
131 | {
|
---|
132 | m_context->warning(message, currentLocation());
|
---|
133 | }
|
---|
134 |
|
---|
135 | template<typename TokenLookupClass,
|
---|
136 | typename LookupKey>
|
---|
137 | typename TokenLookupClass::NodeName MaintainingReader<TokenLookupClass, LookupKey>::currentElementName() const
|
---|
138 | {
|
---|
139 | return m_currentElementName;
|
---|
140 | }
|
---|
141 |
|
---|
142 | template<typename TokenLookupClass,
|
---|
143 | typename LookupKey>
|
---|
144 | void MaintainingReader<TokenLookupClass, LookupKey>::validateElement(const LookupKey elementName) const
|
---|
145 | {
|
---|
146 | Q_ASSERT(tokenType() == QXmlStreamReader::StartElement);
|
---|
147 |
|
---|
148 | if(m_elementDescriptions.contains(elementName))
|
---|
149 | {
|
---|
150 | const ElementDescription<TokenLookupClass, LookupKey> &desc = m_elementDescriptions.value(elementName);
|
---|
151 | const int attCount = m_currentAttributes.count();
|
---|
152 |
|
---|
153 | QSet<typename TokenLookupClass::NodeName> encounteredXSLTAtts;
|
---|
154 |
|
---|
155 | for(int i = 0; i < attCount; ++i)
|
---|
156 | {
|
---|
157 | const QXmlStreamAttribute &attr = m_currentAttributes.at(i);
|
---|
158 | if(attr.namespaceUri().isEmpty())
|
---|
159 | {
|
---|
160 | const typename TokenLookupClass::NodeName attrName(TokenLookupClass::toToken(attr.name()));
|
---|
161 | encounteredXSLTAtts.insert(attrName);
|
---|
162 |
|
---|
163 | if(!desc.requiredAttributes.contains(attrName) &&
|
---|
164 | !desc.optionalAttributes.contains(attrName) &&
|
---|
165 | !m_standardAttributes.contains(attrName) &&
|
---|
166 | !isAnyAttributeAllowed())
|
---|
167 | {
|
---|
168 | QString translationString;
|
---|
169 |
|
---|
170 | QList<typename TokenLookupClass::NodeName> all(desc.requiredAttributes.toList() + desc.optionalAttributes.toList());
|
---|
171 | const int totalCount = all.count();
|
---|
172 | QStringList allowed;
|
---|
173 |
|
---|
174 | for(int i = 0; i < totalCount; ++i)
|
---|
175 | allowed.append(formatKeyword(toString(all.at(i))));
|
---|
176 |
|
---|
177 | /* Note, we can't run toString() on attrName, because we're in this branch,
|
---|
178 | * the token lookup doesn't have the string(!).*/
|
---|
179 | const QString stringedName(attr.name().toString());
|
---|
180 |
|
---|
181 | if(totalCount == 0)
|
---|
182 | {
|
---|
183 | translationString = QtXmlPatterns::tr("Attribute %1 cannot appear on the element %2. Only the standard attributes can appear.")
|
---|
184 | .arg(formatKeyword(stringedName),
|
---|
185 | formatKeyword(name()));
|
---|
186 | }
|
---|
187 | else if(totalCount == 1)
|
---|
188 | {
|
---|
189 | translationString = QtXmlPatterns::tr("Attribute %1 cannot appear on the element %2. Only %3 is allowed, and the standard attributes.")
|
---|
190 | .arg(formatKeyword(stringedName),
|
---|
191 | formatKeyword(name()),
|
---|
192 | allowed.first());
|
---|
193 | }
|
---|
194 | else if(totalCount == 1)
|
---|
195 | {
|
---|
196 | /* Note, allowed has already had formatKeyword() applied. */
|
---|
197 | translationString = QtXmlPatterns::tr("Attribute %1 cannot appear on the element %2. Allowed is %3, %4, and the standard attributes.")
|
---|
198 | .arg(formatKeyword(stringedName),
|
---|
199 | formatKeyword(name()),
|
---|
200 | allowed.first(),
|
---|
201 | allowed.last());
|
---|
202 | }
|
---|
203 | else
|
---|
204 | {
|
---|
205 | /* Note, allowed has already had formatKeyword() applied. */
|
---|
206 | translationString = QtXmlPatterns::tr("Attribute %1 cannot appear on the element %2. Allowed is %3, and the standard attributes.")
|
---|
207 | .arg(formatKeyword(stringedName),
|
---|
208 | formatKeyword(name()),
|
---|
209 | allowed.join(QLatin1String(", ")));
|
---|
210 | }
|
---|
211 |
|
---|
212 | m_context->error(translationString,
|
---|
213 | ReportContext::XTSE0090,
|
---|
214 | currentLocation());
|
---|
215 | }
|
---|
216 | }
|
---|
217 | else if(attr.namespaceUri() == namespaceUri())
|
---|
218 | {
|
---|
219 | m_context->error(QtXmlPatterns::tr("XSL-T attributes on XSL-T elements must be in the null namespace, not in the XSL-T namespace which %1 is.")
|
---|
220 | .arg(formatKeyword(attr.name())),
|
---|
221 | ReportContext::XTSE0090,
|
---|
222 | currentLocation());
|
---|
223 | }
|
---|
224 | /* Else, attributes in other namespaces are allowed, continue. */
|
---|
225 | }
|
---|
226 |
|
---|
227 | const QSet<typename TokenLookupClass::NodeName> requiredButMissing(QSet<typename TokenLookupClass::NodeName>(desc.requiredAttributes).subtract(encounteredXSLTAtts));
|
---|
228 |
|
---|
229 | if(!requiredButMissing.isEmpty())
|
---|
230 | {
|
---|
231 | error(QtXmlPatterns::tr("The attribute %1 must appear on element %2.")
|
---|
232 | .arg(formatKeyword(toString(*requiredButMissing.constBegin())),
|
---|
233 | formatKeyword(name())),
|
---|
234 | ReportContext::XTSE0010);
|
---|
235 | }
|
---|
236 | }
|
---|
237 | else
|
---|
238 | {
|
---|
239 | error(QtXmlPatterns::tr("The element with local name %1 does not exist in XSL-T.").arg(formatKeyword(name())),
|
---|
240 | ReportContext::XTSE0010);
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | template<typename TokenLookupClass,
|
---|
245 | typename LookupKey>
|
---|
246 | bool MaintainingReader<TokenLookupClass, LookupKey>::hasAttribute(const QString &namespaceURI,
|
---|
247 | const QString &localName) const
|
---|
248 | {
|
---|
249 | Q_ASSERT(tokenType() == QXmlStreamReader::StartElement);
|
---|
250 | return m_currentAttributes.hasAttribute(namespaceURI, localName);
|
---|
251 | }
|
---|
252 |
|
---|
253 | template<typename TokenLookupClass,
|
---|
254 | typename LookupKey>
|
---|
255 | bool MaintainingReader<TokenLookupClass, LookupKey>::hasAttribute(const QString &localName) const
|
---|
256 | {
|
---|
257 | return hasAttribute(QString(), localName);
|
---|
258 | }
|
---|
259 |
|
---|
260 | template<typename TokenLookupClass,
|
---|
261 | typename LookupKey>
|
---|
262 | QString MaintainingReader<TokenLookupClass, LookupKey>::readAttribute(const QString &localName,
|
---|
263 | const QString &namespaceURI) const
|
---|
264 | {
|
---|
265 | Q_ASSERT(tokenType() == QXmlStreamReader::StartElement);
|
---|
266 |
|
---|
267 | Q_ASSERT_X(m_currentAttributes.hasAttribute(namespaceURI, localName),
|
---|
268 | Q_FUNC_INFO,
|
---|
269 | "Validation must be done before this function is called.");
|
---|
270 |
|
---|
271 | return m_currentAttributes.value(namespaceURI, localName).toString();
|
---|
272 | }
|
---|
273 |
|
---|