e901a0219d8303b611f0630e9708a037e22f986d
[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         xstring m_schemaLocations;
154         std::map<xstring,xstring> m_schemaLocMap;
155
156         bool m_namespaceAware,m_schemaAware;
157 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
158         std::stack<xercesc::DOMLSParser*> m_pool;
159 #else
160         std::stack<xercesc::DOMBuilder*> m_pool;
161 #endif
162         Mutex* m_lock;
163         xercesc::SecurityManager* m_security;
164     };
165
166     /**
167      * A parser source that wraps a C++ input stream
168      */
169     class XMLTOOL_API StreamInputSource : public xercesc::InputSource
170     {
171     MAKE_NONCOPYABLE(StreamInputSource);
172     public:
173         /**
174          * Constructs an input source around an input stream reference.
175          *
176          * @param is        reference to an input stream
177          * @param systemId  optional system identifier to attach to the stream
178          */
179         StreamInputSource(std::istream& is, const char* systemId=NULL) : xercesc::InputSource(systemId), m_is(is) {}
180         /// @cond off
181         virtual xercesc::BinInputStream* makeStream() const { return new StreamBinInputStream(m_is); }
182         /// @endcond
183
184         /**
185          * A Xerces input stream that wraps a C++ input stream
186          */
187         class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
188         {
189         public:
190             /**
191              * Constructs a Xerces input stream around a C++ input stream reference.
192              *
193              * @param is            reference to an input stream
194              */
195             StreamBinInputStream(std::istream& is) : m_is(is), m_pos(0) {}
196             /// @cond off
197 #ifdef XMLTOOLING_XERCESC_64BITSAFE
198             XMLFilePos
199 #else
200             unsigned int
201 #endif
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; }
206 #endif
207             /// @endcond
208         private:
209             std::istream& m_is;
210             xsecsize_t m_pos;
211         };
212
213     private:
214         std::istream& m_is;
215     };
216
217     /**
218      * A URL-based parser source that supports a more advanced input stream.
219      */
220     class XMLTOOL_API URLInputSource : public xercesc::InputSource
221     {
222     MAKE_NONCOPYABLE(URLInputSource);
223     public:
224         /**
225          * Constructor.
226          *
227          * @param url       source of input
228          * @param systemId  optional system identifier to attach to the source
229          */
230         URLInputSource(const XMLCh* url, const char* systemId=NULL);
231
232         /**
233          * Constructor taking a DOM element supporting the following content:
234          *
235          * <dl>
236          *  <dt>uri | url</dt>
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>&lt;TransportOption provider="CURL" option="150"&gt;0&lt;/TransportOption&gt;</dd>
242          * </dl>
243          *
244          * @param e         DOM to supply configuration
245          * @param systemId  optional system identifier to attach to the source
246          */
247         URLInputSource(const xercesc::DOMElement* e, const char* systemId=NULL);
248
249         /// @cond off
250         virtual xercesc::BinInputStream* makeStream() const;
251         /// @endcond
252
253     private:
254 #ifdef XMLTOOLING_LITE
255         xercesc::XMLURL m_url;
256 #else
257         xmltooling::auto_ptr_char m_url;
258         const xercesc::DOMElement* m_root;
259 #endif
260     };
261 };
262
263 #if defined (_MSC_VER)
264     #pragma warning( pop )
265 #endif
266
267 #endif /* __xmltooling_pool_h__ */