BDE 4.14.0 Production release
Loading...
Searching...
No Matches
balxml_util.h
Go to the documentation of this file.
1/// @file balxml_util.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balxml_util.h -*-C++-*-
8#ifndef INCLUDED_BALXML_UTIL
9#define INCLUDED_BALXML_UTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup balxml_util balxml_util
15/// @brief Provide a suite of common XML utilities.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balxml
19/// @{
20/// @addtogroup balxml_util
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balxml_util-purpose"> Purpose</a>
25/// * <a href="#balxml_util-classes"> Classes </a>
26/// * <a href="#balxml_util-description"> Description </a>
27/// * <a href="#balxml_util-usage"> Usage </a>
28///
29/// # Purpose {#balxml_util-purpose}
30/// Provide a suite of common XML utilities.
31///
32/// # Classes {#balxml_util-classes}
33///
34/// - balxml::Util: namespace for common XML utilities
35///
36/// @see
37///
38/// # Description {#balxml_util-description}
39/// This component provides a namespace, `balxml::Util`, for
40/// various XML utilities. Included is a method for extracting the
41/// `targetNamespace` from an XSD schema. Any top-level XSD schema must have a
42/// `targetNamespace` attribute in the `<schema>` element that identifies the
43/// namespace that the XSD schema defines.
44///
45/// Note that a valid XSD file must have this attribute for the root element
46/// `<schema>` and the only place that the token "targetNamespace" can appear
47/// prior to its occurrence as the required attribute is in the comment
48/// string(s) before `<schema>`. And any comments cannot be embedded in other
49/// comments or inside a tag. The extraction algorithm relies on these XML
50/// facts to jump over any comments prior to the first occurrence of
51/// "targetNamespace" and extract the attribute value. It leaves all other
52/// validation of `xsdSource` to a parser.
53///
54/// ## Usage {#balxml_util-usage}
55///
56///
57/// @code
58/// // 'etalon' targetNamespace:
59/// static const char *targetNs = "http://localhost:2000/calc.wsdl";
60///
61/// // well-formed and correct XSD:
62/// static const char goodSchema[] =
63/// "<?xml version='1.0' encoding='UTF-8'?>"
64/// " <schema targetNamespace='http://localhost:2000/calc.wsdl'"
65/// " elementFormDefault='qualified'"
66/// " xmlns='http://www.w3.org/2001/XMLSchema'"
67/// " xmlns:xs='http://www.w3.org/2001/XMLSchema'"
68/// " xmlns:bdem='http://bloomberg.com/schemas/bdem'"
69/// " xmlns:m_bascalc='http://localhost:2000/calc.wsdl'"
70/// " xmlns:tns='http://localhost:2000/calc.wsdl'"
71/// " <xs:complexType name='Options'>"
72/// " <xs:sequence>"
73/// " <xs:element name='MaxDepth' type='xs:int'"
74/// " minOccurs='0' maxOccurs='1'"
75/// " default='32'"
76/// " bdem:allowsDirectManipulation='0'>"
77/// " </xs:element>"
78/// " </xs:sequence>"
79/// " </xs:complexType>"
80/// " <complexType name='Configuration'>"
81/// " <sequence>"
82/// " <element name='Options' type='m_bascalc:Options'/>"
83/// " </sequence>"
84/// " </complexType>"
85/// " <element name='Configuration' type='m_bascalc:Options'/>"
86/// "</schema>";
87///
88/// // well-formed XSD with no target namespace:
89/// static const char badSchema1[] =
90/// "<?xml version='1.0' encoding='UTF-8'?>"
91/// " elementFormDefault='qualified'"
92/// " xmlns='http://www.w3.org/2001/XMLSchema'"
93/// " xmlns:xs='http://www.w3.org/2001/XMLSchema'"
94/// " xmlns:bdem='http://bloomberg.com/schemas/bdem'"
95/// " xmlns:m_bascalc='http://localhost:2000/calc.wsdl'"
96/// " xmlns:tns='http://localhost:2000/calc.wsdl'"
97/// " <xs:complexType name='Options'>"
98/// " <xs:sequence>"
99/// " <xs:element name='MaxDepth' type='xs:int'"
100/// " minOccurs='0' maxOccurs='1'"
101/// " default='32'"
102/// " bdem:allowsDirectManipulation='0'>"
103/// " </xs:element>"
104/// " </xs:sequence>"
105/// " </xs:complexType>"
106/// " <complexType name='Configuration'>"
107/// " <sequence>"
108/// " <element name='Options' type='m_bascalc:Options'/>"
109/// " </sequence>"
110/// " </complexType>"
111/// " <element name='Configuration' type='m_bascalc:Options'/>"
112/// "</schema>";
113///
114/// bsl::string strGood(goodSchema);
115/// bsl::string strBad1(badSchema1);
116///
117/// bsl::string resultNs;
118/// bool rc;
119///
120/// rc = balxml::Util::extractNamespaceFromXsd(strGood, &resultNs);
121/// assert(rc);
122/// assert(resultNs == targetNs);
123///
124/// resultNs.clear();
125/// rc = balxml::Util::extractNamespaceFromXsd(strBad1, &resultNs);
126/// assert(!rc);
127/// @endcode
128/// @}
129/** @} */
130/** @} */
131
132/** @addtogroup bal
133 * @{
134 */
135/** @addtogroup balxml
136 * @{
137 */
138/** @addtogroup balxml_util
139 * @{
140 */
141
142#include <balscm_version.h>
143
144#include <bsl_streambuf.h>
145#include <bsl_string.h>
146
147#include <bsls_libraryfeatures.h>
148
149#include <string> // 'std::string', 'std::pmr::string'
150
151
152namespace balxml {
153
154 // ===========
155 // struct Util
156 // ===========
157
158struct Util {
159
160 // CLASS METHODS
162 const bsl::string_view& xsdSource,
163 bsl::string *targetNamespace);
165 const bsl::string_view& xsdSource,
166 std::string *targetNamespace);
167#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
168 static bool extractNamespaceFromXsd(
169 const bsl::string_view& xsdSource,
170 std::pmr::string *targetNamespace);
171#endif
172
173 static bool extractNamespaceFromXsd(bsl::streambuf *xsdSource,
174 bsl::string *targetNamespace);
175 static bool extractNamespaceFromXsd(bsl::streambuf *xsdSource,
176 std::string *targetNamespace);
177#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
178 static bool extractNamespaceFromXsd(bsl::streambuf *xsdSource,
179 std::pmr::string *targetNamespace);
180#endif
181 // Extract the target namespace from the XSD schema in the specified
182 // 'xsdSource' and load it into the specified 'targetNamespace'.
183 // Return 'true' on success, and 'false' otherwise. Note that a valid
184 // XSD schema must have a 'targetNamespace' attribute in the '<schema>'
185 // element that identifies the namespace that the XSD schema defines.
186 // If no such attribute is found, or the 'xsdSource' is ill-formed,
187 // this method returns 'false'. Otherwise, it returns 'true' and
188 // populates the output parameter 'targetNamespace' with the value in
189 // the first 'attribute="value"' pair found for the target namespace.
190};
191
192} // close package namespace
193
194
195#endif
196
197// ----------------------------------------------------------------------------
198// Copyright 2015 Bloomberg Finance L.P.
199//
200// Licensed under the Apache License, Version 2.0 (the "License");
201// you may not use this file except in compliance with the License.
202// You may obtain a copy of the License at
203//
204// http://www.apache.org/licenses/LICENSE-2.0
205//
206// Unless required by applicable law or agreed to in writing, software
207// distributed under the License is distributed on an "AS IS" BASIS,
208// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
209// See the License for the specific language governing permissions and
210// limitations under the License.
211// ----------------------------- END-OF-FILE ----------------------------------
212
213/** @} */
214/** @} */
215/** @} */
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balxml_base64parser.h:150
Definition balxml_util.h:158
static bool extractNamespaceFromXsd(const bsl::string_view &xsdSource, bsl::string *targetNamespace)
static bool extractNamespaceFromXsd(const bsl::string_view &xsdSource, std::string *targetNamespace)
static bool extractNamespaceFromXsd(bsl::streambuf *xsdSource, bsl::string *targetNamespace)
static bool extractNamespaceFromXsd(bsl::streambuf *xsdSource, std::string *targetNamespace)