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>
37 #include <xercesc/dom/DOM.hpp>
38 #include <xercesc/sax/InputSource.hpp>
39 #include <xercesc/util/BinInputStream.hpp>
40 #include <xercesc/util/SecurityManager.hpp>
41 #include <xercesc/util/XMLURL.hpp>
43 #ifndef XMLTOOLING_NO_XMLSEC
44 # include <xsec/framework/XSECDefs.hpp>
47 #if defined (_MSC_VER)
48 #pragma warning( push )
49 #pragma warning( disable : 4250 4251 )
52 namespace xmltooling {
54 class XMLTOOL_API Mutex;
57 * A thread-safe pool of DOMBuilders that share characteristics.
59 class XMLTOOL_API ParserPool :
60 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
61 public xercesc::DOMLSResourceResolver
63 public xercesc::DOMEntityResolver
66 MAKE_NONCOPYABLE(ParserPool);
69 * Constructs a new pool
71 * @param namespaceAware indicates whether parsers should be namespace-aware or not
72 * @param schemaAware indicates whether parsers should be schema-validating or not
74 ParserPool(bool namespaceAware=true, bool schemaAware=false);
78 * Creates a new document using a parser from this pool.
80 * @return new XML document
83 xercesc::DOMDocument* newDocument();
86 * Parses a document using a pooled parser with the proper settings
88 * @param domsrc An input source containing the content to be parsed
89 * @return The DOM document resulting from the parse
90 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
92 xercesc::DOMDocument* parse(
93 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
94 xercesc::DOMLSInput& domsrc
96 xercesc::DOMInputSource& domsrc
101 * Parses a document using a pooled parser with the proper settings
103 * @param is An input stream containing the content to be parsed
104 * @return The DOM document resulting from the parse
105 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
107 xercesc::DOMDocument* parse(std::istream& is);
110 * Load OASIS catalog files to map schema namespace URIs to filenames.
112 * <p>This does not provide real catalog support; only the <uri> element
113 * is supported to map from a namespace URI to a relative path or file:// URI.
115 * <p>Multiple files can be specified using a platform-specific path delimiter.
117 * @param pathname path to one or more catalog files
118 * @return true iff the catalogs were successfully processed
120 bool loadCatalogs(const char* pathnames);
123 * Load an OASIS catalog file to map schema namespace URIs to filenames.
125 * <p>This does not provide real catalog support; only the <uri> element
126 * is supported to map from a namespace URI to a relative path or file:// URI.
128 * @param pathname path to a catalog file
129 * @return true iff the catalog was successfully processed
131 bool loadCatalog(const char* pathnames);
134 * Load an OASIS catalog file to map schema namespace URIs to filenames.
136 * This does not provide real catalog support; only the <uri> element
137 * is supported to map from a namespace URI to a relative path or file:// URI.
139 * @param pathname path to a catalog file
140 * @return true iff the catalog was successfully processed
142 bool loadCatalog(const XMLCh* pathname);
145 * Load a schema explicitly from a local file.
147 * Note that "successful processing" does not imply that the schema is valid,
148 * only that a reference to it was successfully registered with the pool.
150 * @param nsURI XML namespace to load
151 * @param pathname path to schema file
152 * @return true iff the schema was successfully processed
154 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
157 * Supplies all external entities (primarily schemas) to the parser
159 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
160 xercesc::DOMLSInput* resolveResource(
161 const XMLCh *const resourceType,
162 const XMLCh *const namespaceUri,
163 const XMLCh *const publicId,
164 const XMLCh *const systemId,
165 const XMLCh *const baseURI
168 xercesc::DOMInputSource* resolveEntity(
169 const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
174 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
175 xercesc::DOMLSParser* createBuilder();
176 xercesc::DOMLSParser* checkoutBuilder();
177 void checkinBuilder(xercesc::DOMLSParser* builder);
179 xercesc::DOMBuilder* createBuilder();
180 xercesc::DOMBuilder* checkoutBuilder();
181 void checkinBuilder(xercesc::DOMBuilder* builder);
184 xstring m_schemaLocations;
185 std::map<xstring,xstring> m_schemaLocMap;
187 bool m_namespaceAware,m_schemaAware;
188 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
189 std::stack<xercesc::DOMLSParser*> m_pool;
191 std::stack<xercesc::DOMBuilder*> m_pool;
193 std::auto_ptr<Mutex> m_lock;
194 std::auto_ptr<xercesc::SecurityManager> m_security;
198 * A parser source that wraps a C++ input stream
200 class XMLTOOL_API StreamInputSource : public xercesc::InputSource
202 MAKE_NONCOPYABLE(StreamInputSource);
205 * Constructs an input source around an input stream reference.
207 * @param is reference to an input stream
208 * @param systemId optional system identifier to attach to the stream
210 StreamInputSource(std::istream& is, const char* systemId=nullptr);
212 xercesc::BinInputStream* makeStream() const;
216 * A Xerces input stream that wraps a C++ input stream
218 class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
222 * Constructs a Xerces input stream around a C++ input stream reference.
224 * @param is reference to an input stream
226 StreamBinInputStream(std::istream& is);
228 #ifdef XMLTOOLING_XERCESC_64BITSAFE
229 XMLFilePos curPos() const;
230 const XMLCh* getContentType() const;
232 unsigned int curPos() const;
234 xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
246 * A URL-based parser source that supports a more advanced input stream.
248 class XMLTOOL_API URLInputSource : public xercesc::InputSource
250 MAKE_NONCOPYABLE(URLInputSource);
255 * @param url source of input
256 * @param systemId optional system identifier to attach to the source
257 * @param cacheTag optional pointer to string used for cache management
259 URLInputSource(const XMLCh* url, const char* systemId=nullptr, std::string* cacheTag=nullptr);
262 * Constructor taking a DOM element supporting the following content:
266 * <dd>identifies the remote resource</dd>
267 * <dt>verifyHost</dt>
268 * <dd>true iff name of host should be matched against TLS/SSL certificate</dd>
269 * <dt>TransportOption elements, like so:</dt>
270 * <dd><TransportOption provider="CURL" option="150">0</TransportOption></dd>
273 * @param e DOM to supply configuration
274 * @param systemId optional system identifier to attach to the source
275 * @param cacheTag optional pointer to string used for cache management
277 URLInputSource(const xercesc::DOMElement* e, const char* systemId=nullptr, std::string* cacheTag=nullptr);
280 virtual xercesc::BinInputStream* makeStream() const;
283 /** Element name used to signal a non-successful response when fetching a remote document. */
284 static const char asciiStatusCodeElementName[];
286 /** Element name used to signal a non-successful response when fetching a remote document. */
287 static const XMLCh utf16StatusCodeElementName[];
289 #ifdef XMLTOOLING_LITE
290 xercesc::XMLURL m_url;
292 std::string* m_cacheTag;
293 xmltooling::auto_ptr_char m_url;
294 const xercesc::DOMElement* m_root;
299 #if defined (_MSC_VER)
300 #pragma warning( pop )
303 #endif /* __xmltooling_pool_h__ */