64910dfe846030542504009f9fd10e3642c1d431
[shibboleth/cpp-xmltooling.git] / xmltooling / util / ParserPool.h
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * @file ParserPool.h\r
19  * \r
20  * XML parsing\r
21  */\r
22 \r
23 #if !defined(__xmltooling_pool_h__)\r
24 #define __xmltooling_pool_h__\r
25 \r
26 #include <xmltooling/unicode.h>\r
27 #include <xmltooling/util/Threads.h>\r
28 \r
29 #include <map>\r
30 #include <stack>\r
31 #include <istream>\r
32 #include <xercesc/dom/DOM.hpp>\r
33 #include <xercesc/sax/InputSource.hpp>\r
34 #include <xercesc/util/BinInputStream.hpp>\r
35 \r
36 using namespace xercesc;\r
37 \r
38 #if defined (_MSC_VER)\r
39     #pragma warning( push )\r
40     #pragma warning( disable : 4250 4251 )\r
41 #endif\r
42 \r
43 namespace xmltooling {\r
44 \r
45     /**\r
46      * A thread-safe pool of DOMBuilders that share characteristics\r
47      */\r
48     class XMLTOOL_API ParserPool : public DOMEntityResolver, DOMErrorHandler\r
49     {\r
50         MAKE_NONCOPYABLE(ParserPool);\r
51     public:\r
52         /**\r
53          * Constructs a new pool\r
54          * \r
55          * @param namespaceAware    indicates whether parsers should be namespace-aware or not\r
56          * @param schemaAware       indicates whether parsers should be schema-validating or not\r
57          */\r
58         ParserPool(bool namespaceAware=true, bool schemaAware=false);\r
59         ~ParserPool();\r
60 \r
61         /**\r
62          * Creates a new document using a parser from this pool.\r
63          * \r
64          * @return new XML document\r
65          * \r
66          */\r
67         DOMDocument* newDocument();\r
68 \r
69         /**\r
70          * Parses a document using a pooled parser with the proper settings\r
71          * \r
72          * @param domsrc A DOM source containing the content to be parsed\r
73          * @return The DOM document resulting from the parse\r
74          * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML\r
75          */\r
76         DOMDocument* parse(DOMInputSource& domsrc);\r
77 \r
78         /**\r
79          * Parses a document using a pooled parser with the proper settings\r
80          * \r
81          * @param is An input stream containing the content to be parsed\r
82          * @return The DOM document resulting from the parse\r
83          * @throws XMLParserException thrown if there was a problem reading, parsing, or validating the XML\r
84          */\r
85         DOMDocument* parse(std::istream& is);\r
86 \r
87         /**\r
88          * Load an OASIS catalog file to map schema namespace URIs to filenames.\r
89          * \r
90          * This does not provide real catalog support; only the &lt;uri&gt; element\r
91          * is supported to map from a namespace URI to a relative path or file:// URI.\r
92          * \r
93          * @param pathname  path to a catalog file\r
94          * @return true iff the catalog was successfully processed\r
95          */\r
96         bool loadCatalog(const XMLCh* pathname);\r
97         \r
98         /**\r
99          * Load a schema explicitly from a local file.\r
100          * \r
101          * Note that "successful processing" does not imply that the schema is valid,\r
102          * only that a reference to it was successfully registered with the pool.\r
103          * \r
104          * @param nsURI     XML namespace to load\r
105          * @param pathname  path to schema file\r
106          * @return true iff the schema was successfully processed\r
107          */\r
108         bool loadSchema(const XMLCh* nsURI, const XMLCh* pathname);\r
109 \r
110         /**\r
111          * Supplies all external entities (primarily schemas) to the parser\r
112          */\r
113         DOMInputSource* resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI);\r
114 \r
115         /**\r
116          * Handles parsing errors\r
117          */\r
118         bool handleError(const DOMError& e);\r
119 \r
120     private:\r
121         DOMBuilder* createBuilder();\r
122         DOMBuilder* checkoutBuilder();\r
123         void checkinBuilder(DOMBuilder* builder);\r
124 \r
125 #ifdef HAVE_GOOD_STL\r
126         xstring m_schemaLocations;\r
127         std::map<xstring,xstring> m_schemaLocMap;\r
128 #else\r
129         std::string m_schemaLocations;\r
130         std::map<std::string,std::string> m_schemaLocMap;\r
131 #endif\r
132         bool m_namespaceAware,m_schemaAware;\r
133         std::stack<DOMBuilder*> m_pool;\r
134         Mutex* m_lock;\r
135     };\r
136 \r
137     /**\r
138      * A parser source that wraps a C++ input stream\r
139      */\r
140     class XMLTOOL_API StreamInputSource : public InputSource\r
141     {\r
142     MAKE_NONCOPYABLE(StreamInputSource);\r
143     public:\r
144         /**\r
145          * Constructs an input source around an input stream reference.\r
146          * \r
147          * @param is        reference to an input stream\r
148          * @param systemId  optional system identifier to attach to the stream\r
149          */\r
150         StreamInputSource(std::istream& is, const char* systemId=NULL) : InputSource(systemId), m_is(is) {}\r
151         /// @cond off\r
152         virtual BinInputStream* makeStream() const { return new StreamBinInputStream(m_is); }\r
153         /// @endcond\r
154 \r
155         /**\r
156          * A Xerces input stream that wraps a C++ input stream\r
157          */\r
158         class XMLTOOL_API StreamBinInputStream : public BinInputStream\r
159         {\r
160         public:\r
161             /**\r
162              * Constructs a Xerces input stream around a C++ input stream reference.\r
163              * \r
164              * @param is        reference to an input stream\r
165              */\r
166             StreamBinInputStream(std::istream& is) : m_is(is), m_pos(0) {}\r
167             /// @cond off\r
168             virtual unsigned int curPos() const { return m_pos; }\r
169             virtual unsigned int readBytes(XMLByte* const toFill, const unsigned int maxToRead);\r
170             /// @endcond\r
171         private:\r
172             std::istream& m_is;\r
173             unsigned int m_pos;\r
174         };\r
175 \r
176     private:\r
177         std::istream& m_is;\r
178     };\r
179 };\r
180 \r
181 #if defined (_MSC_VER)\r
182     #pragma warning( pop )\r
183 #endif\r
184 \r
185 #endif /* __xmltooling_pool_h__ */\r