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);
153 xstring m_schemaLocations;
154 std::map<xstring,xstring> m_schemaLocMap;
156 bool m_namespaceAware,m_schemaAware;
157 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
158 std::stack<xercesc::DOMLSParser*> m_pool;
160 std::stack<xercesc::DOMBuilder*> m_pool;
163 xercesc::SecurityManager* m_security;
167 * A parser source that wraps a C++ input stream
169 class XMLTOOL_API StreamInputSource : public xercesc::InputSource
171 MAKE_NONCOPYABLE(StreamInputSource);
174 * Constructs an input source around an input stream reference.
176 * @param is reference to an input stream
177 * @param systemId optional system identifier to attach to the stream
179 StreamInputSource(std::istream& is, const char* systemId=NULL) : xercesc::InputSource(systemId), m_is(is) {}
181 virtual xercesc::BinInputStream* makeStream() const { return new StreamBinInputStream(m_is); }
185 * A Xerces input stream that wraps a C++ input stream
187 class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
191 * Constructs a Xerces input stream around a C++ input stream reference.
193 * @param is reference to an input stream
195 StreamBinInputStream(std::istream& is) : m_is(is), m_pos(0) {}
197 #ifdef XMLTOOLING_XERCESC_64BITSAFE
202 curPos() const { return m_pos; }
203 xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
204 #ifdef XMLTOOLING_XERCESC_64BITSAFE
205 const XMLCh* getContentType() const { return NULL; }
218 * A URL-based parser source that supports a more advanced input stream.
220 class XMLTOOL_API URLInputSource : public xercesc::InputSource
222 MAKE_NONCOPYABLE(URLInputSource);
227 * @param url source of input
228 * @param systemId optional system identifier to attach to the source
230 URLInputSource(const XMLCh* url, const char* systemId=NULL);
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
247 URLInputSource(const xercesc::DOMElement* e, const char* systemId=NULL);
250 virtual xercesc::BinInputStream* makeStream() const;
254 #ifdef XMLTOOLING_LITE
255 xercesc::XMLURL m_url;
257 xmltooling::auto_ptr_char m_url;
258 const xercesc::DOMElement* m_root;
263 #if defined (_MSC_VER)
264 #pragma warning( pop )
267 #endif /* __xmltooling_pool_h__ */