2 * Copyright 2001-2009 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>
31 #include <xercesc/dom/DOM.hpp>
32 #include <xercesc/sax/InputSource.hpp>
33 #include <xercesc/util/BinInputStream.hpp>
34 #include <xercesc/util/SecurityManager.hpp>
35 #include <xercesc/util/XMLURL.hpp>
37 #ifndef XMLTOOLING_NO_XMLSEC
38 # include <xsec/framework/XSECDefs.hpp>
41 #if defined (_MSC_VER)
42 #pragma warning( push )
43 #pragma warning( disable : 4250 4251 )
46 namespace xmltooling {
48 class XMLTOOL_API Mutex;
51 * A thread-safe pool of DOMBuilders that share characteristics.
53 class XMLTOOL_API ParserPool :
54 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
55 public xercesc::DOMLSResourceResolver
57 public xercesc::DOMEntityResolver
60 MAKE_NONCOPYABLE(ParserPool);
63 * Constructs a new pool
65 * @param namespaceAware indicates whether parsers should be namespace-aware or not
66 * @param schemaAware indicates whether parsers should be schema-validating or not
68 ParserPool(bool namespaceAware=true, bool schemaAware=false);
72 * Creates a new document using a parser from this pool.
74 * @return new XML document
77 xercesc::DOMDocument* newDocument();
80 * Parses a document using a pooled parser with the proper settings
82 * @param domsrc An input source containing the content to be parsed
83 * @return The DOM document resulting from the parse
84 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
86 xercesc::DOMDocument* parse(
87 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
88 xercesc::DOMLSInput& domsrc
90 xercesc::DOMInputSource& domsrc
95 * Parses a document using a pooled parser with the proper settings
97 * @param is An input stream containing the content to be parsed
98 * @return The DOM document resulting from the parse
99 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
101 xercesc::DOMDocument* parse(std::istream& is);
104 * Load an OASIS catalog file to map schema namespace URIs to filenames.
106 * This does not provide real catalog support; only the <uri> element
107 * is supported to map from a namespace URI to a relative path or file:// URI.
109 * @param pathname path to a catalog file
110 * @return true iff the catalog was successfully processed
112 bool loadCatalog(const XMLCh* pathname);
115 * Load a schema explicitly from a local file.
117 * Note that "successful processing" does not imply that the schema is valid,
118 * only that a reference to it was successfully registered with the pool.
120 * @param nsURI XML namespace to load
121 * @param pathname path to schema file
122 * @return true iff the schema was successfully processed
124 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
127 * Supplies all external entities (primarily schemas) to the parser
129 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
130 xercesc::DOMLSInput* resolveResource(
131 const XMLCh *const resourceType,
132 const XMLCh *const namespaceUri,
133 const XMLCh *const publicId,
134 const XMLCh *const systemId,
135 const XMLCh *const baseURI
138 xercesc::DOMInputSource* resolveEntity(
139 const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
144 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
145 xercesc::DOMLSParser* createBuilder();
146 xercesc::DOMLSParser* checkoutBuilder();
147 void checkinBuilder(xercesc::DOMLSParser* builder);
149 xercesc::DOMBuilder* createBuilder();
150 xercesc::DOMBuilder* checkoutBuilder();
151 void checkinBuilder(xercesc::DOMBuilder* builder);
154 xstring m_schemaLocations;
155 std::map<xstring,xstring> m_schemaLocMap;
157 bool m_namespaceAware,m_schemaAware;
158 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
159 std::stack<xercesc::DOMLSParser*> m_pool;
161 std::stack<xercesc::DOMBuilder*> m_pool;
164 xercesc::SecurityManager* m_security;
168 * A parser source that wraps a C++ input stream
170 class XMLTOOL_API StreamInputSource : public xercesc::InputSource
172 MAKE_NONCOPYABLE(StreamInputSource);
175 * Constructs an input source around an input stream reference.
177 * @param is reference to an input stream
178 * @param systemId optional system identifier to attach to the stream
180 StreamInputSource(std::istream& is, const char* systemId=NULL);
182 xercesc::BinInputStream* makeStream() const;
186 * A Xerces input stream that wraps a C++ input stream
188 class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
192 * Constructs a Xerces input stream around a C++ input stream reference.
194 * @param is reference to an input stream
196 StreamBinInputStream(std::istream& is);
198 #ifdef XMLTOOLING_XERCESC_64BITSAFE
199 XMLFilePos curPos() const;
200 const XMLCh* getContentType() const;
202 unsigned int curPos() const;
204 xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
216 * A URL-based parser source that supports a more advanced input stream.
218 class XMLTOOL_API URLInputSource : public xercesc::InputSource
220 MAKE_NONCOPYABLE(URLInputSource);
225 * @param url source of input
226 * @param systemId optional system identifier to attach to the source
228 URLInputSource(const XMLCh* url, const char* systemId=NULL);
231 * Constructor taking a DOM element supporting the following content:
235 * <dd>identifies the remote resource</dd>
236 * <dt>verifyHost</dt>
237 * <dd>true iff name of host should be matched against TLS/SSL certificate</dd>
238 * <dt>TransportOption elements, like so:</dt>
239 * <dd><TransportOption provider="CURL" option="150">0</TransportOption></dd>
242 * @param e DOM to supply configuration
243 * @param systemId optional system identifier to attach to the source
245 URLInputSource(const xercesc::DOMElement* e, const char* systemId=NULL);
248 virtual xercesc::BinInputStream* makeStream() const;
252 #ifdef XMLTOOLING_LITE
253 xercesc::XMLURL m_url;
255 xmltooling::auto_ptr_char m_url;
256 const xercesc::DOMElement* m_root;
261 #if defined (_MSC_VER)
262 #pragma warning( pop )
265 #endif /* __xmltooling_pool_h__ */