Update copyright.
[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 using namespace xercesc;
37
38 #if defined (_MSC_VER)
39     #pragma warning( push )
40     #pragma warning( disable : 4250 4251 )
41 #endif
42
43 namespace xmltooling {
44
45     /**
46      * A thread-safe pool of DOMBuilders that share characteristics
47      */
48     class XMLTOOL_API ParserPool : public DOMEntityResolver, DOMErrorHandler
49     {
50         MAKE_NONCOPYABLE(ParserPool);
51     public:
52         /**
53          * Constructs a new pool
54          * 
55          * @param namespaceAware    indicates whether parsers should be namespace-aware or not
56          * @param schemaAware       indicates whether parsers should be schema-validating or not
57          */
58         ParserPool(bool namespaceAware=true, bool schemaAware=false);
59         ~ParserPool();
60
61         /**
62          * Creates a new document using a parser from this pool.
63          * 
64          * @return new XML document
65          * 
66          */
67         DOMDocument* newDocument();
68
69         /**
70          * Parses a document using a pooled parser with the proper settings
71          * 
72          * @param domsrc A DOM source containing the content to be parsed
73          * @return The DOM document resulting from the parse
74          * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML
75          */
76         DOMDocument* parse(DOMInputSource& domsrc);
77
78         /**
79          * Parses a document using a pooled parser with the proper settings
80          * 
81          * @param is An input stream 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         DOMDocument* parse(std::istream& is);
86
87         /**
88          * Load an OASIS catalog file to map schema namespace URIs to filenames.
89          * 
90          * This does not provide real catalog support; only the &lt;uri&gt; element
91          * is supported to map from a namespace URI to a relative path or file:// URI.
92          * 
93          * @param pathname  path to a catalog file
94          * @return true iff the catalog was successfully processed
95          */
96         bool loadCatalog(const XMLCh* pathname);
97         
98         /**
99          * Load a schema explicitly from a local file.
100          * 
101          * Note that "successful processing" does not imply that the schema is valid,
102          * only that a reference to it was successfully registered with the pool.
103          * 
104          * @param nsURI     XML namespace to load
105          * @param pathname  path to schema file
106          * @return true iff the schema was successfully processed
107          */
108         bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);
109
110         /**
111          * Supplies all external entities (primarily schemas) to the parser
112          */
113         DOMInputSource* resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI);
114
115         /**
116          * Handles parsing errors
117          */
118         bool handleError(const DOMError& e);
119
120     private:
121         DOMBuilder* createBuilder();
122         DOMBuilder* checkoutBuilder();
123         void checkinBuilder(DOMBuilder* builder);
124
125 #ifdef HAVE_GOOD_STL
126         xstring m_schemaLocations;
127         std::map<xstring,xstring> m_schemaLocMap;
128 #else
129         std::string m_schemaLocations;
130         std::map<std::string,std::string> m_schemaLocMap;
131 #endif
132         bool m_namespaceAware,m_schemaAware;
133         std::stack<DOMBuilder*> m_pool;
134         Mutex* m_lock;
135     };
136
137     /**
138      * A parser source that wraps a C++ input stream
139      */
140     class XMLTOOL_API StreamInputSource : public 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) : InputSource(systemId), m_is(is) {}
151         /// @cond off
152         virtual 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 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__ */