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