Rework support for libcurl-based input to parser.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / ParserPool.h
1 /*
2  *  Copyright 2001-2008 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file xmltooling/util/ParserPool.h
19  * 
20  * A thread-safe pool of parsers that share characteristics.
21  */
22
23 #ifndef __xmltooling_pool_h__
24 #define __xmltooling_pool_h__
25
26 #include <xmltooling/unicode.h>
27 #include <xmltooling/util/Threads.h>
28
29 #include <map>
30 #include <stack>
31 #include <istream>
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>
37
38 #ifndef XMLTOOLING_NO_XMLSEC
39 # include <xsec/framework/XSECDefs.hpp>
40 #endif
41
42 #if defined (_MSC_VER)
43     #pragma warning( push )
44     #pragma warning( disable : 4250 4251 )
45 #endif
46
47 namespace xmltooling {
48
49     /**
50      * A thread-safe pool of DOMBuilders that share characteristics.
51      */
52     class XMLTOOL_API ParserPool :
53 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
54         public xercesc::DOMLSResourceResolver
55 #else
56         public xercesc::DOMEntityResolver
57 #endif
58     {
59         MAKE_NONCOPYABLE(ParserPool);
60     public:
61         /**
62          * Constructs a new pool
63          * 
64          * @param namespaceAware    indicates whether parsers should be namespace-aware or not
65          * @param schemaAware       indicates whether parsers should be schema-validating or not
66          */
67         ParserPool(bool namespaceAware=true, bool schemaAware=false);
68         ~ParserPool();
69
70         /**
71          * Creates a new document using a parser from this pool.
72          * 
73          * @return new XML document
74          * 
75          */
76         xercesc::DOMDocument* newDocument();
77
78         /**
79          * Parses a document using a pooled parser with the proper settings
80          * 
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
84          */
85         xercesc::DOMDocument* parse(
86 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
87             xercesc::DOMLSInput& domsrc
88 #else
89             xercesc::DOMInputSource& domsrc
90 #endif
91             );
92
93         /**
94          * Parses a document using a pooled parser with the proper settings
95          * 
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
99          */
100         xercesc::DOMDocument* parse(std::istream& is);
101
102         /**
103          * Load an OASIS catalog file to map schema namespace URIs to filenames.
104          * 
105          * This does not provide real catalog support; only the &lt;uri&gt; element
106          * is supported to map from a namespace URI to a relative path or file:// URI.
107          * 
108          * @param pathname  path to a catalog file
109          * @return true iff the catalog was successfully processed
110          */
111         bool loadCatalog(const XMLCh* pathname);
112         
113         /**
114          * Load a schema explicitly from a local file.
115          * 
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.
118          * 
119          * @param nsURI     XML namespace to load
120          * @param pathname  path to schema file
121          * @return true iff the schema was successfully processed
122          */
123         bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
124
125         /**
126          * Supplies all external entities (primarily schemas) to the parser
127          */
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
135             );
136 #else
137         xercesc::DOMInputSource* resolveEntity(
138             const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
139             );
140 #endif
141
142     private:
143 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
144         xercesc::DOMLSParser* createBuilder();
145         xercesc::DOMLSParser* checkoutBuilder();
146         void checkinBuilder(xercesc::DOMLSParser* builder);
147 #else
148         xercesc::DOMBuilder* createBuilder();
149         xercesc::DOMBuilder* checkoutBuilder();
150         void checkinBuilder(xercesc::DOMBuilder* builder);
151 #endif
152
153 #ifdef HAVE_GOOD_STL
154         xstring m_schemaLocations;
155         std::map<xstring,xstring> m_schemaLocMap;
156 #else
157         std::string m_schemaLocations;
158         std::map<std::string,std::string> m_schemaLocMap;
159 #endif
160         bool m_namespaceAware,m_schemaAware;
161 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
162         std::stack<xercesc::DOMLSParser*> m_pool;
163 #else
164         std::stack<xercesc::DOMBuilder*> m_pool;
165 #endif
166         Mutex* m_lock;
167         xercesc::SecurityManager* m_security;
168     };
169
170     /**
171      * A parser source that wraps a C++ input stream
172      */
173     class XMLTOOL_API StreamInputSource : public xercesc::InputSource
174     {
175     MAKE_NONCOPYABLE(StreamInputSource);
176     public:
177         /**
178          * Constructs an input source around an input stream reference.
179          * 
180          * @param is        reference to an input stream
181          * @param systemId  optional system identifier to attach to the stream
182          */
183         StreamInputSource(std::istream& is, const char* systemId=NULL) : xercesc::InputSource(systemId), m_is(is) {}
184         /// @cond off
185         virtual xercesc::BinInputStream* makeStream() const { return new StreamBinInputStream(m_is); }
186         /// @endcond
187
188         /**
189          * A Xerces input stream that wraps a C++ input stream
190          */
191         class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
192         {
193         public:
194             /**
195              * Constructs a Xerces input stream around a C++ input stream reference.
196              * 
197              * @param is            reference to an input stream
198              */
199             StreamBinInputStream(std::istream& is) : m_is(is), m_pos(0) {}
200             /// @cond off
201 #ifdef XMLTOOLING_XERCESC_64BITSAFE
202             XMLFilePos
203 #else
204             unsigned int
205 #endif
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; }
210 #endif
211             /// @endcond
212         private:
213             std::istream& m_is;
214             xsecsize_t m_pos;
215         };
216
217     private:
218         std::istream& m_is;
219     };
220
221     /**
222      * A URL-based parser source that supports a more advanced input stream.
223      */
224     class XMLTOOL_API URLInputSource : public xercesc::InputSource
225     {
226     MAKE_NONCOPYABLE(URLInputSource);
227     public:
228         /**
229          * Constructor.
230          * 
231          * @param url       source of input
232          * @param systemId  optional system identifier to attach to the source
233          */
234         URLInputSource(const XMLCh* url, const char* systemId=NULL);
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          */
251         URLInputSource(const xercesc::DOMElement* e, const char* systemId=NULL);
252
253         /// @cond off
254         virtual xercesc::BinInputStream* makeStream() const;
255         /// @endcond
256
257     private:
258 #ifdef XMLTOOLING_LITE
259         xercesc::XMLURL m_url;
260 #else
261         xmltooling::auto_ptr_char m_url;
262         const xercesc::DOMElement* m_root;
263 #endif
264     };
265 };
266
267 #if defined (_MSC_VER)
268     #pragma warning( pop )
269 #endif
270
271 #endif /* __xmltooling_pool_h__ */