2 * Copyright 2001-2008 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>
27 #include <xmltooling/util/Threads.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 {
50 * A thread-safe pool of DOMBuilders that share characteristics.
52 class XMLTOOL_API ParserPool :
53 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
54 public xercesc::DOMLSResourceResolver
56 public xercesc::DOMEntityResolver
59 MAKE_NONCOPYABLE(ParserPool);
62 * Constructs a new pool
64 * @param namespaceAware indicates whether parsers should be namespace-aware or not
65 * @param schemaAware indicates whether parsers should be schema-validating or not
67 ParserPool(bool namespaceAware=true, bool schemaAware=false);
71 * Creates a new document using a parser from this pool.
73 * @return new XML document
76 xercesc::DOMDocument* newDocument();
79 * Parses a document using a pooled parser with the proper settings
81 * @param domsrc An input source containing the content to be parsed
82 * @return The DOM document resulting from the parse
83 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
85 xercesc::DOMDocument* parse(
86 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
87 xercesc::DOMLSInput& domsrc
89 xercesc::DOMInputSource& domsrc
94 * Parses a document using a pooled parser with the proper settings
96 * @param is An input stream containing the content to be parsed
97 * @return The DOM document resulting from the parse
98 * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
100 xercesc::DOMDocument* parse(std::istream& is);
103 * Load an OASIS catalog file to map schema namespace URIs to filenames.
105 * This does not provide real catalog support; only the <uri> element
106 * is supported to map from a namespace URI to a relative path or file:// URI.
108 * @param pathname path to a catalog file
109 * @return true iff the catalog was successfully processed
111 bool loadCatalog(const XMLCh* pathname);
114 * Load a schema explicitly from a local file.
116 * Note that "successful processing" does not imply that the schema is valid,
117 * only that a reference to it was successfully registered with the pool.
119 * @param nsURI XML namespace to load
120 * @param pathname path to schema file
121 * @return true iff the schema was successfully processed
123 bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
126 * Supplies all external entities (primarily schemas) to the parser
128 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
129 xercesc::DOMLSInput* resolveResource(
130 const XMLCh *const resourceType,
131 const XMLCh *const namespaceUri,
132 const XMLCh *const publicId,
133 const XMLCh *const systemId,
134 const XMLCh *const baseURI
137 xercesc::DOMInputSource* resolveEntity(
138 const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
143 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
144 xercesc::DOMLSParser* createBuilder();
145 xercesc::DOMLSParser* checkoutBuilder();
146 void checkinBuilder(xercesc::DOMLSParser* builder);
148 xercesc::DOMBuilder* createBuilder();
149 xercesc::DOMBuilder* checkoutBuilder();
150 void checkinBuilder(xercesc::DOMBuilder* builder);
154 xstring m_schemaLocations;
155 std::map<xstring,xstring> m_schemaLocMap;
157 std::string m_schemaLocations;
158 std::map<std::string,std::string> m_schemaLocMap;
160 bool m_namespaceAware,m_schemaAware;
161 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
162 std::stack<xercesc::DOMLSParser*> m_pool;
164 std::stack<xercesc::DOMBuilder*> m_pool;
167 xercesc::SecurityManager* m_security;
171 * A parser source that wraps a C++ input stream
173 class XMLTOOL_API StreamInputSource : public xercesc::InputSource
175 MAKE_NONCOPYABLE(StreamInputSource);
178 * Constructs an input source around an input stream reference.
180 * @param is reference to an input stream
181 * @param systemId optional system identifier to attach to the stream
183 StreamInputSource(std::istream& is, const char* systemId=NULL) : xercesc::InputSource(systemId), m_is(is) {}
185 virtual xercesc::BinInputStream* makeStream() const { return new StreamBinInputStream(m_is); }
189 * A Xerces input stream that wraps a C++ input stream
191 class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
195 * Constructs a Xerces input stream around a C++ input stream reference.
197 * @param is reference to an input stream
199 StreamBinInputStream(std::istream& is) : m_is(is), m_pos(0) {}
201 #ifdef XMLTOOLING_XERCESC_64BITSAFE
206 curPos() const { return m_pos; }
207 xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
208 #ifdef XMLTOOLING_XERCESC_64BITSAFE
209 const XMLCh* getContentType() const { return NULL; }
222 * A URL-based parser source that supports a more advanced input stream.
224 class XMLTOOL_API URLInputSource : public xercesc::InputSource
226 MAKE_NONCOPYABLE(URLInputSource);
231 * @param url source of input
232 * @param systemId optional system identifier to attach to the source
234 URLInputSource(const XMLCh* url, const char* systemId=NULL);
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
251 URLInputSource(const xercesc::DOMElement* e, const char* systemId=NULL);
254 virtual xercesc::BinInputStream* makeStream() const;
258 #ifdef XMLTOOLING_LITE
259 xercesc::XMLURL m_url;
261 xmltooling::auto_ptr_char m_url;
262 const xercesc::DOMElement* m_root;
267 #if defined (_MSC_VER)
268 #pragma warning( pop )
271 #endif /* __xmltooling_pool_h__ */