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 XMLCh* pathname);
120 * Load a schema explicitly from a local file.
122 * Note that "successful processing" does not imply that the schema is valid,
123 * only that a reference to it was successfully registered with the pool.
125 * @param nsURI XML namespace to load
126 * @param pathname path to schema file
127 * @return true iff the schema was successfully processed
129 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
132 * Supplies all external entities (primarily schemas) to the parser
134 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
135 xercesc::DOMLSInput* resolveResource(
136 const XMLCh *const resourceType,
137 const XMLCh *const namespaceUri,
138 const XMLCh *const publicId,
139 const XMLCh *const systemId,
140 const XMLCh *const baseURI
143 xercesc::DOMInputSource* resolveEntity(
144 const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
149 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
150 xercesc::DOMLSParser* createBuilder();
151 xercesc::DOMLSParser* checkoutBuilder();
152 void checkinBuilder(xercesc::DOMLSParser* builder);
154 xercesc::DOMBuilder* createBuilder();
155 xercesc::DOMBuilder* checkoutBuilder();
156 void checkinBuilder(xercesc::DOMBuilder* builder);
159 xstring m_schemaLocations;
160 std::map<xstring,xstring> m_schemaLocMap;
162 bool m_namespaceAware,m_schemaAware;
163 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
164 std::stack<xercesc::DOMLSParser*> m_pool;
166 std::stack<xercesc::DOMBuilder*> m_pool;
169 xercesc::SecurityManager* m_security;
173 * A parser source that wraps a C++ input stream
175 class XMLTOOL_API StreamInputSource : public xercesc::InputSource
177 MAKE_NONCOPYABLE(StreamInputSource);
180 * Constructs an input source around an input stream reference.
182 * @param is reference to an input stream
183 * @param systemId optional system identifier to attach to the stream
185 StreamInputSource(std::istream& is, const char* systemId=nullptr);
187 xercesc::BinInputStream* makeStream() const;
191 * A Xerces input stream that wraps a C++ input stream
193 class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
197 * Constructs a Xerces input stream around a C++ input stream reference.
199 * @param is reference to an input stream
201 StreamBinInputStream(std::istream& is);
203 #ifdef XMLTOOLING_XERCESC_64BITSAFE
204 XMLFilePos curPos() const;
205 const XMLCh* getContentType() const;
207 unsigned int curPos() const;
209 xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
221 * A URL-based parser source that supports a more advanced input stream.
223 class XMLTOOL_API URLInputSource : public xercesc::InputSource
225 MAKE_NONCOPYABLE(URLInputSource);
230 * @param url source of input
231 * @param systemId optional system identifier to attach to the source
232 * @param cacheTag optional pointer to string used for cache management
234 URLInputSource(const XMLCh* url, const char* systemId=nullptr, std::string* cacheTag=nullptr);
237 * Constructor taking a DOM element supporting the following content:
241 * <dd>identifies the remote resource</dd>
242 * <dt>verifyHost</dt>
243 * <dd>true iff name of host should be matched against TLS/SSL certificate</dd>
244 * <dt>TransportOption elements, like so:</dt>
245 * <dd><TransportOption provider="CURL" option="150">0</TransportOption></dd>
248 * @param e DOM to supply configuration
249 * @param systemId optional system identifier to attach to the source
250 * @param cacheTag optional pointer to string used for cache management
252 URLInputSource(const xercesc::DOMElement* e, const char* systemId=nullptr, std::string* cacheTag=nullptr);
255 virtual xercesc::BinInputStream* makeStream() const;
258 /** Element name used to signal a non-successful response when fetching a remote document. */
259 static const char asciiStatusCodeElementName[];
261 /** Element name used to signal a non-successful response when fetching a remote document. */
262 static const XMLCh utf16StatusCodeElementName[];
264 #ifdef XMLTOOLING_LITE
265 xercesc::XMLURL m_url;
267 std::string* m_cacheTag;
268 xmltooling::auto_ptr_char m_url;
269 const xercesc::DOMElement* m_root;
274 #if defined (_MSC_VER)
275 #pragma warning( pop )
278 #endif /* __xmltooling_pool_h__ */