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