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