Add ASCII loadCatalog method.
[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 char* pathname);
118
119         /**
120          * Load an OASIS catalog file to map schema namespace URIs to filenames.
121          *
122          * This does not provide real catalog support; only the &lt;uri&gt; element
123          * is supported to map from a namespace URI to a relative path or file:// URI.
124          *
125          * @param pathname  path to a catalog file
126          * @return true iff the catalog was successfully processed
127          */
128         bool loadCatalog(const XMLCh* pathname);
129
130         /**
131          * Load a schema explicitly from a local file.
132          *
133          * Note that "successful processing" does not imply that the schema is valid,
134          * only that a reference to it was successfully registered with the pool.
135          *
136          * @param nsURI     XML namespace to load
137          * @param pathname  path to schema file
138          * @return true iff the schema was successfully processed
139          */
140         bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
141
142         /**
143          * Supplies all external entities (primarily schemas) to the parser
144          */
145 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
146         xercesc::DOMLSInput* resolveResource(
147             const XMLCh *const resourceType,
148             const XMLCh *const namespaceUri,
149             const XMLCh *const publicId,
150             const XMLCh *const systemId,
151             const XMLCh *const baseURI
152             );
153 #else
154         xercesc::DOMInputSource* resolveEntity(
155             const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
156             );
157 #endif
158
159     private:
160 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
161         xercesc::DOMLSParser* createBuilder();
162         xercesc::DOMLSParser* checkoutBuilder();
163         void checkinBuilder(xercesc::DOMLSParser* builder);
164 #else
165         xercesc::DOMBuilder* createBuilder();
166         xercesc::DOMBuilder* checkoutBuilder();
167         void checkinBuilder(xercesc::DOMBuilder* builder);
168 #endif
169
170         xstring m_schemaLocations;
171         std::map<xstring,xstring> m_schemaLocMap;
172
173         bool m_namespaceAware,m_schemaAware;
174 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
175         std::stack<xercesc::DOMLSParser*> m_pool;
176 #else
177         std::stack<xercesc::DOMBuilder*> m_pool;
178 #endif
179         Mutex* m_lock;
180         xercesc::SecurityManager* m_security;
181     };
182
183     /**
184      * A parser source that wraps a C++ input stream
185      */
186     class XMLTOOL_API StreamInputSource : public xercesc::InputSource
187     {
188     MAKE_NONCOPYABLE(StreamInputSource);
189     public:
190         /**
191          * Constructs an input source around an input stream reference.
192          *
193          * @param is        reference to an input stream
194          * @param systemId  optional system identifier to attach to the stream
195          */
196         StreamInputSource(std::istream& is, const char* systemId=nullptr);
197         /// @cond off
198         xercesc::BinInputStream* makeStream() const;
199         /// @endcond
200
201         /**
202          * A Xerces input stream that wraps a C++ input stream
203          */
204         class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
205         {
206         public:
207             /**
208              * Constructs a Xerces input stream around a C++ input stream reference.
209              *
210              * @param is            reference to an input stream
211              */
212             StreamBinInputStream(std::istream& is);
213             /// @cond off
214 #ifdef XMLTOOLING_XERCESC_64BITSAFE
215             XMLFilePos curPos() const;
216             const XMLCh* getContentType() const;
217 #else
218             unsigned int curPos() const;
219 #endif
220             xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
221             /// @endcond
222         private:
223             std::istream& m_is;
224             xsecsize_t m_pos;
225         };
226
227     private:
228         std::istream& m_is;
229     };
230
231     /**
232      * A URL-based parser source that supports a more advanced input stream.
233      */
234     class XMLTOOL_API URLInputSource : public xercesc::InputSource
235     {
236     MAKE_NONCOPYABLE(URLInputSource);
237     public:
238         /**
239          * Constructor.
240          *
241          * @param url       source of input
242          * @param systemId  optional system identifier to attach to the source
243          * @param cacheTag  optional pointer to string used for cache management
244          */
245         URLInputSource(const XMLCh* url, const char* systemId=nullptr, std::string* cacheTag=nullptr);
246
247         /**
248          * Constructor taking a DOM element supporting the following content:
249          *
250          * <dl>
251          *  <dt>uri | url</dt>
252          *  <dd>identifies the remote resource</dd>
253          *  <dt>verifyHost</dt>
254          *  <dd>true iff name of host should be matched against TLS/SSL certificate</dd>
255          *  <dt>TransportOption elements, like so:</dt>
256          *  <dd>&lt;TransportOption provider="CURL" option="150"&gt;0&lt;/TransportOption&gt;</dd>
257          * </dl>
258          *
259          * @param e         DOM to supply configuration
260          * @param systemId  optional system identifier to attach to the source
261          * @param cacheTag  optional pointer to string used for cache management
262          */
263         URLInputSource(const xercesc::DOMElement* e, const char* systemId=nullptr, std::string* cacheTag=nullptr);
264
265         /// @cond off
266         virtual xercesc::BinInputStream* makeStream() const;
267         /// @endcond
268
269         /** Element name used to signal a non-successful response when fetching a remote document. */
270         static const char asciiStatusCodeElementName[];
271
272         /** Element name used to signal a non-successful response when fetching a remote document. */
273         static const XMLCh utf16StatusCodeElementName[];
274     private:
275 #ifdef XMLTOOLING_LITE
276         xercesc::XMLURL m_url;
277 #else
278         std::string* m_cacheTag;
279         xmltooling::auto_ptr_char m_url;
280         const xercesc::DOMElement* m_root;
281 #endif
282     };
283 };
284
285 #if defined (_MSC_VER)
286     #pragma warning( pop )
287 #endif
288
289 #endif /* __xmltooling_pool_h__ */