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 an OASIS catalog file to map schema namespace URIs to filenames.
112 * 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 * @param pathname path to a catalog file
116 * @return true iff the catalog was successfully processed
118 bool loadCatalog(const char* pathname);
121 * Load an OASIS catalog file to map schema namespace URIs to filenames.
123 * This does not provide real catalog support; only the <uri> element
124 * is supported to map from a namespace URI to a relative path or file:// URI.
126 * @param pathname path to a catalog file
127 * @return true iff the catalog was successfully processed
129 bool loadCatalog(const XMLCh* pathname);
132 * Load a schema explicitly from a local file.
134 * Note that "successful processing" does not imply that the schema is valid,
135 * only that a reference to it was successfully registered with the pool.
137 * @param nsURI XML namespace to load
138 * @param pathname path to schema file
139 * @return true iff the schema was successfully processed
141 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
144 * Supplies all external entities (primarily schemas) to the parser
146 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
147 xercesc::DOMLSInput* resolveResource(
148 const XMLCh *const resourceType,
149 const XMLCh *const namespaceUri,
150 const XMLCh *const publicId,
151 const XMLCh *const systemId,
152 const XMLCh *const baseURI
155 xercesc::DOMInputSource* resolveEntity(
156 const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
161 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
162 xercesc::DOMLSParser* createBuilder();
163 xercesc::DOMLSParser* checkoutBuilder();
164 void checkinBuilder(xercesc::DOMLSParser* builder);
166 xercesc::DOMBuilder* createBuilder();
167 xercesc::DOMBuilder* checkoutBuilder();
168 void checkinBuilder(xercesc::DOMBuilder* builder);
171 xstring m_schemaLocations;
172 std::map<xstring,xstring> m_schemaLocMap;
174 bool m_namespaceAware,m_schemaAware;
175 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
176 std::stack<xercesc::DOMLSParser*> m_pool;
178 std::stack<xercesc::DOMBuilder*> m_pool;
180 std::auto_ptr<Mutex> m_lock;
181 std::auto_ptr<xercesc::SecurityManager> m_security;
185 * A parser source that wraps a C++ input stream
187 class XMLTOOL_API StreamInputSource : public xercesc::InputSource
189 MAKE_NONCOPYABLE(StreamInputSource);
192 * Constructs an input source around an input stream reference.
194 * @param is reference to an input stream
195 * @param systemId optional system identifier to attach to the stream
197 StreamInputSource(std::istream& is, const char* systemId=nullptr);
199 xercesc::BinInputStream* makeStream() const;
203 * A Xerces input stream that wraps a C++ input stream
205 class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
209 * Constructs a Xerces input stream around a C++ input stream reference.
211 * @param is reference to an input stream
213 StreamBinInputStream(std::istream& is);
215 #ifdef XMLTOOLING_XERCESC_64BITSAFE
216 XMLFilePos curPos() const;
217 const XMLCh* getContentType() const;
219 unsigned int curPos() const;
221 xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
233 * A URL-based parser source that supports a more advanced input stream.
235 class XMLTOOL_API URLInputSource : public xercesc::InputSource
237 MAKE_NONCOPYABLE(URLInputSource);
242 * @param url source of input
243 * @param systemId optional system identifier to attach to the source
244 * @param cacheTag optional pointer to string used for cache management
246 URLInputSource(const XMLCh* url, const char* systemId=nullptr, std::string* cacheTag=nullptr);
249 * Constructor taking a DOM element supporting the following content:
253 * <dd>identifies the remote resource</dd>
254 * <dt>verifyHost</dt>
255 * <dd>true iff name of host should be matched against TLS/SSL certificate</dd>
256 * <dt>TransportOption elements, like so:</dt>
257 * <dd><TransportOption provider="CURL" option="150">0</TransportOption></dd>
260 * @param e DOM to supply configuration
261 * @param systemId optional system identifier to attach to the source
262 * @param cacheTag optional pointer to string used for cache management
264 URLInputSource(const xercesc::DOMElement* e, const char* systemId=nullptr, std::string* cacheTag=nullptr);
267 virtual xercesc::BinInputStream* makeStream() const;
270 /** Element name used to signal a non-successful response when fetching a remote document. */
271 static const char asciiStatusCodeElementName[];
273 /** Element name used to signal a non-successful response when fetching a remote document. */
274 static const XMLCh utf16StatusCodeElementName[];
276 #ifdef XMLTOOLING_LITE
277 xercesc::XMLURL m_url;
279 std::string* m_cacheTag;
280 xmltooling::auto_ptr_char m_url;
281 const xercesc::DOMElement* m_root;
286 #if defined (_MSC_VER)
287 #pragma warning( pop )
290 #endif /* __xmltooling_pool_h__ */