2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * @file xmltooling/util/ParserPool.h
24 * A thread-safe pool of parsers that share characteristics.
27 #ifndef __xmltooling_pool_h__
28 #define __xmltooling_pool_h__
30 #include <xmltooling/unicode.h>
36 #include <xercesc/dom/DOM.hpp>
37 #include <xercesc/sax/InputSource.hpp>
38 #include <xercesc/util/BinInputStream.hpp>
39 #include <xercesc/util/SecurityManager.hpp>
40 #include <xercesc/util/XMLURL.hpp>
42 #ifndef XMLTOOLING_NO_XMLSEC
43 # include <xsec/framework/XSECDefs.hpp>
46 #if defined (_MSC_VER)
47 #pragma warning( push )
48 #pragma warning( disable : 4250 4251 )
51 namespace xmltooling {
53 class XMLTOOL_API Mutex;
56 * A thread-safe pool of DOMBuilders that share characteristics.
58 class XMLTOOL_API ParserPool :
59 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
60 public xercesc::DOMLSResourceResolver
62 public xercesc::DOMEntityResolver
65 MAKE_NONCOPYABLE(ParserPool);
68 * Constructs a new pool
70 * @param namespaceAware indicates whether parsers should be namespace-aware or not
71 * @param schemaAware indicates whether parsers should be schema-validating or not
73 ParserPool(bool namespaceAware=true, bool schemaAware=false);
77 * Creates a new document using a parser from this pool.
79 * @return new XML document
82 xercesc::DOMDocument* newDocument();
85 * Parses a document using a pooled parser with the proper settings
87 * @param domsrc An input source containing the content to be parsed
88 * @return The DOM document resulting from the parse
89 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
91 xercesc::DOMDocument* parse(
92 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
93 xercesc::DOMLSInput& domsrc
95 xercesc::DOMInputSource& domsrc
100 * Parses a document using a pooled parser with the proper settings
102 * @param is An input stream containing the content to be parsed
103 * @return The DOM document resulting from the parse
104 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
106 xercesc::DOMDocument* parse(std::istream& is);
109 * Load an OASIS catalog file to map schema namespace URIs to filenames.
111 * This does not provide real catalog support; only the <uri> element
112 * is supported to map from a namespace URI to a relative path or file:// URI.
114 * @param pathname path to a catalog file
115 * @return true iff the catalog was successfully processed
117 bool loadCatalog(const char* pathname);
120 * Load an OASIS catalog file to map schema namespace URIs to filenames.
122 * This does not provide real catalog support; only the <uri> element
123 * is supported to map from a namespace URI to a relative path or file:// URI.
125 * @param pathname path to a catalog file
126 * @return true iff the catalog was successfully processed
128 bool loadCatalog(const XMLCh* pathname);
131 * Load a schema explicitly from a local file.
133 * Note that "successful processing" does not imply that the schema is valid,
134 * only that a reference to it was successfully registered with the pool.
136 * @param nsURI XML namespace to load
137 * @param pathname path to schema file
138 * @return true iff the schema was successfully processed
140 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
143 * Supplies all external entities (primarily schemas) to the parser
145 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
146 xercesc::DOMLSInput* resolveResource(
147 const XMLCh *const resourceType,
148 const XMLCh *const namespaceUri,
149 const XMLCh *const publicId,
150 const XMLCh *const systemId,
151 const XMLCh *const baseURI
154 xercesc::DOMInputSource* resolveEntity(
155 const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
160 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
161 xercesc::DOMLSParser* createBuilder();
162 xercesc::DOMLSParser* checkoutBuilder();
163 void checkinBuilder(xercesc::DOMLSParser* builder);
165 xercesc::DOMBuilder* createBuilder();
166 xercesc::DOMBuilder* checkoutBuilder();
167 void checkinBuilder(xercesc::DOMBuilder* builder);
170 xstring m_schemaLocations;
171 std::map<xstring,xstring> m_schemaLocMap;
173 bool m_namespaceAware,m_schemaAware;
174 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
175 std::stack<xercesc::DOMLSParser*> m_pool;
177 std::stack<xercesc::DOMBuilder*> m_pool;
180 xercesc::SecurityManager* m_security;
184 * A parser source that wraps a C++ input stream
186 class XMLTOOL_API StreamInputSource : public xercesc::InputSource
188 MAKE_NONCOPYABLE(StreamInputSource);
191 * Constructs an input source around an input stream reference.
193 * @param is reference to an input stream
194 * @param systemId optional system identifier to attach to the stream
196 StreamInputSource(std::istream& is, const char* systemId=nullptr);
198 xercesc::BinInputStream* makeStream() const;
202 * A Xerces input stream that wraps a C++ input stream
204 class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
208 * Constructs a Xerces input stream around a C++ input stream reference.
210 * @param is reference to an input stream
212 StreamBinInputStream(std::istream& is);
214 #ifdef XMLTOOLING_XERCESC_64BITSAFE
215 XMLFilePos curPos() const;
216 const XMLCh* getContentType() const;
218 unsigned int curPos() const;
220 xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
232 * A URL-based parser source that supports a more advanced input stream.
234 class XMLTOOL_API URLInputSource : public xercesc::InputSource
236 MAKE_NONCOPYABLE(URLInputSource);
241 * @param url source of input
242 * @param systemId optional system identifier to attach to the source
243 * @param cacheTag optional pointer to string used for cache management
245 URLInputSource(const XMLCh* url, const char* systemId=nullptr, std::string* cacheTag=nullptr);
248 * Constructor taking a DOM element supporting the following content:
252 * <dd>identifies the remote resource</dd>
253 * <dt>verifyHost</dt>
254 * <dd>true iff name of host should be matched against TLS/SSL certificate</dd>
255 * <dt>TransportOption elements, like so:</dt>
256 * <dd><TransportOption provider="CURL" option="150">0</TransportOption></dd>
259 * @param e DOM to supply configuration
260 * @param systemId optional system identifier to attach to the source
261 * @param cacheTag optional pointer to string used for cache management
263 URLInputSource(const xercesc::DOMElement* e, const char* systemId=nullptr, std::string* cacheTag=nullptr);
266 virtual xercesc::BinInputStream* makeStream() const;
269 /** Element name used to signal a non-successful response when fetching a remote document. */
270 static const char asciiStatusCodeElementName[];
272 /** Element name used to signal a non-successful response when fetching a remote document. */
273 static const XMLCh utf16StatusCodeElementName[];
275 #ifdef XMLTOOLING_LITE
276 xercesc::XMLURL m_url;
278 std::string* m_cacheTag;
279 xmltooling::auto_ptr_char m_url;
280 const xercesc::DOMElement* m_root;
285 #if defined (_MSC_VER)
286 #pragma warning( pop )
289 #endif /* __xmltooling_pool_h__ */