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