Xerces 3 revisions.
[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
37 #ifndef XMLTOOLING_NO_XMLSEC\r
38 # include <xsec/framework/XSECDefs.hpp>\r
39 #endif
40
41 #if defined (_MSC_VER)
42     #pragma warning( push )
43     #pragma warning( disable : 4250 4251 )
44 #endif
45
46 namespace xmltooling {
47
48     /**
49      * A thread-safe pool of DOMBuilders that share characteristics.
50      */
51     class XMLTOOL_API ParserPool : xercesc::DOMErrorHandler,
52 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
53         public xercesc::DOMLSResourceResolver
54 #else
55         public xercesc::DOMEntityResolver
56 #endif
57     {
58         MAKE_NONCOPYABLE(ParserPool);
59     public:
60         /**
61          * Constructs a new pool
62          * 
63          * @param namespaceAware    indicates whether parsers should be namespace-aware or not
64          * @param schemaAware       indicates whether parsers should be schema-validating or not
65          */
66         ParserPool(bool namespaceAware=true, bool schemaAware=false);
67         ~ParserPool();
68
69         /**
70          * Creates a new document using a parser from this pool.
71          * 
72          * @return new XML document
73          * 
74          */
75         xercesc::DOMDocument* newDocument();
76
77         /**
78          * Parses a document using a pooled parser with the proper settings
79          * 
80          * @param domsrc An input source containing the content to be parsed
81          * @return The DOM document resulting from the parse
82          * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
83          */
84         xercesc::DOMDocument* parse(
85 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
86             xercesc::DOMLSInput& domsrc
87 #else
88             xercesc::DOMInputSource& domsrc
89 #endif
90             );
91
92         /**
93          * Parses a document using a pooled parser with the proper settings
94          * 
95          * @param is An input stream containing the content to be parsed
96          * @return The DOM document resulting from the parse
97          * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
98          */
99         xercesc::DOMDocument* parse(std::istream& is);
100
101         /**
102          * Load an OASIS catalog file to map schema namespace URIs to filenames.
103          * 
104          * This does not provide real catalog support; only the &lt;uri&gt; element
105          * is supported to map from a namespace URI to a relative path or file:// URI.
106          * 
107          * @param pathname  path to a catalog file
108          * @return true iff the catalog was successfully processed
109          */
110         bool loadCatalog(const XMLCh* pathname);
111         
112         /**
113          * Load a schema explicitly from a local file.
114          * 
115          * Note that "successful processing" does not imply that the schema is valid,
116          * only that a reference to it was successfully registered with the pool.
117          * 
118          * @param nsURI     XML namespace to load
119          * @param pathname  path to schema file
120          * @return true iff the schema was successfully processed
121          */
122         bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
123
124         /**
125          * Supplies all external entities (primarily schemas) to the parser
126          */
127 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
128         xercesc::DOMLSInput* resolveResource(
129             const XMLCh *const resourceType,
130             const XMLCh *const namespaceUri,
131             const XMLCh *const publicId,
132             const XMLCh *const systemId,
133             const XMLCh *const baseURI
134             );
135 #else
136         xercesc::DOMInputSource* resolveEntity(
137             const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
138             );
139 #endif
140
141         /**
142          * Handles parsing errors
143          */
144         bool handleError(const xercesc::DOMError& e);
145
146     private:
147 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
148         xercesc::DOMLSParser* createBuilder();
149         xercesc::DOMLSParser* checkoutBuilder();
150         void checkinBuilder(xercesc::DOMLSParser* builder);
151 #else
152         xercesc::DOMBuilder* createBuilder();
153         xercesc::DOMBuilder* checkoutBuilder();
154         void checkinBuilder(xercesc::DOMBuilder* builder);
155 #endif
156
157 #ifdef HAVE_GOOD_STL
158         xstring m_schemaLocations;
159         std::map<xstring,xstring> m_schemaLocMap;
160 #else
161         std::string m_schemaLocations;
162         std::map<std::string,std::string> m_schemaLocMap;
163 #endif
164         bool m_namespaceAware,m_schemaAware;
165 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
166         std::stack<xercesc::DOMLSParser*> m_pool;
167 #else
168         std::stack<xercesc::DOMBuilder*> m_pool;
169 #endif
170         Mutex* m_lock;
171         xercesc::SecurityManager* m_security;
172     };
173
174     /**
175      * A parser source that wraps a C++ input stream
176      */
177     class XMLTOOL_API StreamInputSource : public xercesc::InputSource
178     {
179     MAKE_NONCOPYABLE(StreamInputSource);
180     public:
181         /**
182          * Constructs an input source around an input stream reference.
183          * 
184          * @param is        reference to an input stream
185          * @param systemId  optional system identifier to attach to the stream
186          */
187         StreamInputSource(std::istream& is, const char* systemId=NULL) : xercesc::InputSource(systemId), m_is(is) {}
188         /// @cond off
189         virtual xercesc::BinInputStream* makeStream() const { return new StreamBinInputStream(m_is); }
190         /// @endcond
191
192         /**
193          * A Xerces input stream that wraps a C++ input stream
194          */
195         class XMLTOOL_API StreamBinInputStream : public xercesc::BinInputStream
196         {
197         public:
198             /**
199              * Constructs a Xerces input stream around a C++ input stream reference.
200              * 
201              * @param is            reference to an input stream
202              */
203             StreamBinInputStream(std::istream& is) : m_is(is), m_pos(0) {}
204             /// @cond off
205 #ifdef XMLTOOLING_XERCESC_64BITSAFE
206             XMLFilePos
207 #else
208             unsigned int
209 #endif
210                 curPos() const { return m_pos; }
211             xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
212 #ifdef XMLTOOLING_XERCESC_64BITSAFE
213             const XMLCh* getContentType() const { return NULL; }
214 #endif
215             /// @endcond
216         private:
217             std::istream& m_is;
218             xsecsize_t m_pos;
219         };
220
221     private:
222         std::istream& m_is;
223     };
224 };
225
226 #if defined (_MSC_VER)
227     #pragma warning( pop )
228 #endif
229
230 #endif /* __xmltooling_pool_h__ */