2 * Copyright 2001-2010 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file xmltooling/util/ParserPool.h
20 * A thread-safe pool of parsers that share characteristics.
23 #ifndef __xmltooling_pool_h__
24 #define __xmltooling_pool_h__
26 #include <xmltooling/unicode.h>
32 #include <xercesc/dom/DOM.hpp>
33 #include <xercesc/sax/InputSource.hpp>
34 #include <xercesc/util/BinInputStream.hpp>
35 #include <xercesc/util/SecurityManager.hpp>
36 #include <xercesc/util/XMLURL.hpp>
38 #ifndef XMLTOOLING_NO_XMLSEC
39 # include <xsec/framework/XSECDefs.hpp>
42 #if defined (_MSC_VER)
43 #pragma warning( push )
44 #pragma warning( disable : 4250 4251 )
47 namespace xmltooling {
49 class XMLTOOL_API Mutex;
52 * A thread-safe pool of DOMBuilders that share characteristics.
54 class XMLTOOL_API ParserPool :
55 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
56 public xercesc::DOMLSResourceResolver
58 public xercesc::DOMEntityResolver
61 MAKE_NONCOPYABLE(ParserPool);
64 * Constructs a new pool
66 * @param namespaceAware indicates whether parsers should be namespace-aware or not
67 * @param schemaAware indicates whether parsers should be schema-validating or not
69 ParserPool(bool namespaceAware=true, bool schemaAware=false);
73 * Creates a new document using a parser from this pool.
75 * @return new XML document
78 xercesc::DOMDocument* newDocument();
81 * Parses a document using a pooled parser with the proper settings
83 * @param domsrc An input source containing the content to be parsed
84 * @return The DOM document resulting from the parse
85 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
87 xercesc::DOMDocument* parse(
88 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
89 xercesc::DOMLSInput& domsrc
91 xercesc::DOMInputSource& domsrc
96 * Parses a document using a pooled parser with the proper settings
98 * @param is An input stream containing the content to be parsed
99 * @return The DOM document resulting from the parse
100 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
102 xercesc::DOMDocument* parse(std::istream& is);
105 * Load an OASIS catalog file to map schema namespace URIs to filenames.
107 * This does not provide real catalog support; only the <uri> element
108 * is supported to map from a namespace URI to a relative path or file:// URI.
110 * @param pathname path to a catalog file
111 * @return true iff the catalog was successfully processed
113 bool loadCatalog(const XMLCh* pathname);
116 * Load a schema explicitly from a local file.
118 * Note that "successful processing" does not imply that the schema is valid,
119 * only that a reference to it was successfully registered with the pool.
121 * @param nsURI XML namespace to load
122 * @param pathname path to schema file
123 * @return true iff the schema was successfully processed
125 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
128 * Supplies all external entities (primarily schemas) to the parser
130 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
131 xercesc::DOMLSInput* resolveResource(
132 const XMLCh *const resourceType,
133 const XMLCh *const namespaceUri,
134 const XMLCh *const publicId,
135 const XMLCh *const systemId,
136 const XMLCh *const baseURI
139 xercesc::DOMInputSource* resolveEntity(
140 const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
145 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
146 xercesc::DOMLSParser* createBuilder();
147 xercesc::DOMLSParser* checkoutBuilder();
148 void checkinBuilder(xercesc::DOMLSParser* builder);
150 xercesc::DOMBuilder* createBuilder();
151 xercesc::DOMBuilder* checkoutBuilder();
152 void checkinBuilder(xercesc::DOMBuilder* builder);
155 xstring m_schemaLocations;
156 std::map<xstring,xstring> m_schemaLocMap;
158 bool m_namespaceAware,m_schemaAware;
159 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
160 std::stack<xercesc::DOMLSParser*> m_pool;
162 std::stack<xercesc::DOMBuilder*> m_pool;
165 xercesc::SecurityManager* m_security;
169 * A parser source that wraps a C++ input stream
171 class XMLTOOL_API StreamInputSource : public xercesc::InputSource
173 MAKE_NONCOPYABLE(StreamInputSource);
176 * Constructs an input source around an input stream reference.
178 * @param is reference to an input stream
179 * @param systemId optional system identifier to attach to the stream
181 StreamInputSource(std::istream& is, const char* systemId=nullptr);
183 xercesc::BinInputStream* makeStream() const;
187 * A Xerces input stream that wraps a C++ input stream
189 class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
193 * Constructs a Xerces input stream around a C++ input stream reference.
195 * @param is reference to an input stream
197 StreamBinInputStream(std::istream& is);
199 #ifdef XMLTOOLING_XERCESC_64BITSAFE
200 XMLFilePos curPos() const;
201 const XMLCh* getContentType() const;
203 unsigned int curPos() const;
205 xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
217 * A URL-based parser source that supports a more advanced input stream.
219 class XMLTOOL_API URLInputSource : public xercesc::InputSource
221 MAKE_NONCOPYABLE(URLInputSource);
226 * @param url source of input
227 * @param systemId optional system identifier to attach to the source
228 * @param cacheTag optional pointer to string used for cache management
230 URLInputSource(const XMLCh* url, const char* systemId=nullptr, std::string* cacheTag=nullptr);
233 * Constructor taking a DOM element supporting the following content:
237 * <dd>identifies the remote resource</dd>
238 * <dt>verifyHost</dt>
239 * <dd>true iff name of host should be matched against TLS/SSL certificate</dd>
240 * <dt>TransportOption elements, like so:</dt>
241 * <dd><TransportOption provider="CURL" option="150">0</TransportOption></dd>
244 * @param e DOM to supply configuration
245 * @param systemId optional system identifier to attach to the source
246 * @param cacheTag optional pointer to string used for cache management
248 URLInputSource(const xercesc::DOMElement* e, const char* systemId=nullptr, std::string* cacheTag=nullptr);
251 virtual xercesc::BinInputStream* makeStream() const;
254 /** Element name used to signal a non-successful response when fetching a remote document. */
255 static const char asciiStatusCodeElementName[];
257 /** Element name used to signal a non-successful response when fetching a remote document. */
258 static const XMLCh utf16StatusCodeElementName[];
260 #ifdef XMLTOOLING_LITE
261 xercesc::XMLURL m_url;
263 std::string* m_cacheTag;
264 xmltooling::auto_ptr_char m_url;
265 const xercesc::DOMElement* m_root;
270 #if defined (_MSC_VER)
271 #pragma warning( pop )
274 #endif /* __xmltooling_pool_h__ */