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