Merge branch '1.x' of ssh://authdev.it.ohio-state.edu/~scantor/git/cpp-xmltooling...
[shibboleth/cpp-xmltooling.git] / xmltooling / util / ParserPool.h
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * @file xmltooling/util/ParserPool.h
23  *
24  * A thread-safe pool of parsers that share characteristics.
25  */
26
27 #ifndef __xmltooling_pool_h__
28 #define __xmltooling_pool_h__
29
30 #include <xmltooling/unicode.h>
31
32 #include <map>
33 #include <stack>
34 #include <string>
35 #include <istream>
36 #include <xercesc/dom/DOM.hpp>
37 #include <xercesc/sax/InputSource.hpp>
38 #include <xercesc/util/BinInputStream.hpp>
39 #include <xercesc/util/SecurityManager.hpp>
40 #include <xercesc/util/XMLURL.hpp>
41
42 #ifndef XMLTOOLING_NO_XMLSEC
43 # include <xsec/framework/XSECDefs.hpp>
44 #endif
45
46 #if defined (_MSC_VER)
47     #pragma warning( push )
48     #pragma warning( disable : 4250 4251 )
49 #endif
50
51 namespace xmltooling {
52
53     class XMLTOOL_API Mutex;
54
55     /**
56      * A thread-safe pool of DOMBuilders that share characteristics.
57      */
58     class XMLTOOL_API ParserPool :
59 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
60         public xercesc::DOMLSResourceResolver
61 #else
62         public xercesc::DOMEntityResolver
63 #endif
64     {
65         MAKE_NONCOPYABLE(ParserPool);
66     public:
67         /**
68          * Constructs a new pool
69          *
70          * @param namespaceAware    indicates whether parsers should be namespace-aware or not
71          * @param schemaAware       indicates whether parsers should be schema-validating or not
72          */
73         ParserPool(bool namespaceAware=true, bool schemaAware=false);
74         ~ParserPool();
75
76         /**
77          * Creates a new document using a parser from this pool.
78          *
79          * @return new XML document
80          *
81          */
82         xercesc::DOMDocument* newDocument();
83
84         /**
85          * Parses a document using a pooled parser with the proper settings
86          *
87          * @param domsrc An input source containing the content to be parsed
88          * @return The DOM document resulting from the parse
89          * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
90          */
91         xercesc::DOMDocument* parse(
92 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
93             xercesc::DOMLSInput& domsrc
94 #else
95             xercesc::DOMInputSource& domsrc
96 #endif
97             );
98
99         /**
100          * Parses a document using a pooled parser with the proper settings
101          *
102          * @param is An input stream containing the content to be parsed
103          * @return The DOM document resulting from the parse
104          * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
105          */
106         xercesc::DOMDocument* parse(std::istream& is);
107
108         /**
109          * Load an OASIS catalog file to map schema namespace URIs to filenames.
110          *
111          * This does not provide real catalog support; only the &lt;uri&gt; element
112          * is supported to map from a namespace URI to a relative path or file:// URI.
113          *
114          * @param pathname  path to a catalog file
115          * @return true iff the catalog was successfully processed
116          */
117         bool loadCatalog(const XMLCh* pathname);
118
119         /**
120          * Load a schema explicitly from a local file.
121          *
122          * Note that "successful processing" does not imply that the schema is valid,
123          * only that a reference to it was successfully registered with the pool.
124          *
125          * @param nsURI     XML namespace to load
126          * @param pathname  path to schema file
127          * @return true iff the schema was successfully processed
128          */
129         bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
130
131         /**
132          * Supplies all external entities (primarily schemas) to the parser
133          */
134 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
135         xercesc::DOMLSInput* resolveResource(
136             const XMLCh *const resourceType,
137             const XMLCh *const namespaceUri,
138             const XMLCh *const publicId,
139             const XMLCh *const systemId,
140             const XMLCh *const baseURI
141             );
142 #else
143         xercesc::DOMInputSource* resolveEntity(
144             const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
145             );
146 #endif
147
148     private:
149 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
150         xercesc::DOMLSParser* createBuilder();
151         xercesc::DOMLSParser* checkoutBuilder();
152         void checkinBuilder(xercesc::DOMLSParser* builder);
153 #else
154         xercesc::DOMBuilder* createBuilder();
155         xercesc::DOMBuilder* checkoutBuilder();
156         void checkinBuilder(xercesc::DOMBuilder* builder);
157 #endif
158
159         xstring m_schemaLocations;
160         std::map<xstring,xstring> m_schemaLocMap;
161
162         bool m_namespaceAware,m_schemaAware;
163 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
164         std::stack<xercesc::DOMLSParser*> m_pool;
165 #else
166         std::stack<xercesc::DOMBuilder*> m_pool;
167 #endif
168         Mutex* m_lock;
169         xercesc::SecurityManager* m_security;
170     };
171
172     /**
173      * A parser source that wraps a C++ input stream
174      */
175     class XMLTOOL_API StreamInputSource : public xercesc::InputSource
176     {
177     MAKE_NONCOPYABLE(StreamInputSource);
178     public:
179         /**
180          * Constructs an input source around an input stream reference.
181          *
182          * @param is        reference to an input stream
183          * @param systemId  optional system identifier to attach to the stream
184          */
185         StreamInputSource(std::istream& is, const char* systemId=nullptr);
186         /// @cond off
187         xercesc::BinInputStream* makeStream() const;
188         /// @endcond
189
190         /**
191          * A Xerces input stream that wraps a C++ input stream
192          */
193         class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
194         {
195         public:
196             /**
197              * Constructs a Xerces input stream around a C++ input stream reference.
198              *
199              * @param is            reference to an input stream
200              */
201             StreamBinInputStream(std::istream& is);
202             /// @cond off
203 #ifdef XMLTOOLING_XERCESC_64BITSAFE
204             XMLFilePos curPos() const;
205             const XMLCh* getContentType() const;
206 #else
207             unsigned int curPos() const;
208 #endif
209             xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
210             /// @endcond
211         private:
212             std::istream& m_is;
213             xsecsize_t m_pos;
214         };
215
216     private:
217         std::istream& m_is;
218     };
219
220     /**
221      * A URL-based parser source that supports a more advanced input stream.
222      */
223     class XMLTOOL_API URLInputSource : public xercesc::InputSource
224     {
225     MAKE_NONCOPYABLE(URLInputSource);
226     public:
227         /**
228          * Constructor.
229          *
230          * @param url       source of input
231          * @param systemId  optional system identifier to attach to the source
232          * @param cacheTag  optional pointer to string used for cache management
233          */
234         URLInputSource(const XMLCh* url, const char* systemId=nullptr, std::string* cacheTag=nullptr);
235
236         /**
237          * Constructor taking a DOM element supporting the following content:
238          *
239          * <dl>
240          *  <dt>uri | url</dt>
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>&lt;TransportOption provider="CURL" option="150"&gt;0&lt;/TransportOption&gt;</dd>
246          * </dl>
247          *
248          * @param e         DOM to supply configuration
249          * @param systemId  optional system identifier to attach to the source
250          * @param cacheTag  optional pointer to string used for cache management
251          */
252         URLInputSource(const xercesc::DOMElement* e, const char* systemId=nullptr, std::string* cacheTag=nullptr);
253
254         /// @cond off
255         virtual xercesc::BinInputStream* makeStream() const;
256         /// @endcond
257
258         /** Element name used to signal a non-successful response when fetching a remote document. */
259         static const char asciiStatusCodeElementName[];
260
261         /** Element name used to signal a non-successful response when fetching a remote document. */
262         static const XMLCh utf16StatusCodeElementName[];
263     private:
264 #ifdef XMLTOOLING_LITE
265         xercesc::XMLURL m_url;
266 #else
267         std::string* m_cacheTag;
268         xmltooling::auto_ptr_char m_url;
269         const xercesc::DOMElement* m_root;
270 #endif
271     };
272 };
273
274 #if defined (_MSC_VER)
275     #pragma warning( pop )
276 #endif
277
278 #endif /* __xmltooling_pool_h__ */